import {z} from 'zod' export type GetApiBooksReportsCashFlowArgs = {booksId?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, period?: ('daily' | 'monthly' | 'quarterly' | 'yearly') | undefined, includeOnlyClosed?: boolean | undefined}; export type GetApiBooksReportsCashFlowOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const booksAPIReportColumn = z.object({endDate: z.string().nullable().optional(), isPartial: z.boolean(), label: z.string(), startDate: z.string().nullable().optional()}); export const dollar = z.number(); export const booksAPICashBalanceRow: z.ZodType = z.object({balances: z.array(dollar), children: z.array(z.lazy(() => booksAPICashBalanceRow)), ledgerId: z.string(), name: z.string(), parentId: z.string().nullable().optional(), sortCode: z.string(), totalBalances: z.array(dollar)}); export const booksAPICashFlowLedgerRow: z.ZodType = z.object({children: z.array(z.lazy(() => booksAPICashFlowLedgerRow)), ledgerId: z.string(), name: z.string(), netCashFlows: z.array(dollar), parentId: z.string().nullable().optional(), totalNetCashFlows: z.array(dollar)}); export const booksAPICashFlowReport = z.object({columns: z.array(booksAPIReportColumn), endingBalances: z.array(booksAPICashBalanceRow), endingCashBalances: z.array(dollar), ledgers: z.array(booksAPICashFlowLedgerRow), startingBalances: z.array(booksAPICashBalanceRow), startingCashBalances: z.array(dollar)}); 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 }; /** * Retrieve a cash flow report * * GET /books/reports/cash-flow */ export const getApiBooksReportsCashFlow = async (args: GetApiBooksReportsCashFlowArgs, options?: GetApiBooksReportsCashFlowOptions) => { 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)) if (args.period != null) query.append("period", String(args.period)) if (args.includeOnlyClosed != null) query.append("includeOnlyClosed", String(args.includeOnlyClosed)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/books/reports/cash-flow${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(booksAPICashFlowReport, await res.json()) };