Open History to see the last commit
1import type { Todo } from '../../app/services/todos/types'
2β
3export default defineEventHandler(async (event) => {
4 const body: unknown = await readBody(event).catch(() => null)
5β
6 const text =
7 body !== null &&
8 typeof body === 'object' &&
9 'text' in body &&
10 typeof body.text === 'string'
11 ? body.text.trim()
12 : ''
13β
14 if (!text) {
15 throw createError({ statusCode: 400, message: 'text is required' })
16 }
17β
18 await new Promise((resolve) => setTimeout(resolve, 500))
19β
20 if (Math.random() < 0.3) {
21 throw createError({
22 statusCode: 500,
23 message: 'Server error β please try again',
24 })
25 }
26β
27 const newTodo: Todo = {
28 id: crypto.randomUUID(),
29 text,
30 createdAt: Date.now(),
31 }
32β
33 todos.push(newTodo)
34β
35 setResponseStatus(event, 201)
36 return newTodo
37})