import {z} from 'zod' export type GetApiProductsV1CatalogOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiProductsV1CatalogResponse = z.object({data: z.object({categories: z.array(z.object({id: z.number().int().gte(-9007199254740991).lte(9007199254740991), title: z.string(), description: z.string().nullable(), createdAt: z.string(), isNumismatic: z.boolean(), allowCardPayments: z.boolean(), isInstantShip: z.boolean()})), subcategories: z.array(z.object({id: z.number().int().gte(-9007199254740991).lte(9007199254740991), createdAt: z.string(), title: z.string(), description: z.string().nullable(), premiumCalculationType: z.enum(["unit", "troy_ounce"]), feeRate: z.number().gte(-140737488355328).lte(140737488355327), advancedFeeRate: z.number().gte(-140737488355328).lte(140737488355327).nullable()})), manufacturers: z.array(z.object({id: z.number().int().gte(-9007199254740991).lte(9007199254740991), title: z.string(), description: z.string().nullable(), updatedAt: z.string(), createdAt: z.string(), images: z.array(z.string()).nullable(), isEsg: z.boolean()})), materials: z.array(z.string()), attributes: z.array(z.object({id: z.number().int().gte(-9007199254740991).lte(9007199254740991), type: z.string(), title: z.string(), description: z.string()})), certificateProviders: z.array(z.object({id: z.number().int().gte(-9007199254740991).lte(9007199254740991), code: z.string().nullable(), createdAt: z.string(), description: z.string().nullable(), title: z.string().nullable()}))})}); 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 product catalog metadata * * GET /products/v1/catalog */ export const getApiProductsV1Catalog = async (options?: GetApiProductsV1CatalogOptions) => { const {baseUrl = "https://api.collectpure.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/products/v1/catalog${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(getApiProductsV1CatalogResponse, await res.json()) };