fastify 5.2.1 → 5.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +22 -0
- package/LICENSE +1 -1
- package/PROJECT_CHARTER.md +7 -7
- package/README.md +24 -25
- package/SPONSORS.md +2 -0
- package/docs/Guides/Benchmarking.md +4 -4
- package/docs/Guides/Database.md +1 -1
- package/docs/Guides/Delay-Accepting-Requests.md +10 -10
- package/docs/Guides/Ecosystem.md +5 -1
- package/docs/Guides/Fluent-Schema.md +1 -1
- package/docs/Guides/Getting-Started.md +9 -5
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +1 -1
- package/docs/Guides/Migration-Guide-V5.md +12 -2
- package/docs/Guides/Plugins-Guide.md +6 -6
- package/docs/Guides/Serverless.md +14 -48
- package/docs/Guides/Style-Guide.md +2 -2
- package/docs/Guides/Testing.md +2 -2
- package/docs/Guides/Write-Plugin.md +2 -3
- package/docs/Reference/ContentTypeParser.md +58 -78
- package/docs/Reference/Decorators.md +50 -60
- package/docs/Reference/Encapsulation.md +28 -33
- package/docs/Reference/Errors.md +50 -53
- package/docs/Reference/HTTP2.md +7 -7
- package/docs/Reference/Hooks.md +31 -30
- package/docs/Reference/LTS.md +10 -15
- package/docs/Reference/Lifecycle.md +19 -24
- package/docs/Reference/Logging.md +59 -56
- package/docs/Reference/Middleware.md +19 -19
- package/docs/Reference/Plugins.md +55 -71
- package/docs/Reference/Principles.md +25 -30
- package/docs/Reference/Reply.md +11 -10
- package/docs/Reference/Request.md +89 -98
- package/docs/Reference/Routes.md +108 -128
- package/docs/Reference/Server.md +18 -16
- package/docs/Reference/Type-Providers.md +19 -21
- package/docs/Reference/TypeScript.md +1 -18
- package/docs/Reference/Validation-and-Serialization.md +134 -159
- package/docs/Reference/Warnings.md +22 -25
- package/fastify.js +1 -1
- package/lib/contentTypeParser.js +7 -8
- package/lib/error-handler.js +14 -12
- package/lib/headRoute.js +4 -2
- package/lib/pluginUtils.js +4 -2
- package/lib/server.js +5 -0
- package/lib/validation.js +1 -1
- package/lib/warnings.js +9 -0
- package/lib/wrapThenable.js +8 -1
- package/package.json +10 -10
- package/test/build/error-serializer.test.js +2 -1
- package/test/bundler/esbuild/package.json +1 -1
- package/test/close.test.js +125 -108
- package/test/custom-parser-async.test.js +34 -36
- package/test/genReqId.test.js +125 -174
- package/test/has-route.test.js +1 -3
- package/test/internals/content-type-parser.test.js +1 -1
- package/test/issue-4959.test.js +84 -0
- package/test/listen.1.test.js +37 -34
- package/test/listen.2.test.js +47 -40
- package/test/listen.3.test.js +28 -32
- package/test/listen.4.test.js +61 -45
- package/test/listen.5.test.js +23 -0
- package/test/register.test.js +55 -50
- package/test/request-error.test.js +114 -94
- package/test/route-shorthand.test.js +36 -32
- package/test/server.test.js +0 -175
- package/test/stream.5.test.js +35 -33
- package/test/throw.test.js +87 -91
- package/test/toolkit.js +32 -0
- package/test/trust-proxy.test.js +23 -23
- package/test/types/instance.test-d.ts +1 -0
- package/test/upgrade.test.js +32 -30
- package/types/instance.d.ts +4 -0
package/docs/Reference/Routes.md
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Routes
|
|
4
4
|
|
|
5
|
-
The route methods
|
|
6
|
-
|
|
7
|
-
declaration.
|
|
5
|
+
The route methods configure the endpoints of the application. Routes can be
|
|
6
|
+
declared using the shorthand method or the full declaration.
|
|
8
7
|
|
|
9
8
|
- [Full declaration](#full-declaration)
|
|
10
9
|
- [Routes options](#routes-options)
|
|
@@ -138,11 +137,11 @@ fastify.route(options)
|
|
|
138
137
|
|
|
139
138
|
* `reply` is defined in [Reply](./Reply.md).
|
|
140
139
|
|
|
141
|
-
|
|
142
|
-
`preHandler`, `preSerialization`, `onSend`, and `onResponse`
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
hook](./Hooks.md#respond-to-a-request-from-a-hook).
|
|
140
|
+
> 🛈 Note: The documentation for `onRequest`, `preParsing`, `preValidation`,
|
|
141
|
+
> `preHandler`, `preSerialization`, `onSend`, and `onResponse` is detailed in
|
|
142
|
+
> [Hooks](./Hooks.md). To send a response before the request is handled by the
|
|
143
|
+
> `handler`, see [Respond to a request from
|
|
144
|
+
> a hook](./Hooks.md#respond-to-a-request-from-a-hook).
|
|
146
145
|
|
|
147
146
|
Example:
|
|
148
147
|
```js
|
|
@@ -234,17 +233,17 @@ const opts = {
|
|
|
234
233
|
fastify.get('/', opts)
|
|
235
234
|
```
|
|
236
235
|
|
|
237
|
-
> Note:
|
|
238
|
-
>
|
|
236
|
+
> 🛈 Note: Specifying the handler in both `options` and as the third parameter to
|
|
237
|
+
> the shortcut method throws a duplicate `handler` error.
|
|
239
238
|
|
|
240
239
|
### Url building
|
|
241
240
|
<a id="url-building"></a>
|
|
242
241
|
|
|
243
242
|
Fastify supports both static and dynamic URLs.
|
|
244
243
|
|
|
245
|
-
To register a **parametric** path, use
|
|
246
|
-
|
|
247
|
-
|
|
244
|
+
To register a **parametric** path, use a *colon* before the parameter name. For
|
|
245
|
+
**wildcard**, use a *star*. Static routes are always checked before parametric
|
|
246
|
+
and wildcard routes.
|
|
248
247
|
|
|
249
248
|
```js
|
|
250
249
|
// parametric
|
|
@@ -266,9 +265,8 @@ fastify.get('/example/:userId/:secretToken', function (request, reply) {
|
|
|
266
265
|
fastify.get('/example/*', function (request, reply) {})
|
|
267
266
|
```
|
|
268
267
|
|
|
269
|
-
Regular expression routes are supported
|
|
270
|
-
|
|
271
|
-
performance!
|
|
268
|
+
Regular expression routes are supported, but slashes must be escaped.
|
|
269
|
+
Take note that RegExp is also very expensive in terms of performance!
|
|
272
270
|
```js
|
|
273
271
|
// parametric with regexp
|
|
274
272
|
fastify.get('/example/:file(^\\d+).png', function (request, reply) {
|
|
@@ -306,24 +304,24 @@ fastify.get('/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', function (request, r
|
|
|
306
304
|
In this case as parameter separator it is possible to use whatever character is
|
|
307
305
|
not matched by the regular expression.
|
|
308
306
|
|
|
309
|
-
The last parameter can be made optional
|
|
310
|
-
end of the
|
|
307
|
+
The last parameter can be made optional by adding a question mark ("?") to the
|
|
308
|
+
end of the parameter name.
|
|
311
309
|
```js
|
|
312
310
|
fastify.get('/example/posts/:id?', function (request, reply) {
|
|
313
311
|
const { id } = request.params;
|
|
314
312
|
// your code here
|
|
315
313
|
})
|
|
316
314
|
```
|
|
317
|
-
In this case
|
|
318
|
-
|
|
315
|
+
In this case, `/example/posts` and `/example/posts/1` are both valid. The
|
|
316
|
+
optional param will be `undefined` if not specified.
|
|
319
317
|
|
|
320
|
-
Having a route with multiple parameters may negatively affect performance
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
318
|
+
Having a route with multiple parameters may negatively affect performance.
|
|
319
|
+
Prefer a single parameter approach, especially on routes that are on the hot
|
|
320
|
+
path of your application. For more details, see
|
|
321
|
+
[find-my-way](https://github.com/delvedor/find-my-way).
|
|
324
322
|
|
|
325
|
-
|
|
326
|
-
|
|
323
|
+
To include a colon in a path without declaring a parameter, use a double colon.
|
|
324
|
+
For example:
|
|
327
325
|
```js
|
|
328
326
|
fastify.post('/name::verb') // will be interpreted as /name:verb
|
|
329
327
|
```
|
|
@@ -340,12 +338,12 @@ fastify.get('/', options, async function (request, reply) {
|
|
|
340
338
|
})
|
|
341
339
|
```
|
|
342
340
|
|
|
343
|
-
As
|
|
344
|
-
|
|
341
|
+
As shown, `reply.send` is not called to send data back to the user. Simply
|
|
342
|
+
return the body and you are done!
|
|
345
343
|
|
|
346
|
-
If
|
|
347
|
-
|
|
348
|
-
|
|
344
|
+
If needed, you can also send data back with `reply.send`. In this case, do not
|
|
345
|
+
forget to `return reply` or `await reply` in your `async` handler to avoid race
|
|
346
|
+
conditions.
|
|
349
347
|
|
|
350
348
|
```js
|
|
351
349
|
fastify.get('/', options, async function (request, reply) {
|
|
@@ -378,48 +376,42 @@ fastify.get('/', options, async function (request, reply) {
|
|
|
378
376
|
})
|
|
379
377
|
```
|
|
380
378
|
|
|
381
|
-
|
|
382
|
-
* When using both `return value` and `reply.send(value)
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
*
|
|
387
|
-
attention. For more details read [promise-resolution](#promise-resolution).
|
|
388
|
-
* You cannot return `undefined`. For more details read
|
|
389
|
-
[promise-resolution](#promise-resolution).
|
|
379
|
+
> ⚠ Warning:
|
|
380
|
+
> * When using both `return value` and `reply.send(value)`, the first one takes
|
|
381
|
+
> precedence, the second is discarded, and a *warn* log is emitted.
|
|
382
|
+
> * Calling `reply.send()` outside of the promise is possible but requires special
|
|
383
|
+
> attention. See [promise-resolution](#promise-resolution).
|
|
384
|
+
> * `undefined` cannot be returned. See [promise-resolution](#promise-resolution).
|
|
390
385
|
|
|
391
386
|
### Promise resolution
|
|
392
387
|
<a id="promise-resolution"></a>
|
|
393
388
|
|
|
394
|
-
If
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
in your handler.
|
|
389
|
+
If the handler is an `async` function or returns a promise, be aware of the
|
|
390
|
+
special behavior to support callback and promise control-flow. When the
|
|
391
|
+
handler's promise resolves, the reply is automatically sent with its value
|
|
392
|
+
unless you explicitly await or return `reply` in the handler.
|
|
399
393
|
|
|
400
|
-
1. If
|
|
401
|
-
`reply.send`:
|
|
394
|
+
1. If using `async/await` or promises but responding with `reply.send`:
|
|
402
395
|
- **Do** `return reply` / `await reply`.
|
|
403
396
|
- **Do not** forget to call `reply.send`.
|
|
404
|
-
2. If
|
|
397
|
+
2. If using `async/await` or promises:
|
|
405
398
|
- **Do not** use `reply.send`.
|
|
406
|
-
- **Do** return the value
|
|
399
|
+
- **Do** return the value to send.
|
|
407
400
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
your application.
|
|
401
|
+
This approach supports both `callback-style` and `async-await` with minimal
|
|
402
|
+
trade-off. However, it is recommended to use only one style for consistent
|
|
403
|
+
error handling within your application.
|
|
412
404
|
|
|
413
|
-
|
|
405
|
+
> 🛈 Note: Every async function returns a promise by itself.
|
|
414
406
|
|
|
415
407
|
### Route Prefixing
|
|
416
408
|
<a id="route-prefixing"></a>
|
|
417
409
|
|
|
418
|
-
Sometimes
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
410
|
+
Sometimes maintaining multiple versions of the same API is necessary. A common
|
|
411
|
+
approach is to prefix routes with the API version number, e.g., `/v1/user`.
|
|
412
|
+
Fastify offers a fast and smart way to create different versions of the same API
|
|
413
|
+
without changing all the route names by hand, called *route prefixing*. Here is
|
|
414
|
+
how it works:
|
|
423
415
|
|
|
424
416
|
```js
|
|
425
417
|
// server.js
|
|
@@ -446,19 +438,18 @@ module.exports = function (fastify, opts, done) {
|
|
|
446
438
|
done()
|
|
447
439
|
}
|
|
448
440
|
```
|
|
449
|
-
Fastify will not complain
|
|
450
|
-
|
|
451
|
-
|
|
441
|
+
Fastify will not complain about using the same name for two different routes
|
|
442
|
+
because it handles the prefix automatically at compilation time. This ensures
|
|
443
|
+
performance is not affected.
|
|
452
444
|
|
|
453
|
-
Now
|
|
445
|
+
Now clients will have access to the following routes:
|
|
454
446
|
- `/v1/user`
|
|
455
447
|
- `/v2/user`
|
|
456
448
|
|
|
457
|
-
|
|
458
|
-
|
|
449
|
+
This can be done multiple times and works for nested `register`. Route
|
|
450
|
+
parameters are also supported.
|
|
459
451
|
|
|
460
|
-
|
|
461
|
-
plugin:
|
|
452
|
+
To use a prefix for all routes, place them inside a plugin:
|
|
462
453
|
|
|
463
454
|
```js
|
|
464
455
|
const fastify = require('fastify')()
|
|
@@ -483,10 +474,8 @@ await fastify.listen({ port: 3000 })
|
|
|
483
474
|
### Route Prefixing and fastify-plugin
|
|
484
475
|
<a id="fastify-plugin"></a>
|
|
485
476
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
routes, this option will not work. You can still make it work by wrapping a
|
|
489
|
-
plugin in a plugin, e. g.:
|
|
477
|
+
If using [`fastify-plugin`](https://github.com/fastify/fastify-plugin) to wrap
|
|
478
|
+
routes, this option will not work. To make it work, wrap a plugin in a plugin:
|
|
490
479
|
```js
|
|
491
480
|
const fp = require('fastify-plugin')
|
|
492
481
|
const routes = require('./lib/routes')
|
|
@@ -502,27 +491,23 @@ module.exports = fp(async function (app, opts) {
|
|
|
502
491
|
|
|
503
492
|
#### Handling of / route inside prefixed plugins
|
|
504
493
|
|
|
505
|
-
The `/` route
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
494
|
+
The `/` route behaves differently based on whether the prefix ends with `/`.
|
|
495
|
+
For example, with a prefix `/something/`, adding a `/` route matches only
|
|
496
|
+
`/something/`. With a prefix `/something`, adding a `/` route matches both
|
|
497
|
+
`/something` and `/something/`.
|
|
509
498
|
|
|
510
499
|
See the `prefixTrailingSlash` route option above to change this behavior.
|
|
511
500
|
|
|
512
501
|
### Custom Log Level
|
|
513
502
|
<a id="custom-log-level"></a>
|
|
514
503
|
|
|
515
|
-
|
|
516
|
-
|
|
504
|
+
Different log levels can be set for routes in Fastify by passing the `logLevel`
|
|
505
|
+
option to the plugin or route with the desired
|
|
506
|
+
[value](https://github.com/pinojs/pino/blob/master/docs/api.md#level-string).
|
|
517
507
|
|
|
518
|
-
|
|
519
|
-
option with the
|
|
520
|
-
[value](https://github.com/pinojs/pino/blob/master/docs/api.md#level-string)
|
|
521
|
-
that you need.
|
|
522
|
-
|
|
523
|
-
Be aware that if you set the `logLevel` at plugin level, also the
|
|
508
|
+
Be aware that setting `logLevel` at the plugin level also affects
|
|
524
509
|
[`setNotFoundHandler`](./Server.md#setnotfoundhandler) and
|
|
525
|
-
[`setErrorHandler`](./Server.md#seterrorhandler)
|
|
510
|
+
[`setErrorHandler`](./Server.md#seterrorhandler).
|
|
526
511
|
|
|
527
512
|
```js
|
|
528
513
|
// server.js
|
|
@@ -534,22 +519,21 @@ fastify.register(require('./routes/events'), { logLevel: 'debug' })
|
|
|
534
519
|
fastify.listen({ port: 3000 })
|
|
535
520
|
```
|
|
536
521
|
|
|
537
|
-
Or
|
|
522
|
+
Or pass it directly to a route:
|
|
538
523
|
```js
|
|
539
524
|
fastify.get('/', { logLevel: 'warn' }, (request, reply) => {
|
|
540
525
|
reply.send({ hello: 'world' })
|
|
541
526
|
})
|
|
542
527
|
```
|
|
543
|
-
*Remember that the custom log level
|
|
544
|
-
|
|
528
|
+
*Remember that the custom log level applies only to routes, not to the global
|
|
529
|
+
Fastify Logger, accessible with `fastify.log`.*
|
|
545
530
|
|
|
546
531
|
### Custom Log Serializer
|
|
547
532
|
<a id="custom-log-serializer"></a>
|
|
548
533
|
|
|
549
|
-
In some contexts,
|
|
550
|
-
resources for some routes. In this case, you can define custom
|
|
534
|
+
In some contexts, logging a large object may waste resources. Define custom
|
|
551
535
|
[`serializers`](https://github.com/pinojs/pino/blob/master/docs/api.md#serializers-object)
|
|
552
|
-
and attach them in the
|
|
536
|
+
and attach them in the appropriate context.
|
|
553
537
|
|
|
554
538
|
```js
|
|
555
539
|
const fastify = require('fastify')({ logger: true })
|
|
@@ -568,7 +552,7 @@ fastify.register(require('./routes/events'), {
|
|
|
568
552
|
fastify.listen({ port: 3000 })
|
|
569
553
|
```
|
|
570
554
|
|
|
571
|
-
|
|
555
|
+
Serializers can be inherited by context:
|
|
572
556
|
|
|
573
557
|
```js
|
|
574
558
|
const fastify = Fastify({
|
|
@@ -629,23 +613,22 @@ fastify.listen({ port: 3000 })
|
|
|
629
613
|
### Constraints
|
|
630
614
|
<a id="constraints"></a>
|
|
631
615
|
|
|
632
|
-
Fastify supports constraining routes to match
|
|
633
|
-
|
|
616
|
+
Fastify supports constraining routes to match certain requests based on
|
|
617
|
+
properties like the `Host` header or any other value via
|
|
634
618
|
[`find-my-way`](https://github.com/delvedor/find-my-way) constraints.
|
|
635
619
|
Constraints are specified in the `constraints` property of the route options.
|
|
636
|
-
Fastify has two built-in constraints
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
request.
|
|
620
|
+
Fastify has two built-in constraints: `version` and `host`. Custom constraint
|
|
621
|
+
strategies can be added to inspect other parts of a request to decide if a route
|
|
622
|
+
should be executed.
|
|
640
623
|
|
|
641
624
|
#### Version Constraints
|
|
642
625
|
|
|
643
626
|
You can provide a `version` key in the `constraints` option to a route.
|
|
644
|
-
Versioned routes
|
|
645
|
-
path,
|
|
646
|
-
|
|
647
|
-
[semver](https://semver.org/) specification, and routes should be declared
|
|
648
|
-
exact semver versions for matching.
|
|
627
|
+
Versioned routes allows multiple handlers to be declared for the same HTTP
|
|
628
|
+
route path, matched according to the request's `Accept-Version` header.
|
|
629
|
+
The `Accept-Version` header value should follow the
|
|
630
|
+
[semver](https://semver.org/) specification, and routes should be declared
|
|
631
|
+
with exact semver versions for matching.
|
|
649
632
|
|
|
650
633
|
Fastify will require a request `Accept-Version` header to be set if the route
|
|
651
634
|
has a version set, and will prefer a versioned route to a non-versioned route
|
|
@@ -676,20 +659,20 @@ fastify.inject({
|
|
|
676
659
|
})
|
|
677
660
|
```
|
|
678
661
|
|
|
679
|
-
>
|
|
680
|
-
>
|
|
662
|
+
> ⚠ Warning:
|
|
663
|
+
> Set a
|
|
681
664
|
> [`Vary`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary)
|
|
682
|
-
> header in
|
|
683
|
-
>
|
|
684
|
-
> can also
|
|
665
|
+
> header in responses with the value used for versioning
|
|
666
|
+
> (e.g., `'Accept-Version'`) to prevent cache poisoning attacks.
|
|
667
|
+
> This can also be configured in a Proxy/CDN.
|
|
685
668
|
>
|
|
686
669
|
> ```js
|
|
687
670
|
> const append = require('vary').append
|
|
688
671
|
> fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
689
|
-
> if (req.headers['accept-version']) { // or the custom header
|
|
672
|
+
> if (req.headers['accept-version']) { // or the custom header being used
|
|
690
673
|
> let value = reply.getHeader('Vary') || ''
|
|
691
674
|
> const header = Array.isArray(value) ? value.join(', ') : String(value)
|
|
692
|
-
> if ((value = append(header, 'Accept-Version'))) { // or the custom header
|
|
675
|
+
> if ((value = append(header, 'Accept-Version'))) { // or the custom header being used
|
|
693
676
|
> reply.header('Vary', value)
|
|
694
677
|
> }
|
|
695
678
|
> }
|
|
@@ -697,22 +680,20 @@ fastify.inject({
|
|
|
697
680
|
> })
|
|
698
681
|
> ```
|
|
699
682
|
|
|
700
|
-
If
|
|
683
|
+
If multiple versions with the same major or minor are declared, Fastify will
|
|
701
684
|
always choose the highest compatible with the `Accept-Version` header value.
|
|
702
685
|
|
|
703
|
-
If the request
|
|
704
|
-
returned.
|
|
686
|
+
If the request lacks an `Accept-Version` header, a 404 error will be returned.
|
|
705
687
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
688
|
+
Custom version matching logic can be defined through the
|
|
689
|
+
[`constraints`](./Server.md#constraints) configuration when creating a Fastify
|
|
690
|
+
server instance.
|
|
709
691
|
|
|
710
692
|
#### Host Constraints
|
|
711
693
|
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
arbitrary host matching.
|
|
694
|
+
Provide a `host` key in the `constraints` route option to limit the route to
|
|
695
|
+
certain values of the request `Host` header. `host` constraint values can be
|
|
696
|
+
specified as strings for exact matches or RegExps for arbitrary host matching.
|
|
716
697
|
|
|
717
698
|
```js
|
|
718
699
|
fastify.route({
|
|
@@ -761,10 +742,9 @@ fastify.route({
|
|
|
761
742
|
|
|
762
743
|
#### Asynchronous Custom Constraints
|
|
763
744
|
|
|
764
|
-
Custom constraints can be provided and the `constraint` criteria can be
|
|
765
|
-
fetched from another source such as
|
|
766
|
-
|
|
767
|
-
performance.
|
|
745
|
+
Custom constraints can be provided, and the `constraint` criteria can be
|
|
746
|
+
fetched from another source such as a database. Use asynchronous custom
|
|
747
|
+
constraints as a last resort, as they impact router performance.
|
|
768
748
|
|
|
769
749
|
```js
|
|
770
750
|
function databaseOperation(field, done) {
|
|
@@ -791,11 +771,11 @@ const secret = {
|
|
|
791
771
|
}
|
|
792
772
|
```
|
|
793
773
|
|
|
794
|
-
>
|
|
795
|
-
> When using
|
|
796
|
-
>
|
|
797
|
-
>
|
|
798
|
-
>
|
|
774
|
+
> ⚠ Warning:
|
|
775
|
+
> When using asynchronous constraints, avoid returning errors inside the
|
|
776
|
+
> callback. If errors are unavoidable, provide a custom `frameworkErrors`
|
|
777
|
+
> handler to manage them. Otherwise, route selection may break or expose
|
|
778
|
+
> sensitive information.
|
|
799
779
|
>
|
|
800
780
|
> ```js
|
|
801
781
|
> const Fastify = require('fastify')
|
package/docs/Reference/Server.md
CHANGED
|
@@ -166,7 +166,7 @@ When set to `true`, upon [`close`](#close) the server will iterate the current
|
|
|
166
166
|
persistent connections and [destroy their
|
|
167
167
|
sockets](https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketdestroyerror).
|
|
168
168
|
|
|
169
|
-
>
|
|
169
|
+
> ⚠ Warning:
|
|
170
170
|
> Connections are not inspected to determine if requests have
|
|
171
171
|
> been completed.
|
|
172
172
|
|
|
@@ -193,7 +193,7 @@ to understand the effect of this option. This option only applies when HTTP/1.1
|
|
|
193
193
|
is in use. Also, when `serverFactory` option is specified, this option is
|
|
194
194
|
ignored.
|
|
195
195
|
|
|
196
|
-
>
|
|
196
|
+
> 🛈 Note:
|
|
197
197
|
> At the time of writing, only node >= v16.10.0 supports this option.
|
|
198
198
|
|
|
199
199
|
### `requestTimeout`
|
|
@@ -211,7 +211,7 @@ It must be set to a non-zero value (e.g. 120 seconds) to protect against potenti
|
|
|
211
211
|
Denial-of-Service attacks in case the server is deployed without a reverse proxy
|
|
212
212
|
in front.
|
|
213
213
|
|
|
214
|
-
>
|
|
214
|
+
> 🛈 Note:
|
|
215
215
|
> At the time of writing, only node >= v14.11.0 supports this option
|
|
216
216
|
|
|
217
217
|
### `ignoreTrailingSlash`
|
|
@@ -529,7 +529,7 @@ Especially in distributed systems, you may want to override the default ID
|
|
|
529
529
|
generation behavior as shown below. For generating `UUID`s you may want to check
|
|
530
530
|
out [hyperid](https://github.com/mcollina/hyperid).
|
|
531
531
|
|
|
532
|
-
>
|
|
532
|
+
> 🛈 Note:
|
|
533
533
|
> `genReqId` will be not called if the header set in
|
|
534
534
|
> <code>[requestIdHeader](#requestidheader)</code> is available (defaults to
|
|
535
535
|
> 'request-id').
|
|
@@ -581,7 +581,7 @@ fastify.get('/', (request, reply) => {
|
|
|
581
581
|
})
|
|
582
582
|
```
|
|
583
583
|
|
|
584
|
-
>
|
|
584
|
+
> 🛈 Note:
|
|
585
585
|
> If a request contains multiple `x-forwarded-host` or `x-forwarded-proto`
|
|
586
586
|
> headers, it is only the last one that is used to derive `request.hostname`
|
|
587
587
|
> and `request.protocol`.
|
|
@@ -599,8 +599,9 @@ controls [avvio](https://www.npmjs.com/package/avvio) 's `timeout` parameter.
|
|
|
599
599
|
### `querystringParser`
|
|
600
600
|
<a id="factory-querystring-parser"></a>
|
|
601
601
|
|
|
602
|
-
The default query string parser that Fastify uses is
|
|
603
|
-
`querystring` module
|
|
602
|
+
The default query string parser that Fastify uses is a more performant fork
|
|
603
|
+
of Node.js's core `querystring` module called
|
|
604
|
+
[`fast-querystring`](https://github.com/anonrig/fast-querystring).
|
|
604
605
|
|
|
605
606
|
You can use this option to use a custom parser, such as
|
|
606
607
|
[`qs`](https://www.npmjs.com/package/qs).
|
|
@@ -619,7 +620,7 @@ You can also use Fastify's default parser but change some handling behavior,
|
|
|
619
620
|
like the example below for case insensitive keys and values:
|
|
620
621
|
|
|
621
622
|
```js
|
|
622
|
-
const querystring = require('
|
|
623
|
+
const querystring = require('fast-querystring')
|
|
623
624
|
const fastify = require('fastify')({
|
|
624
625
|
querystringParser: str => querystring.parse(str.toLowerCase())
|
|
625
626
|
})
|
|
@@ -735,7 +736,7 @@ Fastify provides default error handlers for the most common use cases. It is
|
|
|
735
736
|
possible to override one or more of those handlers with custom code using this
|
|
736
737
|
option.
|
|
737
738
|
|
|
738
|
-
>
|
|
739
|
+
> 🛈 Note:
|
|
739
740
|
> Only `FST_ERR_BAD_URL` and `FST_ERR_ASYNC_CONSTRAINT` are implemented at present.
|
|
740
741
|
|
|
741
742
|
```js
|
|
@@ -788,7 +789,7 @@ function defaultClientErrorHandler (err, socket) {
|
|
|
788
789
|
}
|
|
789
790
|
```
|
|
790
791
|
|
|
791
|
-
>
|
|
792
|
+
> 🛈 Note:
|
|
792
793
|
> `clientErrorHandler` operates with raw sockets. The handler is expected to
|
|
793
794
|
> return a properly formed HTTP response that includes a status line, HTTP headers
|
|
794
795
|
> and a message body. Before attempting to write the socket, the handler should
|
|
@@ -877,7 +878,7 @@ fastify.get('/dev', async (request, reply) => {
|
|
|
877
878
|
[server](https://nodejs.org/api/http.html#http_class_http_server) object as
|
|
878
879
|
returned by the [**`Fastify factory function`**](#factory).
|
|
879
880
|
|
|
880
|
-
>
|
|
881
|
+
> ⚠ Warning:
|
|
881
882
|
> If utilized improperly, certain Fastify features could be disrupted.
|
|
882
883
|
> It is recommended to only use it for attaching listeners.
|
|
883
884
|
|
|
@@ -973,7 +974,8 @@ server.listen({
|
|
|
973
974
|
By default, the server will listen on the address(es) resolved by `localhost`
|
|
974
975
|
when no specific host is provided. If listening on any available interface is
|
|
975
976
|
desired, then specifying `0.0.0.0` for the address will listen on all IPv4
|
|
976
|
-
addresses. The
|
|
977
|
+
addresses. The address argument provided above will then return the first such
|
|
978
|
+
IPv4 address. The following table details the possible values for `host` when
|
|
977
979
|
targeting `localhost`, and what the result of those values for `host` will be.
|
|
978
980
|
|
|
979
981
|
Host | IPv4 | IPv6
|
|
@@ -1219,7 +1221,7 @@ different ways to define a name (in order).
|
|
|
1219
1221
|
Newlines are replaced by ` -- `. This will help to identify the root cause when
|
|
1220
1222
|
you deal with many plugins.
|
|
1221
1223
|
|
|
1222
|
-
>
|
|
1224
|
+
> ⚠ Warning:
|
|
1223
1225
|
> If you have to deal with nested plugins, the name differs with the usage of
|
|
1224
1226
|
> the [fastify-plugin](https://github.com/fastify/fastify-plugin) because
|
|
1225
1227
|
> no new scope is created and therefore we have no place to attach contextual
|
|
@@ -1355,7 +1357,7 @@ Set the schema error formatter for all routes. See
|
|
|
1355
1357
|
Set the schema serializer compiler for all routes. See
|
|
1356
1358
|
[#schema-serializer](./Validation-and-Serialization.md#schema-serializer).
|
|
1357
1359
|
|
|
1358
|
-
>
|
|
1360
|
+
> 🛈 Note:
|
|
1359
1361
|
> [`setReplySerializer`](#set-reply-serializer) has priority if set!
|
|
1360
1362
|
|
|
1361
1363
|
#### validatorCompiler
|
|
@@ -1488,7 +1490,7 @@ lifecycle](./Lifecycle.md#lifecycle). *async-await* is supported as well.
|
|
|
1488
1490
|
You can also register [`preValidation`](./Hooks.md#route-hooks) and
|
|
1489
1491
|
[`preHandler`](./Hooks.md#route-hooks) hooks for the 404 handler.
|
|
1490
1492
|
|
|
1491
|
-
>
|
|
1493
|
+
> 🛈 Note:
|
|
1492
1494
|
> The `preValidation` hook registered using this method will run for a
|
|
1493
1495
|
> route that Fastify does not recognize and **not** when a route handler manually
|
|
1494
1496
|
> calls [`reply.callNotFound`](./Reply.md#call-not-found). In which case, only
|
|
@@ -1524,7 +1526,7 @@ plugins are registered. If you would like to augment the behavior of the default
|
|
|
1524
1526
|
arguments `fastify.setNotFoundHandler()` within the context of these registered
|
|
1525
1527
|
plugins.
|
|
1526
1528
|
|
|
1527
|
-
>
|
|
1529
|
+
> 🛈 Note:
|
|
1528
1530
|
> Some config properties from the request object will be
|
|
1529
1531
|
> undefined inside the custom not found handler. E.g.:
|
|
1530
1532
|
> `request.routerPath`, `routerMethod` and `context.config`.
|
|
@@ -2,18 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## Type Providers
|
|
4
4
|
|
|
5
|
-
Type Providers are a TypeScript
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
Type Providers are a TypeScript feature that enables Fastify to infer type
|
|
6
|
+
information from inline JSON Schema. They are an alternative to specifying
|
|
7
|
+
generic arguments on routes and can reduce the need to keep associated types for
|
|
8
|
+
each schema in a project.
|
|
9
9
|
|
|
10
10
|
### Providers
|
|
11
11
|
|
|
12
|
-
Type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Provider packages follow a `@fastify/type-provider-{provider-name}` naming
|
|
16
|
-
convention, and there are several community ones available as well.
|
|
12
|
+
Official Type Provider packages follow the
|
|
13
|
+
`@fastify/type-provider-{provider-name}` naming convention.
|
|
14
|
+
Several community providers are also available.
|
|
17
15
|
|
|
18
16
|
The following inference packages are supported:
|
|
19
17
|
|
|
@@ -30,7 +28,7 @@ See also the Type Provider wrapper packages for each of the packages respectivel
|
|
|
30
28
|
|
|
31
29
|
### Json Schema to Ts
|
|
32
30
|
|
|
33
|
-
The following sets up a `json-schema-to-ts` Type Provider
|
|
31
|
+
The following sets up a `json-schema-to-ts` Type Provider:
|
|
34
32
|
|
|
35
33
|
```bash
|
|
36
34
|
$ npm i @fastify/type-provider-json-schema-to-ts
|
|
@@ -62,7 +60,7 @@ server.get('/route', {
|
|
|
62
60
|
|
|
63
61
|
### TypeBox
|
|
64
62
|
|
|
65
|
-
The following sets up a TypeBox Type Provider
|
|
63
|
+
The following sets up a TypeBox Type Provider:
|
|
66
64
|
|
|
67
65
|
```bash
|
|
68
66
|
$ npm i @fastify/type-provider-typebox
|
|
@@ -89,14 +87,14 @@ server.get('/route', {
|
|
|
89
87
|
})
|
|
90
88
|
```
|
|
91
89
|
|
|
92
|
-
See
|
|
93
|
-
documentation](https://github.com/sinclairzx81/typebox#validation)
|
|
94
|
-
up AJV to work with TypeBox.
|
|
90
|
+
See the [TypeBox
|
|
91
|
+
documentation](https://github.com/sinclairzx81/typebox#validation)
|
|
92
|
+
for setting up AJV to work with TypeBox.
|
|
95
93
|
|
|
96
94
|
### Zod
|
|
97
95
|
|
|
98
96
|
See [official documentation](https://github.com/turkerdev/fastify-type-provider-zod)
|
|
99
|
-
for Zod
|
|
97
|
+
for Zod Type Provider instructions.
|
|
100
98
|
|
|
101
99
|
|
|
102
100
|
### Scoped Type-Provider
|
|
@@ -154,9 +152,9 @@ fastify.register(pluginWithJsonSchema)
|
|
|
154
152
|
fastify.register(pluginWithTypebox)
|
|
155
153
|
```
|
|
156
154
|
|
|
157
|
-
It
|
|
158
|
-
|
|
159
|
-
|
|
155
|
+
It is important to note that since the types do not propagate globally, it is
|
|
156
|
+
currently not possible to avoid multiple registrations on routes when dealing
|
|
157
|
+
with several scopes, as shown below:
|
|
160
158
|
|
|
161
159
|
```ts
|
|
162
160
|
import Fastify from 'fastify'
|
|
@@ -178,7 +176,7 @@ function plugin1(fastify: FastifyInstance, _opts, done): void {
|
|
|
178
176
|
})
|
|
179
177
|
}
|
|
180
178
|
}, (req) => {
|
|
181
|
-
//
|
|
179
|
+
// In a new scope, call `withTypeProvider` again to ensure it works
|
|
182
180
|
const { x, y, z } = req.body
|
|
183
181
|
});
|
|
184
182
|
done()
|
|
@@ -205,8 +203,8 @@ function plugin2(fastify: FastifyInstance, _opts, done): void {
|
|
|
205
203
|
|
|
206
204
|
### Type Definition of FastifyInstance + TypeProvider
|
|
207
205
|
|
|
208
|
-
When working with modules
|
|
209
|
-
|
|
206
|
+
When working with modules, use `FastifyInstance` with Type Provider generics.
|
|
207
|
+
See the example below:
|
|
210
208
|
|
|
211
209
|
```ts
|
|
212
210
|
// index.ts
|