import {z} from 'zod' export type PatchApiExpensesExpenseIdArgs = {expense_id: number, body: {[key: string]: string}}; export type PatchApiExpensesExpenseIdOptions = {baseUrl?: string | undefined, passport?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiExpensesExpenseIdResponse = z.object({message: z.string().optional(), expense: z.object({id: z.number().int().optional(), voucher_header_id: z.number().int().optional(), expense_number: z.string().optional(), paid_on: z.string().optional(), expense_reference: z.string().optional(), expense_category_id: z.number().int().optional(), amount: z.number().optional(), payment_method: z.string().optional(), billing_status: z.string().optional(), status: z.string().optional(), paid_through_account_id: z.number().int().optional(), currency_id: z.number().int().optional(), currency: z.string().optional(), tax_id: z.number().int().nullable().optional(), comments: z.string().optional(), supplier: z.object({id: z.number().int().optional(), name: z.string().optional(), type: z.string().optional(), tax_number: z.string().optional()}).optional(), matter: z.string().nullable().optional(), contract: z.string().nullable().optional(), task: z.string().nullable().optional(), hearing: z.string().nullable().optional(), event: z.string().nullable().optional(), client: z.string().nullable().optional(), client_account_id: z.number().int().nullable().optional(), exchange_rate: z.string().optional(), analytic_accounts: z.array(z.object({})).optional(), task_code_id: z.number().int().nullable().optional(), attachment: z.string().nullable().optional(), expense_category: z.string().optional(), paid_through: z.string().optional(), tax: z.string().nullable().optional(), client_account: z.string().nullable().optional(), custom_field_values: z.object({}).optional(), custom_fields: z.array(z.object({})).optional()}).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 }; /** * Inline Edit an Expense * * PATCH /expenses/{expense_id} */ export const patchApiExpensesExpenseId = async (args: PatchApiExpensesExpenseIdArgs, options?: PatchApiExpensesExpenseIdOptions) => { const {baseUrl = "http://localhost/app4legal/api/v2/money", fetch: fetchFn = fetch, passport} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (passport !== undefined) headers.set('authorization', `Bearer ${passport}`) const url = `${baseUrl.replace(/\/+$/, '')}/expenses/${encodeURIComponent(String(args.expense_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiExpensesExpenseIdResponse, await res.json()) };