import {z} from 'zod' export type PriceRequestParam = {options?: {[key: string]: Record} | undefined, sku?: string | undefined, quantity?: number | undefined, itemId?: number | undefined}; export type GetPricesRequest = {traceId?: string | undefined, priceList?: Array | undefined, sku?: Array | undefined, itemId?: Array | undefined, userId?: string | undefined, userProfile?: {[key: string]: Array} | undefined, isLoggedIn?: boolean | undefined, channel?: Array | undefined, date?: string | undefined, priceParams?: Array | undefined, audit?: boolean | undefined}; export type CreateApiV1PriceGetBySkuArgs = {'x-site-context': string, body: GetPricesRequest}; export type CreateApiV1PriceGetBySkuOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const calculatedPriceObject = z.object({sale: z.number().optional(), cost: z.number().optional(), base: z.number().optional(), finalPrice: z.number().optional(), currency: z.string().optional(), totalPrice: z.number().optional()}); export const rtpePromotionMessages = z.array(z.object({promoId: z.string().optional(), title: z.string().optional(), message: z.string().optional(), pages: z.array(z.enum(["PDP", "PLP", "Cart", "Checkout"])).optional(), locales: z.array(z.string()).optional(), type: z.enum(["DISCOUNT", "PROXIMITY", "POTENTIAL_DISCOUNT"]).optional(), threshold: z.number().int().optional()})); export const promotionDiscount = z.object({amount: z.number().optional(), value: z.number().optional(), groupId: z.string().optional(), promotionId: z.string().optional(), priority: z.number().int().optional(), stackable: z.boolean().optional(), title: z.string().optional(), quantity: z.number().int().optional(), promotionType: z.string().optional(), discountType: z.enum(["PERCENTAGE_OFF", "AMOUNT_OFF", "FIXED_PRICE"]).optional(), application: z.number().int().optional(), isAlwaysApplied: z.boolean().optional(), level: z.number().int().optional(), stackingType: z.enum(["STACKABLE", "EXCLUSIVE", "TYPE_EXCLUSIVE", "UNIVERSAL"]).optional(), proratedAmount: z.number().optional(), proratedQuantity: z.number().int().optional(), promotionMessages: rtpePromotionMessages.optional()}); export const calculatedPriceOffers = z.object({price: calculatedPriceObject.optional(), kind: z.string().optional(), channel: z.number().int().optional(), discounts: z.array(promotionDiscount).optional(), additionalAttributes: z.array(z.object({})).optional(), promotionMessages: rtpePromotionMessages.optional()}); export const calculatedPriceAudit = z.object({userId: z.string().optional(), priceMethodType: z.string().optional(), quantity: z.number().optional(), options: z.object({length: z.number().optional(), width: z.number().optional()}).optional(), appliedAddons: z.array(z.string()).optional()}); export const calculatedPrice = z.object({priceList: z.string().optional(), sku: z.string().optional(), itemId: z.number().int().optional(), offers: calculatedPriceOffers.optional(), audit: calculatedPriceAudit.optional()}); export const createApiV1PriceGetBySkuResponse = z.array(calculatedPrice); 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 calculated prices * * POST /v1/price/get-by-sku */ export const createApiV1PriceGetBySku = async (args: CreateApiV1PriceGetBySkuArgs, options?: CreateApiV1PriceGetBySkuOptions) => { const {baseUrl = "https://live.copilot.fabric.inc", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-site-context'] != null) headers.set("x-site-context", String(args['x-site-context'])) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/price/get-by-sku${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1PriceGetBySkuResponse, await res.json()) };