import {z} from 'zod' export type Tag = {key: string, value: string}; export type ConnectVehicleRequest = {vin: string, make: 'ABARTH' | 'ALFA ROMEO' | 'AUDI' | 'BMW' | 'CHRYSLER' | 'CITROEN' | 'CUPRA' | 'DODGE' | 'DS AUTOMOBILES' | 'FIAT' | 'FORD' | 'HYUNDAI' | 'JEEP' | 'KIA' | 'LANCIA' | 'LEXUS' | 'MASERATI' | 'MERCEDES BENZ' | 'MINI' | 'NISSAN' | 'OPEL' | 'PEUGEOT' | 'POLESTAR' | 'RAM' | 'RENAULT' | 'SEAT' | 'SKODA' | 'TESLA' | 'TOYOTA' | 'VAUXHALL' | 'VOLKSWAGEN' | 'VOLVO', countryCode: string | null, registrationPlate?: (string | null) | undefined, model?: (string | null) | undefined, modelYear?: (number | null) | undefined, batteryCapacity?: (number | null) | undefined, horsepower?: (number | null) | undefined, generation?: (string | null) | undefined, wltpRange?: (number | null) | undefined, registrationDate?: (string | null) | undefined, tags?: (Array | null) | undefined}; export type BulkConnectVehicleRequest = {vehicles: Array}; export type CreateApiV1VehiclesConnectArgs = {body: BulkConnectVehicleRequest}; export type CreateApiV1VehiclesConnectOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const vehicleInputGroupStatusEnum = z.enum(["CREATED", "PROCESSING", "COMPLETED", "FAILED"]); export const bulkConnectVehicleResponse = z.object({id: z.string(), status: vehicleInputGroupStatusEnum, createdAt: 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 }; /** * Bulk Vin Connect * * POST /v1/vehicles/connect */ export const createApiV1VehiclesConnect = async (args: CreateApiV1VehiclesConnectArgs, options?: CreateApiV1VehiclesConnectOptions) => { const {baseUrl = "https://api.volteras.com", fetch: fetchFn = fetch, OAuth2ClientCredentialsBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2ClientCredentialsBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/vehicles/connect${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(bulkConnectVehicleResponse, await res.json()) };