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

api.ts

21 lines688 Bytes

1import type { 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 (value: string) => {
12 const response = await fetch(`${endpoint}?add=${encodeURIComponent(value)}`)
13 if (!response.ok) throw new Error('Failed to add todo')
14 return await response.json()
15 },
16 deleteTodos: async () => {
17 const response = await fetch(`${endpoint}?clear=1`)
18 if (!response.ok) throw new Error('Failed to clear todos')
19 return await response.json()
20 },