import {z} from 'zod' export type ExpenseSource = number; export type GetApiExpensesArgs = {startDate?: string | undefined, endDate?: string | undefined, category?: string | undefined, source?: ExpenseSource | undefined}; export type GetApiExpensesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const expenseSource = z.number().int(); export const expenseDto = z.object({id: z.string().optional(), amount: z.union([z.number(), z.string()]).optional(), category: z.string().optional(), date: z.string().optional(), description: z.string().nullable().optional(), vendorName: z.string().nullable().optional(), referenceNumber: z.string().nullable().optional(), source: expenseSource.optional(), sourceReferenceId: z.string().nullable().optional(), recurringExpenseId: z.string().nullable().optional(), createdById: z.string().nullable().optional(), createdByName: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const expenseListResponseDto = z.object({items: z.array(expenseDto).optional(), total: z.union([z.number().int(), z.string()]).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 expenses for a date range with optional filters * * GET /Expenses */ export const getApiExpenses = async (args: GetApiExpensesArgs, options?: GetApiExpensesOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.category != null) query.append("category", String(args.category)) if (args.source != null) query.append("source", String(args.source)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/Expenses${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(expenseListResponseDto, await res.json()) };