import {z} from 'zod' export type GetApiTeamTimeExportArgs = {from?: string | undefined, to?: string | undefined, fields?: string | undefined, project?: string | undefined, client?: number | undefined, user?: number | undefined, allowTimeWithoutTask?: boolean | undefined}; export type GetApiTeamTimeExportOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const timeExportObject = z.object({time: z.number(), date: z.string().optional(), user: z.object({id: z.number().optional(), name: z.string().optional()}).optional(), project: z.object({id: z.string().optional(), name: z.string().optional(), workspace: z.string().optional()}).optional(), task: z.object({id: z.string().optional(), name: z.string().optional(), status: z.string().optional(), type: z.string().optional(), iteration: z.string().optional(), number: z.number().optional(), dueAt: z.string().optional()}).optional()}); export const getApiTeamTimeExportResponse = z.array(timeExportObject); 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 }; /** * Time Report (deprecated) * * GET /team/time/export */ export const getApiTeamTimeExport = async (args: GetApiTeamTimeExportArgs, options?: GetApiTeamTimeExportOptions) => { 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.fields != null) query.append("fields", String(args.fields)) if (args.project != null) query.append("project", String(args.project)) if (args.client != null) query.append("client", String(args.client)) if (args.user != null) query.append("user", String(args.user)) if (args.allowTimeWithoutTask != null) query.append("allowTimeWithoutTask", String(args.allowTimeWithoutTask)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/team/time/export${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(getApiTeamTimeExportResponse, await res.json()) };