import {z} from 'zod' export type LoyaltiesRewardsListRequestQueryId = {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 LoyaltiesRewardsListRequestQueryName = {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 LoyaltiesRewardsListRequestQueryType = {conditions: {$is?: ('MATERIAL' | 'DIGITAL' | Array<'MATERIAL' | 'DIGITAL'> | null) | undefined, $is_not?: ('MATERIAL' | 'DIGITAL' | Array<'MATERIAL' | 'DIGITAL'> | null) | undefined, $in?: ('MATERIAL' | 'DIGITAL' | Array<'MATERIAL' | 'DIGITAL'> | null) | undefined, $not_in?: ('MATERIAL' | 'DIGITAL' | Array<'MATERIAL' | 'DIGITAL'> | null) | undefined}}; export type RewardDateFilter = {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 LoyaltiesRewardsListRequestQuery = {junction?: ('AND' | 'OR' | null) | undefined, id?: (LoyaltiesRewardsListRequestQueryId | null) | undefined, name?: (LoyaltiesRewardsListRequestQueryName | null) | undefined, type?: (LoyaltiesRewardsListRequestQueryType | null) | undefined, created_at?: (RewardDateFilter | null) | undefined}; export type GetApiV2LoyaltiesRewardsArgs = {limit?: (number | string | null) | undefined, order?: (Array<'-created_at' | 'created_at' | 'name' | '-name'> | '-created_at' | 'created_at' | 'name' | '-name' | null) | undefined, cursor?: (string | null) | undefined, filters?: (LoyaltiesRewardsListRequestQuery | null) | undefined}; export type GetApiV2LoyaltiesRewardsOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltyV2ActivityRewardValidityDailyHoursResponse = z.object({days_of_week: z.array(z.number().int().gte(0).lte(6)), start_time: z.string(), end_time: z.string()}); export const rewardValidityHoursResponse = z.object({type: z.enum(["DAILY", "ANY_TIME"]), daily: z.array(loyaltyV2ActivityRewardValidityDailyHoursResponse).optional()}); export const loyaltyV2ActivityRewardMaterialProduct = z.object({id: z.string()}); export const loyaltyV2ActivityRewardMaterialSKU = z.object({product_id: z.string(), id: z.string()}); export const rewardMaterialResponse = z.object({type: z.enum(["PRODUCT", "SKU"]), product: loyaltyV2ActivityRewardMaterialProduct.optional(), sku: loyaltyV2ActivityRewardMaterialSKU.optional()}); export const loyaltyV2ActivityRewardDigitalDiscountCoupons = z.object({campaign_id: z.string()}); export const loyaltyV2ActivityRewardDigitalGiftVouchersResponse = z.object({campaign_id: z.string(), balance: z.number()}); export const loyaltyV2ActivityRewardDigitalLoyaltyCardPointsResponse = z.object({points: z.number().int(), card_definition_id: z.string()}); export const rewardDigitalResponse = z.object({type: z.enum(["DISCOUNT_COUPONS", "GIFT_VOUCHERS", "LOYALTY_CARD_POINTS"]), discount_coupons: loyaltyV2ActivityRewardDigitalDiscountCoupons.optional(), gift_vouchers: loyaltyV2ActivityRewardDigitalGiftVouchersResponse.optional(), loyalty_card_points: loyaltyV2ActivityRewardDigitalLoyaltyCardPointsResponse.optional()}); export const loyaltyV2ActivityRewardRefunds = z.object({type: z.enum(["NONE", "REFUNDABLE"])}); export const rewardCostRuleGroupResponse = z.object({logic: z.string().optional()}); export const loyaltyV2ActivityRewardCostSpendingResponse = z.object({id: z.string(), name: z.string().max(200).nullable().optional(), points: z.number().int(), card_definition_id: z.string()}); export const rewardCostTierRulesAnyOfEntryResponse = z.object({tier_structure_id: z.string(), tier_ids: z.array(z.string())}); export const rewardCostTierRulesResponse = z.object({type: z.enum(["NO_REQUIREMENTS", "ANY_OF"]), any_of: z.array(rewardCostTierRulesAnyOfEntryResponse).optional()}); export const rewardCostResponse = z.object({id: z.string(), name: z.string().max(200).nullable().optional(), rules: rewardCostRuleGroupResponse.optional(), spending: z.array(loyaltyV2ActivityRewardCostSpendingResponse).nullable().optional(), tier_rules: rewardCostTierRulesResponse}); export const loyaltyV2ActivityRewardPurchaseLimitsCooldownFixedCooldownPeriod = z.object({value: z.number().int().gte(1), unit: z.enum(["HOUR", "DAY", "WEEK", "MONTH", "YEAR"])}); export const rewardPurchaseLimitsCooldownFixedCooldown = z.object({period: loyaltyV2ActivityRewardPurchaseLimitsCooldownFixedCooldownPeriod}); export const rewardPurchaseLimitsCooldown = z.object({type: z.enum(["NO_COOLDOWN", "FIXED_COOLDOWN"]), fixed_cooldown: rewardPurchaseLimitsCooldownFixedCooldown.optional()}); export const loyaltyV2ActivityCardDefinitionLimitTimeBasedPeriod = z.object({type: z.literal("CURRENT_PERIOD"), current_period: z.object({unit: z.enum(["DAY", "WEEK", "MONTH", "QUARTER", "YEAR"])}).optional()}); export const loyaltyV2ActivityRewardPurchaseLimitsFrequencyLimitTriggers = z.object({max: z.number().int().gte(1)}); export const rewardPurchaseLimitsFrequencyLimit = z.object({type: z.literal("TIME_BASED"), period: loyaltyV2ActivityCardDefinitionLimitTimeBasedPeriod, triggers: loyaltyV2ActivityRewardPurchaseLimitsFrequencyLimitTriggers}); export const rewardPurchaseLimitsFrequency = z.object({type: z.enum(["NO_LIMIT", "LIMITED"]), limits: z.array(rewardPurchaseLimitsFrequencyLimit)}); export const rewardPurchaseLimits = z.object({cooldown: rewardPurchaseLimitsCooldown, frequency: rewardPurchaseLimitsFrequency}); export const rewardResponse = z.object({id: z.string(), name: z.string(), type: z.enum(["MATERIAL", "DIGITAL"]), status: z.enum(["ACTIVE", "DRAFT", "INACTIVE", "DELETED"]), start_date: z.string().nullable(), end_date: z.string().nullable(), validity_hours: rewardValidityHoursResponse, material: rewardMaterialResponse.optional(), digital: rewardDigitalResponse.optional(), refunds: loyaltyV2ActivityRewardRefunds, costs: z.array(rewardCostResponse), purchase_limits: rewardPurchaseLimits, metadata: z.object({}), created_at: z.string(), updated_at: z.string().nullable(), object: z.literal("reward")}); export const rewardListCursor = z.object({next: z.string(), expires_at: z.string().optional()}); export const v2LoyaltiesRewardsListResponseBody = z.object({data: z.array(rewardResponse), cursor: rewardListCursor.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 rewards v2 * * GET /v2/loyalties/rewards * * Also accepts auth: bearerAuth */ export const getApiV2LoyaltiesRewards = async (args: GetApiV2LoyaltiesRewardsArgs, options?: GetApiV2LoyaltiesRewardsOptions) => { 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/rewards${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(v2LoyaltiesRewardsListResponseBody, await res.json()) };