import {z} from 'zod' export type GetApiDashboardsUsersArgs = {'date.gte'?: string | undefined, 'date.lte'?: string | undefined, projectId?: string | undefined, clientId?: number | undefined, memberId?: number | undefined}; export type GetApiDashboardsUsersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usersDashboardItem = z.object({memberId: z.number().optional(), memberName: z.string().optional(), memberHeadline: z.string().optional(), memberStatus: z.enum(["active", "invited", "pending", "removed"]).optional(), memberAvatarUrl: z.string().optional(), time: z.number().optional(), billableTime: z.number().optional(), nonBillableTime: z.number().optional(), timeOffTime: z.number().optional(), timeOffDays: z.number().optional(), timerTimePc: z.number().optional(), billableAmount: z.number().optional(), billableAmountTime: z.number().optional(), billableAmountExpenses: z.number().optional(), costs: z.number().optional(), costsTime: z.number().optional(), costsTimeOff: z.number().optional(), costsExpenses: z.number().optional(), profit: z.number().optional(), profitCosts: z.number().optional(), uninvoicedAmount: z.number().optional(), expenses: z.number().optional(), billableExpenses: z.number().optional(), separateExpenses: z.number().optional()}); export const getApiDashboardsUsersResponse = z.array(usersDashboardItem); 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 }; /** * Users Report * * GET /dashboards/users */ export const getApiDashboardsUsers = async (args: GetApiDashboardsUsersArgs, options?: GetApiDashboardsUsersOptions) => { 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['date.gte'] != null) query.append("date.gte", String(args['date.gte'])) if (args['date.lte'] != null) query.append("date.lte", String(args['date.lte'])) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.clientId != null) query.append("clientId", String(args.clientId)) if (args.memberId != null) query.append("memberId", String(args.memberId)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/dashboards/users${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(getApiDashboardsUsersResponse, await res.json()) };