import {z} from 'zod' export type ProjectBillingBilling = {type: 'non_billable' | 'hourly' | 'fixed_fee', fee?: number | undefined}; export type ProjectBillingRate = {type: 'project_rate' | 'user_rate', rate?: number | undefined, userRateOverrides?: {[key: string]: number} | undefined}; export type ProjectBillingBudget = {type: 'money' | 'time' | 'costs', budget: number, progress?: number | undefined, timeProgress?: number | undefined, expenseProgress?: number | undefined, period: 'general' | 'monthly' | 'weekly' | 'daily' | 'quarterly' | 'yearly', appliedFrom?: (string | null) | undefined, disallowOverbudget?: boolean | undefined, excludeUnbillableTime?: boolean | undefined, excludeExpenses?: boolean | undefined, showToUsers?: boolean | undefined, threshold?: number | undefined}; export type ProjectBilling = {billing?: ProjectBillingBilling | undefined, rate?: ProjectBillingRate | undefined, budget?: ProjectBillingBudget | undefined}; export type UpdateApiProjectsProjectIdBillingArgs = {project_id: string, body: ProjectBilling}; export type UpdateApiProjectsProjectIdBillingOptions = {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 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 }; /** * Update Project Billing/Budget * * PUT /projects/{project_id}/billing */ export const updateApiProjectsProjectIdBilling = async (args: UpdateApiProjectsProjectIdBillingArgs, options?: UpdateApiProjectsProjectIdBillingOptions) => { const {baseUrl = "https://api.everhour.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.project_id))}/billing${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(project, await res.json()) };