import {z} from 'zod' export type GetApiV1VehiclesArgs = {vin?: string | undefined, imei?: string | undefined, limit?: number | undefined, skip?: number | undefined, Authorization?: string | undefined, 'Content-Type'?: 'application/json' | undefined}; export type GetApiV1VehiclesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1VehiclesResponse = z.array(z.object({model: z.object({make: z.string(), name: z.string(), year: z.number()}), nickName: z.string(), standardEngine: z.string(), vin: z.string(), imei: z.string(), stats: z.object({localTimeZone: z.string(), odometer: z.number(), lastUpdated: z.string(), location: z.object({lat: z.number(), lon: z.number(), heading: z.number(), address: z.string()}).optional(), fuelLevel: z.number(), isRunning: z.boolean(), speed: z.number(), mil: z.object({milOn: z.boolean(), lastUpdated: z.string(), qualifiedDtcList: z.array(z.object({code: z.string(), name: z.array(z.string())})), battery: z.object({status: z.enum(["normal", "low", "critical"]), lastUpdated: z.string()})})})})); 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 }; /** * Search Vehicles * * GET /v1/vehicles */ export const getApiV1Vehicles = async (args: GetApiV1VehiclesArgs, options?: GetApiV1VehiclesOptions) => { const {baseUrl = "https://api.bouncie.dev", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.vin != null) query.append("vin", String(args.vin)) if (args.imei != null) query.append("imei", String(args.imei)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/vehicles${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(getApiV1VehiclesResponse, await res.json()) };