import { API_BASE_URL } from '@/utils/constants'; import { getToken } from '@/utils/helpers'; export interface FetchOptions extends RequestInit { token?: string; } export class ApiError extends Error { constructor( public status: number, message: string, public data?: any ) { super(message); this.name = 'ApiError'; } } async function fetchWithAuth( endpoint: string, options: FetchOptions = {} ): Promise { const token = options.token || getToken(); // Logs de debug console.log('🔐 [API] PrĂ©paration de la requĂȘte:', { endpoint, hasToken: !!token, tokenPreview: token ? `${token.substring(0, 20)}...` : 'Aucun', }); const headers: Record = { 'Content-Type': 'application/json', ...options.headers as Record, }; if (token) { headers['Authorization'] = `Bearer ${token}`; console.log('✅ [API] Header Authorization ajoutĂ©'); } else { console.warn('⚠ [API] Aucun token disponible - requĂȘte sans authentification'); } const config: RequestInit = { ...options, headers, }; const url = endpoint.startsWith('http') ? endpoint : `${API_BASE_URL}${endpoint}`; console.log('📡 [API] Envoi de la requĂȘte:', { url, method: config.method || 'GET', headers: headers, body: options.body, }); try { const response = await fetch(url, config); if (!response.ok) { let errorMessage = 'Une erreur est survenue'; let errorData; try { errorData = await response.json(); errorMessage = errorData.message || errorData.error || errorMessage; console.error('❌ [API] Erreur du serveur:', { status: response.status, statusText: response.statusText, errorData, errorMessage, }); } catch { errorMessage = response.statusText || errorMessage; console.error('❌ [API] Erreur sans JSON:', { status: response.status, statusText: response.statusText, }); } throw new ApiError(response.status, errorMessage, errorData); } return response; } catch (error) { if (error instanceof ApiError) { throw error; } throw new ApiError(0, 'Erreur de connexion au serveur'); } } export const api = { get: async (endpoint: string, options?: FetchOptions): Promise => { const response = await fetchWithAuth(endpoint, { ...options, method: 'GET', }); return response.json(); }, post: async ( endpoint: string, data?: any, options?: FetchOptions ): Promise => { const response = await fetchWithAuth(endpoint, { ...options, method: 'POST', body: data ? JSON.stringify(data) : undefined, }); return response.json(); }, put: async ( endpoint: string, data?: any, options?: FetchOptions ): Promise => { const response = await fetchWithAuth(endpoint, { ...options, method: 'PUT', body: data ? JSON.stringify(data) : undefined, }); return response.json(); }, patch: async ( endpoint: string, data?: any, options?: FetchOptions ): Promise => { const response = await fetchWithAuth(endpoint, { ...options, method: 'PATCH', body: data ? JSON.stringify(data) : undefined, }); return response.json(); }, delete: async (endpoint: string, options?: FetchOptions): Promise => { const response = await fetchWithAuth(endpoint, { ...options, method: 'DELETE', }); return response.json(); }, };