import {z} from 'zod' export type GetApiV2ClassifiedByRimArgs = {bolt_pattern: string, rim_diameter: number, rim_width: number, rim_offset: number, cb?: number | undefined, fd?: number | undefined, fs_poke?: number | undefined, bs_push?: number | undefined, rim_bst_from?: number | undefined, rim_bst_to?: number | undefined, od_tolerance?: number | undefined, ow_tolerance?: number | undefined, diameter_range?: number | undefined, sort?: ('name' | 'fitment' | 'load') | undefined, ordering?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV2ClassifiedByRimOptions = {baseUrl: string, userKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tiresByRimData = z.object({value: z.string(), total: z.number().int(), params: z.string(), tire_list: z.string()}); export const tiresByRimMeta = z.object({count: z.number().int()}); export const tiresByRimReportOutput = z.object({data: z.array(tiresByRimData), meta: tiresByRimMeta}); 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 }; /** * Returns matched tires for the Rim * * GET /v2/classified/by_rim/ */ export const getApiV2ClassifiedByRim = async (args: GetApiV2ClassifiedByRimArgs, options: GetApiV2ClassifiedByRimOptions) => { const {baseUrl, fetch: fetchFn = fetch, userKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bolt_pattern != null) query.append("bolt_pattern", String(args.bolt_pattern)) if (args.rim_diameter != null) query.append("rim_diameter", String(args.rim_diameter)) if (args.rim_width != null) query.append("rim_width", String(args.rim_width)) if (args.rim_offset != null) query.append("rim_offset", String(args.rim_offset)) if (args.cb != null) query.append("cb", String(args.cb)) if (args.fd != null) query.append("fd", String(args.fd)) if (args.fs_poke != null) query.append("fs_poke", String(args.fs_poke)) if (args.bs_push != null) query.append("bs_push", String(args.bs_push)) if (args.rim_bst_from != null) query.append("rim_bst_from", String(args.rim_bst_from)) if (args.rim_bst_to != null) query.append("rim_bst_to", String(args.rim_bst_to)) if (args.od_tolerance != null) query.append("od_tolerance", String(args.od_tolerance)) if (args.ow_tolerance != null) query.append("ow_tolerance", String(args.ow_tolerance)) if (args.diameter_range != null) query.append("diameter_range", String(args.diameter_range)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (userKey !== undefined) query.set("user_key", userKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/classified/by_rim/${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(tiresByRimReportOutput, await res.json()) };