import {z} from 'zod' export type GetApiCustomersArgs = {page?: number | undefined, per_page?: (1 | 2 | 5 | 10 | 20 | 50) | undefined, brand_person_id?: (string | null) | undefined}; export type GetApiCustomersOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({address_id: z.string().optional(), address_type: z.enum(["MAILING", "PHYSICAL"]), street: z.string(), street2: z.string().optional(), city: z.string().min(2).max(40), state: z.string(), zip_code: z.string(), country: z.string(), is_primary: z.boolean(), date_created: z.string().optional()}); export const customer = z.object({customer_id: z.string().optional(), brand_person_id: z.string().optional(), bond_brand_id: z.string().optional(), business_id: z.string().nullable().optional(), dob: z.string().nullable().optional(), first_name: z.string(), middle_name: z.string().optional(), last_name: z.string().optional(), ssn: z.string().optional(), phone: z.string().optional(), phone_country_code: z.string().optional(), email: z.string().optional(), addresses: z.array(address), date_created: z.string().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 }; /** * Retrieve all customers * * GET /customers */ export const getApiCustomers = async (args: GetApiCustomersArgs, options?: GetApiCustomersOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.brand_person_id != null) query.append("brand_person_id", String(args.brand_person_id)) if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/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(customer, await res.json()) };