import {z} from 'zod' export type GetApiV1VehicleLocationsArgs = {'vehicle_ids[]'?: Array | undefined, vehicle_status?: string | undefined, per_page?: number | undefined, page_no?: number | undefined, 'X-Time-Zone'?: string | undefined, 'X-Metric-Units'?: boolean | undefined, 'X-User-Id'?: number | undefined}; export type GetApiV1VehicleLocationsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1VehicleLocationsResponse = z.object({vehicles: z.array(z.object({vehicle: z.object({id: z.number().int().optional(), number: z.string().optional(), year: z.unknown(), make: z.unknown(), model: z.unknown(), vin: z.unknown(), fuel_type: z.string().optional(), current_location: z.object({id: z.string().optional(), lat: z.number().optional(), lon: z.number().optional(), description: z.string().optional(), located_at: z.string().optional(), bearing: z.number().int().optional(), battery_voltage: z.number().int().optional(), type: z.string().optional(), speed: z.number().optional(), engine_hours: z.number().optional(), fuel: z.number().optional(), fuel_primary_remaining_percentage: z.number().optional(), fuel_secondary_remaining_percentage: z.number().optional(), odometer: z.number().optional(), true_odometer: z.number().optional(), true_engine_hours: z.number().optional(), veh_range: z.number().optional(), hvb_state_of_charge: z.number().optional(), hvb_charge_status: z.number().int().optional(), hvb_charge_source: z.number().int().optional(), hvb_lifetime_energy_output: z.number().optional()}).optional(), current_driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), username: z.unknown(), email: z.string().optional(), driver_company_id: z.unknown(), status: z.string().optional(), role: z.string().optional()}).optional()}).optional()})).optional(), pagination: z.object({per_page: z.number().int().optional(), page_no: z.number().int().optional(), total: z.number().int().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 }; /** * List all the vehicles and their locations * * GET /v1/vehicle_locations */ export const getApiV1VehicleLocations = async (args: GetApiV1VehicleLocationsArgs, options?: GetApiV1VehicleLocationsOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['vehicle_ids[]'] ?? []) query.append("vehicle_ids[]", String(item)) if (args.vehicle_status != null) query.append("vehicle_status", String(args.vehicle_status)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page_no != null) query.append("page_no", String(args.page_no)) if (args['X-Time-Zone'] != null) headers.set("X-Time-Zone", String(args['X-Time-Zone'])) if (args['X-Metric-Units'] != null) headers.set("X-Metric-Units", String(args['X-Metric-Units'])) if (args['X-User-Id'] != null) headers.set("X-User-Id", String(args['X-User-Id'])) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/vehicle_locations${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(getApiV1VehicleLocationsResponse, await res.json()) };