Skip to content

Commit 763a6ea

Browse files
committed
📘 doc: expand more in order of code
1 parent ce7333f commit 763a6ea

File tree

4 files changed

+68
-18
lines changed

4 files changed

+68
-18
lines changed

docs/essential/handler.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ new Elysia()
204204
205205
<Playground :elysia="handler2" />
206206
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:
208208
209209
- allows TypeScript to check if a return value is correctly type to response schema
210210
- autocompletion for type narrowing base on status code
@@ -228,7 +228,7 @@ new Elysia()
228228
.listen(3000)
229229
```
230230
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.
232232
233233
::: tip
234234
HTTP Status indicates the type of response. If the route handler is executed successfully without error, Elysia will return the status code 200.

docs/essential/life-cycle.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,20 @@ See [scope](/essential/plugin#scope) to find out why
171171

172172
The order of Elysia's life-cycle code is very important.
173173

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.
175177

176178
```typescript
177179
import { Elysia } from 'elysia'
178180

179181
new Elysia()
180-
.onBeforeHandle(() => {
182+
.onBeforeHandle(() => {
181183
console.log('1')
182184
})
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')
190188
})
191189
.listen(3000)
192190
```
@@ -195,10 +193,32 @@ Console should log the following:
195193

196194
```bash
197195
1
198-
2
199-
3
200196
```
201197

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+
new Elysia()
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+
202222
## Request
203223

204224
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`.
769789

770790
It could either be **return** or **throw** based on your specific needs.
771791

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.
774794

775795
See the following code:
776796

@@ -828,8 +848,6 @@ new Elysia()
828848
})
829849
```
830850

831-
Properties of `error` code is based on the properties of `error`, the said properties will be used to classify the error code.
832-
833851
### Local Error
834852

835853
Same as others life-cycle, we provide an error into an [scope](/essential/plugin.html#scope) using guard:

docs/essential/validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ This is an Elysia-specific feature, allowing us to make a field optional.
674674

675675
There are two ways to provide a custom error message when the validation fails:
676676

677-
1. Inline `error` property
677+
1. Inline `status` property
678678
2. Using [onError](/essential/life-cycle.html#on-error) event
679679

680680
### Error Property

docs/key-concept.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,38 @@ const main = new Elysia()
217217

218218
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.
219219

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+
new Elysia()
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+
220252
## Type Inference
221253
Elysia has a complex type system that allows you to infer types from the instance.
222254

0 commit comments

Comments
 (0)