import {z} from 'zod' export type AddressWrite = {street: string, street2?: string | undefined, city: string, state: string, zip_code: string, country: string}; export type CustomerWrite = {brand_person_id?: string | undefined, business_id?: (string | null) | undefined, dob?: (unknown | null) | undefined, first_name: string, middle_name?: string | undefined, last_name: string, ssn?: string | undefined, phone?: string | undefined, phone_country_code?: string | undefined, email?: string | undefined, addresses: Array}; export type CreateApiCustomersArgs = {body: CustomerWrite}; export type CreateApiCustomersOptions = {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 }; /** * Create a customer * * POST /customers/ */ export const createApiCustomers = async (args: CreateApiCustomersArgs, options?: CreateApiCustomersOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) 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()) };