Skip to content

Commit 17287db

Browse files
committed
🎉 feat: add twoslash
1 parent c926ca5 commit 17287db

Some content is hidden

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

56 files changed

+1153
-466
lines changed

bun.lockb

43.5 KB
Binary file not shown.

components/midori/e2e-type-safety.vue

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,31 @@
1414
Move fast and break nothing like tRPC.
1515
</p>
1616

17-
<a class="text-lg font-medium bg-blue-50 text-blue-500 dark:text-blue-400 dark:bg-blue-500/20 mr-auto md:mx-auto px-4 py-2 rounded-xl mt-6"
17+
<p class="text-lg font-medium bg-green-100/50 text-green-500 dark:text-green-400 dark:bg-green-500/20 mr-auto md:mx-auto px-4 py-2 rounded-xl mt-6">
18+
Hover code below to see type definition
19+
</p>
20+
21+
<!-- <a class="text-lg font-medium bg-blue-50 text-blue-500 dark:text-blue-400 dark:bg-blue-500/20 mr-auto md:mx-auto px-4 py-2 rounded-xl mt-6"
1822
href="/eden/overview">
1923
See how it works
20-
</a>
24+
</a> -->
2125

2226
<section class="flex flex-col lg:flex-row gap-8 w-full max-w-5xl my-8">
2327
<div class="w-full !text-base !font-mono rounded-xl">
2428
<slot name="server"></slot>
2529
</div>
2630
<div class="relative w-full !text-base !font-mono rounded-xl">
2731
<slot name="client"></slot>
28-
<div class="absolute p-1 rounded bg-red-400/25" style="
29-
top: 13.2em;
30-
left: 5.9em;
32+
<!-- <div class="absolute p-1 rounded bg-red-400/25" style="
33+
top: 13.1em;
34+
left: 6.5em;
3135
width: 2.2em;
3236
height: 1.375em;
3337
" />
3438
<p class="absolute px-3 py-1.5 rounded bg-white dark:bg-gray-700 border dark:border-gray-600"
3539
style="top: 15.25em; left: 3.25em">
3640
Type 'string' is not assignable to type 'number'
37-
</p>
41+
</p> -->
3842
</div>
3943
</section>
4044
</section>

components/midori/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ const isDark = useDark()
5858
</template>
5959
</E2ETypeSafety>
6060
<Plugins />
61-
<Suspense>
61+
<!-- <Suspense>
6262
<Editor />
6363
<template #fallback>
6464
<video muted autoplay loop>
6565
<source src="/assets/elysia.mp4" />
6666
</video>
6767
</template>
68-
</Suspense>
68+
</Suspense> -->
6969
<Community />
7070
<Sponsor />
7171
<hr class="border-gray-200 dark:border-gray-600" />

docs/.vitepress/config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineConfig } from 'vitepress'
22

3+
import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
4+
35
const description =
46
'Ergonomic Framework for Humans. TypeScript framework supercharged by Bun with End - to - End Type Safety, unified type system and outstanding developer experience'
57

@@ -13,7 +15,8 @@ export default defineConfig({
1315
theme: {
1416
light: 'github-light',
1517
dark: 'github-dark'
16-
}
18+
},
19+
codeTransformers: [transformerTwoslash()]
1720
},
1821
// ![INFO] uncomment for support hot reload on WSL - https://github.com/vitejs/vite/issues/1153#issuecomment-785467271
1922
vite: {
@@ -344,13 +347,13 @@ export default defineConfig({
344347
{
345348
text: 'Legacy (Treaty 1)',
346349
link: '/eden/treaty/legacy.md'
347-
}
350+
}
348351
]
349352
},
350353
{
351354
text: 'Eden Fetch',
352355
link: '/eden/fetch.md'
353-
},
356+
}
354357
]
355358
},
356359
{

docs/.vitepress/theme/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
// https://vitepress.dev/guide/custom-theme
22
import DefaultTheme from 'vitepress/theme'
33
import Layout from './layout.vue'
4+
5+
import TwoslashFloatingVue from '@shikijs/vitepress-twoslash/client'
6+
import '@shikijs/vitepress-twoslash/style.css'
7+
48
import './custom.css'
5-
import { Theme } from 'vitepress'
9+
10+
import type { EnhanceAppContext } from 'vitepress'
11+
import type { Theme } from 'vitepress'
612

713
export default {
814
extends: DefaultTheme,
9-
Layout
15+
Layout,
16+
enhanceApp({ app }: EnhanceAppContext) {
17+
app.use(TwoslashFloatingVue)
18+
}
1019
} satisfies Theme

docs/a.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Elysia } from 'elysia'
2+
3+
new Elysia()
4+
.get('/', ({ query: { name } }) => {
5+
return (
6+
<h1>hello {name}</h1>
7+
)
8+
})

