import {z} from 'zod' export type GetApiApiAccountsAccountIdSummaryArgs = {account_id: string, from?: (string | null) | undefined, to?: (string | null) | undefined, bill_id?: (string | null) | undefined, unbilled_only?: boolean | undefined, 'X-Workspace-Id'?: (string | null) | undefined}; export type GetApiApiAccountsAccountIdSummaryOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const accountSummary = z.object({account_id: z.string(), current_balance: z.number(), opening_balance: z.number(), monthly_income: z.number(), monthly_expenses: z.number(), projected_income: z.number().optional(), projected_expenses: z.number().optional(), current_balance_primary: z.number().nullable().optional(), opening_balance_primary: z.number().nullable().optional(), monthly_income_primary: z.number().nullable().optional(), monthly_expenses_primary: z.number().nullable().optional(), projected_income_primary: z.number().nullable().optional(), projected_expenses_primary: z.number().nullable().optional(), fx_fallback: z.boolean().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 }; /** * Get Account Summary * * GET /api/accounts/{account_id}/summary */ export const getApiApiAccountsAccountIdSummary = async (args: GetApiApiAccountsAccountIdSummaryArgs, options: GetApiApiAccountsAccountIdSummaryOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.bill_id != null) query.append("bill_id", String(args.bill_id)) if (args.unbilled_only != null) query.append("unbilled_only", String(args.unbilled_only)) if (args['X-Workspace-Id'] != null) headers.set("X-Workspace-Id", String(args['X-Workspace-Id'])) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/accounts/${encodeURIComponent(String(args.account_id))}/summary${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(accountSummary, await res.json()) };