/** * Client HTTP de base pour toutes les requêtes API */ export class HttpClient { private baseUrl: string; constructor(baseUrl: string = '') { this.baseUrl = baseUrl; } private async request( endpoint: string, options: RequestInit = {} ): Promise { const url = `${this.baseUrl}${endpoint}`; const config: RequestInit = { headers: { 'Content-Type': 'application/json', ...options.headers, }, ...options, }; try { const response = await fetch(url, config); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error( errorData.error || `HTTP ${response.status}: ${response.statusText}` ); } return await response.json(); } catch (error) { console.error(`HTTP Request failed: ${url}`, error); throw error; } } async get(endpoint: string, params?: Record): Promise { const url = params ? `${endpoint}?${new URLSearchParams(params)}` : endpoint; return this.request(url, { method: 'GET' }); } async post(endpoint: string, data?: unknown): Promise { return this.request(endpoint, { method: 'POST', body: data ? JSON.stringify(data) : undefined, }); } async put(endpoint: string, data?: unknown): Promise { return this.request(endpoint, { method: 'PUT', body: data ? JSON.stringify(data) : undefined, }); } async patch(endpoint: string, data?: unknown): Promise { return this.request(endpoint, { method: 'PATCH', body: data ? JSON.stringify(data) : undefined, }); } async delete( endpoint: string, params?: Record ): Promise { const url = params ? `${endpoint}?${new URLSearchParams(params)}` : endpoint; return this.request(url, { method: 'DELETE' }); } } // Instance par défaut export const httpClient = new HttpClient('/api');