import {z} from 'zod' export type GetApiV3SelectionsArgs = {list?: string | undefined, id?: string | undefined, year?: number | undefined, make?: string | undefined, model?: string | undefined, trim?: string | undefined, style?: string | undefined, format?: ('json' | 'xml' | 'csv') | undefined, collection?: ('in-market' | 'popular' | 'all') | undefined}; export type GetApiV3SelectionsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const selectionNode: z.ZodType = z.object({name: z.string().optional(), id: z.string().optional(), makes: z.array(z.lazy(() => selectionNode)).optional(), models: z.array(z.lazy(() => selectionNode)).optional(), trims: z.array(z.lazy(() => selectionNode)).optional(), styles: z.array(z.lazy(() => selectionNode)).optional()}); export const getApiV3SelectionsResponse = z.object({selections: z.object({years: z.array(selectionNode).optional(), makes: z.array(selectionNode).optional()}).optional(), error: z.string().optional(), details: 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 }; /** * VIN Decoder API -- vehicle selections * * GET /v3/selections */ export const getApiV3Selections = async (args: GetApiV3SelectionsArgs, options?: GetApiV3SelectionsOptions) => { 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.list != null) query.append("list", String(args.list)) if (args.id != null) query.append("id", String(args.id)) if (args.year != null) query.append("year", String(args.year)) if (args.make != null) query.append("make", String(args.make)) if (args.model != null) query.append("model", String(args.model)) if (args.trim != null) query.append("trim", String(args.trim)) if (args.style != null) query.append("style", String(args.style)) if (args.format != null) query.append("format", String(args.format)) if (args.collection != null) query.append("collection", String(args.collection)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/selections${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(getApiV3SelectionsResponse, await res.json()) };