import {z} from 'zod' export type GetApiV2ByRimBpArgs = {rim_diameter?: number | undefined, rim_width?: number | undefined, rim_offset_min?: number | undefined, rim_offset_max?: number | undefined, cb_min?: number | undefined, cb_max?: number | undefined, stud_holes?: number | undefined, pcd?: number | undefined, region?: (Array<'usdm' | 'cdm' | 'mxndm' | 'ladm' | 'eudm' | 'russia' | 'jdm' | 'chdm' | 'skdm' | 'sam' | 'medm' | 'nadm' | 'sadm' | 'audm'>) | undefined, ordering?: string | undefined}; export type GetApiV2ByRimBpOptions = {baseUrl: string, userKey?: string | undefined, fetch?: typeof fetch | undefined}; export const boltPatternData = z.object({value: z.string(), total: z.number().int()}); export const searchParamsMeta = z.object({count: z.number().int()}); export const boltPatternReportOutput = z.object({data: z.array(boltPatternData), meta: searchParamsMeta}); 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 rim bolt patterns * * GET /v2/by_rim/bp/ */ export const getApiV2ByRimBp = async (args: GetApiV2ByRimBpArgs, options: GetApiV2ByRimBpOptions) => { const {baseUrl, fetch: fetchFn = fetch, userKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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_min != null) query.append("rim_offset_min", String(args.rim_offset_min)) if (args.rim_offset_max != null) query.append("rim_offset_max", String(args.rim_offset_max)) if (args.cb_min != null) query.append("cb_min", String(args.cb_min)) if (args.cb_max != null) query.append("cb_max", String(args.cb_max)) if (args.stud_holes != null) query.append("stud_holes", String(args.stud_holes)) if (args.pcd != null) query.append("pcd", String(args.pcd)) for (const item of args.region ?? []) query.append("region", String(item)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (userKey !== undefined) query.set("user_key", userKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/by_rim/bp/${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(boltPatternReportOutput, await res.json()) };