48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
// api/axios.ts
|
|
import axios, { AxiosInstance } from "axios";
|
|
|
|
const defaultHeaders = {
|
|
"Content-Type": "application/json",
|
|
"ngrok-skip-browser-warning": "69420",
|
|
};
|
|
|
|
const axiosInstance: AxiosInstance = axios.create({
|
|
baseURL: import.meta.env.VITE_BASE_URI,
|
|
withCredentials: true,
|
|
headers: defaultHeaders,
|
|
validateStatus: (status) => status >= 200 && status < 400,
|
|
});
|
|
|
|
|
|
axiosInstance.interceptors.response.use(
|
|
(response) => {
|
|
|
|
if (typeof response.data === 'string' && response.data.toLowerCase().includes('<!doctype html>')) {
|
|
// Likely a full HTML page, handle accordingly
|
|
window.location.href = response.request.responseURL;
|
|
}
|
|
|
|
if (response && (response.status === 301 || response.status === 302)) {
|
|
const redirectUrl = response.headers["location"];
|
|
if (redirectUrl) {
|
|
window.location.href = redirectUrl;
|
|
}
|
|
}
|
|
|
|
// Otherwise just continue as normal
|
|
return response;
|
|
},
|
|
(error) => {
|
|
// Optionally handle backend 302 or 3xx here (if returned as error)
|
|
if (error.response?.status === 302 && error.response?.headers?.location) {
|
|
window.location.replace(error.response.headers.location);
|
|
return Promise.reject({ redirected: true });
|
|
}
|
|
|
|
// Pass through all other errors
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default axiosInstance;
|