import { getConfig } from "./config"; import type { Project, Task } from "./types"; class ApiError extends Error { constructor(public status: number, message: string) { super(message); } } async function req(path: string, init?: RequestInit): Promise { const { url, token } = await getConfig(); if (!token) throw new ApiError(401, "No API token set — open Settings"); const res = await fetch(`${url}${path}`, { ...init, headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", ...init?.headers, }, }); if (!res.ok) { const body = await res.json().catch(() => ({ error: res.statusText })); throw new ApiError(res.status, (body as { error?: string }).error ?? `HTTP ${res.status}`); } return res.json(); } export const api = { tasks: { list: (params: { status?: string; project_id?: string } = {}) => { const q = new URLSearchParams( Object.entries(params).filter(([, v]) => v != null) as [string, string][], ).toString(); return req(`/v1/tasks${q ? `?${q}` : ""}`); }, get: (id: string) => req(`/v1/tasks/${id}`), create: (data: Partial & { title: string }) => req("/v1/tasks", { method: "POST", body: JSON.stringify(data) }), update: (id: string, data: Partial) => req(`/v1/tasks/${id}`, { method: "PATCH", body: JSON.stringify(data) }), trash: (id: string) => req(`/v1/tasks/${id}`, { method: "DELETE" }), }, projects: { list: () => req("/v1/projects"), create: (data: Partial & { name: string }) => req("/v1/projects", { method: "POST", body: JSON.stringify(data) }), update: (id: string, data: Partial) => req(`/v1/projects/${id}`, { method: "PATCH", body: JSON.stringify(data) }), drop: (id: string) => req(`/v1/projects/${id}`, { method: "DELETE" }), }, contexts: { list: () => req("/v1/contexts") }, };