import {z} from 'zod' export type RewardUpdateRequest = {sandbox?: string | undefined}; export type UpdateApiV2RewardsRewardIdArgs = {reward_id: string, body: RewardUpdateRequest}; export type UpdateApiV2RewardsRewardIdOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const journeyKey = z.object({name: z.string().optional(), value: z.string().optional()}); export const rewardResponse = z.object({action_id: z.string(), campaign_id: z.string(), cause_event_id: z.string(), container: z.string(), created_at: zonedDateTime, data: z.record(z.string(), z.string()), email: z.string().nullable(), expires_at: 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"]), journey_key: journeyKey, journey_name: z.string(), partner_reward_id: z.string().nullable(), partner_reward_key_type: z.enum(["COUPON", "ID", "LINK"]), partner_reward_supplier_id: z.string().nullable(), partner_user_id: z.string().nullable(), person_id: z.string(), reward_id: z.string(), reward_supplier_id: z.string(), root_event_id: z.string(), sandbox: z.string(), state: z.enum(["CANCELED", "EARNED", "FAILED", "FULFILLED", "REDEEMED", "REVOKED", "SENT"])}); 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 }; /** * Update a reward * * PUT /v2/rewards/{reward_id} * * Also accepts auth: QUERY, COOKIE */ export const updateApiV2RewardsRewardId = async (args: UpdateApiV2RewardsRewardIdArgs, options?: UpdateApiV2RewardsRewardIdOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v2/rewards/${encodeURIComponent(String(args.reward_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rewardResponse, await res.json()) };