Fix marbas integration
This commit is contained in:
46
src/marbas/marbasService.ts
Normal file
46
src/marbas/marbasService.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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 => {
|
||||
const next = r.data?.next;
|
||||
let results = r.data?.results;
|
||||
|
||||
if (next) {
|
||||
results = results.concat((await marbasAxiosInstance.request({
|
||||
...r.config,
|
||||
url: next,
|
||||
baseURL: '',
|
||||
})).data);
|
||||
}
|
||||
if (results) {
|
||||
r.data = results;
|
||||
}
|
||||
return r;
|
||||
})
|
||||
Reference in New Issue
Block a user