53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
export interface Task {
|
|
id: number;
|
|
title: string;
|
|
description: string;
|
|
status: "pending" | "in_progress" | "completed";
|
|
priority: "low" | "medium" | "high";
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface APIResponse<T> {
|
|
success: boolean;
|
|
data?: T;
|
|
error?: string;
|
|
}
|
|
|
|
const API_BASE = "/api";
|
|
|
|
export async function fetchTasks(): Promise<Task[]> {
|
|
const res = await fetch(`${API_BASE}/tasks`);
|
|
const json: APIResponse<Task[]> = await res.json();
|
|
if (!json.success) throw new Error(json.error || "Failed to fetch tasks");
|
|
return json.data || [];
|
|
}
|
|
|
|
export async function fetchTask(id: number): Promise<Task> {
|
|
const res = await fetch(`${API_BASE}/tasks/${id}`);
|
|
const json: APIResponse<Task> = await res.json();
|
|
if (!json.success) throw new Error(json.error || "Failed to fetch task");
|
|
return json.data!;
|
|
}
|
|
|
|
export async function createTask(
|
|
task: Pick<Task, "title" | "description" | "priority" | "status">
|
|
): Promise<Task> {
|
|
const res = await fetch(`${API_BASE}/tasks`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(task),
|
|
});
|
|
const json: APIResponse<Task> = await res.json();
|
|
if (!json.success) throw new Error(json.error || "Failed to create task");
|
|
return json.data!;
|
|
}
|
|
|
|
export async function deleteTask(id: number): Promise<void> {
|
|
const res = await fetch(`${API_BASE}/tasks/${id}`, {
|
|
method: "DELETE",
|
|
});
|
|
const json: APIResponse<string> = await res.json();
|
|
if (!json.success) throw new Error(json.error || "Failed to delete task");
|
|
}
|