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

api.ts

27 lines712 Bytes

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