import {z} from 'zod' export type GetApiLedgerSubaccountsSubAccountIdRefAccountDetailsOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cashAccountDetails = z.object({accountInfoType: z.string(), accountName: z.string(), accountNumber: z.string(), reference: z.string().optional()}); export const paymentRail = z.enum(["ACH", "FEDWIRE", "SWIFT", "SEPA", "FPS", "FPS_CROSS_SCHEME", "NIP"]); export const paymentMethodFee = z.object({percentage: z.number().gte(0).lte(1), fixedAmount: z.number().gte(0), currency: z.string()}); export const partyDepositLimits = z.object({maxPerTransaction: z.number().gte(0).optional(), maxPerDay: z.number().gte(0).optional()}); export const paymentMethodLimits = z.object({minAmount: z.number().gte(0), firstParty: partyDepositLimits.optional(), thirdParty: partyDepositLimits.optional(), currency: z.string()}); export const paymentMethodTimeline = z.object({minValue: z.number().int().gte(0), maxValue: z.number().int().gte(0), unit: z.enum(["MINUTES", "HOURS", "BUSINESS_DAYS"])}); export const paymentMethodCapability = z.object({rail: paymentRail, fee: paymentMethodFee, limits: paymentMethodLimits.optional(), timeline: paymentMethodTimeline.optional()}); export const cashDepositPaymentDetails = z.object({details: cashAccountDetails.optional(), payinCurrency: z.string(), paymentMethods: z.array(paymentMethodCapability).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 the bank account number for a sub-account. * * GET /ledger/subaccounts/{subAccountIdRef}/account-details */ export const getApiLedgerSubaccountsSubAccountIdRefAccountDetails = async (options?: GetApiLedgerSubaccountsSubAccountIdRefAccountDetailsOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/subaccounts/${encodeURIComponent(String(args.subAccountIdRef))}/account-details${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(cashDepositPaymentDetails, await res.json()) };