2024-05-07 11:54:45 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import axios, { AxiosInstance } from "axios";
|
2023-08-11 11:48:33 +00:00
|
|
|
|
|
|
|
abstract class APIService {
|
|
|
|
protected baseURL: string;
|
2024-05-07 11:54:45 +00:00
|
|
|
private axiosInstance: AxiosInstance;
|
2023-08-11 11:48:33 +00:00
|
|
|
|
2024-04-29 06:42:33 +00:00
|
|
|
constructor(baseURL: string) {
|
|
|
|
this.baseURL = baseURL;
|
2024-05-07 11:54:45 +00:00
|
|
|
this.axiosInstance = axios.create({
|
|
|
|
baseURL,
|
|
|
|
withCredentials: true,
|
|
|
|
});
|
2023-08-11 11:48:33 +00:00
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
this.setupInterceptors();
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
private setupInterceptors() {
|
|
|
|
this.axiosInstance.interceptors.response.use(
|
|
|
|
(response) => response,
|
|
|
|
(error) => {
|
2024-05-08 06:55:36 +00:00
|
|
|
if (error.response && error.response.status === 401) window.location.href = "/";
|
2024-05-07 11:54:45 +00:00
|
|
|
return Promise.reject(error.response?.data ?? error);
|
|
|
|
}
|
|
|
|
);
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
get(url: string, params = {}) {
|
|
|
|
return this.axiosInstance.get(url, { params });
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
post(url: string, data: any, config = {}) {
|
|
|
|
return this.axiosInstance.post(url, data, config);
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
put(url: string, data: any, config = {}) {
|
|
|
|
return this.axiosInstance.put(url, data, config);
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
patch(url: string, data: any, config = {}) {
|
|
|
|
return this.axiosInstance.patch(url, data, config);
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 11:54:45 +00:00
|
|
|
delete(url: string, data?: any, config = {}) {
|
|
|
|
return this.axiosInstance.delete(url, { data, ...config });
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 11:48:33 +00:00
|
|
|
request(config = {}) {
|
2024-05-07 11:54:45 +00:00
|
|
|
return this.axiosInstance(config);
|
2023-08-11 11:48:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default APIService;
|