Files
Checkmate/client/src/utils/ApiClient.ts
T

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-11-13 13:47:26 -08:00
import axios from "axios";
import type { AxiosError } from "axios";
import type { AxiosRequestConfig, AxiosResponse } from "axios";
2025-12-10 19:01:59 +00:00
import { config } from "@/config";
const BASE_URL = config.API_BASE_URL;
2025-11-13 13:47:26 -08:00
const api = axios.create({
2025-12-10 19:01:59 +00:00
baseURL: BASE_URL,
2025-11-13 13:47:26 -08:00
withCredentials: true,
});
const onSuccess = (response: AxiosResponse) => response;
const onError = (error: AxiosError) => {
if (error.response?.status === 401) {
window.location.href = "/login";
}
return Promise.reject(error);
};
api.interceptors.response.use(onSuccess, onError);
export const get = <T>(
url: string,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.get<T>(url, config);
export const post = <T>(
url: string,
data: any,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.post<T>(url, data, config);
export const patch = <T>(
url: string,
data: any,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.patch<T>(url, data, config);
export const deleteOp = <T>(
url: string,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.delete<T>(url, config);
export default api;