import {z} from 'zod' export type GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsArgs = {pageSize?: number | undefined, skip?: number | undefined, sort?: string | undefined, loyaltyProgramId: number, identifier?: string | undefined, profileId?: number | undefined, batchId?: string | undefined}; export type GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const loyaltyCardIdentifier = z.string().min(4).max(108); export const loyaltyCardProfileRegistration = z.object({integrationId: z.string().max(1000), timestamp: z.string()}); export const tier = z.object({id: z.number().int(), name: z.string(), startDate: z.string().optional(), expiryDate: z.string().optional(), downgradePolicy: z.enum(["one_down", "balance_based"]).optional(), source: z.enum(["boost", "points"]).optional(), reason: z.string().optional()}); export const ledgerInfo = z.object({currentBalance: z.number(), pendingBalance: z.number(), negativeBalance: z.number().optional(), expiredBalance: z.number(), spentBalance: z.number(), tentativeCurrentBalance: z.number(), tentativePendingBalance: z.number().optional(), tentativeNegativeBalance: z.number().optional(), currentTier: tier.optional(), pointsToNextTier: z.number().optional(), nextTierName: z.string().optional()}); export const loyaltyCard = z.object({id: z.number().int(), created: z.string(), programID: z.number().int(), programName: z.string().optional(), programTitle: z.string().optional(), status: z.string(), blockReason: z.string().optional(), attributes: z.object({}).optional(), identifier: loyaltyCardIdentifier, usersPerCardLimit: z.number().int().gte(0), profiles: z.array(loyaltyCardProfileRegistration).optional(), ledger: ledgerInfo.optional(), subledgers: z.record(z.string(), ledgerInfo).optional(), modified: z.string().optional(), oldCardIdentifier: loyaltyCardIdentifier.optional(), newCardIdentifier: loyaltyCardIdentifier.optional(), batchId: z.string().optional()}); export const getApiV1LoyaltyProgramsLoyaltyProgramIdCardsResponse = z.object({hasMore: z.boolean(), data: z.array(loyaltyCard)}); 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 loyalty cards * * GET /v1/loyalty_programs/{loyaltyProgramId}/cards * * Also accepts auth: management_key */ export const getApiV1LoyaltyProgramsLoyaltyProgramIdCards = async (args: GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsArgs, options?: GetApiV1LoyaltyProgramsLoyaltyProgramIdCardsOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.identifier != null) query.append("identifier", String(args.identifier)) if (args.profileId != null) query.append("profileId", String(args.profileId)) if (args.batchId != null) query.append("batchId", String(args.batchId)) if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loyalty_programs/${encodeURIComponent(String(args.loyaltyProgramId))}/cards${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(getApiV1LoyaltyProgramsLoyaltyProgramIdCardsResponse, await res.json()) };