import {z} from 'zod' export type GetApiProductsGetProductAnalyticsV1Args = {productId: string, variantId: string, range: string}; export type GetApiProductsGetProductAnalyticsV1Options = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiProductsGetProductAnalyticsV1Response = z.object({data: z.object({marketPrice: z.object({value: z.number(), change: z.number(), changePercent: z.number(), dataPoints: z.array(z.object({value: z.number(), timestamp: z.string()}))}), premium: z.object({value: z.number(), change: z.number(), changePercent: z.number(), dataPoints: z.array(z.object({value: z.number(), timestamp: z.string()}))}), depth: z.object({value: z.number(), change: z.number(), changePercent: z.number(), dataPoints: z.array(z.object({value: z.number(), timestamp: 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 product analytics * * GET /products/get-product-analytics/v1 */ export const getApiProductsGetProductAnalyticsV1 = async (args: GetApiProductsGetProductAnalyticsV1Args, options?: GetApiProductsGetProductAnalyticsV1Options) => { 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 (args.productId != null) query.append("productId", String(args.productId)) if (args.variantId != null) query.append("variantId", String(args.variantId)) if (args.range != null) query.append("range", String(args.range)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/products/get-product-analytics/v1${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(getApiProductsGetProductAnalyticsV1Response, await res.json()) };