import {z} from 'zod' export type ExpenseFrequency = number; export type DayOfWeek = number; export type CreateRecurringExpenseDto = {name?: string | undefined, amount?: (number | unknown) | undefined, category?: string | undefined, frequency?: ExpenseFrequency | undefined, startDate?: string | undefined, endDate?: (string | null) | undefined, vendorName?: (string | null) | undefined, description?: (string | null) | undefined, dayOfMonth?: (number | unknown | null) | undefined, dayOfWeek?: (DayOfWeek | null) | undefined}; export type CreateApiRecurringExpensesArgs = {body: CreateRecurringExpenseDto}; export type CreateApiRecurringExpensesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const expenseFrequency = z.number().int(); export const dayOfWeek = z.number().int(); export const recurringExpenseDto = z.object({id: z.string().optional(), name: z.string().optional(), amount: z.union([z.number(), z.string()]).optional(), category: z.string().optional(), frequency: expenseFrequency.optional(), startDate: z.string().optional(), endDate: z.string().nullable().optional(), vendorName: z.string().nullable().optional(), description: z.string().nullable().optional(), isActive: z.boolean().optional(), dayOfMonth: z.union([z.number().int(), z.string()]).nullable().optional(), dayOfWeek: dayOfWeek.nullable().optional(), createdById: z.string().nullable().optional(), createdByName: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: 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 }; /** * Create a recurring expense * * POST /RecurringExpenses */ export const createApiRecurringExpenses = async (args: CreateApiRecurringExpensesArgs, options?: CreateApiRecurringExpensesOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/RecurringExpenses${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(recurringExpenseDto, await res.json()) };