import axios, { AxiosInstance } from "axios"; import { BaseClient } from "./clients/clientBase"; export default class HTTP { public http: AxiosInstance; private client: BaseClient; constructor(client: BaseClient) { this.client = client; this.http = axios.create({ baseURL: this.client.serverAdress + "/api", headers: { "Cache-Control": "no-cache", Pragma: "no-cache", Expires: "0", }, }); this.setRequestInterceptor(); this.setResponseInterceptor(); } private setRequestInterceptor() { this.http.interceptors.request.use( (config) => { if (config.headers && config.headers.Authorization == "") { config.headers.Authorization = `Bearer ${this.client.accessToken}`; } return config; }, (error) => { return Promise.reject(error); } ); } private setResponseInterceptor() { this.http.interceptors.response.use( (response) => { return response; }, async (error) => { if (!error.config.url.includes("/admin")) { return Promise.reject(error); } const originalRequest = error.config; // Handle token expiration and retry the request with a refreshed token if (error.response && error.response.status === 401 && !originalRequest._retry) { originalRequest._retry = true; return await this.client .refreshToken() .then(() => { return this.http(originalRequest); }) .catch(() => {}); } return Promise.reject(error); } ); } }