Skip to content

Commit 17aa6d2

Browse files
committed
🎉 feat: add elysia playground
1 parent 31130a9 commit 17aa6d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+777
-411
lines changed

bun.lockb

-316 Bytes
Binary file not shown.

components/nearl/kyuukurarin.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<iframe width="560" height="315" src="https://www.youtube.com/embed/2b1IexhKPz4?si=VlzjeVXV8BTPNTKs" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

components/nearl/playground.vue

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<template>
2+
<article class="mockup-browser dark:bg-slate-800 border dark:border-slate-700 playground">
3+
<div class="mockup-browser-toolbar">
4+
<div class="input flex dark:!bg-slate-700">
5+
<span>localhost</span>
6+
<select
7+
name="route"
8+
class="flex-1 rounded text-blue-500 font-bold bg-blue-500/10 hover:bg-blue-500/20 dark:bg-blue-500/25 dark:hover:bg-blue-500/40 border border-solid border-blue-500/50 dark:border-blue-500/75 px-1 cursor-pointer transition-colors"
9+
v-model="current"
10+
>
11+
<option
12+
v-for="{ method, path } in routes"
13+
:key="method + '_' + path"
14+
:value="{ method, path }"
15+
>
16+
{{ alias && path in alias ? alias[path] : path }}
17+
</option>
18+
</select>
19+
</div>
20+
<p
21+
class="!my-0 font-bold pl-2 min-w-[5ch] text-right"
22+
:class="
23+
current.method === 'GET'
24+
? 'text-green-600'
25+
: 'text-blue-500'
26+
"
27+
>
28+
{{ current.method }}
29+
</p>
30+
</div>
31+
<section
32+
class="flex justify-start items-stretch w-full h-full px-4 whitespace-pre-wrap pb-3"
33+
style="min-height: 16rem"
34+
>
35+
{{ response }}
36+
</section>
37+
</article>
38+
<!-- <ul class="flex flex-col">
39+
<li v-for="{ method, path } in routes" :key="method + '_' + path">
40+
<button @click="current = { method, path }">
41+
{{ method }} {{ path }}
42+
</button>
43+
</li>
44+
</ul> -->
45+
</template>
46+
47+
<script setup lang="ts">
48+
import { defineProps, ref, watch } from 'vue'
49+
import type { Elysia } from 'elysia'
50+
51+
const { elysia, mock = {}, alias = {} } = defineProps<{
52+
elysia: Elysia<any, any, any, any, any, any>
53+
mock?: Record<string, Record<string, string>>
54+
alias?: Record<string, string>
55+
}>()
56+
57+
const routes = elysia?.router.history
58+
59+
const current = ref({
60+
method: routes?.[0]?.method ?? 'get',
61+
path: routes?.[0]?.path ?? '/'
62+
})
63+
64+
const response = ref('')
65+
66+
const compute = async () => {
67+
const { method, path } = current.value
68+
69+
if (mock && path in mock && method in mock[path]) {
70+
response.value = mock[path][method]
71+
return
72+
}
73+
74+
const res = await elysia
75+
.handle(
76+
new Request('http://elysiajs.com' + path, {
77+
method
78+
})
79+
)
80+
.then((x) => x.text())
81+
82+
response.value = res
83+
}
84+
85+
compute()
86+
watch([current], compute)
87+
</script>
88+
89+
<style global>
90+
.playground {
91+
box-shadow: 0 7px 25px rgba(0,0,0,.0625);
92+
}
93+
94+
@media screen and (max-width: 567.9px) {
95+
.playground > .mockup-browser-toolbar {
96+
padding-left: 0.7em;
97+
padding-right: 0.7em;
98+
}
99+
100+
.playground > .mockup-browser-toolbar::before {
101+
display: none !important;
102+
}
103+
}
104+
</style>

