// API client for SmartBotic CRM backend const API_BASE = '/api' interface RequestOptions { method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' body?: unknown headers?: Record } class ApiClient { private accessToken: string | null = null setAccessToken(token: string | null) { this.accessToken = token } getAccessToken(): string | null { return this.accessToken } async request(endpoint: string, options: RequestOptions = {}): Promise { const { method = 'GET', body, headers = {} } = options const requestHeaders: Record = { 'Content-Type': 'application/json', ...headers, } if (this.accessToken) { requestHeaders['Authorization'] = `Bearer ${this.accessToken}` } const response = await fetch(`${API_BASE}${endpoint}`, { method, headers: requestHeaders, body: body ? JSON.stringify(body) : undefined, }) if (!response.ok) { const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) throw new Error(errorData.error || `HTTP ${response.status}`) } return response.json() } // Convenience methods get(endpoint: string, headers?: Record): Promise { return this.request(endpoint, { method: 'GET', headers }) } post(endpoint: string, body?: unknown, headers?: Record): Promise { return this.request(endpoint, { method: 'POST', body, headers }) } patch(endpoint: string, body?: unknown, headers?: Record): Promise { return this.request(endpoint, { method: 'PATCH', body, headers }) } delete(endpoint: string, headers?: Record): Promise { return this.request(endpoint, { method: 'DELETE', headers }) } } export const apiClient = new ApiClient() export default apiClient