import {z} from 'zod' export type TaskRequestUpdateRequest = {name?: string | undefined, section?: number | undefined, labels?: Array | undefined, status?: ('open' | 'closed') | undefined, color?: string | undefined, dueOn?: string | undefined, startOn?: string | undefined, description?: string | undefined, assignees?: (Array<{userId?: number | undefined, accountId?: string | undefined}>) | undefined, tags?: Array | undefined, fields?: Array> | undefined, cover?: ({coverId?: number | undefined, coverPick?: string | undefined}) | undefined}; export type UpdateApiTasksTaskIdArgs = {task_id: string, body: TaskRequestUpdateRequest}; export type UpdateApiTasksTaskIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const taskAssignee = z.object({userId: z.number().optional(), accountId: z.string().optional(), accountName: z.string().optional()}); export const taskTime = z.object({total: z.number(), users: z.record(z.string(), z.number()), timerTime: z.number().optional()}); export const taskEstimate = z.object({total: z.number(), type: z.enum(["overall", "users"]), users: z.record(z.string(), z.number()).optional()}); export const task = z.object({id: z.string(), name: z.string(), type: z.string().optional(), url: z.string().optional(), number: z.string().optional(), projects: z.array(z.string()), section: z.number().nullable().optional(), labels: z.array(z.string()).optional(), position: z.number().optional(), description: z.string().nullable().optional(), dueAt: z.string().nullable().optional(), dueOn: z.string().nullable().optional(), status: z.enum(["open", "completed", "closed"]).optional(), completed: z.boolean().optional(), completedAt: z.string().nullable().optional(), createdAt: z.string().optional(), createdBy: z.number().optional(), favorite: z.boolean().optional(), assignees: z.array(taskAssignee).nullable().optional(), iteration: z.string().optional(), color: z.string().optional(), comments: z.number().optional(), time: taskTime.optional(), estimate: taskEstimate.optional(), attributes: z.record(z.string(), z.string()).optional(), metrics: z.record(z.string(), z.number()).optional(), unbillable: z.boolean().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 /tasks/{task_id} */ export const updateApiTasksTaskId = async (args: UpdateApiTasksTaskIdArgs, options?: UpdateApiTasksTaskIdOptions) => { const {baseUrl = "https://api.everhour.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/tasks/${encodeURIComponent(String(args.task_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()) };