import {z} from 'zod' export type CountryCode = 'AFG' | 'ALB' | 'DZA' | 'ASM' | 'AND' | 'AGO' | 'AIA' | 'ATA' | 'ATG' | 'ARG' | 'ARM' | 'ABW' | 'AUS' | 'AUT' | 'AZE' | 'BHS' | 'BHR' | 'BGD' | 'BRB' | 'BLR' | 'BEL' | 'BLZ' | 'BEN' | 'BMU' | 'BTN' | 'BOL' | 'BES' | 'BIH' | 'BWA' | 'BVT' | 'BRA' | 'IOT' | 'BRN' | 'BGR' | 'BFA' | 'BDI' | 'CPV' | 'KHM' | 'CMR' | 'CAN' | 'CYM' | 'CAF' | 'TCD' | 'CHL' | 'CHN' | 'CXR' | 'CCK' | 'COL' | 'COM' | 'COD' | 'COG' | 'COK' | 'CRI' | 'HRV' | 'CUB' | 'CUW' | 'CYP' | 'CZE' | 'CIV' | 'DNK' | 'DJI' | 'DMA' | 'DOM' | 'ECU' | 'EGY' | 'SLV' | 'GNQ' | 'ERI' | 'EST' | 'SWZ' | 'ETH' | 'FLK' | 'FRO' | 'FJI' | 'FIN' | 'FRA' | 'GUF' | 'PYF' | 'ATF' | 'GAB' | 'GMB' | 'GEO' | 'DEU' | 'GHA' | 'GIB' | 'GRC' | 'GRL' | 'GRD' | 'GLP' | 'GUM' | 'GTM' | 'GGY' | 'GIN' | 'GNB' | 'GUY' | 'HTI' | 'HMD' | 'VAT' | 'HND' | 'HKG' | 'HUN' | 'ISL' | 'IND' | 'IDN' | 'IRN' | 'IRQ' | 'IRL' | 'IMN' | 'ISR' | 'ITA' | 'JAM' | 'JPN' | 'JEY' | 'JOR' | 'KAZ' | 'KEN' | 'KIR' | 'PRK' | 'KOR' | 'KWT' | 'KGZ' | 'LAO' | 'LVA' | 'LBN' | 'LSO' | 'LBR' | 'LBY' | 'LIE' | 'LTU' | 'LUX' | 'MAC' | 'MDG' | 'MWI' | 'MYS' | 'MDV' | 'MLI' | 'MLT' | 'MHL' | 'MTQ' | 'MRT' | 'MUS' | 'MYT' | 'MEX' | 'FSM' | 'MDA' | 'MCO' | 'MNG' | 'MNE' | 'MSR' | 'MAR' | 'MOZ' | 'MMR' | 'NAM' | 'NRU' | 'NPL' | 'NLD' | 'NCL' | 'NZL' | 'NIC' | 'NER' | 'NGA' | 'NIU' | 'NFK' | 'MNP' | 'NOR' | 'OMN' | 'PAK' | 'PLW' | 'PSE' | 'PAN' | 'PNG' | 'PRY' | 'PER' | 'PHL' | 'PCN' | 'POL' | 'PRT' | 'PRI' | 'QAT' | 'MKD' | 'ROU' | 'RUS' | 'RWA' | 'REU' | 'BLM' | 'SHN' | 'KNA' | 'LCA' | 'MAF' | 'SPM' | 'VCT' | 'WSM' | 'SMR' | 'STP' | 'SAU' | 'SEN' | 'SRB' | 'SYC' | 'SLE' | 'SGP' | 'SXM' | 'SVK' | 'SVN' | 'SLB' | 'SOM' | 'ZAF' | 'SGS' | 'SSD' | 'ESP' | 'LKA' | 'SDN' | 'SUR' | 'SJM' | 'SWE' | 'CHE' | 'SYR' | 'TWN' | 'TJK' | 'TZA' | 'THA' | 'TLS' | 'TGO' | 'TKL' | 'TON' | 'TTO' | 'TUN' | 'TUR' | 'TKM' | 'TCA' | 'TUV' | 'UGA' | 'UKR' | 'ARE' | 'GBR' | 'UMI' | 'USA' | 'URY' | 'UZB' | 'VUT' | 'VEN' | 'VNM' | 'VGB' | 'VIR' | 'WLF' | 'ESH' | 'YEM' | 'ZMB' | 'ZWE' | 'ALA'; export type V2IdentifyingDocument = {country_code: CountryCode, document_type: string, document_id: string}; export type V2BasicInfo = {legal_name: string, legal_name_en: string, trade_name: string, trade_name_en?: string | undefined, description: string, entity_type: 'LIMITED_LIABILITY_COMPANY' | 'PUBLICLY_LISTED_COMPANY' | 'SOLE_PROPRIETOR' | 'PARTNERSHIP' | 'CORPORATION' | 'TRUST' | 'PRIVATE_FOUNDATION' | 'CHARITY' | 'NONPROFIT_ORGANIZATION' | 'PUBLIC_AGENCY_OR_AUTHORITY', email: string, phone: string, incorporation_date: string, country_of_incorporation: CountryCode, registration_number: string, legal_entity_identifier?: string | undefined, is_dao?: boolean | undefined, tax_info: Array, websites: Array}; export type V2FinancialProfile = {purpose_id: number, purpose_remarks?: string | undefined, source_of_fund_ids: Array, source_of_funds_description?: string | undefined, source_of_wealth_ids: Array, estimated_annual_revenue_usd: number, expected_monthly_deposits_usd: number, expected_monthly_withdrawals_usd: number, expected_transaction_value_usd?: number | undefined, expected_monthly_transaction_count?: number | undefined, third_party_fund_usage: boolean}; export type V2Address = {street_line_1: string, street_line_1_en: string, street_line_2?: string | undefined, street_line_2_en?: string | undefined, city: string, state: string, subdivision_code: string, postal_code: string, country: CountryCode}; export type V2Addresses = {is_incorporated_address_same: boolean, incorporated_address: V2Address, physical_address?: V2Address | undefined}; export type V2IndividualIdentifyingDocument = {country_code: CountryCode, document_type: string, document_id: string}; export type V2APBasicInfo = {first_name: string, first_name_en: string, middle_name?: string | undefined, middle_name_en?: string | undefined, last_name: string, last_name_en: string, dob: string, email: string, phone: string, country_of_residence: CountryCode, primary_nationality: CountryCode, secondary_nationality?: CountryCode | undefined, tax_info: Array}; export type V2OwnershipInfo = {designation: string, percentage_of_ownership: number, relationship_establishment_date: string, has_control: boolean, is_signer: boolean, is_director: boolean}; export type V2AssociatedParty = {ref?: string | undefined, basic_info: V2APBasicInfo, address: V2Address, roles: Array<'shareholder' | 'ubo'>, ownership_info?: V2OwnershipInfo | undefined}; export type V2CorporateShareholder = {entity_name: string, entity_name_en: string, registration_country_code: CountryCode, ownership_percentage: number, registration_number?: string | undefined, entity_type?: ('LIMITED_LIABILITY_COMPANY' | 'PUBLICLY_LISTED_COMPANY' | 'SOLE_PROPRIETOR' | 'PARTNERSHIP' | 'CORPORATION' | 'TRUST' | 'PRIVATE_FOUNDATION' | 'CHARITY' | 'NONPROFIT_ORGANIZATION' | 'PUBLIC_AGENCY_OR_AUTHORITY') | undefined, incorporation_date?: string | undefined}; export type V2ForeignBranch = {registration_country_code: CountryCode, name?: string | undefined, registration_number?: string | undefined}; export type V2HoldingStructure = {has_material_intermediary_ownership?: boolean | undefined, corporate_shareholders?: Array | undefined, foreign_branches?: Array | undefined}; export type V2PublicListing = {mic?: string | undefined, isin?: string | undefined, ticker_symbol?: string | undefined}; export type V2RiskProfile = {high_risk_activities?: (Array<'adult_entertainment' | 'crypto_exchange' | 'gambling' | 'cannabis' | 'weapons' | 'money_services' | 'cross_border_payments'>) | undefined, high_risk_activities_explanation?: string | undefined, conducts_money_services?: boolean | undefined, conducts_money_services_via_fin?: boolean | undefined, conducts_money_services_description?: string | undefined}; export type V2RegulatedActivity = {description: string, primary_authority_country_code: CountryCode, primary_authority_name: string, license_number: string}; export type V2CustomerRiskSplit = {low_risk: number, medium_risk: number, high_risk: number}; export type V2VerificationMethod = {method: string, system: string}; export type V2AML = {supervisory_authority_name?: string | undefined, license_number?: string | undefined, has_appointed_mlro?: boolean | undefined, customer_risk_split?: V2CustomerRiskSplit | undefined, prohibits_anonymous_or_fictitious_accounts?: boolean | undefined, prohibits_accounts_for_unlicensed_or_shell_customers?: boolean | undefined, customer_identity_verification?: V2VerificationMethod | undefined, pep_and_sanctions_screening?: V2VerificationMethod | undefined, sanction_lists?: Array | undefined, customer_risk_classification_from_due_diligence?: boolean | undefined, enhanced_due_diligence_process?: boolean | undefined, transaction_monitoring?: V2VerificationMethod | undefined, procedures_for_transaction_monitoring?: boolean | undefined, subject_to_ml_or_tf_investigation?: string | undefined, subject_to_regulatory_enforcement_past_2_years?: string | undefined, confirms_no_service_to_sanctioned_countries?: boolean | undefined, client_funds_accessibility?: string | undefined, aml_ctf_audit_completed?: boolean | undefined, planned_audit_date?: string | undefined}; export type V2Compliance = {operates_in_prohibited_countries?: boolean | undefined, additional_description_for_compliance_screening?: string | undefined, risk_profile?: V2RiskProfile | undefined, regulated_activity?: V2RegulatedActivity | undefined, aml?: V2AML | undefined}; export type CreateBusinessCustomerV2Input = {verification_type: 'STANDARD' | 'RELIANCE', industry_codes: Array, basic_info: V2BasicInfo, financial_profile: V2FinancialProfile, addresses: V2Addresses, associated_parties: Array, holding_structure?: V2HoldingStructure | undefined, public_listings?: Array | undefined, compliance?: V2Compliance | undefined, meta_data?: ({reference?: string | undefined} | {[key: string]: unknown}) | undefined}; export type CreateApiV2CustomersBusinessArgs = {body: CreateBusinessCustomerV2Input}; export type CreateApiV2CustomersBusinessOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2CustomersBusinessResponse = z.object({customer_id: z.string().optional(), tos_policies_url: 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 Business Customer V2 * * POST /v2/customers/business */ export const createApiV2CustomersBusiness = async (args: CreateApiV2CustomersBusinessArgs, options?: CreateApiV2CustomersBusinessOptions) => { const {baseUrl = "https://sandbox.api.fin.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/customers/business${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(createApiV2CustomersBusinessResponse, await res.json()) };