import {z} from 'zod' export type GetPromoList = {offset?: number | undefined, limit?: number | undefined, keyword?: string | undefined, sortBy?: ('updatedAt' | 'title' | 'startDate' | 'endDate') | undefined, sortOrder?: ('desc' | 'asc') | undefined}; export type CreateApiApiOffersCouponListArgs = {'x-site-context': string, body: GetPromoList}; export type CreateApiApiOffersCouponListOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const couponListResponse = z.object({query: z.object({limit: z.number().optional(), offset: z.number().optional(), count: z.number().optional()}).optional(), promos: z.array(z.object({_id: z.string().optional(), promoId: z.number().optional(), promoCodes: z.array(z.string()).optional(), promoCount: z.number().optional(), state: z.enum(["ACTIVE", "SCHEDULED", "DISABLED", "EXPIRED"]).optional(), isImplicit: z.boolean().optional(), title: z.string().optional(), startDate: z.string().optional(), endDate: z.string().optional(), isExclusive: z.boolean().optional(), eligiblePriceList: z.array(z.number()).optional(), level: z.number().int().optional(), stackingType: z.enum(["STACKABLE", "EXCLUSIVE", "TYPE_EXCLUSIVE", "UNIVERSAL"]).optional(), updatedAt: z.string().optional()})).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 }; /** * Get list of all coupons * * POST /api-offers/coupon/list */ export const createApiApiOffersCouponList = async (args: CreateApiApiOffersCouponListArgs, options?: CreateApiApiOffersCouponListOptions) => { const {baseUrl = "https://live.copilot.fabric.inc", 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-site-context'] != null) headers.set("x-site-context", String(args['x-site-context'])) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api-offers/coupon/list${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(couponListResponse, await res.json()) };