import {z} from 'zod' export type GetApiEntitiesEntityIdCurrencyAccountsCurrencyAccountIdTopUpInstructionsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const topUpFundingDetails = z.object({beneficiary_account_name: z.string(), beneficiary_address: z.string().optional(), bank_name: z.string(), bank_address: z.string().optional(), account_number: z.string().optional(), sort_code: z.string().optional(), routing_number: z.string().optional(), iban: z.string().optional(), swift_code: z.string().optional()}); export const topUpBankDetails = z.object({domestic: topUpFundingDetails.optional(), international: topUpFundingDetails.optional()}); export const topUpInstructionsResponse = z.object({currency_account_id: z.string(), currency: z.string(), payment_reference: z.string(), bank_details: topUpBankDetails}); 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 }; /** * Retrieve top-up instructions * * GET /entities/{entityId}/currency-accounts/{currencyAccountId}/top-up-instructions * * Also accepts auth: ApiSecretKey */ export const getApiEntitiesEntityIdCurrencyAccountsCurrencyAccountIdTopUpInstructions = async (options: GetApiEntitiesEntityIdCurrencyAccountsCurrencyAccountIdTopUpInstructionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/${encodeURIComponent(String(args.entityId))}/currency-accounts/${encodeURIComponent(String(args.currencyAccountId))}/top-up-instructions${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(topUpInstructionsResponse, await res.json()) };