import {z} from 'zod' export type GetApiV1ListingsArgs = {format?: ('json' | 'xml') | undefined, vin?: string | undefined, vehicle_make?: string | undefined, spec_vin?: string | undefined, spec_vin_match?: ('model' | 'trim') | undefined, spec_id?: string | undefined, listing_type?: string | undefined, listing_price_min?: number | undefined, listing_price_max?: number | undefined, postal?: string | undefined, latitude?: number | undefined, longitude?: number | undefined, radius?: number | undefined, city?: string | undefined, region?: string | undefined, country?: ('usa' | 'canada') | undefined, seller_type?: ('franchise' | 'independent' | 'private') | undefined, seller_id?: string | undefined, dealer_attribution?: number | undefined, spec_accuracy?: number | undefined, listing_status?: string | undefined, history_days?: number | undefined, vehicle_type?: ('sedan' | 'coupe' | 'truck' | 'suv' | 'hatchback' | 'van' | 'wagon' | 'convertible' | 'minivan') | undefined, vehicle_fuel_type?: ('diesel' | 'electric' | 'flex' | 'gasoline' | 'hybrid' | 'hydrogen' | 'plug-in') | undefined, vehicle_engine_cylinders?: number | undefined, vehicle_transmission_type?: ('automatic' | 'manual') | undefined, vehicle_drivetrain?: ('rwd' | 'fwd' | 'awd' | '4wd') | undefined, vehicle_doors?: number | undefined, page_size?: number | undefined, page?: number | undefined}; export type GetApiV1ListingsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ListingsResponse = z.object({total: z.number().int().optional(), query_total: z.number().int().optional(), page: z.number().int().optional(), page_size: z.number().int().optional(), listings: z.array(z.record(z.string(), z.unknown())).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 Listings API * * GET /v1/listings */ export const getApiV1Listings = async (args: GetApiV1ListingsArgs, options?: GetApiV1ListingsOptions) => { 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.format != null) query.append("format", String(args.format)) if (args.vin != null) query.append("vin", String(args.vin)) if (args.vehicle_make != null) query.append("vehicle_make", String(args.vehicle_make)) if (args.spec_vin != null) query.append("spec_vin", String(args.spec_vin)) if (args.spec_vin_match != null) query.append("spec_vin_match", String(args.spec_vin_match)) if (args.spec_id != null) query.append("spec_id", String(args.spec_id)) if (args.listing_type != null) query.append("listing_type", String(args.listing_type)) if (args.listing_price_min != null) query.append("listing_price_min", String(args.listing_price_min)) if (args.listing_price_max != null) query.append("listing_price_max", String(args.listing_price_max)) if (args.postal != null) query.append("postal", String(args.postal)) if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.city != null) query.append("city", String(args.city)) if (args.region != null) query.append("region", String(args.region)) if (args.country != null) query.append("country", String(args.country)) if (args.seller_type != null) query.append("seller_type", String(args.seller_type)) if (args.seller_id != null) query.append("seller_id", String(args.seller_id)) if (args.dealer_attribution != null) query.append("dealer_attribution", String(args.dealer_attribution)) if (args.spec_accuracy != null) query.append("spec_accuracy", String(args.spec_accuracy)) if (args.listing_status != null) query.append("listing_status", String(args.listing_status)) if (args.history_days != null) query.append("history_days", String(args.history_days)) if (args.vehicle_type != null) query.append("vehicle_type", String(args.vehicle_type)) if (args.vehicle_fuel_type != null) query.append("vehicle_fuel_type", String(args.vehicle_fuel_type)) if (args.vehicle_engine_cylinders != null) query.append("vehicle_engine_cylinders", String(args.vehicle_engine_cylinders)) if (args.vehicle_transmission_type != null) query.append("vehicle_transmission_type", String(args.vehicle_transmission_type)) if (args.vehicle_drivetrain != null) query.append("vehicle_drivetrain", String(args.vehicle_drivetrain)) if (args.vehicle_doors != null) query.append("vehicle_doors", String(args.vehicle_doors)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page != null) query.append("page", String(args.page)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/listings${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(getApiV1ListingsResponse, await res.json()) };