Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

api.ts

140 lines4.2 KB

1import { Platform } from 'react-native'
2import { createOfflineAwareFetch } from '../network/simulatedOffline'
3​
4const SERVER_PORT = 3001
5export const API_URL = Platform.select({
6 android: `http://10.0.2.2:${SERVER_PORT}`,
7 ios: `http://localhost:${SERVER_PORT}`,
8 default: `http://localhost:${SERVER_PORT}`,
9})
10const offlineAwareFetch = createOfflineAwareFetch(fetch)
11​
12// ─── Types ──────────────────────────────────────────────
13​
14export interface ShoppingList {
15 id: string
16 name: string
17 createdAt: string
19​
20export interface ShoppingItem {
21 id: string
22 listId: string
23 text: string
24 checked: boolean
25 createdAt: string
27​
28type ApiTxResult<T> = { txid: number } & T
29​
30// ─── Lists API ──────────────────────────────────────────
31​
32export const listsApi = {
33 async getAll(): Promise<Array<ShoppingList>> {
34 const response = await offlineAwareFetch(`${API_URL}/api/lists`)
35 if (!response.ok) {
36 throw new Error(`Failed to fetch lists: ${response.status}`)
37 }
38 return response.json()
39 },
40​
41 async create(data: {
42 id?: string
43 name: string
44 createdAt?: string
45 }): Promise<ApiTxResult<{ list: ShoppingList }>> {
46 const response = await offlineAwareFetch(`${API_URL}/api/lists`, {
47 method: 'POST',
48 headers: { 'Content-Type': 'application/json' },
49 body: JSON.stringify(data),
50 })
51 if (!response.ok) {
52 throw new Error(`Failed to create list: ${response.status}`)
53 }
54 return response.json()
55 },
56​
57 async update(
58 id: string,
59 data: { name?: string },
60 ): Promise<ApiTxResult<{ list: ShoppingList }> | null> {
61 const response = await offlineAwareFetch(`${API_URL}/api/lists/${id}`, {
62 method: 'PUT',
63 headers: { 'Content-Type': 'application/json' },
64 body: JSON.stringify(data),
65 })
66 if (response.status === 404) return null
67 if (!response.ok) {
68 throw new Error(`Failed to update list: ${response.status}`)
69 }
70 return response.json()
71 },
72​
73 async delete(id: string): Promise<ApiTxResult<{ success: boolean }> | null> {
74 const response = await offlineAwareFetch(`${API_URL}/api/lists/${id}`, {
75 method: 'DELETE',
76 })
77 if (response.status === 404) return null
78 if (!response.ok) {
79 throw new Error(`Failed to delete list: ${response.status}`)
80 }
81 return response.json()
82 },
84​
85// ─── Items API ──────────────────────────────────────────
86​
87export const itemsApi = {
88 async getAll(): Promise<Array<ShoppingItem>> {
89 const response = await offlineAwareFetch(`${API_URL}/api/items`)
90 if (!response.ok) {
91 throw new Error(`Failed to fetch items: ${response.status}`)
92 }
93 return response.json()
94 },
95​
96 async create(data: {
97 id?: string
98 listId: string
99 text: string
100 checked?: boolean
101 createdAt?: string
102 }): Promise<ApiTxResult<{ item: ShoppingItem }>> {
103 const response = await offlineAwareFetch(`${API_URL}/api/items`, {
104 method: 'POST',
105 headers: { 'Content-Type': 'application/json' },
106 body: JSON.stringify(data),
107 })
108 if (!response.ok) {
109 throw new Error(`Failed to create item: ${response.status}`)
110 }
111 return response.json()
112 },
113​
114 async update(
115 id: string,
116 data: { text?: string; checked?: boolean },
117 ): Promise<ApiTxResult<{ item: ShoppingItem }> | null> {
118 const response = await offlineAwareFetch(`${API_URL}/api/items/${id}`, {
119 method: 'PUT',
120 headers: { 'Content-Type': 'application/json' },
121 body: JSON.stringify(data),
122 })
123 if (response.status === 404) return null
124 if (!response.ok) {
125 throw new Error(`Failed to update item: ${response.status}`)
126 }
127 return response.json()
128 },
129​
130 async delete(id: string): Promise<ApiTxResult<{ success: boolean }> | null> {
131 const response = await offlineAwareFetch(`${API_URL}/api/items/${id}`, {
132 method: 'DELETE',
133 })
134 if (response.status === 404) return null
135 if (!response.ok) {
136 throw new Error(`Failed to delete item: ${response.status}`)
137 }
138 return response.json()
139 },