import {z} from 'zod' export type GetApiAccountsEntitiesIdBusinessDetailsArgs = {id: string, status?: ('active' | 'pending') | undefined}; export type GetApiAccountsEntitiesIdBusinessDetailsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAccountsEntitiesIdBusinessDetailsResponse = z.union([z.object({status: z.enum(["active", "pending"]).optional(), legal_name: z.string().min(2).max(300), trading_name: z.string().min(2).max(300), additional_trading_names: z.array(z.string().min(2).max(300)).optional(), registered_address: z.object({address_line1: z.string().min(1).max(300), address_line2: z.string().max(300).nullable().optional(), city: z.string().min(2).max(300), state: z.string().min(1).max(300).optional(), zip: z.string().max(16).nullable(), country: z.string().min(2).max(2)}), principal_address: z.object({address_line1: z.string().min(1).max(300), address_line2: z.string().max(300).nullable().optional(), city: z.string().min(2).max(300), state: z.string().min(1).max(300).optional(), zip: z.string().max(16).nullable(), country: z.string().min(2).max(2)}), documents: z.object({company_verification: z.object({type: z.literal("incorporation_document"), front: z.string().min(31).max(31)}).optional(), proof_of_principal_address: z.object({type: z.literal("proof_of_address"), front: z.string().min(31).max(31)}).optional(), proof_of_registered_address: z.object({type: z.literal("proof_of_address"), front: z.string().min(31).max(31)}).optional()}).optional()}), z.object({status: z.enum(["active", "pending"]).optional(), trading_name: z.string().min(2).max(300), additional_trading_names: z.array(z.string().min(2).max(300)).optional(), principal_address: z.object({address_line1: z.string().min(1).max(300), address_line2: z.string().max(300).nullable().optional(), city: z.string().min(2).max(300), state: z.string().min(1).max(300).optional(), zip: z.string().max(16).nullable(), country: z.string().min(2).max(2)}), documents: z.object({company_verification: z.object({type: z.literal("incorporation_document"), front: z.string().min(31).max(31)}).optional(), proof_of_principal_address: z.object({type: z.literal("proof_of_address"), front: z.string().min(31).max(31)}).optional()}).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 }; /** * Get a sub-entity's business details * * GET /accounts/entities/{id}/business-details * * Also accepts auth: ApiSecretKey */ export const getApiAccountsEntitiesIdBusinessDetails = async (args: GetApiAccountsEntitiesIdBusinessDetailsArgs, options: GetApiAccountsEntitiesIdBusinessDetailsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/entities/${encodeURIComponent(String(args.id))}/business-details${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(getApiAccountsEntitiesIdBusinessDetailsResponse, await res.json()) };