import {z} from 'zod' export type GetApiLedgerV1WalletsProfilesArgs = {currencyCodes?: (Array<'USD' | 'EUR' | 'GBP' | 'USDT' | 'POL' | 'PYUSD' | 'BTC' | 'SOL' | 'BNB' | 'XRP' | 'ETH' | 'LTC' | 'USDC' | 'USDG' | 'TRX'>) | undefined, paymentMethods?: (Array<'ACH' | 'ACH_SAME_DAY' | 'FEDWIRE' | 'SWIFT' | 'FASTER_PAYMENT' | 'SEPA_CT' | 'SEPA_INST'>) | undefined}; export type GetApiLedgerV1WalletsProfilesOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiLedgerV1WalletsProfilesResponse = z.object({profiles: z.array(z.object({reference: z.string(), currencyCodes: z.array(z.enum(["USD", "EUR", "GBP", "USDT", "POL", "PYUSD", "BTC", "SOL", "BNB", "XRP", "ETH", "LTC", "USDC", "USDG", "TRX"])), paymentMethods: z.array(z.enum(["ACH", "ACH_SAME_DAY", "FEDWIRE", "SWIFT", "FASTER_PAYMENT", "SEPA_CT", "SEPA_INST"]))}))}); 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 profiles * * GET /ledger/v1/wallets/profiles */ export const getApiLedgerV1WalletsProfiles = async (args: GetApiLedgerV1WalletsProfilesArgs, options?: GetApiLedgerV1WalletsProfilesOptions) => { 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") for (const item of args.currencyCodes ?? []) query.append("currencyCodes", String(item)) for (const item of args.paymentMethods ?? []) query.append("paymentMethods", String(item)) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/v1/wallets/profiles${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(getApiLedgerV1WalletsProfilesResponse, await res.json()) };