import {z} from 'zod' export type GetApiV3ImagesArgs = {vin?: string | undefined, id?: string | undefined, format?: ('json' | 'xml' | 'html') | undefined, pose?: ('front_left' | 'front_right' | 'front' | 'back_left' | 'back_right' | 'back' | 'right' | 'left') | undefined, color?: ('white' | 'black' | 'gray' | 'grey' | 'silver' | 'blue' | 'red' | 'green' | 'brown' | 'beige' | 'gold' | 'orange' | 'purple' | 'yellow') | undefined, background?: ('white' | 'gallery') | undefined, size?: string | undefined, granularity?: ('trim' | 'model') | undefined, crop?: boolean | undefined}; export type GetApiV3ImagesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3ImagesResponse = z.object({id: z.string().optional(), success: z.boolean().optional(), error: z.string().optional(), images: z.array(z.object({content_type: z.string().optional(), data: z.string().optional()})).optional(), ymmt: z.object({year: z.string().optional(), make: z.string().optional(), model: z.string().optional(), trim: z.string().optional()}).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 }; /** * Vehicle Images API * * GET /v3/images */ export const getApiV3Images = async (args: GetApiV3ImagesArgs, options?: GetApiV3ImagesOptions) => { const {baseUrl = "https://api.vinaudit.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.vin != null) query.append("vin", String(args.vin)) if (args.id != null) query.append("id", String(args.id)) if (args.format != null) query.append("format", String(args.format)) if (args.pose != null) query.append("pose", String(args.pose)) if (args.color != null) query.append("color", String(args.color)) if (args.background != null) query.append("background", String(args.background)) if (args.size != null) query.append("size", String(args.size)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.crop != null) query.append("crop", String(args.crop)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/images${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(getApiV3ImagesResponse, await res.json()) };