import {z} from 'zod' export type GetApiV6RewardSuppliersArgs = {include_archived?: boolean | undefined, types?: (Array<'CUSTOM_REWARD' | 'MANUAL_COUPON' | 'PAYPAL_PAYOUTS' | 'SALESFORCE_COUPON' | 'TANGO_V2'>) | undefined}; export type GetApiV6RewardSuppliersOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const cashBackMaxInRewardSupplierResponseBase = z.union([z.number(), z.string(), z.string()]); export const cashBackMinInRewardSupplierResponseBase = z.union([z.number(), z.string(), z.string()]); export const cashBackPercentageInRewardSupplierResponseBase = z.union([z.number(), z.string(), z.string()]); export const componentReferenceResponse = z.object({component_id: z.string().optional(), socket_names: z.array(z.string()).optional()}); export const zonedDateTime = z.string(); export const displayNameInRewardSupplierResponseBase = z.union([z.string(), z.string(), z.string()]); export const displayTypeInRewardSupplierResponseBase = z.union([z.string(), z.string()]); export const enabledInRewardSupplierResponseBase = z.union([z.boolean(), z.string(), z.string()]); export const faceValueInRewardSupplierResponseBase = z.union([z.number(), z.string(), z.string()]); export const faceValueAlgorithmTypeInRewardSupplierResponseBase = z.union([z.enum(["CASH_BACK", "FIXED"]), z.string(), z.string()]); export const faceValueTypeInRewardSupplierResponseBase = z.union([z.enum(["AUD", "BRL", "CAD", "CNY", "EUR", "GBP", "HKD", "INR", "JPY", "KRW", "MONTH", "MXN", "NZD", "PERCENT_OFF", "POINTS", "TRY", "TWD", "USD"]), z.string(), z.string()]); export const limitPerDayInRewardSupplierResponseBase = z.union([z.number().int(), z.string(), z.string()]); export const limitPerHourInRewardSupplierResponseBase = z.union([z.number().int(), z.string(), z.string()]); export const nameInRewardSupplierResponseBase = z.union([z.string(), z.string()]); export const rewardExpiresAtInRewardSupplierResponseBase = z.union([z.string(), z.string(), z.string(), z.string(), z.string()]); export const descriptionInPayPalPayoutsRewardSupplierResponse = z.union([z.string(), z.string(), z.string()]); export const couponPoolIdInSalesforceCouponRewardSupplierResponse = z.union([z.string(), z.string()]); export const descriptionInTangoRewardSupplierResponse = z.union([z.string(), z.string(), z.string()]); export const rewardSupplierResponse = z.discriminatedUnion("reward_supplier_type", [z.object({cash_back_max: cashBackMaxInRewardSupplierResponseBase, cash_back_min: cashBackMinInRewardSupplierResponseBase, cash_back_percentage: cashBackPercentageInRewardSupplierResponseBase, component_ids: z.array(z.string()), component_references: z.array(componentReferenceResponse), created_date: zonedDateTime, display_name: displayNameInRewardSupplierResponseBase, display_type: displayTypeInRewardSupplierResponseBase, enabled: enabledInRewardSupplierResponseBase, face_value: faceValueInRewardSupplierResponseBase, face_value_algorithm_type: faceValueAlgorithmTypeInRewardSupplierResponseBase, face_value_type: faceValueTypeInRewardSupplierResponseBase, id: z.string(), limit_per_day: limitPerDayInRewardSupplierResponseBase, limit_per_hour: limitPerHourInRewardSupplierResponseBase, name: nameInRewardSupplierResponseBase, partner_reward_key_type: z.enum(["COUPON", "ID", "LINK"]), partner_reward_supplier_id: z.string(), reward_expires_at: rewardExpiresAtInRewardSupplierResponseBase, reward_supplier_type: z.literal("CUSTOM_REWARD"), state_transitions: z.record(z.string(), z.array(z.enum(["CANCELED", "EARNED", "FAILED", "FULFILLED", "REDEEMED", "REVOKED", "SENT"]))), tags: z.array(z.string()), updated_date: zonedDateTime, auto_fulfillment_enabled: z.boolean(), missing_fulfillment_alert_delay_ms: z.number().int(), missing_fulfillment_alert_enabled: z.boolean(), missing_fulfillment_auto_fail_delay_ms: z.number().int(), missing_fulfillment_auto_fail_enabled: z.boolean(), reward_email_auto_send_enabled: z.boolean(), type: z.enum(["ACCOUNT_CREDIT", "LOYALTY_POINTS"])}), z.object({cash_back_max: cashBackMaxInRewardSupplierResponseBase, cash_back_min: cashBackMinInRewardSupplierResponseBase, cash_back_percentage: cashBackPercentageInRewardSupplierResponseBase, component_ids: z.array(z.string()), component_references: z.array(componentReferenceResponse), created_date: zonedDateTime, display_name: displayNameInRewardSupplierResponseBase, display_type: displayTypeInRewardSupplierResponseBase, enabled: enabledInRewardSupplierResponseBase, face_value: faceValueInRewardSupplierResponseBase, face_value_algorithm_type: faceValueAlgorithmTypeInRewardSupplierResponseBase, face_value_type: faceValueTypeInRewardSupplierResponseBase, id: z.string(), limit_per_day: limitPerDayInRewardSupplierResponseBase, limit_per_hour: limitPerHourInRewardSupplierResponseBase, name: nameInRewardSupplierResponseBase, partner_reward_key_type: z.enum(["COUPON", "ID", "LINK"]), partner_reward_supplier_id: z.string(), reward_expires_at: rewardExpiresAtInRewardSupplierResponseBase, reward_supplier_type: z.literal("MANUAL_COUPON"), state_transitions: z.record(z.string(), z.array(z.enum(["CANCELED", "EARNED", "FAILED", "FULFILLED", "REDEEMED", "REVOKED", "SENT"]))), tags: z.array(z.string()), updated_date: zonedDateTime, coupon_count_warn_limit: z.number().int(), default_coupon_expiry_date: zonedDateTime, minimum_coupon_lifetime: z.number().int()}), z.object({cash_back_max: cashBackMaxInRewardSupplierResponseBase, cash_back_min: cashBackMinInRewardSupplierResponseBase, cash_back_percentage: cashBackPercentageInRewardSupplierResponseBase, component_ids: z.array(z.string()), component_references: z.array(componentReferenceResponse), created_date: zonedDateTime, display_name: displayNameInRewardSupplierResponseBase, display_type: displayTypeInRewardSupplierResponseBase, enabled: enabledInRewardSupplierResponseBase, face_value: faceValueInRewardSupplierResponseBase, face_value_algorithm_type: faceValueAlgorithmTypeInRewardSupplierResponseBase, face_value_type: faceValueTypeInRewardSupplierResponseBase, id: z.string(), limit_per_day: limitPerDayInRewardSupplierResponseBase, limit_per_hour: limitPerHourInRewardSupplierResponseBase, name: nameInRewardSupplierResponseBase, partner_reward_key_type: z.enum(["COUPON", "ID", "LINK"]), partner_reward_supplier_id: z.string(), reward_expires_at: rewardExpiresAtInRewardSupplierResponseBase, reward_supplier_type: z.literal("PAYPAL_PAYOUTS"), state_transitions: z.record(z.string(), z.array(z.enum(["CANCELED", "EARNED", "FAILED", "FULFILLED", "REDEEMED", "REVOKED", "SENT"]))), tags: z.array(z.string()), updated_date: zonedDateTime, description: descriptionInPayPalPayoutsRewardSupplierResponse, merchant_token: z.string()}), z.object({cash_back_max: cashBackMaxInRewardSupplierResponseBase, cash_back_min: cashBackMinInRewardSupplierResponseBase, cash_back_percentage: cashBackPercentageInRewardSupplierResponseBase, component_ids: z.array(z.string()), component_references: z.array(componentReferenceResponse), created_date: zonedDateTime, display_name: displayNameInRewardSupplierResponseBase, display_type: displayTypeInRewardSupplierResponseBase, enabled: enabledInRewardSupplierResponseBase, face_value: faceValueInRewardSupplierResponseBase, face_value_algorithm_type: faceValueAlgorithmTypeInRewardSupplierResponseBase, face_value_type: faceValueTypeInRewardSupplierResponseBase, id: z.string(), limit_per_day: limitPerDayInRewardSupplierResponseBase, limit_per_hour: limitPerHourInRewardSupplierResponseBase, name: nameInRewardSupplierResponseBase, partner_reward_key_type: z.enum(["COUPON", "ID", "LINK"]), partner_reward_supplier_id: z.string(), reward_expires_at: rewardExpiresAtInRewardSupplierResponseBase, reward_supplier_type: z.literal("SALESFORCE_COUPON"), state_transitions: z.record(z.string(), z.array(z.enum(["CANCELED", "EARNED", "FAILED", "FULFILLED", "REDEEMED", "REVOKED", "SENT"]))), tags: z.array(z.string()), updated_date: zonedDateTime, balance_refill_amount: z.number().int(), coupon_pool_id: couponPoolIdInSalesforceCouponRewardSupplierResponse, initial_offset: z.number().int(), settings_id: z.string()}), z.object({cash_back_max: cashBackMaxInRewardSupplierResponseBase, cash_back_min: cashBackMinInRewardSupplierResponseBase, cash_back_percentage: cashBackPercentageInRewardSupplierResponseBase, component_ids: z.array(z.string()), component_references: z.array(componentReferenceResponse), created_date: zonedDateTime, display_name: displayNameInRewardSupplierResponseBase, display_type: displayTypeInRewardSupplierResponseBase, enabled: enabledInRewardSupplierResponseBase, face_value: faceValueInRewardSupplierResponseBase, face_value_algorithm_type: faceValueAlgorithmTypeInRewardSupplierResponseBase, face_value_type: faceValueTypeInRewardSupplierResponseBase, id: z.string(), limit_per_day: limitPerDayInRewardSupplierResponseBase, limit_per_hour: limitPerHourInRewardSupplierResponseBase, name: nameInRewardSupplierResponseBase, partner_reward_key_type: z.enum(["COUPON", "ID", "LINK"]), partner_reward_supplier_id: z.string(), reward_expires_at: rewardExpiresAtInRewardSupplierResponseBase, reward_supplier_type: z.literal("TANGO_V2"), state_transitions: z.record(z.string(), z.array(z.enum(["CANCELED", "EARNED", "FAILED", "FULFILLED", "REDEEMED", "REVOKED", "SENT"]))), tags: z.array(z.string()), updated_date: zonedDateTime, account_id: z.string(), brand_description: z.string(), brand_disclaimer: z.string(), brand_image_url: z.string(), brand_name: z.string(), description: descriptionInTangoRewardSupplierResponse, utid: z.string()})]); export const getApiV6RewardSuppliersResponse = z.array(rewardSupplierResponse); 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 reward suppliers * * GET /v6/reward-suppliers * * Also accepts auth: QUERY, COOKIE */ export const getApiV6RewardSuppliers = async (args: GetApiV6RewardSuppliersArgs, options?: GetApiV6RewardSuppliersOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_archived != null) query.append("include_archived", String(args.include_archived)) for (const item of args.types ?? []) query.append("types", String(item)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/reward-suppliers${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(getApiV6RewardSuppliersResponse, await res.json()) };