import axios from "axios"; import type { AxiosError } from "axios"; import type { AxiosRequestConfig, AxiosResponse } from "axios"; import { config } from "@/config"; const BASE_URL = config.API_BASE_URL; const api = axios.create({ baseURL: BASE_URL, 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 = ( url: string, config: AxiosRequestConfig = {} ): Promise> => api.get(url, config); export const post = ( url: string, data: any, config: AxiosRequestConfig = {} ): Promise> => api.post(url, data, config); export const patch = ( url: string, data: any, config: AxiosRequestConfig = {} ): Promise> => api.patch(url, data, config); export const deleteOp = ( url: string, config: AxiosRequestConfig = {} ): Promise> => api.delete(url, config); export default api;