import {z} from 'zod' export type GetApiV2MarketvalueArgs = {vin?: string | undefined, id?: string | undefined, format?: ('json' | 'xml' | 'html') | undefined, mileage?: string | undefined, period?: number | undefined, type?: ('retail' | 'tradein') | undefined, condition?: ('average' | 'like_new' | 'not_great') | undefined, known_damage?: ('frame_damage' | 'airbags_deployed' | 'salvage' | 'brand' | 'rollback' | 'minor_damage' | 'lemon' | 'rental' | 'prior_theft') | undefined, country?: ('usa' | 'canada') | undefined}; export type GetApiV2MarketvalueOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const marketValue = z.object({success: z.boolean().optional(), vin: z.string().nullable().optional(), id: z.string().optional(), vehicle: z.string().optional(), mileage: z.number().optional(), count: z.number().int().optional(), mean: z.number().optional(), stdev: z.number().optional(), certainty: z.number().int().optional(), period: z.array(z.string()).optional(), prices: z.object({average: z.number().optional(), below: z.number().optional(), above: z.number().optional(), distribution: z.array(z.object({group: z.object({min: z.number().optional(), max: z.number().optional(), count: z.number().int().optional()}).optional()})).optional()}).optional(), adjustments: z.object({mileage: z.record(z.string(), z.unknown()).optional(), history: z.record(z.string(), z.unknown()).optional(), condition: z.record(z.string(), z.unknown()).optional(), known_damage: z.record(z.string(), z.unknown()).optional()}).optional(), type: z.enum(["retail", "wholesale", "tradein"]).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 }; /** * Market Values API * * GET /v2/marketvalue */ export const getApiV2Marketvalue = async (args: GetApiV2MarketvalueArgs, options?: GetApiV2MarketvalueOptions) => { 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.mileage != null) query.append("mileage", String(args.mileage)) if (args.period != null) query.append("period", String(args.period)) if (args.type != null) query.append("type", String(args.type)) if (args.condition != null) query.append("condition", String(args.condition)) if (args.known_damage != null) query.append("known_damage", String(args.known_damage)) if (args.country != null) query.append("country", String(args.country)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/marketvalue${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(marketValue, await res.json()) };