import {z} from 'zod' export type GetApiStoresSubscriberIdLoyaltyEarningRulesArgs = {subscriberId: string, 'okendo-api-version': '2025-02-01', 'okendo-partner-id'?: string | undefined}; export type GetApiStoresSubscriberIdLoyaltyEarningRulesOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiStoresSubscriberIdLoyaltyEarningRulesResponse = z.object({earningRules: z.array(z.object({earningRuleId: z.string(), action: z.enum(["sign-up", "anniversary", "birthday", "custom-fixed", "custom-points-per", "purchase", "answer-question", "referral-invite", "submit-review", "facebook-like", "facebook-share", "instagram-follow", "tiktok-follow", "twitter-follow", "twitter-post", "submit-survey"]), actionCategory: z.enum(["account", "custom", "order", "profile-question", "referral", "review", "social", "survey"]), actionLink: z.string().optional(), dateComponentsToCapture: z.enum(["day-month", "day-month-year"]).optional(), icon: z.union([z.string(), z.object({url: z.string(), width: z.number().int().gte(1), height: z.number().int().gte(1)})]), imageUrl: z.string().optional(), pointsPerText: z.string().optional(), promotion: z.object({promotionId: z.string(), title: z.string(), description: z.string(), type: z.enum(["fixed-point", "point-multiplier"]), value: z.number(), icon: z.union([z.string(), z.object({url: z.string(), width: z.number().int().gte(1), height: z.number().int().gte(1)})]), dateStart: z.string(), dateEnd: z.string().optional(), appliesToEarningRules: z.array(z.string()), eligibleVipTiers: z.array(z.string()).optional()}).optional(), socialPageLink: z.string().optional(), title: z.string(), reward: z.discriminatedUnion("type", [z.object({type: z.literal("fixed-amount"), amount: z.number().int().gte(1)}), z.object({type: z.literal("points-per"), amount: z.number().gte(0.01)}), z.object({type: z.literal("points-per-spend"), amount: z.number().gte(0.01), waitTimeDays: z.number().int().gte(0).optional()}), z.object({type: z.literal("submit-review"), amountForReview: z.number().int().gte(0), amountForSocialLogin: z.number().int().gte(0), amountForImage: z.number().int().gte(0), amountForVideo: z.number().int().gte(0), capPerOrder: z.number().int().gte(0), earnEachAmountOncePer: z.enum(["order", "review"])})])})).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 }; /** * Get Earning Rules * * GET /stores/{subscriberId}/loyalty/earning_rules */ export const getApiStoresSubscriberIdLoyaltyEarningRules = async (args: GetApiStoresSubscriberIdLoyaltyEarningRulesArgs, options?: GetApiStoresSubscriberIdLoyaltyEarningRulesOptions) => { const {baseUrl = "https://api.okendo.io/enterprise", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['okendo-api-version'] != null) headers.set("okendo-api-version", String(args['okendo-api-version'])) if (args['okendo-partner-id'] != null) headers.set("okendo-partner-id", String(args['okendo-partner-id'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores/${encodeURIComponent(String(args.subscriberId))}/loyalty/earning_rules${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(getApiStoresSubscriberIdLoyaltyEarningRulesResponse, await res.json()) };