import {z} from 'zod' export type GetApiTeamTimeArgs = {from?: string | undefined, to?: string | undefined, limit?: number | undefined, page?: number | undefined, opts_include_billing?: number | undefined}; export type GetApiTeamTimeOptions = {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 timeHistory = z.object({id: z.number(), createdBy: z.number(), time: z.number(), previousTime: z.number(), action: z.enum(["TIMER", "ADD", "EDIT", "REMOVE", "COMMENT", "MOVE"]), createdAt: z.string().optional()}); export const timeRecordExtended = 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(), history: z.array(timeHistory).optional(), createdAt: z.string().optional(), warning: z.object({}).nullable().optional(), lockReasons: z.array(z.string()).optional(), website: z.object({url: z.string().optional(), title: z.string().optional()}).nullable().optional()}); export const taskTimeBilling = z.object({billable: z.boolean(), rate: z.number(), amount: z.number()}); export const taskTimeBillable = z.object({id: z.number(), time: z.number(), user: z.number(), date: z.string(), task: task.optional(), comment: z.string().optional(), billing: taskTimeBilling.optional()}); export const getApiTeamTimeResponse = z.array(z.union([timeRecordExtended, taskTimeBillable])); 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 }; /** * Get All Time Records * * GET /team/time */ export const getApiTeamTime = async (args: GetApiTeamTimeArgs, options?: GetApiTeamTimeOptions) => { 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 (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.opts_include_billing != null) query.append("opts_include_billing", String(args.opts_include_billing)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/team/time${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiTeamTimeResponse, await res.json()) };