import {z} from 'zod' export type GetApiV1CustomersVirtualAccountArgs = {customer_id: string}; export type GetApiV1CustomersVirtualAccountOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const virtualAccount = z.object({id: z.string(), status: z.string(), developer_fee_percent: z.number(), customer_id: z.string(), created_at: z.string(), updated_at: z.string(), deposit_instructions: z.object({currency: z.string().optional(), bank_name: z.string().optional(), bank_address: z.string().optional(), bank_routing_number: z.string().optional(), bank_account_number: z.string().optional(), bank_beneficiary_name: z.string().optional(), bank_beneficiary_address: z.string().optional(), payment_rails: z.array(z.string()).optional()}), destination: z.object({currency: z.literal("USDC").optional(), destination_chain: z.enum(["POLYGON", "ETHEREUM"]).optional(), address: z.string().optional()})}); export const getApiV1CustomersVirtualAccountResponse = z.object({data: z.object({virtual_accounts: z.array(virtualAccount), pagination: z.object({current_page: z.number().int().optional(), per_page: z.number().int().optional(), total_page: z.number().int().optional(), total: z.number().int().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 Virtual Accounts V1 * * GET /v1/customers/virtual-account */ export const getApiV1CustomersVirtualAccount = async (args: GetApiV1CustomersVirtualAccountArgs, options?: GetApiV1CustomersVirtualAccountOptions) => { const {baseUrl = "https://sandbox.api.fin.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customers/virtual-account${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(getApiV1CustomersVirtualAccountResponse, await res.json()) };