import {z} from 'zod' export type GetApiRewardFulfillmentsArgs = {customer_id?: number | undefined, fulfillment_status?: ('pending' | 'issued' | 'cancelled' | 'failed') | undefined, usage_status?: ('used' | 'unused' | 'untracked') | undefined, updated_at_min?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiRewardFulfillmentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rewardFulfillment = z.object({id: z.number().int().optional(), name: z.string().optional(), code: z.string().optional(), customer_id: z.number().int().optional(), fulfillment_status: z.enum(["pending", "issued", "cancelled", "failed"]).optional(), image_url: z.string().optional(), action_text: z.string().nullable().optional(), action_url: z.string().nullable().optional(), usage_instructions: z.string().nullable().optional(), terms_and_conditions: z.string().nullable().optional(), expires_at: z.string().nullable().optional(), usage_status: z.enum(["used", "unused", "untracked"]).optional(), used_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const paginationMetadata = z.object({next_cursor: z.string().nullable().optional(), previous_cursor: z.string().nullable().optional()}); export const getApiRewardFulfillmentsResponse = z.object({reward_fulfillments: z.array(rewardFulfillment).optional(), metadata: paginationMetadata.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 reward fulfillments * * GET /reward_fulfillments */ export const getApiRewardFulfillments = async (args: GetApiRewardFulfillmentsArgs, options?: GetApiRewardFulfillmentsOptions) => { const {baseUrl = "https://api.smile.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.fulfillment_status != null) query.append("fulfillment_status", String(args.fulfillment_status)) if (args.usage_status != null) query.append("usage_status", String(args.usage_status)) if (args.updated_at_min != null) query.append("updated_at_min", String(args.updated_at_min)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reward_fulfillments${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(getApiRewardFulfillmentsResponse, await res.json()) };