import {z} from 'zod' export type TaskRequest = {description: string, client_id?: string | undefined, project_id?: string | undefined, time_log?: string | undefined, status_id?: number | undefined, custom_value1?: string | undefined, custom_value2?: string | undefined, custom_value3?: string | undefined, custom_value4?: string | undefined, is_deleted?: boolean | undefined, archived_at?: number | undefined, due_date?: string | undefined, priority?: number | undefined, tags?: Array | undefined}; export type UpdateApiApiV1TasksIdArgs = {'X-API-TOKEN': string, 'X-Requested-With': string, include?: string | undefined, id: unknown, body: TaskRequest}; export type UpdateApiApiV1TasksIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tagAssignment = z.object({id: z.string(), name: z.string(), color: z.string().nullable().optional()}); export const task = z.object({id: z.string().optional(), user_id: z.string().optional(), assigned_user_id: z.string().optional(), client_id: z.string().optional(), invoice_id: z.string().optional(), project_id: z.string().optional(), number: z.string().optional(), time_log: z.string().optional(), is_running: z.boolean().optional(), is_deleted: z.boolean().optional(), task_status_id: z.string().optional(), description: z.string().optional(), duration: z.number().int().optional(), task_status_order: z.number().int().optional(), rate: z.number().optional(), custom_value1: z.string().optional(), custom_value2: z.string().optional(), custom_value3: z.string().optional(), custom_value4: z.string().optional(), is_date_based: z.boolean().optional(), calculated_start_date: z.string().optional(), invoice_documents: z.boolean().optional(), created_at: z.number().optional(), updated_at: z.number().optional(), archived_at: z.number().optional(), hash: z.string().optional(), meta: z.record(z.string(), z.unknown()).optional(), status_id: z.string().optional(), status_order: z.number().int().optional(), status_sort_order: z.number().int().optional(), tags: z.array(tagAssignment).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 }; /** * Update task * * PUT /api/v1/tasks/{id} */ export const updateApiApiV1TasksId = async (args: UpdateApiApiV1TasksIdArgs, options?: UpdateApiApiV1TasksIdOptions) => { const {baseUrl = "https://demo.invoiceninja.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-TOKEN'] != null) headers.set("X-API-TOKEN", String(args['X-API-TOKEN'])) if (args['X-Requested-With'] != null) headers.set("X-Requested-With", String(args['X-Requested-With'])) if (args.include != null) query.append("include", String(args.include)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/tasks/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(task, await res.json()) };