import {z} from 'zod' export type GetApiStoresStoreIdCustomersArgs = {storeId: string, email?: string | undefined, ipAddress?: string | undefined, balanceFrom?: number | undefined, balanceTo?: number | undefined, stripeCustomerId?: string | undefined, affiliateCode?: string | undefined, Filters?: string | undefined, Sorts?: string | undefined, Page?: number | undefined, PageSize?: number | undefined}; export type GetApiStoresStoreIdCustomersOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const publicCustomerReference = z.object({id: z.string(), dateCreated: z.string(), emailAddress: z.string(), status: z.string(), isAffiliate: z.boolean(), totalSpend: z.number(), averageSpend: z.number(), totalOrders: z.number(), isBlocked: z.boolean()}); export const publicCustomerReferencePaginatedResponse = z.object({success: z.boolean(), pages: z.number().int(), data: z.array(publicCustomerReference)}); 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 customers * * GET /stores/{storeId}/customers */ export const getApiStoresStoreIdCustomers = async (args: GetApiStoresStoreIdCustomersArgs, options?: GetApiStoresStoreIdCustomersOptions) => { const {baseUrl = "https://api.komerza.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.email != null) query.append("email", String(args.email)) if (args.ipAddress != null) query.append("ipAddress", String(args.ipAddress)) if (args.balanceFrom != null) query.append("balanceFrom", String(args.balanceFrom)) if (args.balanceTo != null) query.append("balanceTo", String(args.balanceTo)) if (args.stripeCustomerId != null) query.append("stripeCustomerId", String(args.stripeCustomerId)) if (args.affiliateCode != null) query.append("affiliateCode", String(args.affiliateCode)) if (args.Filters != null) query.append("Filters", String(args.Filters)) if (args.Sorts != null) query.append("Sorts", String(args.Sorts)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores/${encodeURIComponent(String(args.storeId))}/customers${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(publicCustomerReferencePaginatedResponse, await res.json()) };