import {z} from 'zod' export type ParameterLimit = number; export type ParameterOrderListPendingPoints = 'id' | '-id'; export type ParameterString = string; export type GetApiV1LoyaltiesCampaignIdPendingPointsArgs = {limit?: ParameterLimit | undefined, order?: ParameterOrderListPendingPoints | undefined, starting_after_id?: ParameterString | undefined}; export type GetApiV1LoyaltiesCampaignIdPendingPointsOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const eventSource = z.object({channel: z.enum(["USER_PORTAL", "API", "CLIENT_API", "INTERNAL"]), user: z.object({id: z.string()}).optional(), api_key: z.object({name: z.string(), app_id: z.string()}).optional()}); export const loyaltyPendingPointsDetails = z.object({loyalty_tier: z.object({id: z.string(), name: z.string()}).optional(), holder_loyalty_tier: z.object({id: z.string(), name: z.string()}).optional(), event: z.object({id: z.string().optional(), type: z.literal("customer.order.paid").optional(), group_id: z.string().optional(), entity_id: z.string().optional(), created_at: z.string().optional(), category: z.enum(["ACTION", "EFFECT"]).optional(), event_source: eventSource.optional()}), earning_rule: z.object({id: z.string(), source: z.object({banner: z.string()})}), order: z.object({id: z.string(), source_id: z.string().nullable()})}); export const loyaltyPendingPoints = z.object({id: z.string(), voucher_id: z.string(), campaign_id: z.string(), customer_id: z.string(), order_id: z.string(), points: z.number().int(), activates_at: z.string(), details: loyaltyPendingPointsDetails, created_at: z.string(), updated_at: z.string().optional()}); export const loyaltiesPendingPointsListResponseBody = z.object({object: z.literal("list"), data_ref: z.literal("data"), data: z.array(loyaltyPendingPoints), has_more: z.boolean(), more_starting_after: z.string().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 }; /** * List Campaign Pending Points * * GET /v1/loyalties/{campaignId}/pending-points * * Also accepts auth: X-Voucherify-OAuth */ export const getApiV1LoyaltiesCampaignIdPendingPoints = async (args: GetApiV1LoyaltiesCampaignIdPendingPointsArgs, options?: GetApiV1LoyaltiesCampaignIdPendingPointsOptions) => { 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.starting_after_id != null) query.append("starting_after_id", String(args.starting_after_id)) if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loyalties/${encodeURIComponent(String(args.campaignId))}/pending-points${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(loyaltiesPendingPointsListResponseBody, await res.json()) };