Skip to content

Commit e7f7b61

Browse files
committed
📘 doc: add after handle
1 parent 16bb4d0 commit e7f7b61

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

docs/.vitepress/config.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ export default defineConfig({
163163
{
164164
text: 'Patterns',
165165
items: [
166+
{
167+
text: 'After Handle',
168+
link: '/patterns/after-handle'
169+
},
166170
{
167171
text: 'Method Chaining',
168172
link: '/patterns/method-chaining'
@@ -215,14 +219,14 @@ export default defineConfig({
215219
text: 'Body Parser',
216220
link: '/patterns/body-parser'
217221
},
218-
{
219-
text: 'Mount',
220-
link: '/patterns/mount'
221-
},
222222
{
223223
text: 'Trace',
224224
link: '/patterns/trace'
225225
},
226+
{
227+
text: 'Mount',
228+
link: '/patterns/mount'
229+
},
226230
{
227231
text: 'End-to-End Type Safety',
228232
link: '/patterns/end-to-end-type-safety'

docs/patterns/after-handle.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: After Handle - ElysiaJS
3+
head:
4+
- - meta
5+
- property: 'og:title'
6+
content: After Handle - ElysiaJS
7+
8+
- - meta
9+
- name: 'description'
10+
content: After Handle can transform a response into a new response. This is like a `transform` but before returning response to user, you can remap the returned value into a new value or force the value to early return.
11+
12+
- - meta
13+
- property: 'og:description'
14+
content: After Handle can transform a response into a new response. This is like a `transform` but before returning response to user, you can remap the returned value into a new value or force the value to early return.
15+
---
16+
17+
# After Handle
18+
**After Handle** can transform a response into a new response.
19+
20+
**After Handle** is like `transform`, but instead of transforming incoming request, it is use to transform a response returned from main handler.
21+
22+
Allowing you to remap the returned value into a new value or force the value to early return.
23+
24+
To use `afterHandle`, you can either use in life-cycle event and inline hook.
25+
```typescript
26+
import { Elysia } from 'elysia'
27+
28+
new Elysia()
29+
.onAfterHandle((context) => {
30+
if(typeof context.response === 'number')
31+
context.response += 1
32+
})
33+
.get('/', () => 1, {
34+
afterHandle(context) {
35+
if(typeof context.response === 'number')
36+
context.response += 1
37+
}
38+
})
39+
.listen(3000)
40+
```
41+
42+
The above code will return `3` as a result.
43+
44+
## Early return
45+
Sometime, you may want to prevent the on-going chain of **afterHandle**, just like **beforeHandle**, if you return the value in a **afterHandle** function, it will be an early return and skip the rest of **afterHandle**
46+
47+
```typescript
48+
import { Elysia } from 'elysia'
49+
50+
new Elysia()
51+
.onAfterHandle(({ response }) => {
52+
if(typeof response === 'number')
53+
return response + 1
54+
})
55+
.get('/', () => 1, {
56+
afterHandle(context) {
57+
if(typeof context.response === 'number')
58+
context.response += 1
59+
}
60+
})
61+
.listen(3000)
62+
```
63+
64+
The above code will return `2` as a result, because the **onAfterHandle** is early returned

0 commit comments

Comments
 (0)