37 lines
1.7 KiB
TypeScript
37 lines
1.7 KiB
TypeScript
import type {
|
|
ApiEnvelope,
|
|
DailyMileageRow,
|
|
DashboardSummary,
|
|
HistoryLocationRow,
|
|
OpsHealth,
|
|
Page,
|
|
QualityIssueRow,
|
|
RawFrameRow,
|
|
RealtimeLocationRow,
|
|
VehicleCoverageRow,
|
|
VehicleDetail,
|
|
VehicleRow
|
|
} from './types';
|
|
|
|
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
const response = await fetch(path, init);
|
|
if (!response.ok) {
|
|
throw new Error(`request failed ${response.status}`);
|
|
}
|
|
const envelope = (await response.json()) as ApiEnvelope<T>;
|
|
return envelope.data;
|
|
}
|
|
|
|
export const api = {
|
|
dashboardSummary: () => request<DashboardSummary>('/api/dashboard/summary'),
|
|
vehicles: (params = new URLSearchParams()) => request<Page<VehicleRow>>(`/api/vehicles?${params.toString()}`),
|
|
vehicleCoverage: (params = new URLSearchParams()) => request<Page<VehicleCoverageRow>>(`/api/vehicles/coverage?${params.toString()}`),
|
|
vehicleDetail: (params = new URLSearchParams()) => request<VehicleDetail>(`/api/vehicles/detail?${params.toString()}`),
|
|
realtimeLocations: (params = new URLSearchParams()) => request<Page<RealtimeLocationRow>>(`/api/realtime/locations?${params.toString()}`),
|
|
historyLocations: (params = new URLSearchParams()) => request<Page<HistoryLocationRow>>(`/api/history/locations?${params.toString()}`),
|
|
rawFrames: (params = new URLSearchParams()) => request<Page<RawFrameRow>>(`/api/history/raw-frames?${params.toString()}`),
|
|
dailyMileage: (params = new URLSearchParams()) => request<Page<DailyMileageRow>>(`/api/mileage/daily?${params.toString()}`),
|
|
qualityIssues: (params = new URLSearchParams()) => request<Page<QualityIssueRow>>(`/api/quality/issues?${params.toString()}`),
|
|
opsHealth: () => request<OpsHealth>('/api/ops/health')
|
|
};
|