import {z} from 'zod' export type GetApiV5PersonsPersonIdRewardsRewardIdArgs = {person_id: string, reward_id: string}; export type GetApiV5PersonsPersonIdRewardsRewardIdOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const personRewardDataResponse = z.object({name: z.string().optional(), value: z.object({}).optional()}); export const journeyKey = z.object({name: z.string().optional(), value: z.string().optional()}); export const personRewardResponse = z.object({campaign_id: z.string(), container: z.string(), created_date: zonedDateTime, data: z.record(z.string(), personRewardDataResponse), earned_date: zonedDateTime, expiry_date: zonedDateTime, face_value: z.number(), face_value_type: z.enum(["AUD", "BRL", "CAD", "CNY", "EUR", "GBP", "HKD", "INR", "JPY", "KRW", "MONTH", "MXN", "NZD", "PERCENT_OFF", "POINTS", "TRY", "TWD", "USD"]), id: z.string(), journey_key: journeyKey, journey_name: z.string(), name: z.string(), partner_reward_id: z.string(), partner_reward_supplier_id: z.string(), program: z.string(), redeemed_date: zonedDateTime, reward_supplier_id: z.string(), sandbox: z.string(), state: z.enum(["CANCELED", "EARNED", "FAILED", "FULFILLED", "REDEEMED", "REVOKED", "SENT"]), tags: z.array(z.string()), updated_date: zonedDateTime, value_of_rewarded_event: z.number()}); 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 a person reward * * GET /v5/persons/{person_id}/rewards/{reward_id} * * Also accepts auth: QUERY, COOKIE */ export const getApiV5PersonsPersonIdRewardsRewardId = async (args: GetApiV5PersonsPersonIdRewardsRewardIdArgs, options?: GetApiV5PersonsPersonIdRewardsRewardIdOptions) => { 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 (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v5/persons/${encodeURIComponent(String(args.person_id))}/rewards/${encodeURIComponent(String(args.reward_id))}${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(personRewardResponse, await res.json()) };