import {z} from 'zod' export type GetApiImagesArgs = {make: string, model: string, year?: string | undefined, trim?: string | undefined, color?: string | undefined, transparent?: boolean | undefined, angle?: ('front' | 'side' | 'back') | undefined, photoType?: ('interior' | 'exterior' | 'engine') | undefined, size?: ('Small' | 'Medium' | 'Large' | 'Wallpaper' | 'All') | undefined, license?: string | undefined, format?: ('json' | 'xml') | undefined}; export type GetApiImagesOptions = {baseUrl?: string | undefined, ApiKeyQuery?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiImagesResponse = z.object({success: z.boolean().optional(), error: z.string().optional(), images: z.array(z.object({mime: z.string().optional(), link: z.string().optional(), contextLink: z.string().optional(), height: z.number().int().optional(), width: z.number().int().optional(), byteSize: z.number().int().optional(), thumbnailLink: z.string().optional(), accentColor: z.string().optional()}).and(z.record(z.string(), z.unknown()))).optional(), query: z.record(z.string(), z.unknown()).optional()}).and(z.record(z.string(), z.unknown())); 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 * * GET /images * * Also accepts auth: X402Payment */ export const getApiImages = async (args: GetApiImagesArgs, options?: GetApiImagesOptions) => { const {baseUrl = "https://api.carsxe.com", fetch: fetchFn = fetch, ApiKeyQuery} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.make != null) query.append("make", String(args.make)) if (args.model != null) query.append("model", String(args.model)) if (args.year != null) query.append("year", String(args.year)) if (args.trim != null) query.append("trim", String(args.trim)) if (args.color != null) query.append("color", String(args.color)) if (args.transparent != null) query.append("transparent", String(args.transparent)) if (args.angle != null) query.append("angle", String(args.angle)) if (args.photoType != null) query.append("photoType", String(args.photoType)) if (args.size != null) query.append("size", String(args.size)) if (args.license != null) query.append("license", String(args.license)) if (args.format != null) query.append("format", String(args.format)) if (ApiKeyQuery !== undefined) query.set("key", ApiKeyQuery) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiImagesResponse, await res.json()) };