import {z} from 'zod' export type VirtualAccountStatus = 'pending' | 'approved' | 'declined' | 'expired' | 'closed' | 'reviewed'; export type VirtualAccountCurrency = 'NGN' | 'GHS' | 'KES' | 'TZS' | 'RWF' | 'UGX' | 'XAF' | 'USD' | 'EUR' | 'GBP' | 'CAD'; export type VirtualAccountType = 'individual' | 'corporate'; export type GetApiProfileVirtualAccountsArgs = {page?: number | undefined, perPage?: number | undefined, status?: VirtualAccountStatus | undefined, currency?: VirtualAccountCurrency | undefined, accountType?: VirtualAccountType | undefined, entityType?: ('main_account' | 'sub_account') | undefined, requestedDate?: string | undefined, issuedDate?: string | undefined, businessName?: string | undefined, virtualAccountType?: string | undefined, accountNumber?: string | undefined, merchantReference?: string | undefined}; export type GetApiProfileVirtualAccountsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const virtualAccountCurrency = z.enum(["NGN", "GHS", "KES", "TZS", "RWF", "UGX", "XAF", "USD", "EUR", "GBP", "CAD"]); export const virtualAccountType = z.enum(["individual", "corporate"]); export const virtualAccountStatus = z.enum(["pending", "approved", "declined", "expired", "closed", "reviewed"]); export const virtualAccountBankDetails = z.object({accountNumber: z.string().optional(), accountName: z.string().optional(), bankName: z.string().optional(), bankCode: z.string().optional(), iban: z.string().optional(), swiftCode: z.string().optional(), routingNumber: z.string().optional(), paymentScheme: z.string().optional(), alternateAccountDetails: z.array(z.record(z.string(), z.unknown())).optional()}).and(z.record(z.string(), z.unknown())); export const virtualAccount = z.object({_id: z.string().optional(), business: z.union([z.string(), z.object({})]).optional(), parentBusiness: z.union([z.string(), z.object({})]).nullable().optional(), currency: virtualAccountCurrency.optional(), accountType: virtualAccountType.optional(), status: virtualAccountStatus.optional(), isActive: z.boolean().optional(), isPermanent: z.literal(true).optional(), accountNumber: z.string().nullable().optional(), accountInformation: virtualAccountBankDetails.nullable().optional(), merchantReference: z.string().nullable().optional(), KYCInformation: z.record(z.string(), z.unknown()).optional(), entityType: z.enum(["main_account", "sub_account"]).optional(), virtualAccountType: z.string().optional(), accountOpeningFee: z.number().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const virtualAccountListResponse = z.object({success: z.literal(true), message: z.string(), data: z.object({results: z.array(virtualAccount), total: z.number().int().gte(0)})}); 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 }; /** * List permanent virtual accounts * * GET /profile/virtual-accounts */ export const getApiProfileVirtualAccounts = async (args: GetApiProfileVirtualAccountsArgs, options?: GetApiProfileVirtualAccountsOptions) => { const {baseUrl = "https://sandboxapi.fincra.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.status != null) query.append("status", String(args.status)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.accountType != null) query.append("accountType", String(args.accountType)) if (args.entityType != null) query.append("entityType", String(args.entityType)) if (args.requestedDate != null) query.append("requestedDate", String(args.requestedDate)) if (args.issuedDate != null) query.append("issuedDate", String(args.issuedDate)) if (args.businessName != null) query.append("businessName", String(args.businessName)) if (args.virtualAccountType != null) query.append("virtualAccountType", String(args.virtualAccountType)) if (args.accountNumber != null) query.append("accountNumber", String(args.accountNumber)) if (args.merchantReference != null) query.append("merchantReference", String(args.merchantReference)) if (sec0 !== undefined) headers.set("api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/profile/virtual-accounts${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(virtualAccountListResponse, await res.json()) };