({
transaction,
idempotencyKey,
}: {
transaction: { mutations: Array<PendingMutation> }
idempotencyKey: string
})
| 110 | // API client functions |
| 111 | export const todoAPI = { |
| 112 | async syncTodos({ |
| 113 | transaction, |
| 114 | idempotencyKey, |
| 115 | }: { |
| 116 | transaction: { mutations: Array<PendingMutation> } |
| 117 | idempotencyKey: string |
| 118 | }) { |
| 119 | const mutations = transaction.mutations |
| 120 | |
| 121 | console.log( |
| 122 | `sync todos`, |
| 123 | mutations[0].changes, |
| 124 | (mutations[0].original as Record<string, unknown>).text, |
| 125 | ) |
| 126 | for (const mutation of mutations) { |
| 127 | try { |
| 128 | switch (mutation.type) { |
| 129 | case `insert`: { |
| 130 | const todoData = mutation.modified as unknown as Todo |
| 131 | const response = await fetchWithRetry(`/api/todos`, { |
| 132 | method: `POST`, |
| 133 | headers: { |
| 134 | 'Content-Type': `application/json`, |
| 135 | 'Idempotency-Key': idempotencyKey, |
| 136 | }, |
| 137 | body: JSON.stringify({ |
| 138 | text: todoData.text, |
| 139 | completed: todoData.completed, |
| 140 | }), |
| 141 | }) |
| 142 | |
| 143 | if (!response.ok) { |
| 144 | throw new Error(`Failed to sync insert: ${response.statusText}`) |
| 145 | } |
| 146 | break |
| 147 | } |
| 148 | |
| 149 | case `update`: { |
| 150 | const todoData = mutation.modified as Partial<Todo> |
| 151 | const response = await fetch( |
| 152 | `/api/todos/${(mutation.modified as unknown as Todo).id}`, |
| 153 | { |
| 154 | method: `PUT`, |
| 155 | headers: { |
| 156 | 'Content-Type': `application/json`, |
| 157 | 'Idempotency-Key': idempotencyKey, |
| 158 | }, |
| 159 | body: JSON.stringify({ |
| 160 | text: todoData.text, |
| 161 | completed: todoData.completed, |
| 162 | }), |
| 163 | }, |
| 164 | ) |
| 165 | |
| 166 | if (!response.ok) { |
| 167 | throw new Error(`Failed to sync update: ${response.statusText}`) |
| 168 | } |
| 169 | break |
nothing calls this directly
no test coverage detected