import {z} from 'zod' export type GetApiApiOffersGetProductSkuArgs = {'x-site-context': string, sku: string, priceListId?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiOffersGetProductSkuOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getProductResponse = z.object({id: z.string().optional(), channel: z.array(z.number()).optional(), startDate: z.string().optional(), endDate: z.string().optional(), group: z.array(z.string()).optional(), imageSetOnAttributes: z.array(z.string()).optional(), isDefault: z.boolean().optional(), isActive: z.boolean().optional(), isSoftDeleted: z.boolean().optional(), title: z.string().optional(), itemFamily: z.object({id: z.string().optional(), name: z.string().optional()}).optional(), sku: z.string().optional(), description: z.string().optional(), state: z.string().optional(), itemId: z.number().optional(), images: z.array(z.object({id: z.string().optional(), order: z.number().optional(), label: z.string().optional(), source: z.array(z.object({id: z.string().optional(), url: z.string().optional()})).optional()})).optional(), attributes: z.array(z.object({attributeGroupId: z.string().optional(), value: z.string().optional(), isDefault: z.boolean().optional(), id: z.string().optional()})).optional(), variants: z.array(z.string()).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), items: z.array(z.object({_id: z.string().optional()})).optional(), price: z.object({id: z.string().optional(), isSoftDeleted: z.boolean().optional(), itemId: z.number().optional(), offerId: z.number().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), offers: z.array(z.object({price: z.object({sale: z.number().optional(), base: z.number().optional(), currency: z.string().optional()}).optional(), channel: z.number().optional(), startDate: z.string().optional(), endDate: z.string().optional(), kind: z.number().optional()})).optional(), query: z.object({limit: z.number().optional(), count: z.number().optional(), offset: z.number().optional()}).optional()}).optional(), priceRange: z.object({min: z.number().optional(), max: z.number().optional()}).optional(), livePrice: z.array(z.object({price: z.object({base: z.number().optional(), sale: z.number().optional(), cost: z.number().optional(), currency: z.string().optional()}).optional(), channel: z.number().optional(), startDate: z.string().optional(), endDate: z.string().optional(), kind: z.object({id: z.string().optional(), name: z.string().optional(), channel: z.number().optional(), priceKindId: z.number().optional()}).optional(), offerCode: z.number().optional(), range: z.object({min: z.number().optional(), max: z.number().optional()}).optional()})).optional(), promoDates: z.object({startDate: z.string().optional(), endDate: z.string().optional()}).optional()}); 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 and price by sku * * GET /api-offers/get-product/{sku} */ export const getApiApiOffersGetProductSku = async (args: GetApiApiOffersGetProductSkuArgs, options?: GetApiApiOffersGetProductSkuOptions) => { const {baseUrl = "https://live.copilot.fabric.inc", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-site-context'] != null) headers.set("x-site-context", String(args['x-site-context'])) if (args.priceListId != null) query.append("priceListId", String(args.priceListId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api-offers/get-product/${encodeURIComponent(String(args.sku))}${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(getProductResponse, await res.json()) };