import {z} from 'zod' export type GetApiLedgerV1WalletsIdArgs = {id: string}; export type GetApiLedgerV1WalletsIdOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const balance = z.object({value: z.number(), currencyCode: z.enum(["USD", "EUR", "GBP", "USDT", "POL", "PYUSD", "BTC", "SOL", "BNB", "XRP", "ETH", "LTC", "USDC", "USDG", "TRX"])}); export const address = z.object({addressLine1: z.string().max(35).optional(), addressLine2: z.string().max(35).optional(), region: z.string().optional(), city: z.string().optional(), country: z.string(), postCode: z.string().optional()}); export const fiatLedger = z.object({type: z.literal("FIAT"), accountHolderName: z.string().optional(), accountNumber: z.string(), code: z.string(), accountNumberFormat: z.string().optional(), paymentReference: z.string().optional(), bank: z.object({identificationCode: z.string().optional(), name: z.string().optional(), address: address.optional()}).optional()}); export const cryptoLedger = z.object({type: z.literal("CRYPTO"), address: z.string(), network: z.string(), tag: z.string().optional()}); export const ledger = z.discriminatedUnion("type", [fiatLedger, cryptoLedger]); export const walletResponse = z.object({id: z.string(), accountReference: z.string(), customerReference: z.string(), name: z.string(), status: z.enum(["ACTIVE", "INACTIVE", "TERMINATED"]), balance: balance, ledgers: z.array(ledger), capabilities: z.literal("SAFEGUARDED").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 fiat wallet * * GET /ledger/v1/wallets/{id} */ export const getApiLedgerV1WalletsId = async (args: GetApiLedgerV1WalletsIdArgs, options?: GetApiLedgerV1WalletsIdOptions) => { 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/v1/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(walletResponse, await res.json()) };