docs/at-glance.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Designed with simplicity and type safety in mind with familiar API with extensiv
3838

3939
Here's a simple hello world in Elysia.
4040

41-
```typescript
41+
```typescript twoslash
4242
import { Elysia } from 'elysia'
4343

4444
new Elysia()
@@ -101,7 +101,7 @@ Elysia's Type System is fine-tuned to infer your code into type automatically wi
101101

102102
Take a look at this example:
103103

104-
```typescript
104+
```typescript twoslash
105105
import { Elysia } from 'elysia'
106106

107107
new Elysia()
@@ -133,7 +133,7 @@ To take a step further, Elysia provide **Elysia.t**, a schema builder to validat
133133

134134
Let's modify the previous code to accept only a numeric value instead of a string.
135135

136-
```typescript
136+
```typescript twoslash
137137
import { Elysia, t } from 'elysia'
138138

139139
new Elysia()
@@ -147,6 +147,10 @@ new Elysia()
147147

148148
This code ensures that our path parameter **id**, will always be a numeric string and then transform to a number automatically in both runtime and compile-time (type-level).
149149

150+
::: tip
151+
Hover over "id" in the above code snippet to see a type definition.
152+
:::
153+
150154
With Elysia schema builder, we can ensure type safety like a strong-typed language with a single source of truth.
151155

152156
## Standard
@@ -155,7 +159,7 @@ Elysia adopts many standards by default, like OpenAPI, and WinterCG compliance,
155159

156160
For instance, as Elysia adopts OpenAPI by default, generating a documentation with Swagger is as easy as adding a one-liner:
157161

158-
```typescript
162+
```typescript twoslash
159163
import { Elysia, t } from 'elysia'
160164
import { swagger } from '@elysiajs/swagger'
161165

@@ -177,7 +181,7 @@ With Elysia, type safety is not only limited to server-side only.
177181

178182
With Elysia, you can synchronize your type with your frontend team automatically like tRPC, with Elysia's client library, "Eden".
179183

180-
```typescript
184+
```typescript twoslash
181185
import { Elysia, t } from 'elysia'
182186
import { swagger } from '@elysiajs/swagger'
183187

@@ -195,12 +199,27 @@ export type App = typeof app
195199
196200
And on your client-side:
197201
198-
```typescript
202+
```typescript twoslash
203+
// @filename: server.ts
204+
import { Elysia, t } from 'elysia'
205+
206+
const app = new Elysia()
207+
.get('/user/:id', ({ params: { id } }) => id, {
208+
params: t.Object({
209+
id: t.Numeric()
210+
})
211+
})
212+
.listen(3000)
213+
214+
export type App = typeof app
215+
216+
// @filename: client.ts
217+
// ---cut---
199218
// client.ts
200219
import { treaty } from '@elysiajs/eden'
201220
import type { App } from './server'
202221

203-
const app = treaty<App>('http://localhost:3000')
222+
const app = treaty<App>('localhost:3000')
204223

205224
// Get data from /user/617
206225
const { data } = await app.user({ id: 617 }).get()

