import {z} from 'zod' export type LoyaltiesBenefitsListRequestQueryId = {conditions: {$is?: (string | Array | null) | undefined, $is_not?: (string | Array | null) | undefined, $in?: (string | Array | null) | undefined, $not_in?: (string | Array | null) | undefined}}; export type LoyaltiesBenefitsListRequestQueryName = {conditions: {$is?: (string | Array | null) | undefined, $is_not?: (string | Array | null) | undefined, $contains?: (string | Array | null) | undefined, $not_contain?: (string | Array | null) | undefined, $starts_with?: (string | Array | null) | undefined, $ends_with?: (string | Array | null) | undefined, $in?: (string | Array | null) | undefined, $not_in?: (string | Array | null) | undefined, $has_value?: (string | Array | null) | undefined, $is_unknown?: (string | Array | null) | undefined}}; export type LoyaltiesBenefitsListRequestQueryStatus = {conditions: {$is?: ('ACTIVE' | 'DRAFT' | Array<'ACTIVE' | 'DRAFT'> | null) | undefined, $is_not?: ('ACTIVE' | 'DRAFT' | Array<'ACTIVE' | 'DRAFT'> | null) | undefined, $in?: ('ACTIVE' | 'DRAFT' | Array<'ACTIVE' | 'DRAFT'> | null) | undefined, $not_in?: ('ACTIVE' | 'DRAFT' | Array<'ACTIVE' | 'DRAFT'> | null) | undefined}}; export type LoyaltiesBenefitsListRequestQueryType = {conditions: {$is?: ('POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL' | Array<'POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL'> | null) | undefined, $is_not?: ('POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL' | Array<'POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL'> | null) | undefined, $in?: ('POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL' | Array<'POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL'> | null) | undefined, $not_in?: ('POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL' | Array<'POINTS' | 'POINTS_PROPORTIONAL' | 'MATERIAL' | 'DIGITAL'> | null) | undefined}}; export type BenefitDateFilterConditions = {conditions: {$before?: (string | Array | null) | undefined, $after?: (string | Array | null) | undefined, $is?: (string | Array | null) | undefined, $more_than?: (string | Array | null) | undefined, $less_than?: (string | Array | null) | undefined, $has_value?: (string | Array | null) | undefined, $is_unknown?: (string | Array | null) | undefined}}; export type LoyaltiesBenefitsListRequestQuery = {junction?: ('AND' | 'OR' | null) | undefined, id?: (LoyaltiesBenefitsListRequestQueryId | null) | undefined, name?: (LoyaltiesBenefitsListRequestQueryName | null) | undefined, status?: (LoyaltiesBenefitsListRequestQueryStatus | null) | undefined, type?: (LoyaltiesBenefitsListRequestQueryType | null) | undefined, created_at?: (BenefitDateFilterConditions | null) | undefined}; export type GetApiV2LoyaltiesBenefitsArgs = {limit?: (number | string | null) | undefined, order?: (Array<'-created_at' | 'created_at' | 'name' | '-name' | '-type' | 'type'> | '-created_at' | 'created_at' | 'name' | '-name' | '-type' | 'type' | null) | undefined, cursor?: (string | null) | undefined, filters?: (LoyaltiesBenefitsListRequestQuery | null) | undefined}; export type GetApiV2LoyaltiesBenefitsOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltyV2ActivityBenefitStockLimited = z.object({quantity: z.number().int().gte(0).lte(9007199254740991)}); export const benefitStock = z.object({type: z.enum(["UNLIMITED", "LIMITED"]), limited: loyaltyV2ActivityBenefitStockLimited.nullable().optional()}); export const loyaltyV2ActivityBenefitPoints = z.object({value: z.number().int().gte(0).lte(9007199254740991), card_definition_id: z.string()}); export const loyaltyV2ActivityBenefitPointsProportionalEvery = z.object({every: z.number().int().gte(1).lte(9007199254740991), value: z.number().gte(0).lte(9007199254740991)}); export const loyaltyV2ActivityBenefitPointsProportionalMetadata = z.object({every: z.number().int().gte(1).lte(9007199254740991), value: z.number().gte(0).lte(9007199254740991), property: z.string().min(1)}); export const benefitPointsProportionalOrder = z.object({amount: loyaltyV2ActivityBenefitPointsProportionalEvery.optional(), total_amount: loyaltyV2ActivityBenefitPointsProportionalEvery.optional(), metadata: loyaltyV2ActivityBenefitPointsProportionalMetadata.optional()}); export const benefitPointsProportionalCustomer = z.object({metadata: loyaltyV2ActivityBenefitPointsProportionalMetadata}); export const benefitPointsProportionalCustomEvent = z.object({metadata: loyaltyV2ActivityBenefitPointsProportionalMetadata}); export const loyaltyV2ActivityBenefitPointsProportionalApplicableToProduct = z.object({id: z.string()}); export const loyaltyV2ActivityBenefitPointsProportionalApplicableToSku = z.object({id: z.string()}); export const loyaltyV2ActivityBenefitPointsProportionalApplicableToProductsCollection = z.object({id: z.string()}); export const benefitPointsProportionalApplicableTo = z.object({type: z.enum(["product", "sku", "products_collection"]), product: loyaltyV2ActivityBenefitPointsProportionalApplicableToProduct.nullable().optional(), sku: loyaltyV2ActivityBenefitPointsProportionalApplicableToSku.nullable().optional(), products_collection: loyaltyV2ActivityBenefitPointsProportionalApplicableToProductsCollection.nullable().optional()}); export const benefitPointsProportionalOrderItemsCalculation = z.object({every: z.number().int().gte(1).lte(9007199254740991), value: z.number().gte(0).lte(9007199254740991), applicable_to: z.array(benefitPointsProportionalApplicableTo)}); export const benefitPointsProportionalOrderItems = z.object({amount: benefitPointsProportionalOrderItemsCalculation.optional(), subtotal_amount: benefitPointsProportionalOrderItemsCalculation.optional(), quantity: benefitPointsProportionalOrderItemsCalculation.optional()}); export const benefitPointsProportional = z.object({calculation_type: z.enum(["PRE_DISCOUNT_ORDER_AMOUNT", "POST_DISCOUNT_ORDER_AMOUNT", "PRE_DISCOUNT_ORDER_ITEMS_AMOUNT", "POST_DISCOUNT_ORDER_ITEMS_AMOUNT", "ORDER_ITEMS_QUANTITY", "ORDER_METADATA_VALUE", "CUSTOMER_METADATA_VALUE", "CUSTOM_EVENT_METADATA_VALUE"]), card_definition_id: z.string(), order: benefitPointsProportionalOrder.nullable().optional(), customer: benefitPointsProportionalCustomer.nullable().optional(), custom_event: benefitPointsProportionalCustomEvent.nullable().optional(), order_items: benefitPointsProportionalOrderItems.nullable().optional()}); export const loyaltyV2ActivityBenefitMaterialProduct = z.object({id: z.string()}); export const loyaltyV2ActivityBenefitMaterialSKU = z.object({product_id: z.string(), id: z.string()}); export const benefitMaterial = z.object({type: z.enum(["PRODUCT", "SKU"]), product: loyaltyV2ActivityBenefitMaterialProduct.nullable().optional(), sku: loyaltyV2ActivityBenefitMaterialSKU.nullable().optional()}); export const loyaltyV2ActivityBenefitDigitalGiftVouchers = z.object({campaign_id: z.string(), balance: z.number().gte(0).lte(9007199254740991)}); export const loyaltyV2ActivityBenefitDigitalDiscountCoupons = z.object({campaign_id: z.string()}); export const benefitDigital = z.object({type: z.enum(["GIFT_VOUCHERS", "DISCOUNT_COUPONS"]), gift_vouchers: loyaltyV2ActivityBenefitDigitalGiftVouchers.nullable().optional(), discount_coupons: loyaltyV2ActivityBenefitDigitalDiscountCoupons.nullable().optional()}); export const benefit = z.object({id: z.string(), name: z.string(), type: z.enum(["POINTS", "POINTS_PROPORTIONAL", "MATERIAL", "DIGITAL"]), status: z.enum(["DRAFT", "ACTIVE", "DELETED"]), stock: benefitStock, points: loyaltyV2ActivityBenefitPoints.optional(), points_proportional: benefitPointsProportional.optional(), material: benefitMaterial.optional(), digital: benefitDigital.optional(), created_at: z.string(), updated_at: z.string().nullable(), object: z.literal("benefit")}); export const benefitListCursor = z.object({next: z.string(), expires_at: z.string().optional()}); export const v2LoyaltiesBenefitsListResponseBody = z.object({data: z.array(benefit), cursor: benefitListCursor.nullable(), object: z.literal("list")}); 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 }; /** * List benefits * * GET /v2/loyalties/benefits * * Also accepts auth: bearerAuth */ export const getApiV2LoyaltiesBenefits = async (args: GetApiV2LoyaltiesBenefitsArgs, options?: GetApiV2LoyaltiesBenefitsOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.filters != null) query.append("filters", JSON.stringify(args.filters)) if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/loyalties/benefits${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(v2LoyaltiesBenefitsListResponseBody, await res.json()) };