// Health check service for backend connectivity export type HealthStatus = 'checking' | 'healthy' | 'unhealthy'; export interface HealthCheckResult { status: HealthStatus; latencyMs?: number; error?: string; } /** * Check backend health by calling the /api/status endpoint */ export async function checkBackendHealth(): Promise { const startTime = performance.now(); try { const response = await fetch('/api/status', { method: 'GET', headers: { 'Content-Type': 'application/json', }, // Short timeout for health checks signal: AbortSignal.timeout(5000), }); const latencyMs = Math.round(performance.now() - startTime); if (!response.ok) { return { status: 'unhealthy', latencyMs, error: `HTTP ${response.status}`, }; } const data = await response.json(); if (data.status === 'OK') { return { status: 'healthy', latencyMs, }; } return { status: 'unhealthy', latencyMs, error: 'Unexpected response', }; } catch (error) { const latencyMs = Math.round(performance.now() - startTime); if (error instanceof Error) { if (error.name === 'TimeoutError' || error.name === 'AbortError') { return { status: 'unhealthy', latencyMs, error: 'Request timeout', }; } return { status: 'unhealthy', latencyMs, error: error.message, }; } return { status: 'unhealthy', latencyMs, error: 'Connection failed', }; } }