import {z} from 'zod' export type ProgramRewardAssignmentStockLimited = {quantity: number}; export type ProgramRewardAssignmentStock = {type: 'UNLIMITED' | 'LIMITED', limited?: ProgramRewardAssignmentStockLimited | undefined}; export type V2LoyaltiesProgramsRewardsUpdateRequestBody = {stock: ProgramRewardAssignmentStock}; export type UpdateApiV2LoyaltiesProgramsProgramIdRewardsRewardIdArgs = {body: V2LoyaltiesProgramsRewardsUpdateRequestBody}; export type UpdateApiV2LoyaltiesProgramsProgramIdRewardsRewardIdOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const programRewardAssignmentStockLimited = z.object({quantity: z.number().int().gte(0).lte(9007199254740991)}); export const programRewardAssignmentStock = z.object({type: z.enum(["UNLIMITED", "LIMITED"]), limited: programRewardAssignmentStockLimited.optional()}); export const v2LoyaltiesProgramsRewardsUpdateResponseBody = z.object({reward_id: z.string(), stock: programRewardAssignmentStock, redeemed: z.number(), created_at: z.string(), updated_at: z.string().nullable(), object: z.literal("program_reward")}); 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 program reward assignment * * PUT /v2/loyalties/programs/{programId}/rewards/{rewardId} * * Also accepts auth: bearerAuth */ export const updateApiV2LoyaltiesProgramsProgramIdRewardsRewardId = async (args: UpdateApiV2LoyaltiesProgramsProgramIdRewardsRewardIdArgs, options?: UpdateApiV2LoyaltiesProgramsProgramIdRewardsRewardIdOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/loyalties/programs/${encodeURIComponent(String(args.programId))}/rewards/${encodeURIComponent(String(args.rewardId))}${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(v2LoyaltiesProgramsRewardsUpdateResponseBody, await res.json()) };