50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { useAuthStore } from "@/auth";
|
|
import { logResource } from "@/service";
|
|
import axios from "axios";
|
|
|
|
export const marbasAxiosInstance = axios.create({
|
|
baseURL: import.meta.env.VITE_MARBAS_URL,
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
"Content-Type": "application/json",
|
|
},
|
|
})
|
|
|
|
marbasAxiosInstance.interceptors.request.use(r => {
|
|
const authStore = useAuthStore();
|
|
|
|
if (!authStore.isLoggedIn) {
|
|
throw new Error("Not logged in");
|
|
}
|
|
|
|
const accessToken = authStore.accessToken;
|
|
|
|
if (accessToken) {
|
|
r.headers.Authorization = `Bearer ${accessToken}`;
|
|
}
|
|
if (!r.params?.page_size) {
|
|
r.params = { ...r.params, page_size: 250 };
|
|
}
|
|
return r;
|
|
})
|
|
logResource(marbasAxiosInstance)
|
|
marbasAxiosInstance.interceptors.response.use(async r => {
|
|
let next: string = r.data?.next;
|
|
let results = r.data?.results;
|
|
|
|
if (next) {
|
|
if (!next.startsWith(import.meta.env.VITE_MARBAS_URL)) { // FIME remove once the API is fixed
|
|
next = import.meta.env.VITE_MARBAS_URL + next.replace(/http(s)?:\/\/[^/]+\//g, '');
|
|
}
|
|
|
|
results = results.concat((await marbasAxiosInstance.request({
|
|
...r.config,
|
|
url: next,
|
|
baseURL: '',
|
|
})).data);
|
|
}
|
|
if (results) {
|
|
r.data = results;
|
|
}
|
|
return r;
|
|
}) |