import {z} from 'zod' export type GetApiProjectsArgs = {platform?: ('as' | 'ev' | 'b3' | 'b2' | 'pv' | 'gh' | 'in' | 'tr' | 'jr') | undefined, query?: string | undefined, limit?: number | undefined, page?: number | undefined}; export type GetApiProjectsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const projectBillingBilling = z.object({type: z.enum(["non_billable", "hourly", "fixed_fee"]), fee: z.number().optional()}); export const projectBillingRate = z.object({type: z.enum(["project_rate", "user_rate"]), rate: z.number().optional(), userRateOverrides: z.record(z.string(), z.number()).optional()}); export const projectBillingBudget = z.object({type: z.enum(["money", "time", "costs"]), budget: z.number(), progress: z.number().optional(), timeProgress: z.number().optional(), expenseProgress: z.number().optional(), period: z.enum(["general", "monthly", "weekly", "daily", "quarterly", "yearly"]), appliedFrom: z.string().nullable().optional(), disallowOverbudget: z.boolean().optional(), excludeUnbillableTime: z.boolean().optional(), excludeExpenses: z.boolean().optional(), showToUsers: z.boolean().optional(), threshold: z.number().optional()}); export const project = z.object({id: z.string(), name: z.string(), workspaceId: z.string().nullable().optional(), workspaceName: z.string().nullable().optional(), client: z.number().nullable().optional(), type: z.enum(["board", "list"]).optional(), favorite: z.boolean().optional(), users: z.array(z.number()), billing: projectBillingBilling.optional(), rate: projectBillingRate.optional(), budget: projectBillingBudget.optional()}); export const getApiProjectsResponse = z.array(project); 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 Projects * * GET /projects */ export const getApiProjects = async (args: GetApiProjectsArgs, options?: GetApiProjectsOptions) => { 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.platform != null) query.append("platform", String(args.platform)) if (args.query != null) query.append("query", String(args.query)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiProjectsResponse, await res.json()) };