import {z} from 'zod' export type MatchingStatus = 'unprocessed' | 'awaiting_setup' | 'deterministic_suggested' | 'awaiting_ai' | 'ai_in_progress' | 'ai_suggested' | 'ai_no_match' | 'no_match' | 'excluded' | 'matched'; export type GetApiPublicV2BankMatchBankTransactionsArgs = {updated_after?: (string | null) | undefined, updated_before?: (string | null) | undefined, financial_account_id?: (number | null) | undefined, matching_status?: (MatchingStatus | null) | undefined, date_from?: (string | null) | undefined, date_to?: (string | null) | undefined, is_posted?: (boolean | null) | undefined, include_expired?: boolean | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiPublicV2BankMatchBankTransactionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bankTransactionSource = z.enum(["pdf_upload", "csv_upload", "manual_upload", "bai_upload", "meld", "plaid", "waycore", "saltedge", "dualentry_banking", "yapily", "finicity", "customer_api", "sftp", "moss", "cashpro"]); export const matchingStatus = z.enum(["unprocessed", "awaiting_setup", "deterministic_suggested", "awaiting_ai", "ai_in_progress", "ai_suggested", "ai_no_match", "no_match", "excluded", "matched"]); export const publicBankTransactionSchemaOut = z.object({created_at: z.string(), updated_at: z.string(), id: z.number().int(), financial_account_id: z.number().int(), financial_account_name: z.string().nullable().optional(), date: z.string(), posted_at: z.string().nullable(), amount: z.string(), debit_amount: z.string(), credit_amount: z.string(), description: z.string(), payee: z.string().nullable().optional(), currency_iso_4217_code: z.string().nullable(), source: bankTransactionSource, is_posted: z.boolean(), is_expired: z.boolean(), matching_status: matchingStatus, matched_transaction_ids: z.array(z.number().int()).optional(), matched_entry_ids: z.array(z.number().int()).optional()}); export const pagedPublicBankTransactionSchemaOut = z.object({items: z.array(publicBankTransactionSchemaOut), count: z.number().int()}); 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 bank-feed transactions * * GET /public/v2/bank-match/bank-transactions/ */ export const getApiPublicV2BankMatchBankTransactions = async (args: GetApiPublicV2BankMatchBankTransactionsArgs, options?: GetApiPublicV2BankMatchBankTransactionsOptions) => { const {baseUrl = "https://api.dualentry.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.updated_before != null) query.append("updated_before", String(args.updated_before)) if (args.financial_account_id != null) query.append("financial_account_id", String(args.financial_account_id)) if (args.matching_status != null) query.append("matching_status", String(args.matching_status)) if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.is_posted != null) query.append("is_posted", String(args.is_posted)) if (args.include_expired != null) query.append("include_expired", String(args.include_expired)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/public/v2/bank-match/bank-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(pagedPublicBankTransactionSchemaOut, await res.json()) };