import {z} from 'zod' export type GetApiCouponCodesArgs = {'x-fabric-tenant-id': string, 'x-fabric-channel-id'?: string | undefined, 'x-client-id'?: string | undefined, 'x-fabric-request-id'?: string | undefined, size?: number | undefined, startCursor?: string | undefined, userId?: string | undefined, promotionId?: string | undefined, couponCode?: string | undefined, status?: ('ACTIVE' | 'INACTIVE' | 'REDEEMED') | undefined, startFrom?: string | undefined, startTo?: string | undefined, endFrom?: string | undefined, endTo?: string | undefined, additionalAttributes?: string | undefined, promotionAdditionalAttributes?: string | undefined, title?: string | undefined}; export type GetApiCouponCodesOptions = {baseUrl?: string | undefined, AuthorizationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const cursorQuery = z.object({size: z.number().int().gte(1).lte(100).optional(), nextCursor: z.string().max(1000).nullable().optional()}); export const createdAt = z.string(); export const updatedAt = z.string(); export const couponCodes = z.object({id: z.string(), title: z.string().optional(), userId: z.string().nullable().optional(), couponCode: z.string(), versionId: z.string().optional(), promotionId: z.string(), startAt: z.string().optional(), endAt: z.string().optional(), status: z.enum(["ACTIVE", "INACTIVE", "REDEEMED"]), 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(), createdAt: createdAt, updatedAt: updatedAt}); export const getCouponCodesResponse = z.object({query: cursorQuery.optional(), data: z.array(couponCodes).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 all coupon codes * * GET /coupon-codes */ export const getApiCouponCodes = async (args: GetApiCouponCodesArgs, options?: GetApiCouponCodesOptions) => { const {baseUrl = "https://api.fabric.inc/v3", fetch: fetchFn = fetch, AuthorizationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (args.size != null) query.append("size", String(args.size)) if (args.startCursor != null) query.append("startCursor", String(args.startCursor)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.promotionId != null) query.append("promotionId", String(args.promotionId)) if (args.couponCode != null) query.append("couponCode", String(args.couponCode)) if (args.status != null) query.append("status", String(args.status)) if (args.startFrom != null) query.append("startFrom", String(args.startFrom)) if (args.startTo != null) query.append("startTo", String(args.startTo)) if (args.endFrom != null) query.append("endFrom", String(args.endFrom)) if (args.endTo != null) query.append("endTo", String(args.endTo)) if (args.additionalAttributes != null) query.append("additionalAttributes", String(args.additionalAttributes)) if (args.promotionAdditionalAttributes != null) query.append("promotionAdditionalAttributes", String(args.promotionAdditionalAttributes)) if (args.title != null) query.append("title", String(args.title)) if (AuthorizationToken !== undefined) headers.set('authorization', `Bearer ${AuthorizationToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/coupon-codes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getCouponCodesResponse, await res.json()) };