File size: 779 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { json } from '@sveltejs/kit'
import type { RequestHandler } from '@sveltejs/kit'

type Todo = {
  id: string
  text: string
}

const items: Todo[] = []

/** @type {import('./$types').RequestHandler} */
export const GET: RequestHandler = async (req) => {
  await new Promise((r) => setTimeout(r, 1000))
  return json({ ts: Date.now(), items }, { status: 200 })
}

/** @type {import('./$types').RequestHandler} */
export const POST: RequestHandler = async ({ request }) => {
  const { text } = await request.json()

  if (Math.random() > 0.7) {
    json({ message: 'Could not add item!' }, { status: 500 })
  }
  const newTodo = {
    id: Math.random().toString(),
    text: text.toUpperCase() as string,
  }
  items.push(newTodo)
  return json(newTodo, { status: 200 })
}