import {z} from 'zod' export type TransferWrite = {origination_account_id: string, destination_account_id: string, description?: string | undefined, amount: number, ach?: ({class_code: 'CCD' | 'PPD' | 'WEB', same_day: boolean}) | undefined}; export type CreateApiTransfersArgs = {body: TransferWrite}; export type CreateApiTransfersOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const transfer = z.object({transfer_id: z.string().optional(), transaction_id: z.string().optional(), created_time: z.string(), updated_time: z.string(), settled_time: z.string(), origination_account_id: z.string().min(1), destination_account_id: z.string().min(1), description: z.string().min(1).optional(), amount: z.number().int(), status: z.literal("submitted").optional(), ach: z.object({ach_class_code: z.enum(["CCD", "PPD", "WEB"]).min(1), same_day: z.boolean()})}); 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 }; /** * Create a Transfer * * POST /transfers */ export const createApiTransfers = async (args: CreateApiTransfersArgs, options?: CreateApiTransfersOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/transfers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(transfer, await res.json()) };