import {z} from 'zod' export type ReportBreakdownBy = 'SUBSIDIARY' | 'CUSTOM_FIELD'; export type GetApiReportsBalanceSheetArgs = {as_of_date: string, subsidiary_id?: string | undefined, breakdown_by?: ReportBreakdownBy | undefined, book_id?: (string | null) | undefined}; export type GetApiReportsBalanceSheetOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const reportBreakdown = z.object({type: z.enum(["SUBSIDIARY", "CUSTOM_FIELD", "PERIOD"]), name: z.string(), id: z.string().optional()}); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const breakdownBalance = z.object({breakdown_id: z.string(), amount: roundedMonetaryAmount}); export const reportAccountEntry = z.object({id: z.string(), code: z.string(), name: z.string(), balances: z.array(breakdownBalance)}); export const reportAccountGroupNested = z.object({name: z.string(), totals: z.array(breakdownBalance), accounts: z.array(reportAccountEntry)}); export const reportAccountGroup = z.object({name: z.string(), totals: z.array(breakdownBalance), accounts: z.array(reportAccountEntry), groups: z.array(reportAccountGroupNested).optional()}); export const namedReportSection = z.object({name: z.string(), totals: z.array(breakdownBalance), groups: z.array(reportAccountGroup), accounts: z.array(reportAccountEntry).optional()}); export const balanceSheetReport = z.object({as_of_date: z.string(), currency: currencyCode, breakdowns: z.array(reportBreakdown).optional(), sections: z.array(namedReportSection)}); 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 }; /** * Retrieves balance sheet report * * GET /reports/balance-sheet */ export const getApiReportsBalanceSheet = async (args: GetApiReportsBalanceSheetArgs, options?: GetApiReportsBalanceSheetOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.as_of_date != null) query.append("as_of_date", String(args.as_of_date)) if (args.subsidiary_id != null) query.append("subsidiary_id", String(args.subsidiary_id)) if (args.breakdown_by != null) query.append("breakdown_by", String(args.breakdown_by)) if (args.book_id != null) query.append("book_id", String(args.book_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/balance-sheet${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(balanceSheetReport, await res.json()) };