import {z} from 'zod' export type BalanceType = 'closing_available' | 'closing_booked' | 'opening_available' | 'opening_booked' | 'interim_available' | 'interim_booked' | 'previously_closed' | 'expected' | 'forward_available' | 'info' | 'undefined'; export type GetApiBalancesArgs = {limit?: number | undefined, starting_after?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, start_date?: string | undefined, end_date?: string | undefined, connected_account_id?: string | undefined, direction?: ('credit' | 'debit') | undefined, type?: BalanceType | undefined, currency?: string | undefined, amount_from?: number | undefined, amount_to?: number | undefined, file_id?: string | undefined, latest?: boolean | undefined}; export type GetApiBalancesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.literal("EUR"); export const balanceType = z.enum(["closing_available", "closing_booked", "opening_available", "opening_booked", "interim_available", "interim_booked", "previously_closed", "expected", "forward_available", "info", "undefined"]); export const balanceBankData = z.object({file_id: z.string(), statement_id: z.string().optional()}); export const balanceDetails = z.object({id: z.string(), object: z.literal("balance"), connected_account_id: z.string(), amount: z.number().int().gte(0), currency: currency, direction: z.enum(["credit", "debit"]), type: balanceType, date: z.string(), bank_data: balanceBankData, created_at: z.string()}); export const getApiBalancesResponse = z.object({records: z.array(balanceDetails)}); 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 balances * * GET /balances */ export const getApiBalances = async (args: GetApiBalancesArgs, options?: GetApiBalancesOptions) => { 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.connected_account_id != null) query.append("connected_account_id", String(args.connected_account_id)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.type != null) query.append("type", String(args.type)) if (args.currency != null) query.append("currency", String(args.currency)) 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.file_id != null) query.append("file_id", String(args.file_id)) if (args.latest != null) query.append("latest", String(args.latest)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/balances${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(getApiBalancesResponse, await res.json()) };