import {z} from 'zod' export type GetApiBooksLineEntriesArgs = {booksId?: string | undefined, startDate: string, endDate: string, ledgerIds?: Array | undefined, opposingLedgerIds?: Array | undefined, debitCredit?: ('debit' | 'credit') | undefined, minAmount?: number | undefined, maxAmount?: number | undefined, sort?: (Array<'descriptionAsc' | 'descriptionDesc' | 'datetimeAsc' | 'datetimeDesc' | 'creditAsc' | 'creditDesc' | 'debitAsc' | 'debitDesc' | 'journalEntryIdAsc' | 'journalEntryIdDesc' | 'ledgerNameAsc' | 'ledgerNameDesc' | 'contraLedgerNameAsc' | 'contraLedgerNameDesc' | 'tagNameAsc' | 'tagNameDesc'>) | undefined, pageToken?: string | undefined, limit?: number | undefined}; export type GetApiBooksLineEntriesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const booksTag = z.object({id: z.string(), name: z.string(), tagGroupId: z.string()}); export const booksAPILineEntry = z.object({amount: z.number(), datetime: z.string(), debitCredit: z.enum(["debit", "credit"]), description: z.string().nullable().optional(), id: z.string(), journalEntryDescription: z.string(), journalEntryId: z.string(), ledgerId: z.string(), ledgerName: z.string(), opposingLedgerIds: z.array(z.string()), tags: z.array(booksTag)}); export const getBooksAPILineEntriesResponse = z.object({lineEntries: z.array(booksAPILineEntry), nextPageToken: z.string().nullable().optional(), prevPageToken: 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 line entries (general ledger activity) across ledgers * * GET /books/line-entries */ export const getApiBooksLineEntries = async (args: GetApiBooksLineEntriesArgs, options?: GetApiBooksLineEntriesOptions) => { const {baseUrl = "https://api.mercury.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.booksId != null) query.append("booksId", String(args.booksId)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) for (const item of args.ledgerIds ?? []) query.append("ledgerIds", String(item)) for (const item of args.opposingLedgerIds ?? []) query.append("opposingLedgerIds", String(item)) if (args.debitCredit != null) query.append("debitCredit", String(args.debitCredit)) if (args.minAmount != null) query.append("minAmount", String(args.minAmount)) if (args.maxAmount != null) query.append("maxAmount", String(args.maxAmount)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/books/line-entries${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(getBooksAPILineEntriesResponse, await res.json()) };