import {z} from 'zod' export type TransactionCategory = 'credit_transfer' | 'cash_concentration' | 'direct_debit' | 'cheque' | 'card' | 'lockbox' | 'counter_operation' | 'bill_of_order' | 'instant_credit_transfer' | 'other_payment' | 'cash_pooling' | 'account_balancing' | 'other_cash_management' | 'derivative' | 'loan' | 'fx' | 'precious_metals' | 'commodities' | 'trade_services' | 'securities' | 'account_management' | 'unavailable'; export type TransactionReconciliationStatus = 'partially_reconciled' | 'reconciled' | 'excluded' | 'unreconciled'; export type GetApiTransactionsArgs = {limit?: number | undefined, starting_after?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, start_date?: string | undefined, end_date?: string | undefined, start_booking_date?: string | undefined, end_booking_date?: string | undefined, start_value_date?: string | undefined, end_value_date?: string | undefined, direction?: ('credit' | 'debit') | undefined, currency?: string | undefined, counterparty_account_number?: string | undefined, counterparty_name?: string | undefined, external_account_number?: string | undefined, external_account_holder_name?: string | undefined, external_account_id?: string | undefined, internal_account_id?: string | undefined, amount_from?: number | undefined, amount_to?: number | undefined, virtual_account_number?: string | undefined, internal_account_number?: string | undefined, connected_account_id?: string | undefined, file_id?: string | undefined, category?: TransactionCategory | undefined, reconciliation_status?: TransactionReconciliationStatus | undefined, uetr?: string | undefined}; export type GetApiTransactionsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionCategory = z.enum(["credit_transfer", "cash_concentration", "direct_debit", "cheque", "card", "lockbox", "counter_operation", "bill_of_order", "instant_credit_transfer", "other_payment", "cash_pooling", "account_balancing", "other_cash_management", "derivative", "loan", "fx", "precious_metals", "commodities", "trade_services", "securities", "account_management", "unavailable"]); export const currency = z.literal("EUR"); export const transactionCounterpartyAccount = z.object({account_number: z.string(), holder_name: z.string()}); export const transactionExternalAccountHolderAddress = z.object({line_1: z.string().optional(), line_2: z.string().optional(), building_number: z.string().optional(), street_name: z.string().optional(), postal_code: z.string().optional(), region_state: z.string().optional(), city: z.string().optional(), country: z.string().optional()}); export const transactionExternalAccount = z.object({account_number: z.string(), bank_code: z.string().optional(), holder_name: z.string(), holder_address: transactionExternalAccountHolderAddress.optional()}); export const transactionInternalAccount = z.object({account_number: z.string(), holder_name: z.string().optional()}); export const transactionBankData = z.object({file_id: z.string(), statement_id: z.string().optional(), end_to_end_id: z.string().optional()}); export const commonCustomFields = z.object({}); export const transactionReconciliationStatus = z.enum(["partially_reconciled", "reconciled", "excluded", "unreconciled"]); export const transactionDetails = z.object({id: z.string(), object: z.literal("transaction"), connected_account_id: z.string(), booking_date: z.string(), value_date: z.string(), category: transactionCategory, description: z.string(), direction: z.enum(["debit", "credit"]), amount: z.number().int().gte(1), currency: currency, counterparty_account: transactionCounterpartyAccount, external_account: transactionExternalAccount, virtual_account_number: z.string(), internal_account: transactionInternalAccount, internal_account_id: z.string().optional(), external_account_id: z.string().optional(), uetr: z.string().optional(), bank_data: transactionBankData, custom_fields: commonCustomFields, reconciliation_status: transactionReconciliationStatus, reconciled_amount: z.number().int().gte(0), created_at: z.string()}); export const getApiTransactionsResponse = z.object({records: z.array(transactionDetails)}); 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 * * GET /transactions */ export const getApiTransactions = async (args: GetApiTransactionsArgs, options?: GetApiTransactionsOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.start_booking_date != null) query.append("start_booking_date", String(args.start_booking_date)) if (args.end_booking_date != null) query.append("end_booking_date", String(args.end_booking_date)) if (args.start_value_date != null) query.append("start_value_date", String(args.start_value_date)) if (args.end_value_date != null) query.append("end_value_date", String(args.end_value_date)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.counterparty_account_number != null) query.append("counterparty_account_number", String(args.counterparty_account_number)) if (args.counterparty_name != null) query.append("counterparty_name", String(args.counterparty_name)) if (args.external_account_number != null) query.append("external_account_number", String(args.external_account_number)) if (args.external_account_holder_name != null) query.append("external_account_holder_name", String(args.external_account_holder_name)) if (args.external_account_id != null) query.append("external_account_id", String(args.external_account_id)) if (args.internal_account_id != null) query.append("internal_account_id", String(args.internal_account_id)) if (args.amount_from != null) query.append("amount_from", String(args.amount_from)) if (args.amount_to != null) query.append("amount_to", String(args.amount_to)) if (args.virtual_account_number != null) query.append("virtual_account_number", String(args.virtual_account_number)) if (args.internal_account_number != null) query.append("internal_account_number", String(args.internal_account_number)) if (args.connected_account_id != null) query.append("connected_account_id", String(args.connected_account_id)) if (args.file_id != null) query.append("file_id", String(args.file_id)) if (args.category != null) query.append("category", String(args.category)) if (args.reconciliation_status != null) query.append("reconciliation_status", String(args.reconciliation_status)) if (args.uetr != null) query.append("uetr", String(args.uetr)) if (sec0 !== undefined) headers.set("x-api-key", sec0) 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(getApiTransactionsResponse, await res.json()) };