Open History to see the last commit
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 }
31}