import {z} from 'zod' export type GetApiAccountsEntitiesIdRepresentativesArgs = {id: string, status?: ('active' | 'pending') | undefined}; export type GetApiAccountsEntitiesIdRepresentativesOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subEntityRepresentatives = z.object({status: z.enum(["active", "pending"]).optional(), representatives: z.array(z.object({id: z.string().optional(), roles: z.array(z.enum(["ubo", "authorised_signatory", "control_person"])), ownership_percentage: z.number().int().gte(0).lte(100).optional(), company_position: z.enum(["ceo", "cfo", "coo", "managing_member", "general_partner", "president", "vice_president", "treasurer", "other_senior_management", "other_executive_officer", "other_non_executive_non_senior"]).optional(), individual: z.object({first_name: z.string().min(2).max(50), middle_name: z.string().min(2).max(50).optional(), last_name: z.string().min(2).max(50), email_address: z.string(), date_of_birth: z.object({day: z.number().int().gte(1).lte(31), month: z.number().int().gte(1).lte(12), year: z.number().int().gte(1900).lte(2999)}), address: z.object({address_line1: z.string().min(1).max(300), address_line2: z.string().max(300).optional(), city: z.string().min(2).max(300), state: z.string().min(1).max(300).optional(), zip: z.string().max(16), country: z.string().min(2).max(2)}), place_of_birth: z.object({country: z.string().min(2).max(2)}), citizenships: z.array(z.object({type: z.string().optional(), country: z.string().min(2).max(2)})), national_id_type: z.enum(["ssn", "itin", "passport", "driving_license", "national_id_card", "residence_permit", "other"]), national_id_number: z.string().min(5).max(16), phone: z.object({country_code: z.string().min(2).max(2), number: z.string().min(5).max(16)})}), documents: z.object({identity_verification: z.object({type: z.enum(["passport", "national_identity_card", "driving_license", "citizen_card", "residence_permit", "electoral_id"]), front: z.string().min(31).max(31), back: z.string().min(31).max(31).optional()}).optional(), certified_authorised_signatory: z.object({type: z.literal("power_of_attorney"), 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 representatives * * GET /accounts/entities/{id}/representatives * * Also accepts auth: ApiSecretKey */ export const getApiAccountsEntitiesIdRepresentatives = async (args: GetApiAccountsEntitiesIdRepresentativesArgs, options: GetApiAccountsEntitiesIdRepresentativesOptions) => { 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))}/representatives${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(subEntityRepresentatives, await res.json()) };