| /** | |
| * HTTP API Client for Cloud Deployment | |
| * Replaces Tauri IPC with REST API calls | |
| */ | |
| export interface ApiResponse<T> { | |
| success: boolean; | |
| data?: T; | |
| error?: string; | |
| } | |
| /** | |
| * Base API request function | |
| */ | |
| async function apiRequest<T>( | |
| endpoint: string, | |
| options?: RequestInit | |
| ): Promise<T> { | |
| const response = await fetch(endpoint, { | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| ...options?.headers, | |
| }, | |
| ...options, | |
| }); | |
| if (!response.ok) { | |
| const errorText = await response.text(); | |
| throw new Error(errorText || `HTTP ${response.status}`); | |
| } | |
| const result: ApiResponse<T> = await response.json(); | |
| if (!result.success) { | |
| throw new Error(result.error || 'Unknown error'); | |
| } | |
| return result.data as T; | |
| } | |
| /** | |
| * API client with convenience methods | |
| */ | |
| export const api = { | |
| /** | |
| * GET request | |
| */ | |
| get: <T>(url: string): Promise<T> => apiRequest<T>(url), | |
| /** | |
| * POST request | |
| */ | |
| post: <T>(url: string, body?: unknown): Promise<T> => | |
| apiRequest<T>(url, { | |
| method: 'POST', | |
| body: body ? JSON.stringify(body) : undefined, | |
| }), | |
| /** | |
| * DELETE request | |
| */ | |
| delete: <T>(url: string): Promise<T> => | |
| apiRequest<T>(url, { | |
| method: 'DELETE', | |
| }), | |
| /** | |
| * PUT request | |
| */ | |
| put: <T>(url: string, body?: unknown): Promise<T> => | |
| apiRequest<T>(url, { | |
| method: 'PUT', | |
| body: body ? JSON.stringify(body) : undefined, | |
| }), | |
| }; | |
| export default api; | |