import axios, {AxiosResponse, Method} from "axios"; import {configs, isDesktop} from "@/lib/configs"; type ContentType = 'application/json' | 'multipart/form-data'; interface ApiRequestConfig { method: Method; url: string; auth: string; lang?: string; params?: Record; data?: unknown; contentType?: ContentType; } export class ApiError extends Error { statusCode: number; constructor(message: string, statusCode: number) { super(message); this.statusCode = statusCode; this.name = 'ApiError'; } } function handleApiError(error: unknown): never { if (axios.isAxiosError(error)) { const serverMessage: string = error.response?.data?.message || error.response?.data || error.message; const statusCode: number = error.response?.status ?? 500; throw new ApiError(serverMessage, statusCode); } else if (error instanceof Error) { throw new Error(error.message); } throw new Error('An unexpected error occurred'); } async function apiRequest(config: ApiRequestConfig): Promise { try { const headers: Record = { 'Authorization': `Bearer ${config.auth}` }; if (config.contentType) { headers['Content-Type'] = config.contentType; } const response: AxiosResponse = await axios({ method: config.method, headers, params: { lang: config.lang ?? 'fr', plateforme: isDesktop ? 'desktop' : 'web', ...config.params }, url: configs.apiUrl + config.url, data: config.data }); return response.data; } catch (error: unknown) { handleApiError(error); } } export async function apiGet(url: string, auth: string, lang: string = "fr", params: Record = {}): Promise { return apiRequest({method: 'GET', url, auth, lang, params}); } export async function apiPost(url: string, data: object, auth: string, lang: string = "fr"): Promise { return apiRequest({method: 'POST', url, auth, lang, data, contentType: 'application/json'}); } export async function apiPut(url: string, data: object, auth: string, lang: string = "fr"): Promise { return apiRequest({method: 'PUT', url, auth, lang, data, contentType: 'application/json'}); } export async function apiPatch(url: string, data: object, auth: string, lang: string = "fr"): Promise { return apiRequest({method: 'PATCH', url, auth, lang, data, contentType: 'application/json'}); } export async function apiDelete(url: string, data: object, auth: string, lang: string = "fr"): Promise { return apiRequest({method: 'DELETE', url, auth, lang, data, contentType: 'application/json'}); } export async function apiPostPublic(url: string, data: object, lang: string = "fr"): Promise { try { const response: AxiosResponse = await axios({ method: 'POST', headers: {'Content-Type': 'application/json'}, params: {lang, plateforme: isDesktop ? 'desktop' : 'web'}, url: configs.apiUrl + url, data }); return response.data; } catch (error: unknown) { handleApiError(error); } } export async function apiUpload(url: string, file: File, auth: string, lang: string = "fr"): Promise { const formData: FormData = new FormData(); formData.append('file', file); return apiRequest({method: 'POST', url, auth, lang, data: formData, contentType: 'multipart/form-data'}); }