const API_BASE = '/api'; export async function fetchApi(endpoint, options = {}) { const url = `${API_BASE}${endpoint}`; const response = await fetch(url, { headers: { 'Content-Type': 'application/json', ...options.headers, }, ...options, }); if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Unknown error' })); throw new Error(error.error || `HTTP ${response.status}`); } return response.json(); } export const api = { // Settings getSettings: () => fetchApi('/settings'), updateSettings: (settings) => fetchApi('/settings', { method: 'PUT', body: JSON.stringify(settings), }), // Scanning startScan: (hostname, username = 'root', port = 22) => fetchApi('/scan', { method: 'POST', body: JSON.stringify({ hostname, username, port }), }), getScan: (scanId) => fetchApi(`/scan/${scanId}`), // Commands runCommand: (scanId, command, reason) => fetchApi(`/scan/${scanId}/command`, { method: 'POST', body: JSON.stringify({ command, reason }), }), // Approvals getPendingApprovals: () => fetchApi('/approvals'), approveRequest: (approvalId) => fetchApi(`/approvals/${approvalId}/approve`, { method: 'POST', }), rejectRequest: (approvalId) => fetchApi(`/approvals/${approvalId}/reject`, { method: 'POST', }), // Monitors getMonitors: () => fetchApi('/monitors'), createDefaultMonitors: (scanId) => fetchApi('/monitors/create-defaults', { method: 'POST', body: JSON.stringify({ scan_id: scanId }), }), createSuggestedMonitors: (scanId, monitorIndices) => fetchApi('/monitors/create-suggested', { method: 'POST', body: JSON.stringify({ scan_id: scanId, monitors: monitorIndices }), }), deployPushScript: (monitorId, hostname, pushMetric, options = {}) => fetchApi(`/monitors/${monitorId}/deploy-script`, { method: 'POST', body: JSON.stringify({ hostname, push_metric: pushMetric, username: options.username || 'root', port: options.port || 22, interval_minutes: options.intervalMinutes || 5, }), }), deployAllPushScripts: (monitors) => fetchApi('/monitors/deploy-all-scripts', { method: 'POST', body: JSON.stringify({ monitors }), }), // Uptime Kuma testKumaConnection: () => fetchApi('/kuma/test'), getKumaAuthStatus: () => fetchApi('/kuma/auth'), kumaLogin: (username, password, totp) => fetchApi('/kuma/login', { method: 'POST', body: JSON.stringify({ username, password, totp }), }), kumaLogout: () => fetchApi('/kuma/logout', { method: 'POST' }), // Sync triggerSync: () => fetchApi('/sync', { method: 'POST' }), syncHost: (hostname) => fetchApi(`/sync/host/${encodeURIComponent(hostname)}`, { method: 'POST' }), // Hosts and tracked monitors getHosts: () => fetchApi('/hosts'), getHostMonitors: (hostname) => fetchApi(`/hosts/${encodeURIComponent(hostname)}/monitors`), getTrackedMonitors: () => fetchApi('/monitors/tracked'), };