# Elysia

Elysia - Ergonomic Framework for Humans | ElysiaJS TypeScript with End-to-End Type Safety, unified type system and outstanding developer experience. Supercharged by Bun.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Elysia, t } from "elysia";
import { swagger } from "@elysiajs/swagger";

const todos = [
{ id: 1, title: "Learning Bun", done: false },
{ id: 2, title: "Learing Elysia", done: false },
];

new Elysia()
.use(swagger())
.group("/todos", (app) =>
app
.get("/", () => todos)
.get(
"/:id",
({ params: { id } }) => {
return todos.filter((todo) => todo.id === id);
},
{
params: t.Object({
id: t.Numeric(),
}),
}
)
.post(
"/",
({ body: { title }, set }) => {
const todo = {
id: todos.length + 1,
title,
done: false,
};
todos.push(todo);

set.status = 201;

return todo;
},
{
body: t.Object({
title: t.String(),
}),
}
)
.put(
"/:id",
({ params: { id }, body: { title, done } }) => {
const index = todos.findIndex((todo) => todo.id === id);

if (title) {
todos[index] = {
...todos[index],
title,
done,
};
} else {
todos[index] = {
...todos[index],
done,
};
}

return todos[index];
},
{
body: t.Object({
title: t.Optional(t.String()),
done: t.Boolean(),
}),
params: t.Object({
id: t.Numeric(),
}),
}
)
.delete(
"/:id",
({ params: { id } }) => {
const index = todos.findIndex((todo) => todo.id === id);
todos.splice(index, 1);
},
{
params: t.Object({
id: t.Numeric(),
}),
}
)
)
.listen(3000);

# Hono

Hono - Ultrafast web framework for the Edges Fast, lightweight, built on web standards. Support for any JavaScript runtime.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Hono } from "hono";

const todos = [
{ id: 1, title: "Learning Bun", done: false },
{ id: 2, title: "Learing Elysia", done: false },
];

const todoRoute = new Hono();

todoRoute.get("/", (c) => {
return c.json(todos);
});

todoRoute.get("/:id", (c) => {
const id = c.req.param("id");
return c.json(todos.filter((todo) => todo.id === parseInt(id)));
});

todoRoute.post("/", async (c) => {
const { title } = await c.req.json();
const todo = {
id: todos.length + 1,
title,
done: false,
};
todos.push(todo);

c.status(201);

return c.json(todo);
});

todoRoute.put("/:id", async (c) => {
const { id } = c.req.param();
const { title, done } = await c.req.json();

const index = todos.findIndex((todo) => todo.id === parseInt(id));

if (title) {
todos[index] = {
...todos[index],
title,
done,
};
} else {
todos[index] = {
...todos[index],
done,
};
}

return c.json(todos[index]);
});

todoRoute.delete("/:id", async (c) => {
const { id } = c.req.param();

const index = todos.findIndex((todo) => todo.id === parseInt(id));
todos.splice(index, 1);

return c.json({});
});

const app = new Hono();

app.route("/todos", todoRoute);

export default app;

# VSCode REST Client Test

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
32
33
34
35
@base_url = http://localhost:3000/todos

###
GET {{base_url}}

###
GET {{base_url}}/1

###
POST {{base_url}}
Content-Type: application/json

{
"title": "Learning Hono"
}

###
PUT {{base_url}}/1
Content-Type: application/json

{
"title": "Bun!",
"done": true
}

###
PUT {{base_url}}/1
Content-Type: application/json

{
"done": true
}

###
DELETE {{base_url}}/1
Edited on