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

+server.ts

31 lines792 Bytes

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 })
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 })