You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/essential/handler.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -204,7 +204,7 @@ new Elysia()
204
204
205
205
<Playground :elysia="handler2" />
206
206
207
-
It's recommend to use `error` inside main handler as it has better inference:
207
+
It's recommend to use `status` inside main handler as it has better inference:
208
208
209
209
- allows TypeScript to check if a return value is correctly type to response schema
210
210
- autocompletion for type narrowing base on status code
@@ -228,7 +228,7 @@ new Elysia()
228
228
.listen(3000)
229
229
```
230
230
231
-
Unlike `error` function, `set.status` cannot infer the return value type, therefore it can't check if the return value is correctly type to response schema.
231
+
Unlike `status` function, `set.status` cannot infer the return value type, therefore it can't check if the return value is correctly type to response schema.
232
232
233
233
::: tip
234
234
HTTP Status indicates the type of response. If the route handler is executed successfully without error, Elysia will return the status code 200.
Copy file name to clipboardExpand all lines: docs/essential/life-cycle.md
+33-15Lines changed: 33 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -171,22 +171,20 @@ See [scope](/essential/plugin#scope) to find out why
171
171
172
172
The order of Elysia's life-cycle code is very important.
173
173
174
-
Elysia's life-cycle event is stored as a queue, aka first-in first-out. So Elysia will **always** respect the order of code from top-to-bottom followed by the order of life-cycle events.
174
+
Because event will only apply to routes **after** it is registered.
175
+
176
+
If you put the onError before plugin, plugin will not inherit the onError event.
175
177
176
178
```typescript
177
179
import { Elysia } from'elysia'
178
180
179
181
newElysia()
180
-
.onBeforeHandle(() => {
182
+
.onBeforeHandle(() => {
181
183
console.log('1')
182
184
})
183
-
.onAfterHandle(() => {
184
-
console.log('3')
185
-
})
186
-
.get('/', () =>'hi', {
187
-
beforeHandle() {
188
-
console.log('2')
189
-
}
185
+
.get('/', () =>'hi')
186
+
.onBeforeHandle(() => {
187
+
console.log('2')
190
188
})
191
189
.listen(3000)
192
190
```
@@ -195,10 +193,32 @@ Console should log the following:
195
193
196
194
```bash
197
195
1
198
-
2
199
-
3
200
196
```
201
197
198
+
Notice that it doesn't log **3**, because the event is registered after the route so it is not applied to the route.
199
+
200
+
This also applies to the plugin.
201
+
202
+
```typescript
203
+
import { Elysia } from'elysia'
204
+
205
+
newElysia()
206
+
.onBeforeHandle(() => {
207
+
console.log('1')
208
+
})
209
+
.use(someRouter)
210
+
.onBeforeHandle(() => {
211
+
console.log('2')
212
+
})
213
+
.listen(3000)
214
+
```
215
+
216
+
In the code above, only **1** will be logged, because the event is registered after the plugin.
217
+
218
+
This is because each events will be inline into a route handler to create a true encapsulation scope and static code analysis.
219
+
220
+
The only exception is `onRequest` which is executed before the route handler so it couldn't be inlined and tied to the routing process instead.
221
+
202
222
## Request
203
223
204
224
The first life-cycle event to get executed for every new request is recieved.
@@ -769,8 +789,8 @@ If no error response is returned, the error will be returned using `error.name`.
769
789
770
790
It could either be **return** or **throw** based on your specific needs.
771
791
772
-
- If an `error` is **throw**, it will be caught by `onError` middleware.
773
-
- If an `error` is **return**, it will be **NOT** caught by `onError` middleware.
792
+
- If an `status` is **throw**, it will be caught by `onError` middleware.
793
+
- If an `status` is **return**, it will be **NOT** caught by `onError` middleware.
774
794
775
795
See the following code:
776
796
@@ -828,8 +848,6 @@ new Elysia()
828
848
})
829
849
```
830
850
831
-
Properties of `error` code is based on the properties of `error`, the said properties will be used to classify the error code.
832
-
833
851
### Local Error
834
852
835
853
Same as others life-cycle, we provide an error into an [scope](/essential/plugin.html#scope) using guard:
Copy file name to clipboardExpand all lines: docs/key-concept.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -217,6 +217,38 @@ const main = new Elysia()
217
217
218
218
As mentioned in [dependencies](#dependencies), we can use the `name` property to deduplicate the instance so it will not have any performance penalty or lifecycle duplication.
219
219
220
+
## Order of code
221
+
222
+
The order of Elysia's life-cycle code is very important.
223
+
224
+
Because event will only apply to routes **after** it is registered.
225
+
226
+
If you put the onError before plugin, plugin will not inherit the onError event.
227
+
228
+
```typescript
229
+
import { Elysia } from'elysia'
230
+
231
+
newElysia()
232
+
.onBeforeHandle(() => {
233
+
console.log('1')
234
+
})
235
+
.get('/', () =>'hi')
236
+
.onBeforeHandle(() => {
237
+
console.log('2')
238
+
})
239
+
.listen(3000)
240
+
```
241
+
242
+
Console should log the following:
243
+
244
+
```bash
245
+
1
246
+
```
247
+
248
+
Notice that it doesn't log **3**, because the event is registered after the route so it is not applied to the route.
249
+
250
+
Learn more about this in [order of code](/essential/life-cycle.html#order-of-code).
251
+
220
252
## Type Inference
221
253
Elysia has a complex type system that allows you to infer types from the instance.
0 commit comments