import {z} from 'zod' export type GetApiDashboardsProjectsArgs = {'date.gte'?: string | undefined, 'date.lte'?: string | undefined, projectId?: string | undefined, clientId?: number | undefined, memberId?: number | undefined}; export type GetApiDashboardsProjectsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const projectsDashboardItem = z.object({projectId: z.string().optional(), projectName: z.string().optional(), projectStatus: z.enum(["open", "archived"]).optional(), workspaceId: z.string().optional(), workspaceName: z.string().optional(), clientId: z.number().optional(), clientName: z.string().optional(), billingType: z.enum(["non_billable", "hourly", "fixed_fee"]).optional(), time: z.number().optional(), billableTime: z.number().optional(), nonBillableTime: 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(), 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 getApiDashboardsProjectsResponse = z.array(projectsDashboardItem); 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 }; /** * Projects Report * * GET /dashboards/projects */ export const getApiDashboardsProjects = async (args: GetApiDashboardsProjectsArgs, options?: GetApiDashboardsProjectsOptions) => { 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/projects${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(getApiDashboardsProjectsResponse, await res.json()) };