import {z} from 'zod' export type GetApiV1VehiclesVehicleIdOptionsArgs = {vehicle_id: string}; export type GetApiV1VehiclesVehicleIdOptionsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const vehicleConfig = z.object({exteriorColor: z.string().nullable().optional(), chargePortType: z.string().nullable().optional(), driverAssist: z.string().nullable().optional(), efficiencyPackage: z.string().nullable().optional(), hasAirSuspension: z.boolean().nullable().optional(), hasLudicrousMode: z.boolean().nullable().optional(), hasSeatCooling: z.boolean().nullable().optional(), headlampType: z.string().nullable().optional(), interiorTrimType: z.string().nullable().optional(), motorizedChargePort: z.boolean().nullable().optional(), rearDriveUnit: z.string().nullable().optional(), rhd: z.boolean().nullable().optional(), spoilerType: z.string().nullable().optional(), wheelType: z.string().nullable().optional()}); export const option = z.object({code: z.string(), description: z.string().nullable().optional()}); export const vehicleOptions = z.object({vehicleId: z.string(), vehicleConfig: vehicleConfig, options: z.array(option)}); 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 }; /** * Get Vehicle Options * * GET /v1/vehicles/{vehicle_id}/options */ export const getApiV1VehiclesVehicleIdOptions = async (args: GetApiV1VehiclesVehicleIdOptionsArgs, options?: GetApiV1VehiclesVehicleIdOptionsOptions) => { const {baseUrl = "https://api.volteras.com", fetch: fetchFn = fetch, OAuth2ClientCredentialsBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2ClientCredentialsBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/vehicles/${encodeURIComponent(String(args.vehicle_id))}/options${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(vehicleOptions, await res.json()) };