import {z} from 'zod' export type GetApiV2ModificationsArgs = {make: string, model: string, year?: number | undefined, generation?: string | undefined, region?: (Array<'usdm' | 'cdm' | 'mxndm' | 'ladm' | 'eudm' | 'russia' | 'jdm' | 'chdm' | 'skdm' | 'sam' | 'medm' | 'nadm' | 'sadm' | 'audm'>) | undefined, lang?: string | undefined, fuel?: ('biodiesel_blend' | 'cng' | 'diesel' | 'e100' | 'electric' | 'ethanol_blend' | 'flex_fuel' | 'h2' | 'hybrid' | 'lpg' | 'petrol' | 'petrol_cng' | 'petrol_lpg') | undefined, horsepower?: number | undefined, horsepower_min?: number | undefined, horsepower_max?: number | undefined, trim?: string | undefined, trim_level?: string | undefined, ordering?: string | undefined}; export type GetApiV2ModificationsOptions = {baseUrl: string, userKey?: string | undefined, fetch?: typeof fetch | undefined}; export const makesDataBase = z.object({slug: z.string(), name: z.string(), name_en: z.string()}); export const modelsDataBase = z.object({slug: z.string(), name: z.string(), name_en: z.string()}); export const body = z.object({slug: z.string(), name: z.string(), image: z.string()}); export const generation = z.object({slug: z.string(), name: z.string(), platform: z.string(), start: z.number().int(), end: z.number().int(), bodies: z.array(body)}); export const power = z.object({kW: z.number().optional(), PS: z.number().int().optional(), hp: z.number().int().optional()}); export const engine = z.object({fuel: z.string().optional(), capacity: z.string(), type: z.string(), power: power.nullable(), code: z.string()}); export const combustionEngineEnum = z.enum(["present", "not_applicable", "unknown"]); export const electrificationLevelEnum = z.enum(["mild_hybrid", "full_hybrid", "phev", "erev", "bev", "fcev", "not_applicable", "not_reported", "unknown"]); export const fuelRef = z.object({code: z.string(), title: z.string()}); export const enginePower = z.object({kW: z.number().optional(), PS: z.number().int().optional(), hp: z.number().int().optional()}); export const systemPower = z.object({kW: z.number().optional(), PS: z.number().int().optional(), hp: z.number().int().optional()}); export const engineSecondaryPower = z.object({kW: z.number().optional(), PS: z.number().int().optional(), hp: z.number().int().optional()}); export const motorPower = z.object({kW: z.number().optional(), PS: z.number().int().optional(), hp: z.number().int().optional()}); export const motor = z.object({axle: z.string(), power: motorPower.nullable(), code: z.string().nullable().optional()}); export const powertrain = z.object({combustion_engine: combustionEngineEnum, electrification_level: electrificationLevelEnum, primary_fuel: fuelRef, secondary_fuel: fuelRef, engine_power: enginePower.nullable(), system_power: systemPower.nullable(), engine_power_secondary: engineSecondaryPower.nullable(), motors: z.array(motor)}); export const modificationsData = z.object({slug: z.string(), name: z.string(), body: z.string(), trim: z.string(), trim_scoring: z.number().nullable().optional(), trim_attributes: z.string(), trim_body_types: z.string(), trim_levels: z.string(), make: makesDataBase, model: modelsDataBase, generation: generation, start_year: z.number().int(), end_year: z.number().int(), engine: engine, powertrain: powertrain, regions: z.string()}); export const horsepowerRange = z.object({min: z.number().int(), max: z.number().int()}); export const modificationsEngineMeta = z.object({fuels: z.array(z.string()), horsepower: horsepowerRange}); export const modificationsMeta = z.object({count: z.number().int(), engine: modificationsEngineMeta, debug: z.record(z.string(), z.unknown()).nullable()}); export const modificationsReportOutput = z.object({data: z.array(modificationsData), meta: modificationsMeta}); 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 }; /** * List modifications * * GET /v2/modifications/ */ export const getApiV2Modifications = async (args: GetApiV2ModificationsArgs, options: GetApiV2ModificationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, userKey} = 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.generation != null) query.append("generation", String(args.generation)) for (const item of args.region ?? []) query.append("region", String(item)) if (args.lang != null) query.append("lang", String(args.lang)) if (args.fuel != null) query.append("fuel", String(args.fuel)) if (args.horsepower != null) query.append("horsepower", String(args.horsepower)) if (args.horsepower_min != null) query.append("horsepower_min", String(args.horsepower_min)) if (args.horsepower_max != null) query.append("horsepower_max", String(args.horsepower_max)) if (args.trim != null) query.append("trim", String(args.trim)) if (args.trim_level != null) query.append("trim_level", String(args.trim_level)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (userKey !== undefined) query.set("user_key", userKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/modifications/${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(modificationsReportOutput, await res.json()) };