import {z} from 'zod' export type GetApiProductsArgs = {page_token?: string | undefined, limit?: number | undefined}; export type GetApiProductsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const attribute = z.object({id: z.string().min(1).max(255), name: z.string().min(1).max(255), type: z.enum(["STRING", "NUMERIC", "BOOLEAN"]), value: z.object({})}); export const attributes = z.array(attribute).nullable(); export const brand = z.string().max(255).nullable(); export const locale = z.string(); export const categoryLabel = z.object({locale: locale, value: z.string().max(255)}); export const category = z.object({id: z.string().min(1).max(255).nullable().optional(), labels: z.array(categoryLabel)}); export const description = z.object({locale: locale, value: z.string().max(6000)}); export const descriptions = z.array(description).nullable(); export const money = z.object({amount: z.number().gte(0), currency: z.string().min(1)}); export const priceScope = z.string().max(255).nullable(); export const discountPrice = z.object({end_date: z.string().nullable().optional(), price: money, scope: priceScope.optional(), start_date: z.string().nullable().optional()}); export const discountPrices = z.array(discountPrice).nullable(); export const gtin = z.object({value: z.string()}); export const gtins = z.array(gtin); export const productId = z.string().min(1).max(256); export const image = z.object({url: z.string()}); export const images = z.array(image).nullable(); export const availableQuantity = z.number().int().gte(0).lte(1000000000); export const quantity = z.object({available_quantity: availableQuantity, warehouse_code: z.string().min(1).optional()}); export const quantities = z.array(quantity); export const standardPrice = z.object({price: money, scope: priceScope.optional()}); export const standardPrices = z.array(standardPrice); export const title = z.object({locale: locale, value: z.string().max(255)}); export const titles = z.array(title).nullable(); export const partnerQuantity = z.object({available_quantity: availableQuantity, partner_id: z.string()}); export const partnerQuantities = z.array(partnerQuantity); export const listedProduct = z.object({attributes: attributes.optional(), brand: brand.optional(), category: category.nullable().optional(), descriptions: descriptions.optional(), discount_prices: discountPrices.optional(), gtins: gtins.optional(), id: productId, images: images.optional(), quantities: quantities.optional(), standard_prices: standardPrices.optional(), titles: titles.optional(), partner_quantities: partnerQuantities.optional()}); export const listProductsResponse = z.object({next_page_token: z.string().min(1).optional(), previous_page_token: z.string().min(1).optional(), data: z.array(listedProduct)}); 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 }; /** * listProducts - List products from your Mirakl Connect catalog * * GET /products */ export const getApiProducts = async (args: GetApiProductsArgs, options?: GetApiProductsOptions) => { const {baseUrl = "https://miraklconnect.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(listProductsResponse, await res.json()) };