-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
26 lines (21 loc) · 836 Bytes
/
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
import type {Item} from "./app.js"
const BASE_URL = "http://localhost:3000/items"
export const getItems = (): Promise<Item[]> =>
fetch(BASE_URL).then(res => res.json())
export const addItem = (text: string): Promise<Item> =>
fetch(BASE_URL, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({text, checked: false}),
}).then(res => res.json())
export const toggleItem = (id: string, checked: boolean): Promise<Item> =>
fetch(`${BASE_URL}/${id}`, {
method: "PATCH",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({checked}),
}).then(res => res.json())
export const deleteItem = (id: string): Promise<void> =>
fetch(`${BASE_URL}/${id}`, {
method: "DELETE",
headers: {"Content-Type": "application/json"},
}).then(res => res.json())