2023-09-21 16:52:13 +02:00
|
|
|
import axios from "axios"
|
2023-09-28 13:23:58 +02:00
|
|
|
import { useUserStore } from '@/stores/user'
|
2023-09-28 16:13:14 +02:00
|
|
|
import { useNotifyStore } from '@/stores/notify'
|
2023-09-21 16:52:13 +02:00
|
|
|
|
2023-09-27 10:24:03 +02:00
|
|
|
export const httpClientFactory = (endpoint = 'api') => {
|
2023-09-21 16:52:13 +02:00
|
|
|
let baseURL
|
2023-12-29 00:21:52 +01:00
|
|
|
const subdir = window.appConfig.subdirectory
|
2023-09-21 16:52:13 +02:00
|
|
|
|
|
|
|
if (endpoint === 'web') {
|
2023-12-29 00:21:52 +01:00
|
|
|
baseURL = subdir + '/'
|
2023-09-21 16:52:13 +02:00
|
|
|
} else {
|
2023-12-29 00:21:52 +01:00
|
|
|
baseURL = subdir + '/api/v1'
|
2023-09-21 16:52:13 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 10:24:03 +02:00
|
|
|
const httpClient = axios.create({
|
2023-09-21 16:52:13 +02:00
|
|
|
baseURL,
|
|
|
|
headers: { 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json' },
|
|
|
|
withCredentials: true,
|
|
|
|
})
|
|
|
|
|
2023-09-28 16:13:14 +02:00
|
|
|
// httpClient.interceptors.request.use(
|
|
|
|
// async function (config) {
|
|
|
|
// // We get a CSRF token when needed
|
|
|
|
// const cookies = Object.fromEntries(document.cookie.split('; ').map(c => c.split('=')))
|
|
|
|
// console.log(cookies)
|
|
|
|
|
|
|
|
// if (! Object.hasOwnProperty(cookies, 'XSRF-TOKEN') && ['post', 'put', 'patch', 'delete'].includes(config.method))
|
|
|
|
// {
|
|
|
|
// await axios.get('/refresh-csrf', {withCredentials:true})
|
|
|
|
// return config
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return config
|
|
|
|
// },
|
|
|
|
// (error) => {
|
|
|
|
// Promise.reject(error)
|
|
|
|
// }
|
|
|
|
// )
|
2023-09-21 16:52:13 +02:00
|
|
|
|
2023-09-27 10:24:03 +02:00
|
|
|
httpClient.interceptors.response.use(
|
2023-09-21 16:52:13 +02:00
|
|
|
(response) => {
|
|
|
|
return response;
|
|
|
|
},
|
2023-09-28 16:13:14 +02:00
|
|
|
async function (error) {
|
|
|
|
const originalRequestConfig = error.config
|
|
|
|
|
|
|
|
// Here we handle a missing/invalid CSRF cookie
|
|
|
|
// We try to get a fresh on, but only once.
|
|
|
|
if (error.response.status === 419 && ! originalRequestConfig._retried) {
|
|
|
|
originalRequestConfig._retried = true;
|
|
|
|
await axios.get('/refresh-csrf')
|
|
|
|
return httpClient.request(originalRequestConfig)
|
|
|
|
}
|
2024-03-06 08:40:29 +01:00
|
|
|
|
|
|
|
// api calls are stateless so when user inactivity is detected
|
|
|
|
// by the backend middleware, it cannot logout the user directly
|
|
|
|
// so it returns a 418 response.
|
|
|
|
// We catch the 418 response and log the user out
|
|
|
|
if (error.response.status === 418) {
|
|
|
|
const user = useUserStore()
|
|
|
|
user.logout({ kicked: true})
|
|
|
|
}
|
2023-12-29 00:31:30 +01:00
|
|
|
|
|
|
|
if (error.response && [407].includes(error.response.status)) {
|
|
|
|
useNotifyStore().error(error)
|
|
|
|
return new Promise(() => {})
|
|
|
|
}
|
2023-09-28 16:13:14 +02:00
|
|
|
|
|
|
|
// Return the error when we need to handle it at component level
|
|
|
|
if (error.config.hasOwnProperty('returnError') && error.config.returnError === true) {
|
|
|
|
return Promise.reject(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.response && [401].includes(error.response.status)) {
|
2023-09-28 13:23:58 +02:00
|
|
|
const user = useUserStore()
|
2023-11-30 13:18:35 +01:00
|
|
|
user.tossOut()
|
2023-09-21 16:52:13 +02:00
|
|
|
}
|
2023-09-28 16:13:14 +02:00
|
|
|
|
|
|
|
// Always return the form validation errors
|
|
|
|
if (error.response.status === 422) {
|
|
|
|
return Promise.reject(error)
|
|
|
|
}
|
|
|
|
|
2023-11-01 09:31:00 +01:00
|
|
|
// Not found
|
|
|
|
if (error.response.status === 404) {
|
|
|
|
useNotifyStore().notFound()
|
|
|
|
return new Promise(() => {})
|
|
|
|
}
|
|
|
|
|
2023-09-28 16:13:14 +02:00
|
|
|
useNotifyStore().error(error)
|
|
|
|
return new Promise(() => {})
|
2023-09-21 16:52:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-09-27 10:24:03 +02:00
|
|
|
return httpClient
|
2023-09-21 16:52:13 +02:00
|
|
|
}
|