import {z} from 'zod' export type GetApiV2VehicleLocationsIdArgs = {id: string, start_date: string, end_date: string, updated_after: string, 'X-Time-Zone'?: string | undefined, 'X-Metric-Units'?: boolean | undefined, 'X-User-Id'?: number | undefined}; export type GetApiV2VehicleLocationsIdOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2VehicleLocationsIdResponse = z.object({vehicle_locations: z.array(z.object({vehicle_location: z.object({id: z.string().optional(), located_at: z.string().optional(), lat: z.number().int().optional(), lon: z.number().int().optional(), bearing: z.number().optional(), battery_voltage: z.number().optional(), engine_hours: z.number().optional(), type: z.string().optional(), description: z.string().optional(), speed: z.number().optional(), odometer: z.number().optional(), fuel: z.number().optional(), fuel_primary_remaining_percentage: z.number().optional(), fuel_secondary_remaining_percentage: z.number().optional(), driver: z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), username: z.string().optional(), email: z.unknown(), driver_company_id: z.string().optional(), status: z.string().optional(), role: z.string().optional()}).optional(), eld_device: z.object({id: z.number().int().optional(), identifier: z.string().optional(), model: z.string().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 a vehicle's location using its ID (v2) * * GET /v2/vehicle_locations/{id} */ export const getApiV2VehicleLocationsId = async (args: GetApiV2VehicleLocationsIdArgs, options?: GetApiV2VehicleLocationsIdOptions) => { 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.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 (args.updated_after != null) query.append("updated_after", String(args.updated_after)) 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(/\/+$/, '')}/v2/vehicle_locations/${encodeURIComponent(String(args.id))}${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(getApiV2VehicleLocationsIdResponse, await res.json()) };