Download react-query/examples/react/optimistic-updates-ui/src/pages/api/data.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 729 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/optimistic-updates-ui/src/pages/api/data.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/optimistic-updates-ui/src/pages/api/data.ts
-
curl -L -o data.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/optimistic-updates-ui/src/pages/api/data.ts
729 Bytes
| import type { NextApiRequest, NextApiResponse } from 'next' | |
| type Todo = { id: string; text: string } | |
| const items: Array<Todo> = [] | |
| export default async (req: NextApiRequest, res: NextApiResponse) => { | |
| await new Promise((r) => setTimeout(r, 1000)) | |
| if (req.method === 'POST') { | |
| const text: string = req.body.text | |
| // sometimes it will fail, this will cause a regression on the UI | |
| if (Math.random() > 0.7) { | |
| res.status(500) | |
| res.json({ message: 'Could not add item!' }) | |
| return | |
| } | |
| const newTodo = { id: Math.random().toString(), text: text.toUpperCase() } | |
| items.push(newTodo) | |
| res.json(newTodo) | |
| return | |
| } else { | |
| res.json({ | |
| ts: Date.now(), | |
| items, | |
| }) | |
| } | |
| } | |