import {z} from 'zod' export type DeleteApiTimersCurrentOptions = {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 timeRecord = z.object({id: z.number(), time: z.number(), user: z.number(), date: z.string(), task: task.optional(), isLocked: z.boolean().optional(), isInvoiced: z.boolean().optional(), comment: z.string().optional()}); export const deleteApiTimersCurrentResponse = z.object({status: z.string().optional(), taskTime: timeRecord.optional(), timecard: z.object({}).nullable().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 }; /** * Stop Timer * * DELETE /timers/current */ export const deleteApiTimersCurrent = async (options?: DeleteApiTimersCurrentOptions) => { const {baseUrl = "https://api.everhour.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/timers/current${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(deleteApiTimersCurrentResponse, await res.json()) };