35 lines
773 B
TypeScript
Raw Normal View History

import axios from 'axios'
2021-08-11 22:21:26 +02:00
2022-09-19 10:53:31 +02:00
import { pendingRequests, removeRequestIfPending } from '@/api/pending'
import { getApiUrl } from '@/utils'
const api = axios.create({
baseURL: getApiUrl(),
})
2022-09-19 10:53:31 +02:00
api.interceptors.request.use(
(config) => {
const controller = new AbortController()
config.signal = controller.signal
const requestKey = removeRequestIfPending(config)
pendingRequests.set(requestKey, controller)
return config
},
(error) => Promise.reject(error)
)
api.interceptors.response.use(
(response) => {
removeRequestIfPending(response.config)
return response
},
(error) => {
if (error.message !== 'canceled') {
removeRequestIfPending(error.response.config)
}
return Promise.reject(error)
}
)
export default api