import {z} from 'zod' export type GetApiApiOffersPriceArgs = {'x-site-context': string, limit?: number | undefined, page?: number | undefined, priceListId?: number | undefined, sortBy?: ('itemId' | 'priceListId' | 'updatedAt') | undefined, sortOrder?: ('asc' | 'desc') | undefined}; export type GetApiApiOffersPriceOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const price = z.object({base: z.number().optional(), sale: z.number().optional(), cost: z.number().optional(), currency: z.string().optional()}); export const getPriceResponse = z.object({query: z.object({}).optional(), prices: z.array(z.object({_id: z.string().optional(), isSoftDeleted: z.boolean().optional(), itemId: z.number().optional(), itemSku: z.string().optional(), offerId: z.number().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), job: z.boolean().optional(), priceListId: z.number().optional(), offers: z.array(z.object({kind: z.number().optional(), channel: z.number().optional(), startDate: z.string().optional(), endDate: z.string().optional(), price: price.optional(), offerCode: z.number().optional(), range: z.array(z.object({minQuantity: z.number().optional(), price: z.number().optional(), maxQuantity: z.union([z.string(), z.number()]).optional(), width: z.array(z.object({minQuantity: z.number(), maxQuantity: z.union([z.string(), z.number()]), price: z.number()})).optional()})).optional(), priceAttributes: z.array(z.object({})).optional(), priceMethodType: z.string().optional()})).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 all prices * * GET /api-offers/price */ export const getApiApiOffersPrice = async (args: GetApiApiOffersPriceArgs, options?: GetApiApiOffersPriceOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.priceListId != null) query.append("priceListId", String(args.priceListId)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api-offers/price${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(getPriceResponse, await res.json()) };