import {z} from 'zod' export type GetApiApiDashboardProjectedTransactionsArgs = {month?: (string | null) | undefined, account_id?: (string | null) | undefined, from?: (string | null) | undefined, to?: (string | null) | undefined, 'X-Workspace-Id'?: (string | null) | undefined}; export type GetApiApiDashboardProjectedTransactionsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const projectedTransaction = z.object({recurring_id: z.string(), account_id: z.string().nullable().optional(), description: z.string(), amount: z.number(), amount_primary: z.number().nullable().optional(), currency: z.string(), type: z.string(), date: z.string(), category_id: z.string().nullable(), category_name: z.string().nullable(), category_icon: z.string().nullable(), category_color: z.string().nullable().optional(), category_treat_as_transfer: z.boolean().optional()}); export const getApiApiDashboardProjectedTransactionsResponse = z.array(projectedTransaction); 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 Projected Transactions * * GET /api/dashboard/projected-transactions */ export const getApiApiDashboardProjectedTransactions = async (args: GetApiApiDashboardProjectedTransactionsArgs, options: GetApiApiDashboardProjectedTransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.month != null) query.append("month", String(args.month)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) 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/dashboard/projected-transactions${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(getApiApiDashboardProjectedTransactionsResponse, await res.json()) };