import {z} from 'zod' export type GetApiV1VehiclesConnectIdResultsArgs = {id: string, maxPageSize?: (number | null) | undefined, currentPage?: (number | null) | undefined}; export type GetApiV1VehiclesConnectIdResultsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const aPIVehicleUploadStatus = z.enum(["CONNECTED", "PROCESSING", "PENDING", "CONNECTION_REQUIRED", "INCOMPATIBLE", "ERROR", "DISCONNECTED"]); export const vehicleInputResponseError = z.object({code: z.enum(["OEM_INCORRECT_REGION", "OEM_VEHICLE_OFFLINE", "OEM_VEHICLE_REMOTE_ACCESS_DISABLED", "OEM_VEHICLE_DATA_SHARING_DISABLED", "FLEET_CONSENT_MISSING", "OEM_AUTH_DRIVER_IS_NOT_PRIMARY_USER", "OEM_DEVICE_NOT_FOUND", "OEM_VEHICLE_NOT_COMPATIBLE", "OEM_VEHICLE_UPDATE_REQUIRED", "OEM_ACTIVATION_PENDING", "OEM_VEHICLE_IN_TRANSPORT_MODE", "OEM_VEHICLE_ENROLLED_WITH_ANOTHER_PROVIDER", "DEVICE_LIMIT_REACHED", "NOT_PERMITTED", "VIN_UPLOAD_INVALID_FORMAT", "INVALID_VIN", "VIN_CHECK_NOT_SUPPORTED", "VEHICLE_ALREADY_ADDED_TO_ORGANIZATION", "VEHICLE_ALREADY_ADDED_CONNECTION_REQUIRED", "VEHICLE_ALREADY_ADDED_INCOMPATIBLE", "VEHICLE_ALREADY_ADDED_PENDING_CONNECTION", "VIN_UPLOAD_NOT_SUPPORTED", "VIN_BLACKLISTED", "SUBJECTS_NOT_ASSIGNABLE"]), message: z.string(), actionCategory: z.string().nullable().optional()}); export const tag = z.object({key: z.string().max(150), value: z.string().max(150)}); 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 vehicleInputResponse = z.object({vehicleId: z.string().nullable().optional(), vehicleInputId: z.string(), status: aPIVehicleUploadStatus, message: z.string(), errors: z.array(vehicleInputResponseError).optional(), canRetry: z.boolean().nullable().optional(), vin: z.string().nullable().optional(), make: z.string().nullable().optional(), model: z.string().nullable().optional(), countryCode: z.string().nullable().optional(), registrationPlate: z.string().nullable().optional(), modelYear: z.number().int().nullable().optional(), registrationDate: z.string().nullable().optional(), owners: z.array(z.string()).optional(), tags: z.array(tag).optional(), wasAlreadyConnected: z.boolean().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 pageResultsVehicleInputResponse = z.object({results: z.array(vehicleInputResponse).optional(), totalResults: z.number().int().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 }; /** * Bulk Vehicle Connect Vin Results * * GET /v1/vehicles/connect/{id}/results */ export const getApiV1VehiclesConnectIdResults = async (args: GetApiV1VehiclesConnectIdResultsArgs, options?: GetApiV1VehiclesConnectIdResultsOptions) => { 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.currentPage != null) query.append("currentPage", String(args.currentPage)) if (OAuth2ClientCredentialsBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/vehicles/connect/${encodeURIComponent(String(args.id))}/results${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(pageResultsVehicleInputResponse, await res.json()) };