import {z} from 'zod' export type GetApiBusinessesArgs = {page?: number | undefined, count?: number | undefined, brand_business_id?: (string | null) | undefined}; export type GetApiBusinessesOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const addressGet = z.object({address_id: z.string(), address_type: z.enum(["MAILING", "PHYSICAL", "REGISTERED"]).optional(), street: z.string().optional(), street2: z.string().optional(), city: z.string().min(2).max(40).optional(), state: z.string().optional(), zip_code: z.string().optional(), country: z.string().optional(), is_primary: z.boolean().optional(), date_created: z.string().optional(), date_updated: z.string().optional()}); export const beneficialOwnerGetSerializer = z.object({dob: z.string().optional(), first_name: z.string(), middle_name: z.string().optional(), last_name: z.string(), date_created: z.string().optional(), date_updated: z.string().optional(), addresses: z.array(addressGet)}); export const businessesGetSerializer = z.object({business_id: z.string().min(1), brand_business_id: z.string().min(1).optional(), ein: z.string().min(1), phone: z.string().min(1), email: z.string().min(1), website: z.string().min(1), legal_business_name: z.string().min(1), dba_business_name: z.string().min(1), business_type: z.string().min(1), date_updated: z.string().min(1), date_created: z.string().min(1), industry_type: z.string().min(1), number_of_employees: z.string().min(1), addresses: z.array(addressGet), beneficial_owners: z.array(beneficialOwnerGetSerializer)}); export const getApiBusinessesResponse = z.object({page_info: z.object({page: z.number().int().optional(), page_size: z.number().int().optional(), pages: z.number().int().optional()}).optional(), data: z.array(businessesGetSerializer).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 businesses * * GET /businesses */ export const getApiBusinesses = async (args: GetApiBusinessesArgs, options?: GetApiBusinessesOptions) => { 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.count != null) query.append("count", String(args.count)) if (args.brand_business_id != null) query.append("brand_business_id", String(args.brand_business_id)) if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/businesses${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(getApiBusinessesResponse, await res.json()) };