import {z} from 'zod' export type GetApiCouponsCouponIdArgs = {'x-fabric-tenant-id': string, 'x-fabric-channel-id'?: string | undefined, 'x-client-id'?: string | undefined, 'x-fabric-request-id'?: string | undefined, couponId: string}; export type GetApiCouponsCouponIdOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const promotionDiscounts = z.array(z.object({unit: z.enum(["PERCENTAGE_OFF", "AMOUNT_OFF", "FIXED"]), amount: z.number(), type: z.enum(["SKU", "CART", "SERVICE", "SERVICE_CART"]), values: z.union([z.string().nullable(), z.array(z.object({}))]).optional(), maximumQuantity: z.number().optional(), includeOperator: z.enum(["AND", "OR"]).optional(), excludeOperator: z.enum(["AND", "OR"]).optional(), discountId: z.string().optional(), isAutoAdd: z.boolean().optional(), sets: z.array(z.enum(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"])).optional()})); export const promotionTargetProduct = z.array(z.object({type: z.enum(["SKU", "CATEGORY", "ATTRIBUTE", "PRICE_TYPE", "COLLECTION", "PREVIOUS_DISCOUNTS"]), values: z.union([z.string(), z.array(z.union([z.number().int(), z.string(), z.object({name: z.string(), value: z.union([z.string(), z.boolean(), z.number()])})])), z.number().int()]), operator: z.enum(["IN", "NOT_IN", "EQUAL", "GREATER_THAN"]), discountId: z.string().optional()})); export const promotionSegment = z.object({id: z.number().int().optional(), name: z.string().optional(), defaultValue: z.array(z.string()).optional(), value: z.union([z.array(z.string()), z.string()]).optional()}); export const promotionConditionDetail = z.object({type: z.enum(["SKU", "QUANTITY", "CATEGORY", "ATTRIBUTE", "PRICE_TYPE", "CHANEL", "ORDER_VALUE", "USER_SEGMENT", "USER_TYPE", "DATE", "SKU_QUANTITY", "COLLECTION", "PRICE", "ORDER_LINE_ITEMS", "REGION", "PREVIOUS_DISCOUNTS"]), values: z.union([z.string(), z.number(), z.array(z.union([z.number().int(), z.string(), z.union([z.object({name: z.string(), value: z.union([z.string(), z.boolean(), z.number()])}), promotionSegment])]))]), operator: z.enum(["NOT_EQUAL", "EQUAL", "GREATER_THAN", "LESS_THAN", "GREATER_THAN_OR_EQUAL_TO", "LESS_THAN_OR_EQUAL_TO", "NOT_IN", "IN"])}); export const promotionCondition = z.array(z.object({id: z.string().nullable().optional(), operator: z.enum(["AND", "OR"]), includeOperator: z.enum(["AND", "OR"]).optional(), excludeOperator: z.enum(["AND", "OR"]).optional(), conditions: z.array(promotionConditionDetail), set: z.enum(["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]).optional()})); export const promotionTermsAndConditions = z.array(z.object({name: z.string().optional(), description: z.string().optional()})); export const promotionMessages = z.array(z.object({title: z.string().optional(), message: z.string().optional(), pages: z.array(z.string()).optional(), locales: z.array(z.string()).optional()})); export const proximityMessages = z.object({threshold: z.number().int().gte(0).optional(), localizedMessages: z.array(z.object({locales: z.array(z.string()).optional(), message: z.string().optional()})).optional()}); export const promotionShipmentMethods = z.array(z.string()); export const promotionPriceLists = z.array(z.number().int()); export const couponLimits = z.array(z.object({type: z.enum(["SITE_WIDE", "PER_USER", "ORDER"]).optional(), quantity: z.number().int().optional()})); export const couponAdditionalAttributes = z.array(z.object({name: z.string(), values: z.union([z.string(), z.array(z.string())])})); export const createdAt = z.string(); export const updatedAt = z.string(); export const getCouponResponse = z.object({id: z.string(), name: z.string(), status: z.enum(["ACTIVE", "SCHEDULED", "DISABLED", "EXPIRED"]), type: z.enum(["PRODUCT", "CART", "QUANTITY", "SHIPPING", "BUYGET", "SPEND_GET"]), startAt: z.string(), endAt: z.string(), buyOperator: z.enum(["AND", "OR"]).optional(), note: z.string().optional(), isExclusive: z.boolean().optional(), couponCodes: z.array(z.string()).optional(), couponCodesCount: z.number().int().optional(), details: z.array(z.object({discounts: promotionDiscounts.optional(), targetProducts: promotionTargetProduct.optional(), conditions: promotionCondition.optional()})), termsAndConditions: promotionTermsAndConditions.optional(), promotionMessages: promotionMessages.optional(), proximityMessages: proximityMessages.optional(), shipmentMethodIds: promotionShipmentMethods.optional(), eligiblePriceLists: promotionPriceLists.optional(), limits: couponLimits.optional(), additionalAttributes: couponAdditionalAttributes.optional(), createdAt: createdAt.optional(), updatedAt: updatedAt.optional(), level: z.number().int().gte(0).lte(5).optional(), stackingType: z.enum(["STACKABLE", "EXCLUSIVE", "TYPE_EXCLUSIVE", "UNIVERSAL"]).optional(), isStackable: z.boolean().optional(), isAlwaysApplied: z.boolean().optional(), singleMultiUse: z.enum(["SINGLE_USE", "MULTI_USE"]).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 }; /** * Retrieve a coupon * * GET /coupons/{couponId} */ export const getApiCouponsCouponId = async (args: GetApiCouponsCouponIdArgs, options?: GetApiCouponsCouponIdOptions) => { const {baseUrl = "https://api.fabric.inc/v3", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-fabric-tenant-id'] != null) headers.set("x-fabric-tenant-id", String(args['x-fabric-tenant-id'])) if (args['x-fabric-channel-id'] != null) headers.set("x-fabric-channel-id", String(args['x-fabric-channel-id'])) if (args['x-client-id'] != null) headers.set("x-client-id", String(args['x-client-id'])) if (args['x-fabric-request-id'] != null) headers.set("x-fabric-request-id", String(args['x-fabric-request-id'])) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/coupons/${encodeURIComponent(String(args.couponId))}${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(getCouponResponse, await res.json()) };