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

data.ts

31 lines729 Bytes

1import type { NextApiRequest, NextApiResponse } from 'next'
2​
3type Todo = { id: string; text: string }
4​
5const items: Array<Todo> = []
6​
7export default async (req: NextApiRequest, res: NextApiResponse) => {
8 await new Promise((r) => setTimeout(r, 1000))
9​
10 if (req.method === 'POST') {
11 const text: string = req.body.text
12​
13 // sometimes it will fail, this will cause a regression on the UI
14​
15 if (Math.random() > 0.7) {
16 res.status(500)
17 res.json({ message: 'Could not add item!' })
18 return
19 }
20​
21 const newTodo = { id: Math.random().toString(), text: text.toUpperCase() }
22 items.push(newTodo)
23 res.json(newTodo)
24 return
25 } else {
26 res.json({
27 ts: Date.now(),
28 items,
29 })
30 }