import {z} from 'zod' export type GetApiV1SpeedingEventsArgs = {driver_ids?: number | undefined, vehicle_ids?: number | undefined, start_date?: string | undefined, end_date?: string | undefined}; export type GetApiV1SpeedingEventsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1SpeedingEventsResponse = z.object({speeding_events: z.array(z.object({speeding_event: z.object({speeding_distance_in_km: z.number().optional(), max_over_speed_in_kph: z.number().optional(), avg_over_speed_in_kph: z.number().optional(), min_posted_speed_limit_in_kph: z.number().optional(), max_posted_speed_limit_in_kph: z.number().optional(), avg_vehicle_speed: z.number().int().optional(), min_vehicle_speed: z.number().int().optional(), max_vehicle_speed: z.number().int().optional(), id: z.number().int().optional(), duration: z.number().int().optional(), start_lat: z.number().optional(), start_lon: z.number().optional(), end_lat: z.number().optional(), end_lon: z.number().optional(), type: z.string().optional(), start_time: z.string().optional(), end_time: z.string().optional(), coaching_status: z.string().optional(), status: z.string().optional(), driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), deactivated_at: z.unknown(), driver_company_id: z.string().optional()}).optional(), eld_device: z.object({id: z.number().int().optional(), identifier: z.string().optional(), model: z.string().optional()}).optional(), vehicle: z.object({id: z.number().int().optional(), number: z.string().optional(), status: z.string().optional(), metric_units: z.boolean().optional()}).optional(), metadata: z.object({severity: z.string().optional(), trigger: z.string().optional(), is_manually_changed: z.boolean().optional()}).optional()}).optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Fetch the company's speeding events * * GET /v1/speeding_events */ export const getApiV1SpeedingEvents = async (args: GetApiV1SpeedingEventsArgs, options?: GetApiV1SpeedingEventsOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.driver_ids != null) query.append("driver_ids", String(args.driver_ids)) if (args.vehicle_ids != null) query.append("vehicle_ids", String(args.vehicle_ids)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/speeding_events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1SpeedingEventsResponse, await res.json()) };