summaryrefslogtreecommitdiff
path: root/api/static/js/api.js
blob: ecfe30de526e0e2a1f5ea95bd8eb5aa15d1eb43c (plain)
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
/**
 * api.js - Core API communication
 */

const API_BASE = '';

export const api = {
    async get(endpoint) {
        const response = await fetch(`${API_BASE}${endpoint}`);
        if (!response.ok) throw new Error(`API Error: ${response.status}`);
        return await response.json();
    },

    async post(endpoint, data) {
        const response = await fetch(`${API_BASE}${endpoint}`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        if (!response.ok) throw new Error(`API Error: ${response.status}`);
        return await response.json();
    },

    async delete(endpoint) {
        const response = await fetch(`${API_BASE}${endpoint}`, { method: 'DELETE' });
        if (!response.ok) throw new Error(`API Error: ${response.status}`);
        return await response.json();
    },

    streamLogs(onLog, onDone, onError) {
        const eventSource = new EventSource(`${API_BASE}/api/logs/stream`);
        
        eventSource.onmessage = (e) => onLog(e.data);
        eventSource.addEventListener('log', (e) => onLog(e.data));
        eventSource.addEventListener('done', () => {
            eventSource.close();
            onDone();
        });
        eventSource.onerror = (e) => {
            console.error('SSE Error:', e);
            if (onError) onError(e);
        };

        return eventSource;
    }
};