import {z} from 'zod' export type CustomerTypeFilter = 'individual' | 'business'; export type GetApiV1CustomersArgs = {reference?: string | undefined, customer_type?: CustomerTypeFilter | undefined, cursor?: (string | null) | undefined, page_size?: number | undefined}; export type GetApiV1CustomersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const approvalStatus = z.enum(["auto_approved", "pending", "approved", "rejected"]); export const customerTypeFilter = z.enum(["individual", "business"]); export const customerSummary = z.object({approval_status: approvalStatus, created_at: z.string(), customer_type: customerTypeFilter, display_name: z.string(), id: z.string(), reference: z.string().max(36)}); export const cursorString = z.object({next: z.string().nullable().optional(), size: z.number().int().gte(0)}); export const listCustomersResponse = z.object({customers: z.array(customerSummary), pages: cursorString}); 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 }; /** * List customers * * GET /v1/customers */ export const getApiV1Customers = async (args: GetApiV1CustomersArgs, options?: GetApiV1CustomersOptions) => { const {baseUrl = "https://auth-sandbox.fuse.me", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.reference != null) query.append("reference", String(args.reference)) if (args.customer_type != null) query.append("customer_type", String(args.customer_type)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(listCustomersResponse, await res.json()) };