import { API_BASE_URL } from '../constants/api'; import { ApiError } from '../types'; class ApiClient { private baseUrl: string; constructor(baseUrl: string) { this.baseUrl = baseUrl; } private async request( endpoint: string, options: RequestInit = {} ): Promise { const url = `${this.baseUrl}${endpoint}`; const response = await fetch(url, { ...options, headers: { 'Content-Type': 'application/json', ...options.headers, }, }); if (!response.ok) { const error: ApiError = await response.json(); throw new Error(error.error?.message || 'Request failed'); } return response.json(); } async get(endpoint: string): Promise { return this.request(endpoint); } async post(endpoint: string, body?: unknown): Promise { return this.request(endpoint, { method: 'POST', body: body ? JSON.stringify(body) : undefined, }); } async delete(endpoint: string): Promise { return this.request(endpoint, { method: 'DELETE' }); } async uploadFile(endpoint: string, formData: FormData): Promise { const url = `${this.baseUrl}${endpoint}`; const response = await fetch(url, { method: 'POST', body: formData, headers: { 'Content-Type': 'multipart/form-data', }, }); if (!response.ok) { const error: ApiError = await response.json(); throw new Error(error.error?.message || 'Upload failed'); } return response.json(); } } export const apiClient = new ApiClient(API_BASE_URL);