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

api.ts

21 lines604 Bytes

1import type { Todo } from './types'
2​
3export const todosAPI = {
4 getTodos: async (): Promise<Array<Todo>> => {
5 const res = await fetch('/api/todos')
6 if (!res.ok) throw new Error('Failed to fetch todos')
7 return res.json()
8 },
9 createTodo: async (text: string): Promise<Todo> => {
10 const res = await fetch('/api/todos', {
11 method: 'POST',
12 headers: { 'Content-Type': 'application/json' },
13 body: JSON.stringify({ text }),
14 })
15 if (!res.ok) {
16 const err = (await res.json()) as { error: string }
17 throw new Error(err.error)
18 }
19 return res.json()
20 },