import {z} from 'zod' export type GetApiV1ResourceownerVehicles2VehicleInformationArgs = {identifier?: string | undefined, identifierType?: (0 | 1) | undefined}; export type GetApiV1ResourceownerVehicles2VehicleInformationOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const transportstyrelsenInfoModel = z.object({HasInformation: z.boolean().optional(), Make: z.string().optional(), Model: z.string().optional(), MakeImageUrl: z.string().optional(), ModelYear: z.number().int().optional(), BodyStyle: z.string().optional(), FuelType: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10)]).optional(), TransmissionType: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), TrailerHitch: z.boolean().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 }; /** * Returns the external information we have on the vehicle * * GET /v1/resourceowner/vehicles2/VehicleInformation */ export const getApiV1ResourceownerVehicles2VehicleInformation = async (args: GetApiV1ResourceownerVehicles2VehicleInformationArgs, options: GetApiV1ResourceownerVehicles2VehicleInformationOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.identifier != null) query.append("identifier", String(args.identifier)) if (args.identifierType != null) query.append("identifierType", String(args.identifierType)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/resourceowner/vehicles2/VehicleInformation${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(transportstyrelsenInfoModel, await res.json()) };