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

104 lines
2.6 KiB
TypeScript
Raw Normal View History

2026-01-23 17:38:30 +00:00
import axios from "axios";
import type { AxiosError } from "axios";
import type { AxiosRequestConfig, AxiosResponse } from "axios";
import { runtimeConfig } from "@/Utils/runtimeConfig";
2026-01-24 20:26:05 +00:00
const BASE_URL =
runtimeConfig.apiBaseUrl || import.meta.env.VITE_APP_API_BASE_URL || "/api/v1";
2026-01-23 17:38:30 +00:00
const api = axios.create({
baseURL: BASE_URL,
});
2026-01-24 20:26:05 +00:00
type StoreType = {
getState: () => { auth?: { authToken?: string } };
};
let storeInstance: StoreType | null = null;
let interceptorsInitialized = false;
2026-02-10 17:53:00 +00:00
type ServerUnreachableCallback = (unreachable: boolean) => void;
let serverUnreachableCallback: ServerUnreachableCallback | null = null;
export const setServerUnreachableCallback = (
callback: ServerUnreachableCallback
): void => {
serverUnreachableCallback = callback;
};
2026-01-24 20:26:05 +00:00
export const initApiClient = (store: StoreType): void => {
storeInstance = store;
if (interceptorsInitialized) {
return;
}
interceptorsInitialized = true;
api.interceptors.request.use(
(config) => {
const authToken = storeInstance?.getState()?.auth?.authToken ?? "";
config.headers.set("Authorization", `Bearer ${authToken}`);
if (!config.headers.has("Accept-Language")) {
config.headers.set("Accept-Language", "en");
}
2026-01-23 17:38:30 +00:00
2026-01-24 20:26:05 +00:00
return config;
},
(error) => {
return Promise.reject(error);
2026-01-23 17:38:30 +00:00
}
2026-01-24 20:26:05 +00:00
);
2026-01-23 17:38:30 +00:00
2026-02-10 23:53:06 +00:00
const onSuccess = (response: AxiosResponse) => {
// Server is reachable, hide offline banner if shown
serverUnreachableCallback?.(false);
return response;
};
2026-01-24 20:26:05 +00:00
const onError = (error: AxiosError) => {
2026-02-10 17:53:00 +00:00
// Handle network errors (server unreachable)
if (error.code === "ERR_NETWORK") {
serverUnreachableCallback?.(true);
return Promise.reject(error);
}
2026-01-24 20:26:05 +00:00
if (error.response?.status === 401) {
2026-02-06 21:11:46 +00:00
if (window.location.pathname !== "/login") {
window.location.href = "/login";
}
2026-01-24 20:26:05 +00:00
}
2026-01-23 17:38:30 +00:00
return Promise.reject(error);
2026-01-24 20:26:05 +00:00
};
2026-01-23 17:38:30 +00:00
2026-01-24 20:26:05 +00:00
api.interceptors.response.use(onSuccess, onError);
2026-01-23 17:38:30 +00:00
};
export const get = <T>(
url: string,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.get<T>(url, config);
export const post = <T>(
url: string,
data: unknown,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.post<T>(url, data, config);
export const patch = <T>(
url: string,
data: unknown,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.patch<T>(url, data, config);
2026-01-30 19:41:41 +00:00
export const put = <T>(
url: string,
data: unknown,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.put<T>(url, data, config);
2026-01-23 17:38:30 +00:00
export const deleteOp = <T>(
url: string,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse<T>> => api.delete<T>(url, config);
export default api;