import {z} from 'zod' export type BooksAPICategorizationSplit = {amount: number, description?: (unknown | null) | undefined, ledgerId: unknown, tagIds?: (Array | null) | undefined}; export type BooksAPICategorizeTransactionItem = {journalEntryId: unknown, splits: Array}; export type CategorizeBooksAPITransactionsRequest = {items: Array}; export type CreateApiBooksTransactionsCategorizeArgs = {booksId?: string | undefined, body: CategorizeBooksAPITransactionsRequest}; export type CreateApiBooksTransactionsCategorizeOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const booksAPITransactionCategorizationLine = z.object({amount: z.number(), debitCredit: z.enum(["debit", "credit"]), ledgerId: z.string(), ledgerName: z.string(), lineEntryId: z.string()}); export const booksAPICategorizedTransaction = z.object({categorizationLines: z.array(booksAPITransactionCategorizationLine), journalEntryId: z.string()}); export const categorizeBooksAPITransactionsResponse = z.object({items: z.array(booksAPICategorizedTransaction)}); 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 }; /** * Categorize or split transactions * * POST /books/transactions/categorize */ export const createApiBooksTransactionsCategorize = async (args: CreateApiBooksTransactionsCategorizeArgs, options?: CreateApiBooksTransactionsCategorizeOptions) => { const {baseUrl = "https://api.mercury.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.booksId != null) query.append("booksId", String(args.booksId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/books/transactions/categorize${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(categorizeBooksAPITransactionsResponse, await res.json()) };