import {z} from 'zod' export type ParameterLimit = number; export type ParameterPage = number; export type ParameterOrder = 'created_at' | '-created_at' | 'updated_at' | '-updated_at' | 'type' | '-type' | 'code' | '-code' | 'campaign' | '-campaign' | 'category' | '-category'; export type ParameterDate = string; export type GetApiV1ProductCollectionsProductCollectionIdProductsArgs = {limit?: ParameterLimit | undefined, page?: ParameterPage | undefined, order?: ParameterOrder | undefined, starting_after?: ParameterDate | undefined}; export type GetApiV1ProductCollectionsProductCollectionIdProductsOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const productWithoutSkus = z.object({id: z.string(), source_id: z.string().nullable(), name: z.string().nullable(), price: z.number().int().nullable(), attributes: z.array(z.string()), metadata: z.object({}), image_url: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional(), object: z.literal("product")}); export const skuWithProduct = z.object({id: z.string(), source_id: z.string().nullable(), product_id: z.string(), sku: z.string().nullable(), price: z.number().int().nullable(), currency: z.string().nullable().optional(), attributes: z.object({}), image_url: z.string().nullable(), metadata: z.object({}), created_at: z.string(), updated_at: z.string().nullable(), object: z.literal("sku"), product: productWithoutSkus.optional()}); export const productCollectionsProductsListResponseBody = z.object({object: z.string(), data_ref: z.string(), data: z.array(z.union([productWithoutSkus, skuWithProduct])), total: z.number().int()}); 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 Products in Collection * * GET /v1/product-collections/{productCollectionId}/products * * Also accepts auth: X-Voucherify-OAuth */ export const getApiV1ProductCollectionsProductCollectionIdProducts = async (args: GetApiV1ProductCollectionsProductCollectionIdProductsArgs, options?: GetApiV1ProductCollectionsProductCollectionIdProductsOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.order != null) query.append("order", String(args.order)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/product-collections/${encodeURIComponent(String(args.productCollectionId))}/products${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(productCollectionsProductsListResponseBody, await res.json()) };