Skip to content

Commit bb8a9c9

Browse files
committed
📘 doc(docs): hook type guard scope
1 parent e8f5a57 commit bb8a9c9

File tree

3 files changed

+74
-111
lines changed

3 files changed

+74
-111
lines changed

docs/blog/elysia-10.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ Most of the server might not need to apply migration yourself but **heavily depe
346346
347347
For a complete migration note, see [Elysia#513](https://github.com/elysiajs/elysia/issues/513).
348348
349-
For the documentation of hook type, see [Lifecycle#hook-type](https://beta.elysiajs.com/essential/life-cycle.html#hook-type)
349+
For the documentation of hook type, see [Lifecycle#hook-type](https://beta.elysiajs.com/essential/scope.html#hook-type)
350350
351351
## Inline error
352352
Starting from Elysia 0.8, we can use the `error` function to return a response with a status code for Eden inference.

docs/essential/life-cycle.md

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -187,69 +187,4 @@ Console should log as the following:
187187
1
188188
2
189189
3
190-
```
191-
192-
## Hook type
193-
Starting from Elysia 1.0 introduce a **hook type**, to specify if the hook should be local-only, or global.
194-
195-
Elysia hook type are as the following:
196-
- local (default) - apply to only current instance and descendant only
197-
- scoped - apply to only 1 ascendant, current instance and descendants
198-
- global - apply to all instance that apply the plugin (all ascendants, current, and descendants)
199-
200-
If not specified, hook is local by default.
201-
202-
::: tip
203-
Starting from Elysia 1.0 hook is local by default while Elysia < 1.0 will be global only.
204-
205-
This is a breaking change.
206-
:::
207-
208-
To specify hook's type, add a `{ as: hookType }` to hook.
209-
210-
To apply hook to globally, we need to specify hook as global.
211-
```typescript
212-
const plugin = new Elysia()
213-
.onBeforeHandle(() => { // [!code --]
214-
.onBeforeHandle({ as: 'global' }, () => { // [!code ++]
215-
console.log('hi')
216-
})
217-
.get('/child', () => 'log hi')
218-
219-
const main = new Elysia()
220-
.use(plugin)
221-
.get('/parent', () => 'log hi')
222-
```
223-
224-
Let's create a plugin to illustrate how hook type work.
225-
226-
```typescript
227-
// ? Value base on table value provided below
228-
const type = 'local'
229-
230-
const child = new Elysia()
231-
.get('/child', () => 'hello')
232-
233-
const current = new Elysia()
234-
.onBeforeHandle({ as: type }, () => {
235-
console.log('hi')
236-
})
237-
.use(child)
238-
.get('/current', () => 'hello')
239-
240-
const parent = new Elysia()
241-
.use(current)
242-
.get('/parent', () => 'hello')
243-
244-
const main = new Elysia()
245-
.use(parent)
246-
.get('/main', () => 'hello')
247-
```
248-
249-
By changing the `type` value, the result should be as follows:
250-
251-
| type | child | current | parent | main |
252-
| ---------- | ----- | ------- | ------ | ---- |
253-
| 'local' | ✅ | ✅ | ❌ | ❌ |
254-
| 'scope' | ✅ | ✅ | ✅ | ❌ |
255-
| 'global' | ✅ | ✅ | ✅ | ✅ |
190+
```

docs/essential/scope.md

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -153,68 +153,96 @@ const main = new Elysia()
153153
.get('/parent', () => 'log hi')
154154
```
155155
156-
<!-- Probably deprecated in favor of local-first hook? Leave it here as undecided -->
157-
<!-- ## Scoped Plugin
158156
159-
Sometimes we may want to encapsulate the event and not "leak" the event out of the plugin.
157+
## Hook type
158+
Starting from Elysia 1.0 introduce a **hook type**, to specify if the hook should be local-only, or global.
160159
161-
We can accomplish that by adding `scoped: true` to the Elysia instance.
160+
Elysia hook type are as the following:
161+
- local (default) - apply to only current instance and descendant only
162+
- scoped - apply to only 1 ascendant, current instance and descendants
163+
- global - apply to all instance that apply the plugin (all ascendants, current, and descendants)
162164
165+
If not specified, hook is local by default.
166+
167+
::: tip
168+
Starting from Elysia 1.0 hook is local by default while Elysia < 1.0 will be global only.
169+
170+
This is a breaking change.
171+
:::
172+
173+
To specify hook's type, add a `{ as: hookType }` to hook.
174+
175+
To apply hook to globally, we need to specify hook as global.
163176
```typescript
164-
import { Elysia } from 'elysia'
165-
import { isHtml } from '@elysiajs/html'
177+
const plugin = new Elysia()
178+
.onBeforeHandle(() => { // [!code --]
179+
.onBeforeHandle({ as: 'global' }, () => { // [!code ++]
180+
console.log('hi')
181+
})
182+
.get('/child', () => 'log hi')
183+
184+
const main = new Elysia()
185+
.use(plugin)
186+
.get('/parent', () => 'log hi')
187+
```
188+
189+
Let's create a plugin to illustrate how hook type work.
166190
167-
const html = new Elysia({ scoped: true }) // [!code ++]
168-
.onAfterHandle(({ set, response }) => {
169-
if (isHtml(response))
170-
set.headers['Content-Type'] = 'text/html; charset=utf8'
191+
```typescript
192+
// ? Value base on table value provided below
193+
const type = 'local'
194+
195+
const child = new Elysia()
196+
.get('/child', () => 'hello')
197+
198+
const current = new Elysia()
199+
.onBeforeHandle({ as: type }, () => {
200+
console.log('hi')
171201
})
172-
.get('/inner', () => '<h1>Hello World</h1>')
202+
.use(child)
203+
.get('/current', () => 'hello')
173204

174-
new Elysia()
175-
.get('/', () => '<h1>Hello World</h1>')
176-
.use(html)
177-
.get('/outer', () => '<h1>Hello World</h1>')
178-
.listen(3000)
205+
const parent = new Elysia()
206+
.use(current)
207+
.get('/parent', () => 'hello')
208+
209+
const main = new Elysia()
210+
.use(parent)
211+
.get('/main', () => 'hello')
179212
```
180213
181-
Events that are registered in `guard`, and scoped instance will not be exposed to other instances, thus containing the event like `guard`
214+
By changing the `type` value, the result should be as follows:
182215
183-
The response should be listed as follows:
184-
| Path | Content-Type |
185-
| ----- | ----------------------- |
186-
| / | text/plain; charset=utf8 |
187-
| /inner | text/html; charset=utf8 |
188-
| /outer | text/plain; charset=utf8 |
216+
| type | child | current | parent | main |
217+
| ---------- | ----- | ------- | ------ | ---- |
218+
| 'local' | ✅ | ✅ | ❌ | ❌ |
219+
| 'scoped' | ✅ | ✅ | ✅ | ❌ |
220+
| 'global' | ✅ | ✅ | ✅ | ✅ |
189221
190-
### Encapsulation
222+
## guard
223+
Guard is a hard limit for hook type.
191224
192-
It is important to note that the scoped instance, just like `guard`, will inherit the previous events from the main instance but not expose those registered in the scope.
225+
Any life-cycle defined in `guard`, and `group` **will always** be contained in scope, even if hook type is **global**
193226
194227
```typescript
195228
import { Elysia } from 'elysia'
196229

197-
const scoped = new Elysia({ scoped: true })
198-
.onAfterHandle(() => {
199-
console.log('1')
230+
const plugin = new Elysia()
231+
.beforeHandle({ as: 'global' }, () => {
232+
console.log('hi')
200233
})
201-
.get('/inner', () => 'hi')
202234

203-
new Elysia()
204-
.onAfterHandle(() => {
205-
console.log('2')
206-
})
207-
.use(scoped)
208-
.get('/outer', () => 'hi')
235+
const app = new Elysia()
236+
.guard(app => app
237+
.use(plugin)
238+
.get('/inner', () => 'inner')
239+
)
240+
.get('/outer', () => 'outer')
209241
.listen(3000)
210242
```
211243
212-
The response should be listed as follows:
213-
| Path | Log |
214-
| ----- | ----------------------- |
215-
| /inner | 1, 2 |
216-
| /outer | 2 |
217-
218-
Scope and guard only prevent the event from being inherited but the scope itself will inherit the events.
219-
220-
--->
244+
Evaluating the route, should logs as follows:
245+
| route | log |
246+
| ----------- | ---- |
247+
| /inner | ❌ |
248+
| /outer | ✅ |

0 commit comments

Comments
 (0)