@@ -46,9 +46,71 @@ const demo3 = new Elysia()
46
46
.get (' /outer' , () => ' outer' )
47
47
</script >
48
48
49
- As mentioned, global Lifecycle and Schema apply to every route after the registration, allowing the use of multiple routes .
49
+ By default, hook and schema is scope to current instance only not global .
50
50
51
- However, in a real-world scenario, the global event is hard to trace and control properly. This is why Elysia has an encapsulation scope to ensure that the event will only apply to a certain group of routes.
51
+ Elysia has an encapsulation scope for better versatility control of life-cycle.
52
+
53
+ ## Hook type
54
+ Hook type is to specify the scope of hook whether is should be encapsulated or global.
55
+
56
+ Elysia hook type are as the following:
57
+ - ** local** (default) - apply to only current instance and descendant only
58
+ - ** scoped** - apply to parent, current instance and descendants
59
+ - ** global** - apply to all instance that apply the plugin (all parents, current, and descendants)
60
+
61
+ If not specified, hook is local by default.
62
+
63
+ To specify hook's type, add a ` { as: hookType } ` to hook.
64
+
65
+ To apply hook to globally, we need to specify hook as global.
66
+ ``` typescript twoslash
67
+ import { Elysia } from ' elysia'
68
+
69
+ const plugin = new Elysia ()
70
+ .onBeforeHandle ({ as: ' global' }, () => { // [!code ++]
71
+ console .log (' hi' )
72
+ })
73
+ .get (' /child' , () => ' log hi' )
74
+
75
+ const main = new Elysia ()
76
+ .use (plugin )
77
+ .get (' /parent' , () => ' log hi' )
78
+ ```
79
+
80
+ Let's create a plugin to illustrate how hook type work.
81
+
82
+ ``` typescript twoslash
83
+ import { Elysia } from ' elysia'
84
+
85
+ // ? Value base on table value provided below
86
+ const type = ' local'
87
+
88
+ const child = new Elysia ()
89
+ .get (' /child' , () => ' hello' )
90
+
91
+ const current = new Elysia ()
92
+ .onBeforeHandle ({ as: type }, () => {
93
+ console .log (' hi' )
94
+ })
95
+ .use (child )
96
+ .get (' /current' , () => ' hello' )
97
+
98
+ const parent = new Elysia ()
99
+ .use (current )
100
+ .get (' /parent' , () => ' hello' )
101
+
102
+ const main = new Elysia ()
103
+ .use (parent )
104
+ .get (' /main' , () => ' hello' )
105
+ ```
106
+
107
+ By changing the ` type ` value, the result should be as follows:
108
+
109
+ | type | child | current | parent | main |
110
+ | ---------- | ----- | ------- | ------ | ---- |
111
+ | 'local' | ✅ | ✅ | ❌ | ❌ |
112
+ | 'scoped' | ✅ | ✅ | ✅ | ❌ |
113
+ | 'global' | ✅ | ✅ | ✅ | ✅ |
52
114
53
115
## Guard
54
116
@@ -119,6 +181,36 @@ new Elysia()
119
181
.listen (3000 )
120
182
```
121
183
184
+ ### Guard scope
185
+ Guard is a hard limit for hook type.
186
+
187
+ Any life-cycle defined in ` guard ` , and ` group ` ** will always** be contained in scope, even if hook type is ** global**
188
+
189
+ ``` typescript twoslash
190
+ import { Elysia } from ' elysia'
191
+
192
+ const plugin = new Elysia ()
193
+ .onBeforeHandle ({ as: ' global' }, () => {
194
+ return ' overwrite'
195
+ })
196
+
197
+ const app = new Elysia ()
198
+ .guard (app => app
199
+ .use (plugin )
200
+ .get (' /inner' , () => ' inner' )
201
+ )
202
+ .get (' /outer' , () => ' outer' )
203
+ .listen (3000 )
204
+ ```
205
+
206
+ <Playground :elysia =" demo3 " />
207
+
208
+ Evaluating the route, should logs as follows:
209
+ | route | response |
210
+ | ----------- | --------- |
211
+ | /inner | overwrite |
212
+ | /outer | outer |
213
+
122
214
## Grouped Guard
123
215
124
216
We can use a group with prefixes by providing 3 parameters to the group.
@@ -218,101 +310,3 @@ const main = new Elysia()
218
310
```
219
311
220
312
<Playground :elysia =" demo2 " />
221
-
222
- ## Hook type
223
- Starting from Elysia 1.0 introduce a ** hook type** , to specify if the hook should be local-only, or global.
224
-
225
- Elysia hook type are as the following:
226
- - local (default) - apply to only current instance and descendant only
227
- - scoped - apply to only 1 ascendant, current instance and descendants
228
- - global - apply to all instance that apply the plugin (all ascendants, current, and descendants)
229
-
230
- If not specified, hook is local by default.
231
-
232
- ::: tip
233
- Starting from Elysia 1.0 hook is local by default while Elysia < 1.0 will be global only.
234
-
235
- This is a breaking change.
236
- :::
237
-
238
- To specify hook's type, add a ` { as: hookType } ` to hook.
239
-
240
- To apply hook to globally, we need to specify hook as global.
241
- ``` typescript twoslash
242
- import { Elysia } from ' elysia'
243
-
244
- const plugin = new Elysia ()
245
- .onBeforeHandle ({ as: ' global' }, () => {
246
- console .log (' hi' )
247
- })
248
- .get (' /child' , () => ' log hi' )
249
-
250
- const main = new Elysia ()
251
- .use (plugin )
252
- .get (' /parent' , () => ' log hi' )
253
- ```
254
-
255
- Let's create a plugin to illustrate how hook type work.
256
-
257
- ``` typescript twoslash
258
- import { Elysia } from ' elysia'
259
-
260
- // ? Value base on table value provided below
261
- const type = ' local'
262
-
263
- const child = new Elysia ()
264
- .get (' /child' , () => ' hello' )
265
-
266
- const current = new Elysia ()
267
- .onBeforeHandle ({ as: type }, () => {
268
- console .log (' hi' )
269
- })
270
- .use (child )
271
- .get (' /current' , () => ' hello' )
272
-
273
- const parent = new Elysia ()
274
- .use (current )
275
- .get (' /parent' , () => ' hello' )
276
-
277
- const main = new Elysia ()
278
- .use (parent )
279
- .get (' /main' , () => ' hello' )
280
- ```
281
-
282
- By changing the ` type ` value, the result should be as follows:
283
-
284
- | type | child | current | parent | main |
285
- | ---------- | ----- | ------- | ------ | ---- |
286
- | 'local' | ✅ | ✅ | ❌ | ❌ |
287
- | 'scoped' | ✅ | ✅ | ✅ | ❌ |
288
- | 'global' | ✅ | ✅ | ✅ | ✅ |
289
-
290
- ## guard
291
- Guard is a hard limit for hook type.
292
-
293
- Any life-cycle defined in ` guard ` , and ` group ` ** will always** be contained in scope, even if hook type is ** global**
294
-
295
- ``` typescript twoslash
296
- import { Elysia } from ' elysia'
297
-
298
- const plugin = new Elysia ()
299
- .onBeforeHandle ({ as: ' global' }, () => {
300
- return ' overwrite'
301
- })
302
-
303
- const app = new Elysia ()
304
- .guard (app => app
305
- .use (plugin )
306
- .get (' /inner' , () => ' inner' )
307
- )
308
- .get (' /outer' , () => ' outer' )
309
- .listen (3000 )
310
- ```
311
-
312
- <Playground :elysia =" demo3 " />
313
-
314
- Evaluating the route, should logs as follows:
315
- | route | response |
316
- | ----------- | --------- |
317
- | /inner | overwrite |
318
- | /outer | outer |
0 commit comments