import {z} from 'zod' export type RegistrationNumber = string; export type ChassisVin = string; export type KadoeRequest = {enquirerId: string, linkProviderId?: string | undefined, reasonCode: string, registrationNumber?: RegistrationNumber | undefined, chassisVin?: ChassisVin | undefined, eventDate: unknown, referenceNumber: string}; export type CreateApiV1VehicleKeeperArgs = {body: KadoeRequest}; export type CreateApiV1VehicleKeeperOptions = {baseUrl?: string | undefined, jwtAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const registrationNumber = z.string(); export const chassisVin = z.string(); export const address = z.object({line1: z.string().optional(), line2: z.string().optional(), line3: z.string().optional(), line4: z.string().optional(), line5: z.string().optional(), postcode: z.string().optional()}); export const keeper = z.object({title: z.string().optional(), firstNames: z.string().optional(), lastName: z.string().optional(), companyName: z.string().optional(), address: address.optional()}); export const kadoeResponse = z.object({registrationNumber: registrationNumber.optional(), chassisVin: chassisVin.optional(), make: z.string().optional(), model: z.string().optional(), fleetNumber: z.string().optional(), colour: z.string().optional(), secondaryColour: z.string().optional(), keeper: keeper.optional(), message: z.string().optional(), taxStatus: z.enum(["Not Taxed for on Road Use", "SORN", "Untaxed", "Taxed"]).optional(), seatingCapacity: z.number().int().optional(), bodyType: z.string().optional(), taxClass: z.string().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 }; /** * Retrieve Keeper at Date of Event for a vehicle. * * POST /v1/vehicle-keeper * * Also accepts auth: apiKeyAuth */ export const createApiV1VehicleKeeper = async (args: CreateApiV1VehicleKeeperArgs, options?: CreateApiV1VehicleKeeperOptions) => { const {baseUrl = "https://driver-vehicle-licensing.api.gov.uk/kadoe", fetch: fetchFn = fetch, jwtAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (jwtAuth !== undefined) headers.set('authorization', `Bearer ${jwtAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/vehicle-keeper${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(kadoeResponse, await res.json()) };