diff options
| author | CaptainJack2491 <jayrupnakawala@gmail.com> | 2026-04-02 16:52:06 +0100 |
|---|---|---|
| committer | CaptainJack2491 <jayrupnakawala@gmail.com> | 2026-04-02 16:52:06 +0100 |
| commit | bd7341e00a1153ea99e4b1d6ce9761b855fee744 (patch) | |
| tree | 9c6899160ed5948f651c2570a45a3b9289311172 /api/static/js/api.js | |
| parent | 75696e2faea1d095a890d297d9ba74968be5afe6 (diff) | |
| parent | f19aa5de3e3f1c3c0b9ce90913588b9e956d4156 (diff) | |
Merge branch 'feature/web-gui'
Diffstat (limited to 'api/static/js/api.js')
| -rw-r--r-- | api/static/js/api.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/api/static/js/api.js b/api/static/js/api.js new file mode 100644 index 0000000..ecfe30d --- /dev/null +++ b/api/static/js/api.js @@ -0,0 +1,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; + } +}; |
