import {z} from 'zod' export type GetApiTransactionsArgs = {type?: (string | null) | undefined, status?: (string | null) | undefined, tag?: (string | null) | undefined, contactId?: (string | null) | undefined, bankId?: (string | null) | undefined, incomeOnly?: (boolean | null) | undefined, q?: (string | null) | undefined, from?: (string | null) | undefined, to?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined, 'X-Hevn-Account'?: (string | null) | undefined}; export type GetApiTransactionsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatus = z.enum(["pending", "rfi", "success", "failed", "refunded"]); export const transactionSideView = z.object({amount: z.string(), currency: z.string(), usdRate: z.string().nullable().optional()}); export const accountKey = z.enum(["USDC", "EURC", "RD", "USDC_ARB", "FDIC_USD", "FDIC_EUR", "KYC_CARDS"]); export const transactionView = z.object({id: z.string().nullable().optional(), type: z.string(), status: transactionStatus, isIncome: z.boolean(), from: transactionSideView.nullable().optional(), to: transactionSideView.nullable().optional(), fee: transactionSideView.nullable().optional(), account: accountKey.nullable().optional(), counterparty: z.string().nullable().optional(), paymentReference: z.string().nullable().optional(), remitter: z.string().nullable().optional(), txHash: z.string().nullable().optional(), tag: z.string().nullable().optional(), hasAttachments: z.boolean().optional(), createdAt: z.string()}); export const pageTransactionView = z.object({items: z.array(transactionView), nextCursor: z.string().nullable().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 transactions, newest first * * GET /transactions */ export const getApiTransactions = async (args: GetApiTransactionsArgs, options: GetApiTransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.contactId != null) query.append("contactId", String(args.contactId)) if (args.bankId != null) query.append("bankId", String(args.bankId)) if (args.incomeOnly != null) query.append("incomeOnly", String(args.incomeOnly)) if (args.q != null) query.append("q", String(args.q)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args['X-Hevn-Account'] != null) headers.set("X-Hevn-Account", String(args['X-Hevn-Account'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/transactions${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(pageTransactionView, await res.json()) };