import {z} from 'zod' export type GetApiLedgerV2WalletsIdArgs = {id: string}; export type GetApiLedgerV2WalletsIdOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const ledgerWalletV2Customer = z.object({id: z.string().optional(), name: z.string().optional()}); export const ledgerWalletV2BankAddress = z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), city: z.string().optional(), country: z.string().optional(), stateCode: z.string().optional(), postCode: z.string().optional(), fullAddress: z.string().optional()}); export const ledgerWalletV2BankNid = z.object({value: z.string(), type: z.enum(["ROUTING_NUMBER", "SORT_CODE", "OTHER"]).optional()}); export const ledgerWalletV2Bank = z.object({name: z.string(), address: ledgerWalletV2BankAddress.optional(), bic: z.string(), nid: ledgerWalletV2BankNid.optional()}); export const ledgerWalletV2FiatLedger = z.object({type: z.literal("FIAT"), accountHolderName: z.string(), accountHolderAddress: ledgerWalletV2BankAddress.optional(), accountNumber: z.string(), remittanceInformationPrefix: z.string().optional(), bankDetails: ledgerWalletV2Bank.optional()}); export const blockchainNetwork = z.enum(["TRON", "ETHEREUM", "LITECOIN", "BITCOIN", "RIPPLE", "DOGECOIN", "SOLANA", "ALGORAND", "CARDANO", "BINANCE", "POLYGON", "BITCOIN_CASH", "ARC", "ARBITRUM", "BASE"]); export const ledgerWalletV2CryptoLedger = z.object({address: z.string(), network: blockchainNetwork, tag: z.string().nullable().optional()}); export const ledgerWalletV2Response = z.object({id: z.string(), name: z.string(), customer: ledgerWalletV2Customer.optional(), status: z.enum(["ACTIVE", "INACTIVE", "TERMINATED"]), balance: z.object({amount: z.number(), currency: z.string()}).optional(), paymentInstruments: z.array(z.union([ledgerWalletV2FiatLedger, ledgerWalletV2CryptoLedger])).optional(), createdAt: z.string().optional(), updatedAt: 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 }; /** * Get wallet * * GET /ledger/v2/wallets/{id} */ export const getApiLedgerV2WalletsId = async (args: GetApiLedgerV2WalletsIdArgs, options?: GetApiLedgerV2WalletsIdOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/v2/wallets/${encodeURIComponent(String(args.id))}${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(ledgerWalletV2Response, await res.json()) };