import {z} from 'zod' export type CreateApiApiTasksArgs = {body: {title: string, description?: string | undefined, status?: ('inbox' | 'assigned' | 'in_progress' | 'quality_review' | 'done') | undefined, priority?: ('critical' | 'high' | 'medium' | 'low') | undefined, assigned_to?: string | undefined, created_by?: string | undefined, due_date?: string | undefined, estimated_hours?: number | undefined, tags?: Array | undefined, metadata?: Record | undefined}}; export type CreateApiApiTasksOptions = {baseUrl: string, sessionCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const task = z.object({id: z.number().int().optional(), title: z.string().optional(), description: z.string().optional(), status: z.enum(["inbox", "assigned", "in_progress", "quality_review", "done"]).optional(), priority: z.enum(["critical", "high", "medium", "low"]).optional(), assigned_to: z.string().optional(), created_by: z.string().optional(), due_date: z.string().optional(), estimated_hours: z.number().optional(), tags: z.array(z.string()).optional(), metadata: z.object({}).optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional()}); export const createApiApiTasksResponse = z.object({task: task.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 }; /** * Create task * * POST /api/tasks * * Also accepts auth: apiKey */ export const createApiApiTasks = async (args: CreateApiApiTasksArgs, options: CreateApiApiTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch, sessionCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sessionCookie !== undefined) cookies.push("__Host-mc-session=" + encodeURIComponent(sessionCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/api/tasks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiApiTasksResponse, await res.json()) };