import {z} from 'zod' export type GetApiApiV40IntegrationsCustomersCustomerIdCouponsArgs = {customerId: string}; export type GetApiApiV40IntegrationsCustomersCustomerIdCouponsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const customerCouponsResponse = z.object({coupons: z.array(z.object({name: z.string().optional(), code: z.string().optional(), value: z.number().optional(), type: z.enum(["Free Shipping", "Fixed Rate Discount", "Percentage", "Free Product", "Custom", "Percentage-based Fees Discount", "Percentage-based Cashback"]).optional(), target: z.enum(["Online", "POS", "Permanent", "Reward", "Automation"]).optional(), currency: z.string().optional(), startDate: z.string().optional(), expiryDate: z.string().optional(), isExpired: z.boolean().optional(), isActive: z.boolean().optional(), usageLimit: z.number().optional(), limitPerCustomer: z.number().optional(), usedCount: z.number().optional(), customerUsedCount: z.number().optional(), isAvailableToUse: z.boolean().optional(), isLocked: z.boolean().optional(), iconPath: 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 Customer Coupons * * GET /api/v4.0/integrations/customers/{customerId}/coupons */ export const getApiApiV40IntegrationsCustomersCustomerIdCoupons = async (args: GetApiApiV40IntegrationsCustomersCustomerIdCouponsArgs, options?: GetApiApiV40IntegrationsCustomersCustomerIdCouponsOptions) => { const {baseUrl = "https://api.gameball.co", fetch: fetchFn = fetch, apiKey, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("apikey", apiKey) if (secretKey !== undefined) headers.set("secretkey", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4.0/integrations/customers/${encodeURIComponent(String(args.customerId))}/coupons${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(customerCouponsResponse, await res.json()) };