import {z} from 'zod' export type GetApiV2ByRimRdArgs = {bolt_pattern?: string | undefined, region?: (Array<'usdm' | 'cdm' | 'mxndm' | 'ladm' | 'eudm' | 'russia' | 'jdm' | 'chdm' | 'skdm' | 'sam' | 'medm' | 'nadm' | 'sadm' | 'audm'>) | undefined, ordering?: string | undefined}; export type GetApiV2ByRimRdOptions = {baseUrl: string, userKey?: string | undefined, fetch?: typeof fetch | undefined}; export const floatSearchParamsData = z.object({value: z.number(), total: z.number().int()}); export const searchParamsMeta = z.object({count: z.number().int()}); export const diameterReportOutput = z.object({data: z.array(floatSearchParamsData), 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 diameters * * GET /v2/by_rim/rd/ */ export const getApiV2ByRimRd = async (args: GetApiV2ByRimRdArgs, options: GetApiV2ByRimRdOptions) => { 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)) 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/rd/${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(diameterReportOutput, await res.json()) };