import {z} from 'zod' export type GetApiLedgerV1WalletsArgs = {currencyCode?: ('USD' | 'EUR' | 'GBP' | 'USDT' | 'POL' | 'PYUSD' | 'BTC' | 'SOL' | 'BNB' | 'XRP' | 'ETH' | 'LTC' | 'USDC' | 'USDG' | 'TRX') | undefined, customerReference?: string | undefined, page?: number | undefined, size?: number | undefined, sort?: string | undefined, offset?: number | undefined}; export type GetApiLedgerV1WalletsOptions = {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 getApiLedgerV1WalletsResponse = z.object({content: z.array(walletResponse).optional(), pageable: z.object({pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), offset: z.number().int().optional()}).optional(), totalPages: z.number().int().optional(), totalElements: z.number().int().optional(), numberOfElements: z.number().int().optional(), last: z.boolean().optional(), first: z.boolean().optional(), empty: z.boolean().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 }; /** * List customer's fiat wallets * * GET /ledger/v1/wallets */ export const getApiLedgerV1Wallets = async (args: GetApiLedgerV1WalletsArgs, options?: GetApiLedgerV1WalletsOptions) => { 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 (args.currencyCode != null) query.append("currencyCode", String(args.currencyCode)) if (args.customerReference != null) query.append("customerReference", String(args.customerReference)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.offset != null) query.append("offset", String(args.offset)) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/v1/wallets${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(getApiLedgerV1WalletsResponse, await res.json()) };