Spaces:
Building
Building
| /** | |
| * API Service for backend communication | |
| * Migrated from Angular api.service.ts | |
| */ | |
| const apiService = { | |
| baseUrl: '', // Use relative paths for better compatibility with proxies | |
| /** | |
| * Fetch wrapper with error handling | |
| */ | |
| async request(endpoint, options = {}) { | |
| try { | |
| const response = await fetch(`${this.baseUrl}${endpoint}`, { | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| ...options.headers | |
| }, | |
| ...options | |
| }); | |
| if (!response.ok) { | |
| throw new Error(`HTTP error! status: ${response.status}`); | |
| } | |
| return await response.json(); | |
| } catch (error) { | |
| console.error('API request failed:', error); | |
| throw error; | |
| } | |
| }, | |
| /** | |
| * Get species list from backend | |
| */ | |
| async getSpecies() { | |
| return await this.request('api/species'); | |
| }, | |
| /** | |
| * Send a capture to the backend | |
| */ | |
| async sendCapture(capture) { | |
| return await this.request('api/captures', { | |
| method: 'POST', | |
| body: JSON.stringify(capture) | |
| }); | |
| }, | |
| /** | |
| * Batch sync multiple captures | |
| */ | |
| async syncCaptures(captures) { | |
| return await this.request('api/sync', { | |
| method: 'POST', | |
| body: JSON.stringify({ captures }) | |
| }); | |
| }, | |
| /** | |
| * Check if online | |
| */ | |
| isOnline() { | |
| return navigator.onLine; | |
| } | |
| }; | |