docs/api/constructor.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ For Object, `listen` accepts the same value as `Bun.serve`, you can provide any
4141
```typescript
4242
// ✅ This is fine
4343
new Elysia()
44-
.listen(8080)
44+
.listen(3000)
4545

4646
// ✅ This is fine
4747
new Elysia()
4848
.listen({
49-
port: 8080,
49+
port: 3000,
5050
hostname: '0.0.0.0'
5151
})
5252
```
@@ -59,7 +59,7 @@ For providing WebSocket, please use [`WebSocket`](/patterns/websocket)
5959
You can provide a custom port from ENV by using `process.env`
6060
```typescript
6161
new Elysia()
62-
.listen(process.env.PORT ?? 8080)
62+
.listen(process.env.PORT ?? )
6363
```
6464

6565
## Retrieve Port
@@ -69,7 +69,7 @@ Using callback in `.listen`
6969

7070
```typescript
7171
const app = new Elysia()
72-
.listen(8080, ({ hostname, port }) => {
72+
.listen(, ({ hostname, port }) => {
7373
console.log(`Running at http://${hostname}:${port}`)
7474
})
7575

docs/at-glance.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ head:
1717
<script setup>
1818
import Card from '../components/nearl/card.vue'
1919
import Deck from '../components/nearl/card-deck.vue'
20+
import Playground from '../components/nearl/playground.vue'
21+
22+
import { Elysia } from 'elysia'
23+
24+
const demo1 = new Elysia()
25+
.get('/', () => 'Hello Elysia')
26+
.get('/user/:id', ({ params: { id }}) => id)
27+
.post('/form', ({ body }) => body)
28+
29+
const demo2 = new Elysia()
30+
.get('/user/:id', ({ params: { id }}) => id)
31+
.get('/user/abc', () => 'abc')
2032
</script>
2133

2234
# At glance
@@ -30,14 +42,37 @@ Here's a simple hello world in Elysia.
3042
import { Elysia } from 'elysia'
3143

3244
new Elysia()
33-
.get('/', () => 'hi')
45+
.get('/', () => 'Hello Elysia')
3446
.get('/user/:id', ({ params: { id }}) => id)
3547
.post('/form', ({ body }) => body)
36-
.listen(8080)
48+
.listen(3000)
3749
```
3850

3951
Navigate to [localhost:3000](http://localhost:3000/) and it should show 'Hello Elysia' as a result.
4052

53+
<Playground
54+
:elysia="demo1"
55+
:alias="{
56+
'/user/:id': '/user/1'
57+
}"
58+
:mock="{
59+
'/user/:id': {
60+
GET: '1'
61+
},
62+
'/form': {
63+
POST: JSON.stringify({
64+
hello: 'Elysia'
65+
})
66+
}
67+
}"
68+
/>
69+
70+
::: tip
71+
Click on path highlight in blue to change path to preview a response.
72+
73+
Elysia can runs on browser and the result you see are actually run using Elysia.
74+
:::
75+
4176
## Performance
4277

4378
Building on Bun and extensive optimization like Static Code Analysis allows Elysia to generate optimized code on the fly.
@@ -76,6 +111,18 @@ new Elysia()
76111

77112
The above code create a path parameter "id", the value that replace `:id` will be passed to `params.id` both in runtime and type without manual type declaration.
78113

114+
<Playground
115+
:elysia="demo2"
116+
:alias="{
117+
'/user/:id': '/user/123'
118+
}"
119+
:mock="{
120+
'/user/:id': {
121+
GET: '123'
122+
},
123+
}"
124+
/>
125+
79126
Elysia's goal is to help you write less TypeScript and focus more on Business logic. Let the complex type be handled by the framework.
80127

81128
TypeScript is not needed to use Elysia, but it's recommended to use Elysia with TypeScript.

docs/blog/elysia-04.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ new Elysia()
142142
})
143143
}
144144
})
145-
.listen(8080)
145+
.listen(3000)
146146
```
147147

148148
Now you can create a validation error for your API not limited to only the first error.

docs/concept/schema.md

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)