Spaces:
Sleeping
Sleeping
| // lib/api.ts — All API calls to SIKOMO Forecast backend | |
| const BASE = process.env.NEXT_PUBLIC_API_URL || '' | |
| function getToken() { | |
| if (typeof window !== 'undefined') return localStorage.getItem('sikomo_token') || '' | |
| return '' | |
| } | |
| async function req(path: string, opts: RequestInit = {}) { | |
| const res = await fetch(`${BASE}${path}`, { | |
| ...opts, | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${getToken()}`, | |
| ...opts.headers, | |
| }, | |
| }) | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: res.statusText })) | |
| throw new Error(err.detail || 'Request gagal') | |
| } | |
| return res.json() | |
| } | |
| export const api = { | |
| // Auth | |
| login: (password: string) => req('/auth/login', { method: 'POST', body: JSON.stringify({ password }) }), | |
| // Komoditas & Prediksi | |
| getKomoditas: () => req('/komoditas'), | |
| getPrediksi: (id: number) => req(`/prediksi/${id}`), | |
| // Forecast actions | |
| runForecast: (komoditas_id: number) => req('/forecast/run', { method: 'POST', body: JSON.stringify({ komoditas_id }) }), | |
| runForecastAll: () => req('/forecast/run-all', { method: 'POST' }), | |
| stopForecast: () => req('/forecast/stop', { method: 'POST' }), | |
| // Status & Logs (realtime) | |
| getStatus: () => req('/status'), | |
| getLogs: (limit = 200, level?: string) => req(`/logs?limit=${limit}${level && level !== 'ALL' ? `&level=${level}` : ''}`), | |
| clearLogs: () => req('/clear-logs', { method: 'POST' }), | |
| // Scheduler (multiple schedules) | |
| getSchedules: () => req('/schedules'), | |
| addSchedule: (cron_expression: string, label: string) => req('/schedules/add', { method: 'POST', body: JSON.stringify({ cron_expression, label }) }), | |
| removeSchedule: (jobId: string) => req(`/schedules/${jobId}/remove`, { method: 'DELETE' }), | |
| startScheduler: () => req('/schedule/start', { method: 'POST' }), | |
| stopScheduler: () => req('/schedule/stop', { method: 'POST' }), | |
| // Runtime settings | |
| getSettings: () => req('/settings'), | |
| updateSetting: (key: string, value: string) => req('/settings', { method: 'POST', body: JSON.stringify({ key, value }) }), | |
| // CRUD | |
| getInsights: (id: number) => req(`/insight/${id}`), | |
| updateInsight: (id: number, body: object) => req(`/insight/${id}`, { method: 'PUT', body: JSON.stringify(body) }), | |
| addInsight: (id: number, body: object) => req(`/insight/${id}`, { method: 'POST', body: JSON.stringify(body) }), | |
| deleteInsight: (id: number) => req(`/insight/${id}`, { method: 'DELETE' }), | |
| updateRingkasan: (id: number, body: object) => req(`/ringkasan/${id}`, { method: 'PUT', body: JSON.stringify(body) }), | |
| updateModelMl: (id: number, body: object) => req(`/model-ml/${id}`, { method: 'PUT', body: JSON.stringify(body) }), | |
| } |