import {z} from 'zod' export type CreateApiV2BeneficiariesArgs = {body: {customer_id: string, country: string, currency: string, counter_party?: ('FIRST_PARTY' | 'THIRD_PARTY') | undefined, account_holder: {type: 'INDIVIDUAL', first_name: string, last_name: string, email: string, phone: string} | {type: 'BUSINESS', business_name: string, email: string, phone: string}, account_holder_address: {street_line_1: string, street_line_2?: string | undefined, city: string, state: string, postcode: string, country: string}, receiver_meta_data: {transaction_purpose_id: number, transaction_purpose_remarks?: string | undefined, occupation_id?: number | undefined, occupation_remarks: string, relationship: 'EMPLOYEE' | 'FREELANCER' | 'GIG_WORKER' | 'AFFILIATE' | 'CUSTOMER' | 'FAMILY_MEMBER' | 'FRIEND' | 'SELF' | 'OTHER' | 'SUPPLIER' | 'VENDOR' | 'SERVICE_PROVIDER' | 'MERCHANT' | 'CONTRACTOR' | 'SUBSIDIARY' | 'PARENT_COMPANY' | 'AFFILIATE_BUSINESS' | 'BANK_ACCOUNT' | 'BROKER' | 'EXCHANGE' | 'WALLET', relationship_remarks?: string | undefined, nationality: string, govt_id_number?: string | undefined, govt_id_issue_date?: string | undefined, govt_id_expire_date?: string | undefined}, developer_fee: {fixed: number, percentage: number}, deposit_instruction: {currency: 'USDC', rail: 'POLYGON'}, refund_instruction: {wallet_address: string, currency: 'USDC', rail: 'POLYGON'}, settlement_config?: {auto_settlement: boolean} | undefined, bank_account: {bank_name: string, number: string, scheme: 'LOCAL' | 'SWIFT', type: 'CHECKING' | 'SAVINGS' | 'CURRENT'}, bank_routing: Array<{scheme: 'ACH' | 'BANK_CODE' | 'BANK_IDENTIFIER' | 'BRANCH_CODE' | 'BRANCH_IDENTIFIER' | 'BSB' | 'IBAN' | 'IFSC' | 'SWIFT' | 'TRANSIT_NUMBER' | 'WIRE', number: string | number}>, bank_address: {street_line_1: string, street_line_2?: string | undefined, city: string, state: string, postcode: string, country: string}} | {customer_id: string, country: string, currency: string, counter_party?: ('FIRST_PARTY' | 'THIRD_PARTY') | undefined, account_holder: {type: 'INDIVIDUAL', first_name: string, last_name: string, email: string, phone: string} | {type: 'BUSINESS', business_name: string, email: string, phone: string}, account_holder_address: {street_line_1: string, street_line_2?: string | undefined, city: string, state: string, postcode: string, country: string}, receiver_meta_data: {transaction_purpose_id: number, transaction_purpose_remarks?: string | undefined, occupation_id?: number | undefined, occupation_remarks: string, relationship: 'EMPLOYEE' | 'FREELANCER' | 'GIG_WORKER' | 'AFFILIATE' | 'CUSTOMER' | 'FAMILY_MEMBER' | 'FRIEND' | 'SELF' | 'OTHER' | 'SUPPLIER' | 'VENDOR' | 'SERVICE_PROVIDER' | 'MERCHANT' | 'CONTRACTOR' | 'SUBSIDIARY' | 'PARENT_COMPANY' | 'AFFILIATE_BUSINESS' | 'BANK_ACCOUNT' | 'BROKER' | 'EXCHANGE' | 'WALLET', relationship_remarks?: string | undefined, nationality: string, govt_id_number?: string | undefined, govt_id_issue_date?: string | undefined, govt_id_expire_date?: string | undefined}, developer_fee: {fixed: number, percentage: number}, deposit_instruction: {currency: 'USDC', rail: 'POLYGON'}, refund_instruction: {wallet_address: string, currency: 'USDC', rail: 'POLYGON'}, settlement_config?: {auto_settlement: boolean} | undefined, e_wallet: {scheme: string, number: string}}}; export type CreateApiV2BeneficiariesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2BeneficiariesResponse = z.object({data: z.object({id: z.string().optional(), status: 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 Beneficiary * * POST /v2/beneficiaries */ export const createApiV2Beneficiaries = async (args: CreateApiV2BeneficiariesArgs, options?: CreateApiV2BeneficiariesOptions) => { 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/beneficiaries${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(createApiV2BeneficiariesResponse, await res.json()) };