import type { ApiEnvelope, DailyMileageRow, DashboardSummary, HistoryLocationRow, MileageSummary, OnlineStatisticsSummary, OnlineVehicleStatusRow, OpsHealth, QualityNotificationPlan, Page, QualitySummary, QualityIssueRow, RawFrameRow, RealtimeLocationRow, VehicleRealtimeRow, VehicleCoverageRow, VehicleCoverageSummary, VehicleDetail, VehicleIdentityResolution, VehicleServiceOverview, VehicleServiceSummary, VehicleRow } from './types'; export type RawFrameQuery = { keyword?: string; vin?: string; protocol?: string; dateFrom?: string; dateTo?: string; fields?: string[]; includeFields?: boolean; limit?: number; offset?: number; }; export type VehicleOverviewBatchQuery = { keywords: string[]; protocol?: string; limit?: number; offset?: number; }; type ApiErrorEnvelope = { error?: { code?: string; message?: string; detail?: string; }; traceId?: string; }; async function request(path: string, init?: RequestInit): Promise { const response = await fetch(path, init); if (!response.ok) { throw new Error(await responseErrorMessage(response)); } const envelope = (await response.json()) as ApiEnvelope; return envelope.data; } async function responseErrorMessage(response: Response) { try { const envelope = (await response.json()) as ApiErrorEnvelope; const message = envelope.error?.message?.trim(); const detail = envelope.error?.detail?.trim(); const traceID = envelope.traceId?.trim(); if (message && detail) { return withTraceID(`${message}: ${detail}`, traceID); } if (message) { return withTraceID(message, traceID); } } catch { // Fall back to status when the server does not return the platform envelope. } return `request failed ${response.status}`; } function withTraceID(message: string, traceID?: string) { return traceID ? `${message} (traceId: ${traceID})` : message; } export const api = { dashboardSummary: () => request('/api/dashboard/summary'), vehicles: (params = new URLSearchParams()) => request>(`/api/vehicles?${params.toString()}`), vehicleResolve: (params = new URLSearchParams()) => request(`/api/vehicles/resolve?${params.toString()}`), vehicleCoverage: (params = new URLSearchParams()) => request>(`/api/vehicles/coverage?${params.toString()}`), vehicleCoverageSummary: (params = new URLSearchParams()) => request(`/api/vehicles/coverage/summary?${params.toString()}`), vehicleDetail: (params = new URLSearchParams()) => request(`/api/vehicle-service?${params.toString()}`), vehicleServiceSummary: () => request('/api/vehicle-service/summary'), vehicleServiceOverview: (params = new URLSearchParams()) => request(`/api/vehicle-service/overview?${params.toString()}`), vehicleServiceOverviews: (query: VehicleOverviewBatchQuery) => request>('/api/vehicle-service/overviews', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(query) }), vehicleRealtime: (params = new URLSearchParams()) => request>(`/api/realtime/vehicles?${params.toString()}`), realtimeLocations: (params = new URLSearchParams()) => request>(`/api/realtime/locations?${params.toString()}`), historyLocations: (params = new URLSearchParams()) => request>(`/api/history/locations?${params.toString()}`), rawFrames: (params = new URLSearchParams()) => request>(`/api/history/raw-frames?${params.toString()}`), rawFramesQuery: (query: RawFrameQuery) => request>('/api/history/raw-frames/query', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(query) }), mileageSummary: (params = new URLSearchParams()) => request(`/api/mileage/summary?${params.toString()}`), dailyMileage: (params = new URLSearchParams()) => request>(`/api/mileage/daily?${params.toString()}`), onlineStatisticsSummary: (params = new URLSearchParams()) => request(`/api/statistics/online-summary?${params.toString()}`), onlineVehicleStatuses: (params = new URLSearchParams()) => request>(`/api/statistics/online-vehicles?${params.toString()}`), qualitySummary: (params = new URLSearchParams()) => request(`/api/quality/summary?${params.toString()}`), qualityIssues: (params = new URLSearchParams()) => request>(`/api/quality/issues?${params.toString()}`), qualityNotificationPlan: (params = new URLSearchParams()) => request(`/api/quality/notification-plan?${params.toString()}`), alertEventSummary: (params = new URLSearchParams()) => request(`/api/alert-events/summary?${params.toString()}`), alertEvents: (params = new URLSearchParams()) => request>(`/api/alert-events?${params.toString()}`), alertEventNotificationPlan: (params = new URLSearchParams()) => request(`/api/alert-events/notification-plan?${params.toString()}`), opsHealth: () => request('/api/ops/health') };