Open History to see the last commit
1import type { SelectConfig, SelectTodo } from '../db/validation'
2
3// API helper for todos and config
4const API_BASE_URL = `/api`
5
6export const api = {
7 // Todo API methods
8 todos: {
9 getAll: async (): Promise<Array<SelectTodo>> => {
10 const response = await fetch(`${API_BASE_URL}/todos`)
11 if (!response.ok)
12 throw new Error(`HTTP error! Status: ${response.status}`)
13 return response.json()
14 },
15 getById: async (id: number): Promise<SelectTodo> => {
16 const response = await fetch(`${API_BASE_URL}/todos/${id}`)
17 if (!response.ok)
18 throw new Error(`HTTP error! Status: ${response.status}`)
19 return response.json()
20 },
21 create: async (
22 todo: Partial<SelectTodo>,
23 ): Promise<{ todo: SelectTodo; txid: number }> => {
24 const response = await fetch(`${API_BASE_URL}/todos`, {
25 method: `POST`,
26 headers: { 'Content-Type': `application/json` },
27 body: JSON.stringify(todo),
28 })
29 if (!response.ok)
30 throw new Error(`HTTP error! Status: ${response.status}`)
31 return response.json()
32 },
33 update: async (
34 id: unknown,
35 changes: Partial<SelectTodo>,
36 ): Promise<{ todo: SelectTodo; txid: number }> => {
37 const response = await fetch(`${API_BASE_URL}/todos/${id}`, {
38 method: `PUT`,
39 headers: { 'Content-Type': `application/json` },
40 body: JSON.stringify(changes),
41 })
42 if (!response.ok)
43 throw new Error(`HTTP error! Status: ${response.status}`)
44 return response.json()
45 },
46 delete: async (
47 id: unknown,
48 ): Promise<{ success: boolean; txid: number }> => {
49 const response = await fetch(`${API_BASE_URL}/todos/${id}`, {
50 method: `DELETE`,
51 })
52 if (!response.ok)
53 throw new Error(`HTTP error! Status: ${response.status}`)
54 return response.json()
55 },
56 },
57
58 // Config API methods
59 config: {
60 getAll: async (): Promise<Array<SelectConfig>> => {
61 const response = await fetch(`${API_BASE_URL}/config`)
62 if (!response.ok)
63 throw new Error(`HTTP error! Status: ${response.status}`)
64 return response.json()
65 },
66 getById: async (id: number): Promise<SelectConfig> => {
67 const response = await fetch(`${API_BASE_URL}/config/${id}`)
68 if (!response.ok)
69 throw new Error(`HTTP error! Status: ${response.status}`)
70 return response.json()
71 },
72 create: async (
73 config: Partial<SelectConfig>,
74 ): Promise<{ config: SelectConfig; txid: number }> => {
75 const response = await fetch(`${API_BASE_URL}/config`, {
76 method: `POST`,
77 headers: { 'Content-Type': `application/json` },
78 body: JSON.stringify(config),
79 })
80 if (!response.ok)
81 throw new Error(`HTTP error! Status: ${response.status}`)
82 return response.json()
83 },
84 update: async (
85 id: number,
86 changes: Partial<SelectConfig>,
87 ): Promise<{ config: SelectConfig; txid: number }> => {
88 const response = await fetch(`${API_BASE_URL}/config/${id}`, {
89 method: `PUT`,
90 headers: { 'Content-Type': `application/json` },
91 body: JSON.stringify(changes),
92 })
93 if (!response.ok)
94 throw new Error(`HTTP error! Status: ${response.status}`)
95 return response.json()
96 },
97 delete: async (id: number): Promise<{ success: boolean; txid: number }> => {
98 const response = await fetch(`${API_BASE_URL}/config/${id}`, {
99 method: `DELETE`,
100 })
101 if (!response.ok)
102 throw new Error(`HTTP error! Status: ${response.status}`)
103 return response.json()
104 },
105 },
106}