File size: 1,544 Bytes
3ca9355
 
 
 
 
 
bcd22af
3ca9355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcd22af
3ca9355
 
 
 
 
 
bcd22af
3ca9355
 
 
 
 
 
 
 
 
bcd22af
3ca9355
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * 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;
    }
};