import type { Comment, NewTask, Task, TaskPatch, TaskStatus } from "../types"; import { api } from "./client"; // Tasks saved before statuses existed carry only a `done` flag. type TaskRecord = Omit & { status?: TaskStatus; done?: boolean }; const toTask = ({ done, ...t }: TaskRecord): Task => ({ ...t, status: t.status ?? (done ? "done" : "todo") }); export const tasksApi = { list: (query = "") => api.get(query ? `/tasks?q=${encodeURIComponent(query)}` : "/tasks").then((ts) => ts.map(toTask)), get: (id: number) => api.get(`/tasks/${id}`).then(toTask), create: (task: NewTask) => api.post("/tasks", { ...task, status: "todo" }).then(toTask), update: (id: number, patch: TaskPatch) => api.patch(`/tasks/${id}`, patch).then(toTask), remove: (id: number) => api.del<{ ok: true }>(`/tasks/${id}`), comments: (id: number) => api.get(`/tasks/${id}/comments`), };