docs/blog/elysia-07.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Elysia handle Cookie's secret rotation automatically, so if you have to migrate
192192
```typescript
193193
new Elysia({
194194
cookie: {
195-
secret: ['Vengeance will be mine', 'Fischl von Luftschloss Narfidort']
195+
secrets: ['Vengeance will be mine', 'Fischl von Luftschloss Narfidort']
196196
}
197197
})
198198
```

docs/eden/fetch.md

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ With Eden Fetch can interact with Elysia server in a type-safe manner using Fetc
2222
---
2323

2424
First export your existing Elysia server type:
25-
```typescript
25+
```typescript twoslash
2626
// server.ts
2727
import { Elysia, t } from 'elysia'
2828

2929
const app = new Elysia()
30-
.get('/', () => 'Hi Elysia')
30+
.get('/hi', () => 'Hi Elysia')
3131
.get('/id/:id', ({ params: { id } }) => id)
3232
.post('/mirror', ({ body }) => body, {
3333
body: t.Object({
@@ -41,15 +41,32 @@ export type App = typeof app
4141
```
4242
4343
Then import the server type, and consume the Elysia API on client:
44-
```typescript
44+
```typescript twoslash
45+
// @filename: server.ts
46+
import { Elysia, t } from 'elysia'
47+
48+
const app = new Elysia()
49+
.get('/hi', () => 'Hi Elysia')
50+
.get('/id/:id', ({ params: { id } }) => id)
51+
.post('/mirror', ({ body }) => body, {
52+
body: t.Object({
53+
id: t.Number(),
54+
name: t.String()
55+
})
56+
})
57+
.listen(3000)
58+
59+
export type App = typeof app
60+
// @filename: client.ts
61+
// ---cut---
4562
// client.ts
4663
import { edenFetch } from '@elysiajs/eden'
4764
import type { App } from './server'
4865

49-
const fetch = edenFetch<App>('http://localhost:')
66+
const fetch = edenFetch<App>('http://localhost:3000')
5067

5168
// response type: 'Hi Elysia'
52-
const pong = await fetch('/', {})
69+
const pong = await fetch('/hi', {})
5370

5471
// response type: 1895
5572
const id = await fetch('/id/:id', {
@@ -70,31 +87,56 @@ const nendoroid = await fetch('/mirror', {
7087

7188
## Error Handling
7289
You can handle errors the same way as Eden Treaty:
73-
```typescript
90+
```typescript twoslash
91+
// @filename: server.ts
92+
import { Elysia, t } from 'elysia'
93+
94+
const app = new Elysia()
95+
.get('/hi', () => 'Hi Elysia')
96+
.get('/id/:id', ({ params: { id } }) => id)
97+
.post('/mirror', ({ body }) => body, {
98+
body: t.Object({
99+
id: t.Number(),
100+
name: t.String()
101+
})
102+
})
103+
.listen(3000)
104+
105+
export type App = typeof app
106+
107+
// @filename: client.ts
108+
// ---cut---
109+
// client.ts
110+
import { edenFetch } from '@elysiajs/eden'
111+
import type { App } from './server'
112+
113+
const fetch = edenFetch<App>('http://localhost:3000')
114+
74115
// response type: { id: 1895, name: 'Skadi' }
75-
const { data: nendoroid, error } = app.mirror.post({
76-
id: 1895,
77-
name: 'Skadi'
116+
const { data: nendoroid, error } = await fetch('/mirror', {
117+
method: 'POST',
118+
body: {
119+
id: 1895,
120+
name: 'Skadi'
121+
}
78122
})
79123

80124
if(error) {
81125
switch(error.status) {
82126
case 400:
83127
case 401:
84-
warnUser(error.value)
128+
throw error.value
85129
break
86130

87131
case 500:
88132
case 502:
89-
emergencyCallDev(error.value)
133+
throw error.value
90134
break
91135

92136
default:
93-
reportError(error.value)
137+
throw error.value
94138
break
95139
}
96-
97-
throw error
98140
}
99141

100142
const { id, name } = nendoroid

0 commit comments

Comments
 (0)