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

api.ts

52 lines1.0 KB

1import type { Todo } from './types'
2​
3const todos: Array<Todo> = [
4 {
5 id: crypto.randomUUID(),
6 text: 'Buy groceries',
7 createdAt: Date.now() - 3000,
8 },
9 {
10 id: crypto.randomUUID(),
11 text: 'Walk the dog',
12 createdAt: Date.now() - 2000,
13 },
14 {
15 id: crypto.randomUUID(),
16 text: 'Read a book',
17 createdAt: Date.now() - 1000,
18 },
20​
21export const todosAPI = {
22 getTodos: async (): Promise<Array<Todo>> => {
23 'use server'
24​
25 await new Promise((resolve) => setTimeout(resolve, 200))
26 return todos
27 },
28 createTodo: async (text: string): Promise<Todo> => {
29 'use server'
30​
31 const trimmedText = text.trim()
32 if (!trimmedText) {
33 throw new Error('text is required')
34 }
35​
36 await new Promise((resolve) => setTimeout(resolve, 500))
37​
38 if (Math.random() < 0.3) {
39 throw new Error('Server error β€” please try again')
40 }
41​
42 const newTodo: Todo = {
43 id: crypto.randomUUID(),
44 text: trimmedText,
45 createdAt: Date.now(),
46 }
47​
48 todos.push(newTodo)
49​
50 return newTodo
51 },