import {z} from 'zod' export type GetApiApiDashboardSpendingByCategoryArgs = {month?: (string | null) | undefined, account_ids?: (Array | null) | undefined, 'X-Workspace-Id'?: (string | null) | undefined}; export type GetApiApiDashboardSpendingByCategoryOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const spendingByCategory = z.object({category_id: z.string().nullable(), category_name: z.string(), category_icon: z.string(), category_color: z.string(), total: z.number(), projected_total: z.number().optional(), percentage: z.number()}); export const getApiApiDashboardSpendingByCategoryResponse = z.array(spendingByCategory); 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 Spending By Category * * GET /api/dashboard/spending-by-category */ export const getApiApiDashboardSpendingByCategory = async (args: GetApiApiDashboardSpendingByCategoryArgs, options: GetApiApiDashboardSpendingByCategoryOptions) => { 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)) for (const item of args.account_ids ?? []) query.append("account_ids", String(item)) 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/spending-by-category${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(getApiApiDashboardSpendingByCategoryResponse, await res.json()) };