import {z} from 'zod' export type GetApiV2SpecMetadataArgs = {rim_diameter?: number | undefined, rim_width?: number | undefined, rim_offset?: number | undefined, bolt_pattern?: string | undefined, cb?: number | undefined, section_width?: number | undefined, aspect_ratio?: number | undefined, overall_diameter?: number | undefined, hints?: boolean | undefined}; export type GetApiV2SpecMetadataOptions = {baseUrl: string, userKey?: string | undefined, fetch?: typeof fetch | undefined}; export const specMetadataOutput = z.object({mode: z.string()}); 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 }; /** * Get computed geometry, population stats, and hints for a wheel/tire spec * * GET /v2/spec/metadata/ */ export const getApiV2SpecMetadata = async (args: GetApiV2SpecMetadataArgs, options: GetApiV2SpecMetadataOptions) => { 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 != null) query.append("rim_offset", String(args.rim_offset)) if (args.bolt_pattern != null) query.append("bolt_pattern", String(args.bolt_pattern)) if (args.cb != null) query.append("cb", String(args.cb)) if (args.section_width != null) query.append("section_width", String(args.section_width)) if (args.aspect_ratio != null) query.append("aspect_ratio", String(args.aspect_ratio)) if (args.overall_diameter != null) query.append("overall_diameter", String(args.overall_diameter)) if (args.hints != null) query.append("hints", String(args.hints)) if (userKey !== undefined) query.set("user_key", userKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/spec/metadata/${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(specMetadataOutput, await res.json()) };