import {z} from 'zod' export type GetApiProductsV3IdArgs = {id: string}; export type GetApiProductsV3IdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiProductsV3IdResponse = z.object({data: z.object({id: z.string(), title: z.string(), purity: z.string(), weight: z.string(), weightGrams: z.number(), sku: z.string(), isCertified: z.boolean(), isIra: z.boolean(), material: z.string(), attributes: z.array(z.string()), images: z.array(z.string()), manufacturer: z.object({title: z.string(), images: z.array(z.string()), isEsg: z.boolean()}).nullable(), category: z.object({title: z.string(), isNumismatic: z.boolean()}).nullable(), subCategory: z.object({title: z.string(), premiumCalculationType: z.string()}).nullable(), certificateProvider: z.object({title: z.string().nullable()}).nullable(), variants: z.array(z.object({id: z.string(), title: z.string().nullable(), highestOffer: z.object({id: z.string(), price: z.number(), quantity: z.number(), spotPremium: z.number(), spotPremiumDollar: z.number(), expiresAt: z.string(), createdAt: z.string(), updatedAt: z.string()}).nullable(), lowestListing: z.object({id: z.string(), price: z.number(), quantity: z.number(), spotPremium: z.number(), spotPremiumDollar: z.number(), expiresAt: z.string(), createdAt: z.string(), updatedAt: z.string()}).nullable(), images: z.array(z.string()).nullable(), orderBook: z.object({listings: z.array(z.object({price: z.number(), qty: z.number(), spotPremium: z.number(), spotPremiumDollar: z.number()})), offers: z.array(z.object({price: z.number(), qty: z.number(), spotPremium: z.number(), spotPremiumDollar: z.number()}))})})), recentActivity: z.array(z.object({event: z.string(), createdAt: z.string(), price: z.number(), quantity: z.number(), spotPremium: z.number(), spotPremiumDollar: z.number()}))})}); 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 enriched product details * * GET /products/v3/{id} */ export const getApiProductsV3Id = async (args: GetApiProductsV3IdArgs, options?: GetApiProductsV3IdOptions) => { 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/v3/${encodeURIComponent(String(args.id))}${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(getApiProductsV3IdResponse, await res.json()) };