forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
106 lines (103 loc) · 3.53 KB
/
api.ts
File metadata and controls
106 lines (103 loc) · 3.53 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type { SelectConfig, SelectTodo } from "../db/validation"
// API helper for todos and config
const API_BASE_URL = `/api`
export const api = {
// Todo API methods
todos: {
getAll: async (): Promise<Array<SelectTodo>> => {
const response = await fetch(`${API_BASE_URL}/todos`)
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
getById: async (id: number): Promise<SelectTodo> => {
const response = await fetch(`${API_BASE_URL}/todos/${id}`)
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
create: async (
todo: Partial<SelectTodo>
): Promise<{ todo: SelectTodo; txid: number }> => {
const response = await fetch(`${API_BASE_URL}/todos`, {
method: `POST`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify(todo),
})
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
update: async (
id: unknown,
changes: Partial<SelectTodo>
): Promise<{ todo: SelectTodo; txid: number }> => {
const response = await fetch(`${API_BASE_URL}/todos/${id}`, {
method: `PUT`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify(changes),
})
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
delete: async (
id: unknown
): Promise<{ success: boolean; txid: number }> => {
const response = await fetch(`${API_BASE_URL}/todos/${id}`, {
method: `DELETE`,
})
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
},
// Config API methods
config: {
getAll: async (): Promise<Array<SelectConfig>> => {
const response = await fetch(`${API_BASE_URL}/config`)
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
getById: async (id: number): Promise<SelectConfig> => {
const response = await fetch(`${API_BASE_URL}/config/${id}`)
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
create: async (
config: Partial<SelectConfig>
): Promise<{ config: SelectConfig; txid: number }> => {
const response = await fetch(`${API_BASE_URL}/config`, {
method: `POST`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify(config),
})
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
update: async (
id: number,
changes: Partial<SelectConfig>
): Promise<{ config: SelectConfig; txid: number }> => {
const response = await fetch(`${API_BASE_URL}/config/${id}`, {
method: `PUT`,
headers: { "Content-Type": `application/json` },
body: JSON.stringify(changes),
})
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
delete: async (id: number): Promise<{ success: boolean; txid: number }> => {
const response = await fetch(`${API_BASE_URL}/config/${id}`, {
method: `DELETE`,
})
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
},
},
}