2023-06-01 12:54:58 +07:00
|
|
|
import Axios, { type AxiosRequestConfig, type AxiosResponse } from "axios";
|
2024-08-28 15:29:09 +07:00
|
|
|
import { getToken } from "./auth";
|
2023-06-01 12:54:58 +07:00
|
|
|
|
|
|
|
|
const http = Axios.create({
|
|
|
|
|
timeout: 1000000000, // เพิ่มค่า timeout
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
http.interceptors.request.use(
|
|
|
|
|
async function (config: AxiosRequestConfig<any>) {
|
2024-07-23 16:33:59 +07:00
|
|
|
// await keycloak.updateToken(1);
|
2023-06-01 12:54:58 +07:00
|
|
|
config.headers = config.headers ?? {};
|
2024-08-28 15:29:09 +07:00
|
|
|
const token = await getToken();
|
2023-06-01 12:54:58 +07:00
|
|
|
if (token) config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
function (error: any) {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
http.interceptors.response.use(
|
|
|
|
|
function (response: AxiosResponse<any, any>) {
|
|
|
|
|
return response;
|
|
|
|
|
},
|
|
|
|
|
function (error: any) {
|
|
|
|
|
if (typeof error !== undefined) {
|
|
|
|
|
// eslint-disable-next-line no-prototype-builtins
|
|
|
|
|
if (error.hasOwnProperty("response")) {
|
2024-08-26 17:40:11 +07:00
|
|
|
if (error.response.status === 403) {
|
|
|
|
|
window.location.href = "/error";
|
2023-06-01 12:54:58 +07:00
|
|
|
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
|
|
|
|
|
// Store.commit("REMOVE_ACCESS_TOKEN")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default http;
|