import {z} from 'zod' export type GetApiLedgerV2WalletsArgs = {q?: string | undefined, pageNumber?: number | undefined, pageSize?: number | undefined}; export type GetApiLedgerV2WalletsOptions = {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 ledgerWalletV2Balance = z.object({amount: z.number(), currency: z.string()}); export const ledgerWalletV2ListItem = z.object({id: z.string().optional(), name: z.string().optional(), customer: ledgerWalletV2Customer.optional(), status: z.enum(["ACTIVE", "INACTIVE", "TERMINATED"]).optional(), balance: ledgerWalletV2Balance.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const ledgerWalletV2Pageable = z.object({pageNumber: z.number().int().optional(), pageSize: z.number().int().optional()}); export const ledgerWalletV2ListResponse = z.object({content: z.array(ledgerWalletV2ListItem).optional(), pageable: ledgerWalletV2Pageable.optional(), hasNext: 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 }; /** * Get wallets * * GET /ledger/v2/wallets */ export const getApiLedgerV2Wallets = async (args: GetApiLedgerV2WalletsArgs, options?: GetApiLedgerV2WalletsOptions) => { 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.q != null) query.append("q", String(args.q)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/v2/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(ledgerWalletV2ListResponse, await res.json()) };