-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
43 lines (34 loc) · 1.05 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { ITask } from "./types/tasks";
const baseUrl = 'http://localhost:3001';
export const getAllTodos = async () : Promise<ITask[]> => {
const result = await fetch(`${baseUrl}/tasks`, {cache: 'no-store'});
const list = await result.json();
return list;
}
export const addTask = async (task: ITask): Promise<ITask> =>{
const result = await fetch(`${baseUrl}/tasks`,{
method: 'POST',
headers:{
'Content-Type': 'application/json'
},
body: JSON.stringify(task)
})
const newTask = await result.json();
return newTask;
}
export const editTask = async (task: ITask): Promise<ITask> =>{
const result = await fetch(`${baseUrl}/tasks/${task.id}`,{
method: 'PUT',
headers:{
'Content-Type': 'application/json'
},
body: JSON.stringify(task)
})
const updatedTask = await result.json();
return updatedTask;
}
export const deleteTask = async (id: string): Promise<void> =>{
await fetch(`${baseUrl}/tasks/${id}`,{
method: 'DELETE',
})
}