Open History to see the last commit
1import { json } from '@sveltejs/kit'
2import type { RequestHandler } from '@sveltejs/kit'
3β
4type Todo = {
5 id: string
6 text: string
7}
8β
9const items: Array<Todo> = []
10β
11/** @type {import('./$types').RequestHandler} */
12export const GET: RequestHandler = async (req) => {
13 await new Promise((r) => setTimeout(r, 1000))
14 return json({ ts: Date.now(), items }, { status: 200 })
15}
16β
17/** @type {import('./$types').RequestHandler} */
18export const POST: RequestHandler = async ({ request }) => {
19 const { text } = await request.json()
20β
21 if (Math.random() > 0.7) {
22 return json({ message: 'Could not add item!' }, { status: 500 })
23 }
24β
25 const newTodo = {
26 id: Math.random().toString(),
27 text: text.toUpperCase() as string,
28 }
29 items.push(newTodo)
30 return json(newTodo, { status: 200 })
31}