import {z} from 'zod' export type Token = string; export type SaveData = boolean; export type PatchBody = {session: string, token?: Token | undefined, link: string, save_data?: SaveData | undefined}; export type PatchApiApiOwnersArgs = {omit?: string | undefined, fields?: string | undefined, body: PatchBody}; export type PatchApiApiOwnersOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const link = z.string().nullable(); export const collectedAt = z.string(); export const createdAt = z.string(); export const enumOwnerMaritalStatus = z.enum(["SINGLE", "MARRIED", "WIDOWED", "SEPARATED", "DIVORCED", "CIVIL_UNION", "OTHER"]).nullable(); export const enumOwnerGender = z.enum(["FEMALE", "MALE", "OTHER"]).nullable(); export const ownerDocumentIdOpenFinanceBrazil = z.object({document_type: z.string(), document_number: z.string()}); export const enumOwnerIndividualDocumentIdType = z.enum(["DRIVERS_LICENSE", "PASSPORT", "ID_CARD", "FISCAL_ID", "FOREIGNER_REGISTRATION_CARD", "OTHER", "null"]).nullable(); export const ownerIndividualDocumentIds = z.object({type: enumOwnerIndividualDocumentIdType, type_additional_info: z.string().max(70).nullable(), number: z.string().max(40), check_digit: z.string().max(2), issue_date: z.string().max(10).nullable(), expiration_date: z.string().max(10).nullable(), country_of_issuance: z.string().max(3).nullable(), additional_info: z.string().max(100).nullable()}).nullable(); export const ownerIndividualNationalityDocumentId = z.object({type: enumOwnerIndividualDocumentIdType, number: z.string().max(40), issue_date: z.string().max(10).nullable(), expiration_date: z.string().max(10).nullable(), country_of_issuance: z.string().max(3).nullable(), additional_info: z.string().max(100).nullable()}).nullable(); export const ownerNationalities = z.object({info: z.string().max(40).nullable(), documents: z.array(ownerIndividualNationalityDocumentId)}); export const ownerEmails = z.object({is_main: z.boolean(), email: z.string().max(320)}).nullable(); export const ownerAddresses = z.object({is_main: z.boolean(), address: z.string().max(150), additional_info: z.string().max(150).nullable(), district_name: z.string().max(50).nullable(), town: z.string().max(50), town_code: z.string().max(7).nullable(), state: z.string().max(2).nullable(), postcode: z.string().max(8), country_name: z.string().max(80), country_code: z.string().max(3).nullable(), latitude: z.string().max(13).nullable(), longitude: z.string().max(13).nullable()}).nullable(); export const enumOwnerPhoneNumberType = z.enum(["LANDLINE", "MOBILE", "OTHER", "null"]).nullable(); export const ownerPhoneNumbers = z.object({is_main: z.boolean(), type: enumOwnerPhoneNumberType, additional_info: z.string().max(100).nullable(), number: z.string().max(11), country_code: z.string().max(4).nullable(), area_code: z.string().max(2).nullable(), extension: z.string().max(5).nullable()}).nullable(); export const enumOwnerFiliationType = z.enum(["MOTHER", "FATHER", "null"]).nullable(); export const ownerFiliations = z.object({type: enumOwnerFiliationType, civil_name: z.string().max(70), social_name: z.string().max(70).nullable()}); export const enumOwnerIndividualFinancialProfileOccupationCode = z.enum(["BRAZIL_PUBLIC_OFFICE", "BRAZIL_OCCUPATION_CODE", "OTHER", "null"]).nullable(); export const enumOwnerInformedIncomeFrequency = z.enum(["DAILY", "WEEKLY", "FORTNIGHTLY", "MONTHLY", "BIMONTHLY", "QUARTERLY", "BIANNUALLY", "ANNUALLY", "OTHERS"]).nullable(); export const ownerIndividualInformedIncome = z.object({frequency: enumOwnerInformedIncomeFrequency, amount: z.number(), currency: z.string().max(3), date: z.string().max(10)}); export const ownerIndividualPatrimony = z.object({amount: z.number(), currency: z.string().max(3), year: z.number().int().gte(1700).lte(2090)}).nullable(); export const ownerIndividualFinancialProfile = z.object({company_id: z.string().max(14).nullable(), occupation_code: enumOwnerIndividualFinancialProfileOccupationCode, occupation_description: z.string().max(100).nullable(), informed_income: ownerIndividualInformedIncome, patrimony: ownerIndividualPatrimony}).nullable(); export const enumOwnerProcuratorType = z.enum(["LEGAL_REPRESENTATIVE", "ATTORNEY", "null"]).nullable(); export const ownerProcurators = z.object({type: enumOwnerProcuratorType, civil_name: z.string().max(70), social_name: z.string().max(70).nullable(), document_number: z.string().max(11)}).nullable(); export const enumOwnerIndividualProductType = z.enum(["SAVINGS_ACCOUNT", "CHECKING_ACCOUNT", "null"]).nullable(); export const ownerIndividualProducts = z.object({type: enumOwnerIndividualProductType, subtype: z.string().nullable(), agency: z.string().max(4).nullable(), clearing_code: z.string().max(3), number: z.string().max(20), check_digit: z.string().max(2)}).nullable(); export const ownerIndividualSalaryPortabilityRequests = z.object({employer_name: z.string().max(80).optional(), employer_id_number: z.string().min(11).max(14).optional(), employer_bank_id_number: z.string().min(14).max(14).optional(), employer_bank_code: z.string().min(8).max(8).optional(), portability_approval_date: z.string().max(10).optional()}); export const ownerIndividualPayrollAccounts = z.object({employer_name: z.string().max(80).optional(), employer_id_number: z.string().min(11).max(14).optional(), employer_bank_id_number: z.string().min(14).max(14).optional(), employer_bank_code: z.string().min(8).max(8).optional(), account_opening_date: z.string().max(10).optional()}); export const ownerIndividualFinancialRelation = z.object({start_date: z.string().max(20), product_services: z.array(z.string()), product_services_additional_info: z.string().max(100).nullable(), procurators: z.array(ownerProcurators), products: z.array(ownerIndividualProducts), salary_portability_requests: z.array(ownerIndividualSalaryPortabilityRequests).optional(), payroll_accounts: z.array(ownerIndividualPayrollAccounts).optional()}).nullable(); export const ownerIndividualOpenFinanceBrazil = z.object({id: id, link: link, internal_identification: z.string().nullable(), collected_at: collectedAt, created_at: createdAt, display_name: z.string().max(128), social_name: z.string().max(128).nullable(), birth_date: z.string().max(10), marital_status: enumOwnerMaritalStatus, marital_status_additional_info: z.string().max(50).nullable(), gender: enumOwnerGender, companies_id: z.array(z.string().max(14)), is_local_resident: z.boolean(), document_id: ownerDocumentIdOpenFinanceBrazil, additional_documents: z.array(ownerIndividualDocumentIds), nationalities: z.array(ownerNationalities).nullable(), email: z.string().max(320).nullable(), emails: z.array(ownerEmails), address: z.string().nullable(), addresses: z.array(ownerAddresses), phone_number: z.string().nullable(), phone_numbers: z.array(ownerPhoneNumbers), filiations: z.array(ownerFiliations), financial_profile: ownerIndividualFinancialProfile, financial_relation: ownerIndividualFinancialRelation}); export const ownerBusinessDocumentIds = z.object({type: enumOwnerIndividualDocumentIdType, type_additional_info: z.string().max(70).nullable(), number: z.string().max(40), check_digit: z.string().max(2), issue_date: z.string().max(10).nullable(), expiration_date: z.string().max(10).nullable(), country_of_issuance: z.string().max(3).nullable(), additional_info: z.string().max(100).nullable()}).nullable(); export const enumOwnerPartyPersonType = z.enum(["INDIVIDUAL", "COMPANY"]).nullable(); export const enumOwnerPartyType = z.enum(["MEMBER", "ADMINISTRATOR"]).nullable(); export const enumOwnerPartyDocumentType = z.enum(["CPF", "CNPJ", "OTHER_TRAVEL_DOCUMENT", "PASSPORT"]).nullable(); export const ownerParties = z.object({person_type: enumOwnerPartyPersonType, type: enumOwnerPartyType, display_name: z.string().max(128).nullable(), social_name: z.string().max(128).nullable(), company_name: z.string().max(128).nullable().optional(), trade_name: z.string().max(128).nullable(), start_date: z.string().max(10).nullable(), percentage_type: z.number().nullable(), document_type: enumOwnerPartyDocumentType, document_number: z.string().max(40), document_issue_date: z.string().max(10).nullable(), document_expiration_date: z.string().max(10).nullable(), document_country: z.string().max(3).nullable(), document_additional_info: z.string().max(100).nullable()}).nullable(); export const ownerBusinessEconomicActivies = z.object({is_main: z.boolean(), code: z.string().min(2).max(7)}).nullable(); export const enumOwnerBusinessInformedRevenueFrequency = z.enum(["DAILY", "WEEKLY", "FORTNIGHTLY", "MONTHLY", "BIMONTHLY", "QUARTERLY", "BIANNUALLY", "ANNUALLY", "OTHERS", "null"]).nullable(); export const ownerBusinessInformedRevenue = z.object({frequency: enumOwnerBusinessInformedRevenueFrequency, frequency_additional_info: z.string().max(100).nullable(), amount: z.number(), currency: z.string().min(3).max(3), year: z.number().int().gte(1700).lte(2090)}).nullable(); export const ownerBusinessPatrimony = z.object({amount: z.number(), currency: z.string().max(3), date: z.string().max(10)}).nullable(); export const ownerBusinessFinancialProfile = z.object({economic_activities: z.array(ownerBusinessEconomicActivies), informed_revenue: ownerBusinessInformedRevenue, patrimony: ownerBusinessPatrimony}).nullable(); export const enumOwnerBusinessProductType = z.enum(["SAVINGS_ACCOUNT", "CHECKING_ACCOUNT", "null"]).nullable(); export const ownerBusinessProducts = z.object({type: enumOwnerBusinessProductType, subtype: z.string(), agency: z.string().max(4).nullable(), clearing_code: z.string().max(3), number: z.string().max(20), check_digit: z.string().max(2)}).nullable(); export const ownerBusinessFinancialRelation = z.object({start_date: z.string().max(20), product_services: z.array(z.string()), procurators: z.array(ownerProcurators), products: z.array(ownerBusinessProducts)}).nullable(); export const ownerBusinessOpenFinanceBrazil = z.object({id: id, link: link, internal_identification: z.string().nullable(), collected_at: collectedAt, created_at: createdAt, company_name: z.string().max(128), trade_name: z.string().max(128).nullable(), incorporation_date: z.string().max(10), companies_id: z.array(z.string().max(14)), document_id: ownerDocumentIdOpenFinanceBrazil, additional_documents: z.array(ownerBusinessDocumentIds), email: z.string().max(256).nullable(), emails: z.array(ownerEmails), address: z.string().nullable(), addresses: z.array(ownerAddresses), phone_number: z.string().nullable(), phone_numbers: z.array(ownerPhoneNumbers), parties: z.array(ownerParties), financial_profile: ownerBusinessFinancialProfile, financial_relation: ownerBusinessFinancialRelation}); export const patchApiApiOwnersResponse = z.array(z.union([ownerIndividualOpenFinanceBrazil, ownerBusinessOpenFinanceBrazil])); 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 }; /** * Complete an owners request * * PATCH /api/owners/ */ export const patchApiApiOwners = async (args: PatchApiApiOwnersArgs, options?: PatchApiApiOwnersOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/owners/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiApiOwnersResponse, await res.json()) };