import {z} from 'zod' export type Address = {line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, postal_code?: string | undefined}; export type Phone = {calling_code?: string | undefined, number?: string | undefined}; export type CreateApiV3EntityArgs = {body: {name: string, type: 'sole_proprietorship' | 'company' | 'non_profit' | 'government_entity' | 'individual', email?: string | undefined, description?: string | undefined, registration_address?: Address | undefined, operating_address?: Address | undefined, phone?: Phone | undefined, tax_id?: string | undefined, vertical?: string | undefined, website?: string | undefined, registration_number?: string | undefined, registration_date?: string | undefined, submit?: boolean | undefined, statement_descriptor?: string | undefined, representatives?: (Array<{first_name: string, last_name?: string | undefined, date_of_birth: string, address?: Address | undefined, nationality: string, phone?: Phone | undefined, ownership_percentage: number, roles: Array, documents?: (Array<{type: 'proof_of_identity' | 'proof_of_address' | 'registration_documents' | 'article_of_incorporation' | 'shareholder_registry' | 'identity' | 'address' | 'other', sub_type: 'drivers_license' | 'national_id' | 'passport' | 'other' | 'driving_license', tag: 'AddressProofDoc' | 'identityProofTypeFrontDoc' | 'identityProofTypeBackDoc' | 'additionalDocs', description: string, file_name: string, url: string}>) | undefined}>) | undefined, individual?: ({national_identification_number?: ({type?: ('passport' | 'driving_license' | 'national_id' | 'tax_id' | 'residence_permit' | 'other' | 'asylum_registration_card' | 'consular_id' | 'immigration_status_document' | 'indigenous_card' | 'insurance_card' | 'international_driving_license' | 'municipal_identity_card' | 'national_health_id' | 'postal_identity_card' | 'private_operators_card' | 'professional_qualification_card' | 'proof_of_citizenship' | 'residence_status_document' | 'service_id_card' | 'social_security_card' | 'visa' | 'voter_id' | 'work_permit') | undefined, number?: string | undefined, issuer?: ({country?: string | undefined, state?: string | undefined}) | undefined, expiration?: string | undefined, country_of_citizenship?: string | undefined}) | undefined, date_of_birth?: string | undefined, nationality?: string | undefined, profession?: ({occupation?: string | undefined, isco_code?: string | undefined, industry?: string | undefined, employment_status?: ('employed' | 'self_employed' | 'unemployed' | 'student' | 'retired' | 'homemaker' | 'other') | undefined}) | undefined, source_of_funds?: ({primary_source?: ('salary' | 'business_income' | 'investment_income' | 'inheritance' | 'savings' | 'gift' | 'pension' | 'rental_income' | 'sale_of_assets' | 'loan' | 'scholarship' | 'commission' | 'royalty' | 'other') | undefined, description?: string | undefined}) | undefined, employment_details?: {employer_name: string, designation: string} | undefined}) | undefined, transaction_profile?: ({monthly_expected_transactions_value?: number | undefined, monthly_expected_transaction_volume?: number | undefined, expected_transaction_countries?: Array | undefined, expected_transaction_currencies?: Array | undefined, customer_risk?: ('low' | 'medium' | 'high') | undefined}) | undefined, documents?: (Array<{type: 'proof_of_identity' | 'proof_of_address' | 'registration_documents' | 'article_of_incorporation' | 'shareholder_registry' | 'identity' | 'business' | 'other' | 'address' | 'source_of_wealth' | 'person', sub_type: 'drivers_license' | 'national_id' | 'passport' | 'other' | 'driving_license', tag: 'registrationProofDoc' | 'AddressProofDoc' | 'beneficialOwnershipDoc' | 'bankStatementProofDoc' | 'previousCardStatementProofDoc' | 'sampleInvoicesDoc' | 'additionalDocs', file_name: string, description: string, url: string}>) | undefined, reference_id?: string | undefined, metadata?: unknown | undefined, relationship?: ('customer' | 'vendor' | 'fourth_party' | 'other') | undefined, purpose_of_use?: Array | undefined, onboarding_package_url?: string | undefined, on_behalf_of?: string | undefined}}; export type CreateApiV3EntityOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.record(z.string(), z.string()).nullable(); export const phone = z.object({calling_code: z.string().optional(), number: z.string().optional()}); export const address = z.object({line1: z.string().optional(), line2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional()}); export const createApiV3EntityResponse = z.object({status: z.string().optional(), message: z.string().optional(), data: z.object({approval_status: z.string().optional(), approval_status_description: z.string().optional(), approved_at: z.string().optional(), created_at: z.string().optional(), description: z.string().optional(), documents: z.array(z.object({description: z.string().optional(), document_id: z.string().optional(), file_name: z.string().optional(), sub_type: z.string().optional(), tag: z.string().optional(), type: z.string().optional(), updated_at: z.string().optional(), url: z.string().optional()})).optional(), email: z.string().optional(), id: z.string().optional(), metadata: metadata.optional(), name: z.string().optional(), object: z.string().optional(), on_behalf_of: z.string().optional(), onboarding_package_url: z.string().optional(), operating_address: z.string().optional(), pending_documents: z.array(z.object({})).optional(), phone: phone.optional(), purpose_of_use: z.array(z.string()).optional(), reference_id: z.string().optional(), registration_address: address.optional(), registration_date: z.string().optional(), registration_number: z.string().optional(), rejected_at: z.string().optional(), relationship: z.string().optional(), representatives: z.array(z.object({person_id: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), date_of_birth: z.string().optional(), address: address.optional(), nationality: z.string().optional(), phone: phone.optional(), ownership_percentage: z.number().optional(), roles: z.array(z.string()).optional(), documents: z.array(z.object({description: z.string().optional(), document_id: z.string().optional(), file_name: z.string().optional(), sub_type: z.string().optional(), tag: z.string().optional(), type: z.string().optional(), updated_at: z.string().optional(), url: z.string().optional()})).optional()})).optional(), source_of_wealth: z.string().optional(), statement_descriptor: z.string().optional(), submit: z.boolean().optional(), submitted_at: z.string().optional(), tax_id: z.string().optional(), type: z.string().optional(), vertical: z.string().optional(), website: z.string().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 }; /** * Create Entity * * POST /v3/entity */ export const createApiV3Entity = async (args: CreateApiV3EntityArgs, options?: CreateApiV3EntityOptions) => { const {baseUrl = "https://service-sandbox.tazapay.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/entity${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(createApiV3EntityResponse, await res.json()) };