Open History to see the last commit
1const BASE_URL = 'http://localhost:3001'
2
3export interface Todo {
4 id: string
5 text: string
6 completed: boolean
7 createdAt: string
8 updatedAt: string
9}
10
11export const todoApi = {
12 async getAll(): Promise<Array<Todo>> {
13 const response = await fetch(`${BASE_URL}/api/todos`)
14 if (!response.ok) {
15 throw new Error(`Failed to fetch todos: ${response.status}`)
16 }
17 return response.json()
18 },
19
20 async create(data: {
21 id: string
22 text: string
23 completed?: boolean
24 }): Promise<Todo> {
25 const response = await fetch(`${BASE_URL}/api/todos`, {
26 method: 'POST',
27 headers: { 'Content-Type': 'application/json' },
28 body: JSON.stringify(data),
29 })
30 if (!response.ok) {
31 throw new Error(`Failed to create todo: ${response.status}`)
32 }
33 return response.json()
34 },
35
36 async update(
37 id: string,
38 data: { text?: string; completed?: boolean },
39 ): Promise<Todo | null> {
40 const response = await fetch(`${BASE_URL}/api/todos/${id}`, {
41 method: 'PUT',
42 headers: { 'Content-Type': 'application/json' },
43 body: JSON.stringify(data),
44 })
45 if (response.status === 404) return null
46 if (!response.ok) {
47 throw new Error(`Failed to update todo: ${response.status}`)
48 }
49 return response.json()
50 },
51
52 async delete(id: string): Promise<boolean> {
53 const response = await fetch(`${BASE_URL}/api/todos/${id}`, {
54 method: 'DELETE',
55 })
56 if (response.status === 404) return false
57 if (!response.ok) {
58 throw new Error(`Failed to delete todo: ${response.status}`)
59 }
60 return true
61 },
62
63 async deleteAll(): Promise<void> {
64 const response = await fetch(`${BASE_URL}/api/todos`, {
65 method: 'DELETE',
66 })
67 if (!response.ok) {
68 throw new Error(`Failed to delete all todos: ${response.status}`)
69 }
70 },
71}