import {z} from 'zod' export type UpdateCouponRequest = {active?: boolean | undefined, maxRedemptionsPerCustomer?: (number | null) | undefined, expiredBy?: (string | null) | undefined, startsAt?: string | undefined, supportedOfferExternalIds?: Array | undefined, firstTimePurchase?: boolean | undefined, allowedPlayers?: Array | undefined}; export type PatchApiCouponsCouponCouponNameArgs = {body: UpdateCouponRequest}; export type PatchApiCouponsCouponCouponNameOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const couponResponse = z.object({name: z.string().optional(), active: z.boolean().optional(), discountPercentage: z.number().optional(), maxRedemptionsPerCustomer: z.number().int().optional(), expiredBy: z.string().optional(), startsAt: z.string().optional(), supportedOfferExternalIds: z.array(z.string()).optional(), firstTimePurchase: z.boolean().optional(), allowedPlayers: z.array(z.string()).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 }; /** * Updates an existing coupon. Only the fields provided in the request body will be updated. * * PATCH /coupons/coupon/{couponName} */ export const patchApiCouponsCouponCouponName = async (args: PatchApiCouponsCouponCouponNameArgs, options: PatchApiCouponsCouponCouponNameOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/coupons/coupon/${encodeURIComponent(String(args.couponName))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(couponResponse, await res.json()) };