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

+server.ts

30 lines779 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: 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 json({ message: 'Could not add item!' }, { status: 500 })
23 }
24 const newTodo = {
25 id: Math.random().toString(),
26 text: text.toUpperCase() as string,
27 }
28 items.push(newTodo)
29 return json(newTodo, { status: 200 })