fastify 4.0.0 → 4.0.1
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/.markdownlint-cli2.yaml +22 -0
- package/GOVERNANCE.md +30 -20
- package/PROJECT_CHARTER.md +48 -17
- package/README.md +163 -76
- package/SECURITY.md +55 -44
- package/build/build-error-serializer.js +11 -7
- package/docs/Guides/Benchmarking.md +2 -0
- package/docs/Guides/Delay-Accepting-Requests.md +98 -90
- package/docs/Guides/Ecosystem.md +40 -30
- package/docs/Guides/Index.md +2 -0
- package/docs/Guides/Migration-Guide-V4.md +55 -0
- package/docs/Guides/Serverless.md +13 -12
- package/docs/Reference/ContentTypeParser.md +17 -13
- package/docs/Reference/Errors.md +6 -5
- package/docs/Reference/Plugins.md +8 -6
- package/docs/Reference/Reply.md +30 -16
- package/docs/Reference/Request.md +3 -3
- package/docs/Reference/Routes.md +112 -37
- package/docs/Reference/Server.md +109 -72
- package/docs/Reference/Type-Providers.md +28 -8
- package/docs/Reference/TypeScript.md +12 -6
- package/docs/Reference/Validation-and-Serialization.md +39 -37
- package/fastify.js +1 -1
- package/lib/error-serializer.js +32 -204
- package/lib/pluginUtils.js +10 -0
- package/package.json +5 -3
- package/test/plugin.test.js +32 -0
- package/docs/Migration-Guide-V4.md +0 -12
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -12,8 +12,8 @@ section.
|
|
|
12
12
|
[accepts](https://www.npmjs.com/package/accepts) in your request object.
|
|
13
13
|
- [`@fastify/accepts-serializer`](https://github.com/fastify/fastify-accepts-serializer)
|
|
14
14
|
to serialize to output according to `Accept` header.
|
|
15
|
-
- [`@fastify/any-schema`](https://github.com/fastify/any-schema-you-like) Save
|
|
16
|
-
schemas and decide which one to use to serialize the payload
|
|
15
|
+
- [`@fastify/any-schema`](https://github.com/fastify/any-schema-you-like) Save
|
|
16
|
+
multiple schemas and decide which one to use to serialize the payload
|
|
17
17
|
- [`@fastify/auth`](https://github.com/fastify/fastify-auth) Run multiple auth
|
|
18
18
|
functions in Fastify.
|
|
19
19
|
- [`@fastify/autoload`](https://github.com/fastify/fastify-autoload) Require all
|
|
@@ -21,13 +21,13 @@ section.
|
|
|
21
21
|
- [`@fastify/awilix`](https://github.com/fastify/fastify-awilix) Dependency
|
|
22
22
|
injection support for Fastify, based on
|
|
23
23
|
[awilix](https://github.com/jeffijoe/awilix).
|
|
24
|
-
- [`@fastify/aws-lambda`](https://github.com/fastify/aws-lambda-fastify) allows
|
|
25
|
-
easily build serverless web applications/services and RESTful APIs
|
|
26
|
-
on top of AWS Lambda and Amazon API Gateway.
|
|
24
|
+
- [`@fastify/aws-lambda`](https://github.com/fastify/aws-lambda-fastify) allows
|
|
25
|
+
you to easily build serverless web applications/services and RESTful APIs
|
|
26
|
+
using Fastify on top of AWS Lambda and Amazon API Gateway.
|
|
27
27
|
- [`@fastify/basic-auth`](https://github.com/fastify/fastify-basic-auth) Basic
|
|
28
28
|
auth plugin for Fastify.
|
|
29
|
-
- [`@fastify/bearer-auth`](https://github.com/fastify/fastify-bearer-auth)
|
|
30
|
-
auth plugin for Fastify.
|
|
29
|
+
- [`@fastify/bearer-auth`](https://github.com/fastify/fastify-bearer-auth)
|
|
30
|
+
Bearer auth plugin for Fastify.
|
|
31
31
|
- [`@fastify/caching`](https://github.com/fastify/fastify-caching) General
|
|
32
32
|
server-side cache and ETag support.
|
|
33
33
|
- [`@fastify/circuit-breaker`](https://github.com/fastify/fastify-circuit-breaker)
|
|
@@ -38,7 +38,8 @@ section.
|
|
|
38
38
|
cookie headers.
|
|
39
39
|
- [`@fastify/cors`](https://github.com/fastify/fastify-cors) Enables the use of
|
|
40
40
|
CORS in a Fastify application.
|
|
41
|
-
- [`@fastify/csrf-protection`](https://github.com/fastify/csrf-protection) A
|
|
41
|
+
- [`@fastify/csrf-protection`](https://github.com/fastify/csrf-protection) A
|
|
42
|
+
plugin for adding
|
|
42
43
|
[CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) protection to
|
|
43
44
|
Fastify.
|
|
44
45
|
- [`@fastify/diagnostics-channel`](https://github.com/fastify/fastify-diagnostics-channel)
|
|
@@ -59,14 +60,16 @@ section.
|
|
|
59
60
|
function.
|
|
60
61
|
- [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) Important
|
|
61
62
|
security headers for Fastify.
|
|
62
|
-
- [`@fastify/hotwire`](https://github.com/fastify/fastify-hotwire) Use the
|
|
63
|
+
- [`@fastify/hotwire`](https://github.com/fastify/fastify-hotwire) Use the
|
|
64
|
+
Hotwire pattern with Fastify.
|
|
63
65
|
- [`@fastify/http-proxy`](https://github.com/fastify/fastify-http-proxy) Proxy
|
|
64
66
|
your HTTP requests to another server, with hooks.
|
|
65
|
-
- [`@fastify/jwt`](https://github.com/fastify/fastify-jwt) JWT utils for
|
|
66
|
-
internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
|
|
67
|
+
- [`@fastify/jwt`](https://github.com/fastify/fastify-jwt) JWT utils for
|
|
68
|
+
Fastify, internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
|
|
67
69
|
- [`@fastify/leveldb`](https://github.com/fastify/fastify-leveldb) Plugin to
|
|
68
70
|
share a common LevelDB connection across Fastify.
|
|
69
|
-
- [`@fastify/middie`](https://github.com/fastify/middie) Middleware engine for
|
|
71
|
+
- [`@fastify/middie`](https://github.com/fastify/middie) Middleware engine for
|
|
72
|
+
Fastify.
|
|
70
73
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
71
74
|
MongoDB connection plugin, with which you can share the same MongoDB
|
|
72
75
|
connection pool across every part of your server.
|
|
@@ -99,8 +102,8 @@ section.
|
|
|
99
102
|
- [`@fastify/schedule`](https://github.com/fastify/fastify-schedule) Plugin for
|
|
100
103
|
scheduling periodic jobs, based on
|
|
101
104
|
[toad-scheduler](https://github.com/kibertoad/toad-scheduler).
|
|
102
|
-
- [`@fastify/sensible`](https://github.com/fastify/fastify-sensible) Defaults
|
|
103
|
-
Fastify that everyone can agree on. It adds some useful decorators such as
|
|
105
|
+
- [`@fastify/sensible`](https://github.com/fastify/fastify-sensible) Defaults
|
|
106
|
+
for Fastify that everyone can agree on. It adds some useful decorators such as
|
|
104
107
|
HTTP errors and assertions, but also more request and reply methods.
|
|
105
108
|
- [`@fastify/session`](https://github.com/fastify/session) a session plugin for
|
|
106
109
|
Fastify.
|
|
@@ -109,10 +112,11 @@ section.
|
|
|
109
112
|
- [`@fastify/swagger`](https://github.com/fastify/fastify-swagger) Plugin for
|
|
110
113
|
serving Swagger/OpenAPI documentation for Fastify, supporting dynamic
|
|
111
114
|
generation.
|
|
112
|
-
- [`@fastify/under-pressure`](https://github.com/fastify/under-pressure) Measure
|
|
113
|
-
load with automatic handling of _"Service Unavailable"_ plugin for
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
- [`@fastify/under-pressure`](https://github.com/fastify/under-pressure) Measure
|
|
116
|
+
process load with automatic handling of _"Service Unavailable"_ plugin for
|
|
117
|
+
Fastify.
|
|
118
|
+
- [`@fastify/url-data`](https://github.com/fastify/fastify-url-data) Decorate
|
|
119
|
+
the `Request` object with a method to access raw URL components.
|
|
116
120
|
- [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
|
|
117
121
|
rendering (_ejs, pug, handlebars, marko_) plugin support for Fastify.
|
|
118
122
|
- [`@fastify/websocket`](https://github.com/fastify/fastify-websocket) WebSocket
|
|
@@ -134,7 +138,9 @@ section.
|
|
|
134
138
|
- [`@immobiliarelabs/fastify-metrics`](https://github.com/immobiliare/fastify-metrics)
|
|
135
139
|
Minimalistic and opinionated plugin that collects usage/process metrics and
|
|
136
140
|
dispatches to [statsd](https://github.com/statsd/statsd).
|
|
137
|
-
- [`@immobiliarelabs/fastify-sentry`](https://github.com/immobiliare/fastify-sentry)
|
|
141
|
+
- [`@immobiliarelabs/fastify-sentry`](https://github.com/immobiliare/fastify-sentry)
|
|
142
|
+
Sentry errors handler that just works! Install, add your DSN and you're good
|
|
143
|
+
to go!
|
|
138
144
|
- [`@mgcrea/fastify-graceful-exit`](https://github.com/mgcrea/fastify-graceful-exit)
|
|
139
145
|
A plugin to close the server gracefully
|
|
140
146
|
- [`@mgcrea/fastify-request-logger`](https://github.com/mgcrea/fastify-request-logger)
|
|
@@ -250,8 +256,8 @@ section.
|
|
|
250
256
|
Fastify feature flags plugin with multiple providers support (e.g. env,
|
|
251
257
|
[config](https://lorenwest.github.io/node-config/),
|
|
252
258
|
[unleash](https://unleash.github.io/)).
|
|
253
|
-
- [`fastify-file-routes`](https://github.com/spa5k/fastify-file-routes)
|
|
254
|
-
|
|
259
|
+
- [`fastify-file-routes`](https://github.com/spa5k/fastify-file-routes) Get
|
|
260
|
+
Next.js based file system routing into fastify.
|
|
255
261
|
- [`fastify-file-upload`](https://github.com/huangang/fastify-file-upload)
|
|
256
262
|
Fastify plugin for uploading files.
|
|
257
263
|
- [`fastify-firebase`](https://github.com/now-ims/fastify-firebase) Fastify
|
|
@@ -302,8 +308,8 @@ section.
|
|
|
302
308
|
An error handling plugin for Fastify that uses enhanced HTTP errors.
|
|
303
309
|
- [`fastify-https-redirect`](https://github.com/tomsvogel/fastify-https-redirect)
|
|
304
310
|
Fastify plugin for auto-redirect from HTTP to HTTPS.
|
|
305
|
-
- [`fatify-impressions`](https://github.com/manju4ever/fastify-impressions)
|
|
306
|
-
plugin to track impressions of all the routes.
|
|
311
|
+
- [`fatify-impressions`](https://github.com/manju4ever/fastify-impressions)
|
|
312
|
+
Fastify plugin to track impressions of all the routes.
|
|
307
313
|
- [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify
|
|
308
314
|
InfluxDB plugin connecting to an InfluxDB instance via the Influx default
|
|
309
315
|
package.
|
|
@@ -353,7 +359,8 @@ section.
|
|
|
353
359
|
for handling multipart/form-data, which is primarily used for uploading files.
|
|
354
360
|
- [`fastify-nats`](https://github.com/mahmed8003/fastify-nats) Plugin to share
|
|
355
361
|
[NATS](https://nats.io) client across Fastify.
|
|
356
|
-
- [`fastify-next-auth`](https://github.com/wobsoriano/fastify-next-auth)
|
|
362
|
+
- [`fastify-next-auth`](https://github.com/wobsoriano/fastify-next-auth)
|
|
363
|
+
NextAuth.js plugin for Fastify.
|
|
357
364
|
- [`fastify-no-additional-properties`](https://github.com/greguz/fastify-no-additional-properties)
|
|
358
365
|
Add `additionalProperties: false` by default to your JSON Schemas.
|
|
359
366
|
- [`fastify-no-icon`](https://github.com/jsumners/fastify-no-icon) Plugin to
|
|
@@ -412,7 +419,8 @@ section.
|
|
|
412
419
|
- [`fastify-qs`](https://github.com/webdevium/fastify-qs) A plugin for Fastify
|
|
413
420
|
that adds support for parsing URL query parameters with
|
|
414
421
|
[qs](https://github.com/ljharb/qs).
|
|
415
|
-
- [`fastify-racing`](https://github.com/metcoder95/fastify-racing) Fastify's
|
|
422
|
+
- [`fastify-racing`](https://github.com/metcoder95/fastify-racing) Fastify's
|
|
423
|
+
plugin that adds support to handle an aborted request asynchronous.
|
|
416
424
|
- [`fastify-raw-body`](https://github.com/Eomm/fastify-raw-body) Add the
|
|
417
425
|
`request.rawBody` field.
|
|
418
426
|
- [`fastify-rbac`](https://gitlab.com/m03geek/fastify-rbac) Fastify role-based
|
|
@@ -460,7 +468,9 @@ section.
|
|
|
460
468
|
client plugin for Fastify.
|
|
461
469
|
- [`fastify-socket.io`](https://github.com/alemagio/fastify-socket.io) a
|
|
462
470
|
Socket.io plugin for Fastify.
|
|
463
|
-
- [`fastify-split-validator`](https://github.com/MetCoder95/fastify-split-validator)
|
|
471
|
+
- [`fastify-split-validator`](https://github.com/MetCoder95/fastify-split-validator)
|
|
472
|
+
Small plugin to allow you use multiple validators in one route based on each
|
|
473
|
+
HTTP part of the request.
|
|
464
474
|
- [`fastify-sse`](https://github.com/lolo32/fastify-sse) to provide Server-Sent
|
|
465
475
|
Events with `reply.sse( … )` to Fastify.
|
|
466
476
|
- [`fastify-sse-v2`](https://github.com/nodefactoryio/fastify-sse-v2) to provide
|
|
@@ -499,8 +509,8 @@ section.
|
|
|
499
509
|
waterline. Decorates Fastify with waterline models.
|
|
500
510
|
- [`fastify-webpack-hmr`](https://github.com/lependu/fastify-webpack-hmr)
|
|
501
511
|
Webpack hot module reloading plugin for Fastify.
|
|
502
|
-
- [`fastify-webpack-hot`](https://github.com/gajus/fastify-webpack-hot)
|
|
503
|
-
|
|
512
|
+
- [`fastify-webpack-hot`](https://github.com/gajus/fastify-webpack-hot) Webpack
|
|
513
|
+
Hot Module Replacement for Fastify.
|
|
504
514
|
- [`fastify-ws`](https://github.com/gj/fastify-ws) WebSocket integration for
|
|
505
515
|
Fastify — with support for WebSocket lifecycle hooks instead of a single
|
|
506
516
|
handler function. Built upon [ws](https://github.com/websockets/ws) and
|
|
@@ -531,5 +541,5 @@ section.
|
|
|
531
541
|
and lightweight Sequelize plugin for Fastify.
|
|
532
542
|
|
|
533
543
|
#### [Community Tools](#community-tools)
|
|
534
|
-
- [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration
|
|
535
|
-
directory structure.
|
|
544
|
+
- [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration
|
|
545
|
+
generator by directory structure.
|
package/docs/Guides/Index.md
CHANGED
|
@@ -21,6 +21,8 @@ This table of contents is in alphabetical order.
|
|
|
21
21
|
written with a fluent API and used in Fastify.
|
|
22
22
|
+ [Getting Started](./Getting-Started.md): Introduction tutorial for Fastify.
|
|
23
23
|
This is where beginners should start.
|
|
24
|
+
+ [Migration Guide (v4)](./Migration-Guide-V4.md): Details how to migrate to
|
|
25
|
+
Fastify v4 from earlier versions.
|
|
24
26
|
+ [Migration Guide (v3)](./Migration-Guide-V3.md): Details how to migrate to
|
|
25
27
|
Fastify v3 from earlier versions.
|
|
26
28
|
+ [Plugins Guide](./Plugins-Guide.md): An informal introduction to writing
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# V4 Migration Guide
|
|
2
|
+
|
|
3
|
+
This guide is intended to help with migration from Fastify v3 to v4.
|
|
4
|
+
|
|
5
|
+
Before migrating to v4, please ensure that you have fixed all deprecation
|
|
6
|
+
warnings from v3. All v3 deprecations have been removed and they will no longer
|
|
7
|
+
work after upgrading.
|
|
8
|
+
|
|
9
|
+
## Breaking Changes
|
|
10
|
+
|
|
11
|
+
### Deprecation of `app.use()`
|
|
12
|
+
|
|
13
|
+
Starting this version of Fastify, we have deprecated the use of `app.use()`. We
|
|
14
|
+
have decided not to support the use of middlewares. Both
|
|
15
|
+
[`@fastify/middie`](https://github.com/fastify/middie) and
|
|
16
|
+
[`@fastify/express`](https://github.com/fastify/fastify-express) will still be
|
|
17
|
+
there and maintained. Use Fastify's [hooks](./Reference/Hooks.md) instead.
|
|
18
|
+
|
|
19
|
+
## Non Breaking Changes
|
|
20
|
+
|
|
21
|
+
### Change of schema for multiple types
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Since Fastiy v4 has upgraded to Ajv v8. The "type" keywords with multiple types
|
|
25
|
+
(other than with "null") are prohibited. Read more
|
|
26
|
+
['here'](https://ajv.js.org/strict-mode.html#strict-types)
|
|
27
|
+
|
|
28
|
+
You may encounter a console warning such as
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
strict mode: use allowUnionTypes to allow union type keyword at "#/properties/image" (strictTypes)
|
|
32
|
+
```
|
|
33
|
+
So schemas like below will need to be changed from
|
|
34
|
+
```
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {
|
|
37
|
+
api_key: { type: 'string' },
|
|
38
|
+
image: { type: ['object', 'array'] }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
to
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
type: 'object',
|
|
46
|
+
properties: {
|
|
47
|
+
api_key: { type: 'string' },
|
|
48
|
+
image: {
|
|
49
|
+
anyOf: [
|
|
50
|
+
{ type: 'array' },
|
|
51
|
+
{ type: 'object' }
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
@@ -36,8 +36,9 @@ The sample provided allows you to easily build serverless web
|
|
|
36
36
|
applications/services and RESTful APIs using Fastify on top of AWS Lambda and
|
|
37
37
|
Amazon API Gateway.
|
|
38
38
|
|
|
39
|
-
*Note: Using
|
|
40
|
-
is just one
|
|
39
|
+
*Note: Using
|
|
40
|
+
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify) is just one
|
|
41
|
+
possible way.*
|
|
41
42
|
|
|
42
43
|
### app.js
|
|
43
44
|
|
|
@@ -64,8 +65,7 @@ if (require.main === module) {
|
|
|
64
65
|
|
|
65
66
|
When executed in your lambda function we do not need to listen to a specific
|
|
66
67
|
port, so we just export the wrapper function `init` in this case. The
|
|
67
|
-
[`lambda.js`](#lambdajs) file
|
|
68
|
-
will use this export.
|
|
68
|
+
[`lambda.js`](#lambdajs) file will use this export.
|
|
69
69
|
|
|
70
70
|
When you execute your Fastify application like always, i.e. `node app.js` *(the
|
|
71
71
|
detection for this could be `require.main === module`)*, you can normally listen
|
|
@@ -93,11 +93,10 @@ exports.handler = proxy;
|
|
|
93
93
|
We just require
|
|
94
94
|
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify) (make sure
|
|
95
95
|
you install the dependency `npm i @fastify/aws-lambda`) and our
|
|
96
|
-
[`app.js`](#appjs) file and call
|
|
97
|
-
the
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
passed to the `proxy` function of
|
|
96
|
+
[`app.js`](#appjs) file and call the exported `awsLambdaFastify` function with
|
|
97
|
+
the `app` as the only parameter. The resulting `proxy` function has the correct
|
|
98
|
+
signature to be used as a lambda `handler` function. This way all the incoming
|
|
99
|
+
events (API Gateway requests) are passed to the `proxy` function of
|
|
101
100
|
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify).
|
|
102
101
|
|
|
103
102
|
### Example
|
|
@@ -237,7 +236,9 @@ gcloud functions logs read
|
|
|
237
236
|
|
|
238
237
|
#### Example request to `/hello` endpoint
|
|
239
238
|
```bash
|
|
240
|
-
curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me
|
|
239
|
+
curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me \
|
|
240
|
+
-H "Content-Type: application/json" \
|
|
241
|
+
-d '{ "name": "Fastify" }'
|
|
241
242
|
{"message":"Hello Fastify!"}
|
|
242
243
|
```
|
|
243
244
|
|
|
@@ -443,8 +444,8 @@ Then it should work fine
|
|
|
443
444
|
## Vercel
|
|
444
445
|
|
|
445
446
|
[Vercel](https://vercel.com) provides zero-configuration deployment for Node.js
|
|
446
|
-
applications. To use it now, it is as simple as configuring your
|
|
447
|
-
|
|
447
|
+
applications. To use it now, it is as simple as configuring your `vercel.json`
|
|
448
|
+
file like the following:
|
|
448
449
|
|
|
449
450
|
```json
|
|
450
451
|
{
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## `Content-Type` Parser
|
|
4
4
|
Natively, Fastify only supports `'application/json'` and `'text/plain'` content
|
|
5
|
-
types. If the content type is not one of these, an
|
|
6
|
-
error will be thrown.
|
|
5
|
+
types. If the content type is not one of these, an
|
|
6
|
+
`FST_ERR_CTP_INVALID_MEDIA_TYPE` error will be thrown.
|
|
7
7
|
|
|
8
|
-
The default charset is `utf-8`. If you need to support different content
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
The default charset is `utf-8`. If you need to support different content types,
|
|
9
|
+
you can use the `addContentTypeParser` API. *The default JSON and/or plain text
|
|
10
|
+
parser can be changed or removed.*
|
|
11
11
|
|
|
12
12
|
*Note: If you decide to specify your own content type with the `Content-Type`
|
|
13
13
|
header, UTF-8 will not be the default. Be sure to include UTF-8 like this
|
|
@@ -21,8 +21,11 @@ available only in that scope and its children.
|
|
|
21
21
|
Fastify automatically adds the parsed request payload to the [Fastify
|
|
22
22
|
request](./Request.md) object which you can access with `request.body`.
|
|
23
23
|
|
|
24
|
-
Note that for `GET` and `HEAD` requests the payload is never parsed. For
|
|
25
|
-
|
|
24
|
+
Note that for `GET` and `HEAD` requests the payload is never parsed. For
|
|
25
|
+
`OPTIONS` and `DELETE` requests the payload is only parsed if the content type
|
|
26
|
+
is given in the content-type header. If it is not given, the
|
|
27
|
+
[catch-all](#catch-all) parser is not executed as with `POST`, `PUT` and
|
|
28
|
+
`PATCH`, but the payload is simply not parsed.
|
|
26
29
|
|
|
27
30
|
### Usage
|
|
28
31
|
```js
|
|
@@ -69,7 +72,8 @@ more specific one, like in the example below.
|
|
|
69
72
|
fastify.addContentTypeParser('application/vnd.custom+xml', (request, body, done) => {} )
|
|
70
73
|
fastify.addContentTypeParser('application/vnd.custom', (request, body, done) => {} )
|
|
71
74
|
|
|
72
|
-
// Here the desired behavior is achieved because fastify first tries to match the
|
|
75
|
+
// Here the desired behavior is achieved because fastify first tries to match the
|
|
76
|
+
// `application/vnd.custom+xml` content type parser
|
|
73
77
|
fastify.addContentTypeParser('application/vnd.custom', (request, body, done) => {} )
|
|
74
78
|
fastify.addContentTypeParser('application/vnd.custom+xml', (request, body, done) => {} )
|
|
75
79
|
```
|
|
@@ -115,9 +119,9 @@ fastify.removeContentTypeParser(['application/json', 'text/plain'])
|
|
|
115
119
|
In the example from just above, it is noticeable that we need to specify each
|
|
116
120
|
content type that we want to remove. To solve this problem Fastify provides the
|
|
117
121
|
`removeAllContentTypeParsers` API. This can be used to remove all currently
|
|
118
|
-
existing content type parsers. In the example below we achieve the same
|
|
119
|
-
|
|
120
|
-
|
|
122
|
+
existing content type parsers. In the example below we achieve the same as in
|
|
123
|
+
the example above except that we do not need to specify each content type to
|
|
124
|
+
delete. Just like `removeContentTypeParser`, this API supports encapsulation.
|
|
121
125
|
The API is especially useful if you want to register a [catch-all content type
|
|
122
126
|
parser](#catch-all) that should be executed for every content type and the
|
|
123
127
|
built-in parsers should be ignored as well.
|
|
@@ -223,8 +227,8 @@ fastify.route({
|
|
|
223
227
|
For piping file uploads you may want to check out [this
|
|
224
228
|
plugin](https://github.com/fastify/fastify-multipart).
|
|
225
229
|
|
|
226
|
-
If you want the content type parser to be executed on all content types
|
|
227
|
-
|
|
230
|
+
If you want the content type parser to be executed on all content types and not
|
|
231
|
+
only on those that don't have a specific one, you should call the
|
|
228
232
|
`removeAllContentTypeParsers` method first.
|
|
229
233
|
|
|
230
234
|
```js
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -54,9 +54,10 @@ When a custom error handler has been defined through
|
|
|
54
54
|
receive the error passed to the `done()` callback (or through other supported
|
|
55
55
|
automatic error handling mechanisms). If `setErrorHandler` has been used
|
|
56
56
|
multiple times to define multiple handlers, the error will be routed to the most
|
|
57
|
-
precedent handler defined within the error [encapsulation
|
|
58
|
-
Error handlers are fully encapsulated, so a
|
|
59
|
-
plugin will limit the error handler to that
|
|
57
|
+
precedent handler defined within the error [encapsulation
|
|
58
|
+
context](./Encapsulation.md). Error handlers are fully encapsulated, so a
|
|
59
|
+
`setErrorHandler` call within a plugin will limit the error handler to that
|
|
60
|
+
plugin's context.
|
|
60
61
|
|
|
61
62
|
The root error handler is Fastify's generic error handler. This error handler
|
|
62
63
|
will use the headers and status code in the `Error` object, if they exist. The
|
|
@@ -72,8 +73,8 @@ Some things to consider in your custom error handler:
|
|
|
72
73
|
- strings, buffers, and streams are sent to the client, with appropriate
|
|
73
74
|
headers (no serialization)
|
|
74
75
|
|
|
75
|
-
- You can throw a new error in your custom error handler
|
|
76
|
-
|
|
76
|
+
- You can throw a new error in your custom error handler - errors (new error or
|
|
77
|
+
the received error parameter re-thrown) - will call the parent `errorHandler`.
|
|
77
78
|
- `onError` hook will be triggered once only for the first error being thrown.
|
|
78
79
|
- an error will not be triggered twice from a lifecycle hook - Fastify
|
|
79
80
|
internally monitors the error invocation to avoid infinite loops for errors
|
|
@@ -9,10 +9,12 @@ By default, `register` creates a *new scope*, this means that if you make some
|
|
|
9
9
|
changes to the Fastify instance (via `decorate`), this change will not be
|
|
10
10
|
reflected by the current context ancestors, but only by its descendants. This
|
|
11
11
|
feature allows us to achieve plugin *encapsulation* and *inheritance*, in this
|
|
12
|
-
way we create a *directed acyclic graph* (DAG) and we will not have issues
|
|
13
|
-
by cross dependencies.
|
|
12
|
+
way we create a *directed acyclic graph* (DAG) and we will not have issues
|
|
13
|
+
caused by cross dependencies.
|
|
14
14
|
|
|
15
|
-
You may have already seen in the [Getting
|
|
15
|
+
You may have already seen in the [Getting
|
|
16
|
+
Started]((../Guides/Getting-Started.md#your-first-plugin)) guide how easy it is
|
|
17
|
+
to use this API:
|
|
16
18
|
```
|
|
17
19
|
fastify.register(plugin, [options])
|
|
18
20
|
```
|
|
@@ -96,9 +98,9 @@ If you pass an option with the key `prefix` with a `string` value, Fastify will
|
|
|
96
98
|
use it to prefix all the routes inside the register, for more info check
|
|
97
99
|
[here](./Routes.md#route-prefixing).
|
|
98
100
|
|
|
99
|
-
Be aware that if you
|
|
100
|
-
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) this option will
|
|
101
|
-
not work.
|
|
101
|
+
Be aware that if you wrap your routes with
|
|
102
|
+
[`fastify-plugin`](https://github.com/fastify/fastify-plugin), this option will
|
|
103
|
+
not work (there is a [workaround](./Routes.md#fastify-plugin) available).
|
|
102
104
|
|
|
103
105
|
#### Error handling
|
|
104
106
|
<a id="error-handling"></a>
|
package/docs/Reference/Reply.md
CHANGED
|
@@ -129,7 +129,11 @@ fastify.get('/', async function (req, rep) {
|
|
|
129
129
|
Sets a response header. If the value is omitted or undefined, it is coerced to
|
|
130
130
|
`''`.
|
|
131
131
|
|
|
132
|
-
> Note: the header's value must be properly encoded using
|
|
132
|
+
> Note: the header's value must be properly encoded using
|
|
133
|
+
> [`encodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
|
|
134
|
+
> or similar modules such as
|
|
135
|
+
> [`encodeurl`](https://www.npmjs.com/package/encodeurl). Invalid characters
|
|
136
|
+
> will result in a 500 `TypeError` response.
|
|
133
137
|
|
|
134
138
|
For more information, see
|
|
135
139
|
[`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_response_setheader_name_value).
|
|
@@ -208,11 +212,16 @@ Returns a boolean indicating if the specified header has been set.
|
|
|
208
212
|
### .trailer(key, function)
|
|
209
213
|
<a id="trailer"></a>
|
|
210
214
|
|
|
211
|
-
Sets a response trailer. Trailer is usually used when you need a header that
|
|
215
|
+
Sets a response trailer. Trailer is usually used when you need a header that
|
|
216
|
+
requires heavy resources to be sent after the `data`, for example,
|
|
217
|
+
`Server-Timing` and `Etag`. It can ensure the client receives the response data
|
|
218
|
+
as soon as possible.
|
|
212
219
|
|
|
213
|
-
*Note: The header `Transfer-Encoding: chunked` will be added once you use the
|
|
220
|
+
*Note: The header `Transfer-Encoding: chunked` will be added once you use the
|
|
221
|
+
trailer. It is a hard requirement for using trailer in Node.js.*
|
|
214
222
|
|
|
215
|
-
*Note: Currently, the computation function only supports synchronous function.
|
|
223
|
+
*Note: Currently, the computation function only supports synchronous function.
|
|
224
|
+
That means `async-await` and `promise` are not supported.*
|
|
216
225
|
|
|
217
226
|
```js
|
|
218
227
|
reply.trailer('server-timing', function() {
|
|
@@ -254,7 +263,11 @@ reply.getTrailer('server-timing') // undefined
|
|
|
254
263
|
Redirects a request to the specified URL, the status code is optional, default
|
|
255
264
|
to `302` (if status code is not already set by calling `code`).
|
|
256
265
|
|
|
257
|
-
> Note: the input URL must be properly encoded using
|
|
266
|
+
> Note: the input URL must be properly encoded using
|
|
267
|
+
> [`encodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
|
|
268
|
+
> or similar modules such as
|
|
269
|
+
> [`encodeurl`](https://www.npmjs.com/package/encodeurl). Invalid URLs will
|
|
270
|
+
> result in a 500 `TypeError` response.
|
|
258
271
|
|
|
259
272
|
Example (no `reply.code()` call) sets status code to `302` and redirects to
|
|
260
273
|
`/home`
|
|
@@ -310,7 +323,8 @@ Sets the content type for the response. This is a shortcut for
|
|
|
310
323
|
```js
|
|
311
324
|
reply.type('text/html')
|
|
312
325
|
```
|
|
313
|
-
If the `Content-Type` has a JSON subtype, and the charset parameter is not set,
|
|
326
|
+
If the `Content-Type` has a JSON subtype, and the charset parameter is not set,
|
|
327
|
+
`utf-8` will be used as the charset by default.
|
|
314
328
|
|
|
315
329
|
### .serializer(func)
|
|
316
330
|
<a id="serializer"></a>
|
|
@@ -366,22 +380,22 @@ Another example of the misuse of `Reply.raw` is explained in
|
|
|
366
380
|
<a id="sent"></a>
|
|
367
381
|
|
|
368
382
|
As the name suggests, `.sent` is a property to indicate if a response has been
|
|
369
|
-
sent via `reply.send()`.
|
|
370
|
-
|
|
383
|
+
sent via `reply.send()`. It will also be `true` in case `reply.hijack()` was
|
|
384
|
+
used.
|
|
371
385
|
|
|
372
386
|
In case a route handler is defined as an async function or it returns a promise,
|
|
373
387
|
it is possible to call `reply.hijack()` to indicate that the automatic
|
|
374
388
|
invocation of `reply.send()` once the handler promise resolve should be skipped.
|
|
375
|
-
By calling `reply.hijack()`, an application claims full responsibility for
|
|
376
|
-
|
|
389
|
+
By calling `reply.hijack()`, an application claims full responsibility for the
|
|
390
|
+
low-level request and response. Moreover, hooks will not be invoked.
|
|
377
391
|
|
|
378
|
-
*Modifying the `.sent` property directly is deprecated. Please use the
|
|
379
|
-
`.hijack()` method to achieve the same effect.*
|
|
392
|
+
*Modifying the `.sent` property directly is deprecated. Please use the
|
|
393
|
+
aforementioned `.hijack()` method to achieve the same effect.*
|
|
380
394
|
|
|
381
395
|
<a name="hijack"></a>
|
|
382
396
|
### .hijack()
|
|
383
|
-
Sometimes you might need to halt the execution of the normal request lifecycle
|
|
384
|
-
handle sending the response manually.
|
|
397
|
+
Sometimes you might need to halt the execution of the normal request lifecycle
|
|
398
|
+
and handle sending the response manually.
|
|
385
399
|
|
|
386
400
|
To achieve this, Fastify provides the `reply.hijack()` method that can be called
|
|
387
401
|
during the request lifecycle (At any point before `reply.send()` is called), and
|
|
@@ -561,8 +575,8 @@ fastify.setNotFoundHandler(function (request, reply) {
|
|
|
561
575
|
<a id="payload-type"></a>
|
|
562
576
|
|
|
563
577
|
The type of the sent payload (after serialization and going through any
|
|
564
|
-
[`onSend` hooks](./Hooks.md#onsend)) must be one of the following
|
|
565
|
-
|
|
578
|
+
[`onSend` hooks](./Hooks.md#onsend)) must be one of the following types,
|
|
579
|
+
otherwise, an error will be thrown:
|
|
566
580
|
|
|
567
581
|
- `string`
|
|
568
582
|
- `Buffer`
|
|
@@ -13,7 +13,7 @@ Request is a core Fastify object containing the following fields:
|
|
|
13
13
|
- [`headers`](#headers) - the headers getter and setter
|
|
14
14
|
- `raw` - the incoming HTTP request from Node core
|
|
15
15
|
- `server` - The Fastify server instance, scoped to the current [encapsulation
|
|
16
|
-
|
|
16
|
+
context](./Encapsulation.md)
|
|
17
17
|
- `id` - the request ID
|
|
18
18
|
- `log` - the logger instance of the incoming request
|
|
19
19
|
- `ip` - the IP address of the incoming request
|
|
@@ -55,8 +55,8 @@ This operation will add to the request headers the new values that can be read
|
|
|
55
55
|
calling `request.headers.bar`. Moreover, you can still access the standard
|
|
56
56
|
request's headers with the `request.raw.headers` property.
|
|
57
57
|
|
|
58
|
-
> Note: For performance reason on `not found` route, you may see that we will
|
|
59
|
-
an extra property `Symbol('fastify.RequestAcceptVersion')` on the headers.
|
|
58
|
+
> Note: For performance reason on `not found` route, you may see that we will
|
|
59
|
+
add an extra property `Symbol('fastify.RequestAcceptVersion')` on the headers.
|
|
60
60
|
|
|
61
61
|
```js
|
|
62
62
|
fastify.post('/:params', options, function (request, reply) {
|