import {z} from 'zod' export type Path = string; export type BatchRequests = Array; export type CreateApiVehiclesVehicleIdBatchArgs = {body: BatchRequests}; export type CreateApiVehiclesVehicleIdBatchOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const capability = z.object({permission: z.string().optional(), endpoint: z.string().optional(), capable: z.boolean().optional(), reason: z.string().nullable().optional()}); export const header = z.object({"sc-unit-system": z.string().optional(), "sc-data-age": z.string().optional()}); export const response = z.object({code: z.enum(["200", "500"]).optional(), path: z.string().optional(), body: z.union([z.object({latitude: z.number(), longitude: z.number()}), z.object({distance: z.number().optional()}), z.object({range: z.number().optional(), percentRemaining: z.number().optional(), amountRemaining: z.number().optional()}), z.object({frontLeft: z.number().optional(), frontRight: z.number().optional(), backLeft: z.number().optional(), backRight: z.number().optional()}), z.object({lifeRemaining: z.number().optional()}), z.object({isPluggedIn: z.boolean().optional(), state: z.enum(["CHARGING", "FULLY_CHARGED", "NOT_CHARGING"]).optional()}), z.object({limit: z.number().optional()}), z.object({time: z.string().optional()}), z.object({voltage: z.number().optional()}), z.object({percentRemaining: z.number().optional(), range: z.number().optional()}), z.object({capacity: z.number().optional()}), z.object({compatible: z.boolean().optional(), reason: z.string().nullable().optional(), capabilities: z.array(capability).optional()}), z.object({vin: z.string().optional()}), z.object({id: z.string().optional()}), z.object({status: z.enum(["SUCCESS", "FAILED"]).optional(), message: z.string().nullable().optional()})]).nullable().optional(), headers: z.array(header).optional()}); export const batchResponse = z.object({responses: z.array(response).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 }; /** * Batch * * POST /vehicles/{vehicle_id}/batch */ export const createApiVehiclesVehicleIdBatch = async (args: CreateApiVehiclesVehicleIdBatchArgs, options?: CreateApiVehiclesVehicleIdBatchOptions) => { const {baseUrl = "https://vehicle.api.smartcar.com/v3", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/vehicles/${encodeURIComponent(String(args.vehicle_id))}/batch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(batchResponse, await res.json()) };