2021-08-11 19:39:09 +02:00
|
|
|
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'
|
2021-08-11 19:39:09 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-08-11 19:39:09 +02:00
|
|
|
export default api
|