import {z} from 'zod' export type GetApiV1VehiclesArgs = {maxPageSize?: number | undefined, pageToken?: (string | null) | undefined, filter?: (string | null) | undefined, tagKey?: (string | null) | undefined, tagValue?: (string | null) | undefined}; export type GetApiV1VehiclesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const vehicleMetaData = z.object({lastSeenAt: z.string().nullable(), consentId: z.string().nullable(), accountId: z.string().nullable(), registrationDate: z.string().nullable().optional()}); export const investigationState = z.enum(["UNDER_REVIEW", "CONTACT_OEM", "CUSTOMER_ACTION", "RETRY", "ESCALATED_INTERNALLY", "CONFIRMED_INCOMPATIBLE", "SELF_RECOVERED"]); export const investigationOemSubState = z.enum(["NEEDED", "REQUESTED", "ONGOING"]); export const vehicle = z.object({make: z.string(), model: z.string().nullable().optional(), trim: z.string().nullable().optional(), modelYear: z.string().nullable().optional(), vin: z.string(), registrationPlate: z.string().nullable().optional(), isRegNoValidated: z.boolean().nullable().optional(), regNoValidatedAt: z.string().nullable().optional(), id: z.string(), metadata: vehicleMetaData, isListening: z.boolean().optional(), listeningLastUpdated: z.string().nullable().optional(), connectionStatus: z.string().nullable().optional(), connectionErrorCode: z.string().nullable().optional(), connectionErrorMessage: z.string().nullable().optional(), actionCategory: z.string().nullable().optional(), investigationState: investigationState.nullable().optional(), investigationSubState: investigationOemSubState.nullable().optional(), investigationTitle: z.string().nullable().optional(), investigationMessage: z.string().nullable().optional(), investigationUpdatedAt: z.string().nullable().optional()}); export const pageVehicle = z.object({results: z.array(vehicle), nextPageToken: z.string().nullable(), totalResults: z.number().int()}); 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 Vehicles * * GET /v1/vehicles */ export const getApiV1Vehicles = async (args: GetApiV1VehiclesArgs, options?: GetApiV1VehiclesOptions) => { 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 (args.maxPageSize != null) query.append("maxPageSize", String(args.maxPageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.tagKey != null) query.append("tagKey", String(args.tagKey)) if (args.tagValue != null) query.append("tagValue", String(args.tagValue)) if (OAuth2ClientCredentialsBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsBearer}`) 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(pageVehicle, await res.json()) };