import {z} from 'zod' export type CreateApiFednowArgs = {body: {account_id: string, amount: string, description?: string | undefined, external_account_number: string, external_name: string, external_routing_number: string, memo?: string | undefined, originator_name?: string | undefined, security_context?: ({device_id?: string | undefined, ip_address?: string | undefined, user_agent?: string | undefined}) | undefined}}; export type CreateApiFednowOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiFednowResponse = z.object({account_id: z.string().optional(), amount: z.string().optional(), bank_id: z.string().optional(), created_at: z.string().optional(), currency: z.string().optional(), description: z.string().optional(), error: z.string().optional(), external_account_number: z.string().optional(), external_name: z.string().optional(), external_reject_reason: z.object({reason_code: z.string().optional(), reason_description: z.string().optional()}).optional(), external_routing_number: z.string().optional(), external_status: z.string().optional(), fed_settlement_date: z.string().optional(), id: z.string().optional(), memo: z.string().optional(), org_id: z.string().optional(), originator_name: z.string().optional(), payment_id: z.string().optional(), payment_type: z.string().optional(), related_fednow_ids: z.array(z.string()).optional(), security_context: z.object({device_id: z.string().optional(), ip_address: z.string().optional(), user_agent: z.string().optional()}).optional(), status: z.string().optional(), updated_at: z.string().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 }; /** * Create a FedNow Payment * * POST /fednow * * Also accepts auth: apiKey */ export const createApiFednow = async (args: CreateApiFednowArgs, options?: CreateApiFednowOptions) => { const {baseUrl = "https://api.treasuryprime.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/fednow${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(createApiFednowResponse, await res.json()) };