import {z} from 'zod' export type UpdateRedemptionRequest = {promotionId: string | {name: string, values: string}, couponCode?: string | undefined, orderId: string, userId?: (string | null) | undefined, email?: (string | null) | undefined, storeId?: (string | null) | undefined, redeemedAt?: (string | null) | undefined, additionalAttributes?: (Array<{name: string, values: string | Array}>) | undefined, status?: ('INACTIVE' | 'ACTIVE') | undefined}; export type UpdateApiRedemptionsRedemptionIdArgs = {'x-fabric-tenant-id': string, 'x-fabric-channel-id'?: string | undefined, 'x-client-id'?: string | undefined, 'x-fabric-request-id'?: string | undefined, redemptionId: string, body: UpdateRedemptionRequest}; export type UpdateApiRedemptionsRedemptionIdOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const updateRedemptionResponse = z.object({id: z.string(), promotionId: z.string(), couponCode: z.string().optional(), orderId: z.string(), userId: z.string().nullable().optional(), email: z.string().nullable().optional(), storeId: z.string().optional(), redeemedAt: z.string().optional(), status: z.enum(["INACTIVE", "ACTIVE"]).optional(), additionalAttributes: z.array(z.object({name: z.string(), values: z.union([z.string(), z.array(z.string())])})).optional(), promotionAdditionalAttributes: z.array(z.object({name: z.string(), values: z.union([z.string(), z.array(z.string())])})).optional(), promotionName: z.string().optional(), createdAt: z.string(), updatedAt: z.string()}); 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 redemption by ID * * PUT /redemptions/{redemptionId} */ export const updateApiRedemptionsRedemptionId = async (args: UpdateApiRedemptionsRedemptionIdArgs, options?: UpdateApiRedemptionsRedemptionIdOptions) => { const {baseUrl = "https://api.fabric.inc/v3", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-fabric-tenant-id'] != null) headers.set("x-fabric-tenant-id", String(args['x-fabric-tenant-id'])) if (args['x-fabric-channel-id'] != null) headers.set("x-fabric-channel-id", String(args['x-fabric-channel-id'])) if (args['x-client-id'] != null) headers.set("x-client-id", String(args['x-client-id'])) if (args['x-fabric-request-id'] != null) headers.set("x-fabric-request-id", String(args['x-fabric-request-id'])) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/redemptions/${encodeURIComponent(String(args.redemptionId))}${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(updateRedemptionResponse, await res.json()) };