fastify 4.0.0-rc.5 → 4.0.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/.markdownlint-cli2.yaml +22 -0
- package/GOVERNANCE.md +30 -20
- package/PROJECT_CHARTER.md +48 -17
- package/README.md +164 -77
- package/SECURITY.md +55 -44
- package/build/build-error-serializer.js +12 -7
- package/docs/Guides/Benchmarking.md +2 -0
- package/docs/Guides/Delay-Accepting-Requests.md +98 -90
- package/docs/Guides/Ecosystem.md +48 -38
- package/docs/Guides/Index.md +2 -0
- package/docs/Guides/Migration-Guide-V3.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +55 -0
- package/docs/Guides/Plugins-Guide.md +3 -3
- package/docs/Guides/Prototype-Poisoning.md +1 -1
- package/docs/Guides/Recommendations.md +2 -2
- package/docs/Guides/Serverless.md +17 -16
- package/docs/Guides/Write-Plugin.md +3 -3
- package/docs/Reference/ContentTypeParser.md +17 -13
- package/docs/Reference/Errors.md +6 -5
- package/docs/Reference/Middleware.md +3 -3
- 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 +113 -38
- 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 +47 -35
- package/fastify.js +1 -1
- package/lib/error-serializer.js +32 -204
- package/lib/pluginUtils.js +10 -0
- package/lib/reply.js +1 -1
- package/lib/schemas.js +3 -0
- package/lib/validation.js +1 -1
- package/package.json +6 -4
- package/test/build/error-serializer.test.js +28 -0
- package/test/{internals → build}/version.test.js +1 -1
- package/test/plugin.test.js +32 -0
- package/test/reply-error.test.js +7 -1
- package/test/schema-serialization.test.js +30 -0
- package/docs/Migration-Guide-V4.md +0 -12
package/docs/Reference/Server.md
CHANGED
|
@@ -136,16 +136,23 @@ use. Also, when `serverFactory` option is specified, this option is ignored.
|
|
|
136
136
|
### `forceCloseConnections`
|
|
137
137
|
<a id="forcecloseconnections"></a>
|
|
138
138
|
|
|
139
|
-
When set to `true`, upon [`close`](#close) the server will iterate the
|
|
140
|
-
|
|
139
|
+
When set to `true`, upon [`close`](#close) the server will iterate the current
|
|
140
|
+
persistent connections and [destroy their
|
|
141
|
+
sockets](https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketdestroyerror).
|
|
141
142
|
|
|
142
|
-
> Important: connections are not inspected to determine if requests have been
|
|
143
|
+
> Important: connections are not inspected to determine if requests have been
|
|
144
|
+
> completed.
|
|
143
145
|
|
|
144
|
-
Fastify will prefer the HTTP server's
|
|
146
|
+
Fastify will prefer the HTTP server's
|
|
147
|
+
[`closeAllConnections`](https://nodejs.org/dist/latest-v18.x/docs/api/http.html#servercloseallconnections)
|
|
148
|
+
method if supported, otherwise it will use internal connection tracking.
|
|
145
149
|
|
|
146
|
-
When set to `"idle"`, upon [`close`](#close) the server will iterate the
|
|
147
|
-
|
|
148
|
-
The value is supported only if the HTTP server
|
|
150
|
+
When set to `"idle"`, upon [`close`](#close) the server will iterate the current
|
|
151
|
+
persistent connections which are not sending a request or waiting for a response
|
|
152
|
+
and destroy their sockets. The value is supported only if the HTTP server
|
|
153
|
+
supports the
|
|
154
|
+
[`closeIdleConnections`](https://nodejs.org/dist/latest-v18.x/docs/api/http.html#servercloseidleconnections)
|
|
155
|
+
method, otherwise attempting to set it will throw an exception.
|
|
149
156
|
|
|
150
157
|
+ Default: `"idle"` if the HTTP server allows it, `false` otherwise
|
|
151
158
|
|
|
@@ -184,10 +191,10 @@ server is deployed without a reverse proxy in front.
|
|
|
184
191
|
<a id="factory-ignore-slash"></a>
|
|
185
192
|
|
|
186
193
|
Fastify uses [find-my-way](https://github.com/delvedor/find-my-way) to handle
|
|
187
|
-
routing. By default, Fastify is set to take into account the trailing slashes.
|
|
188
|
-
Paths like `/foo` and `/foo/` will be treated as different paths. If you want
|
|
189
|
-
|
|
190
|
-
point to the same route. This option applies to *all* route registrations for
|
|
194
|
+
routing. By default, Fastify is set to take into account the trailing slashes.
|
|
195
|
+
Paths like `/foo` and `/foo/` will be treated as different paths. If you want to
|
|
196
|
+
change this, set this flag to `true`. That way, both `/foo` and `/foo/` will
|
|
197
|
+
point to the same route. This option applies to *all* route registrations for
|
|
191
198
|
the resulting server instance.
|
|
192
199
|
|
|
193
200
|
+ Default: `false`
|
|
@@ -214,9 +221,12 @@ fastify.get('/bar', function (req, reply) {
|
|
|
214
221
|
Fastify uses [find-my-way](https://github.com/delvedor/find-my-way) to handle
|
|
215
222
|
routing. You can use `ignoreDuplicateSlashes` option to remove duplicate slashes
|
|
216
223
|
from the path. It removes duplicate slashes in the route path and in the request
|
|
217
|
-
URL. This option applies to *all* route registrations for the resulting server
|
|
224
|
+
URL. This option applies to *all* route registrations for the resulting server
|
|
225
|
+
instance.
|
|
218
226
|
|
|
219
|
-
Note that when `ignoreTrailingSlash` and `ignoreDuplicateSlashes` are both set
|
|
227
|
+
Note that when `ignoreTrailingSlash` and `ignoreDuplicateSlashes` are both set
|
|
228
|
+
to true, Fastify will remove duplicate slashes, and then trailing slashes,
|
|
229
|
+
meaning //a//b//c// will be converted to /a/b/c.
|
|
220
230
|
|
|
221
231
|
+ Default: `false`
|
|
222
232
|
|
|
@@ -257,8 +267,8 @@ Defines the maximum payload, in bytes, the server is allowed to accept.
|
|
|
257
267
|
Defines what action the framework must take when parsing a JSON object with
|
|
258
268
|
`__proto__`. This functionality is provided by
|
|
259
269
|
[secure-json-parse](https://github.com/fastify/secure-json-parse). See
|
|
260
|
-
[Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more
|
|
261
|
-
|
|
270
|
+
[Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more details about
|
|
271
|
+
prototype poisoning attacks.
|
|
262
272
|
|
|
263
273
|
Possible values are `'error'`, `'remove'` and `'ignore'`.
|
|
264
274
|
|
|
@@ -270,8 +280,8 @@ Possible values are `'error'`, `'remove'` and `'ignore'`.
|
|
|
270
280
|
Defines what action the framework must take when parsing a JSON object with
|
|
271
281
|
`constructor`. This functionality is provided by
|
|
272
282
|
[secure-json-parse](https://github.com/fastify/secure-json-parse). See
|
|
273
|
-
[Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more
|
|
274
|
-
|
|
283
|
+
[Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more details about
|
|
284
|
+
prototype poisoning attacks.
|
|
275
285
|
|
|
276
286
|
Possible values are `'error'`, `'remove'` and `'ignore'`.
|
|
277
287
|
|
|
@@ -462,7 +472,8 @@ way query strings are handled take a look at
|
|
|
462
472
|
### `allowUnsafeRegex`
|
|
463
473
|
<a id="factory-allow-unsafe-regex"></a>
|
|
464
474
|
|
|
465
|
-
The allowUnsafeRegex setting is false by default, so routes only allow safe
|
|
475
|
+
The allowUnsafeRegex setting is false by default, so routes only allow safe
|
|
476
|
+
regular expressions. To use unsafe expressions, set allowUnsafeRegex to true.
|
|
466
477
|
|
|
467
478
|
```js
|
|
468
479
|
fastify.get('/user/:id(^([0-9]+){4}$)', (request, reply) => {
|
|
@@ -470,8 +481,8 @@ fastify.get('/user/:id(^([0-9]+){4}$)', (request, reply) => {
|
|
|
470
481
|
})
|
|
471
482
|
```
|
|
472
483
|
|
|
473
|
-
Under the hood: [FindMyWay](https://github.com/delvedor/find-my-way)
|
|
474
|
-
|
|
484
|
+
Under the hood: [FindMyWay](https://github.com/delvedor/find-my-way) More info
|
|
485
|
+
about safe regexp: [Safe-regex2](https://www.npmjs.com/package/safe-regex2)
|
|
475
486
|
|
|
476
487
|
|
|
477
488
|
### `requestIdHeader`
|
|
@@ -562,8 +573,8 @@ derive <code>request.hostname</code> and <code>request.protocol</code>**
|
|
|
562
573
|
|
|
563
574
|
The maximum amount of time in *milliseconds* in which a plugin can load. If not,
|
|
564
575
|
[`ready`](#ready) will complete with an `Error` with code
|
|
565
|
-
`'ERR_AVVIO_PLUGIN_TIMEOUT'`. When set to `0`, disables this check. This
|
|
566
|
-
[avvio](https://www.npmjs.com/package/avvio) 's `timeout` parameter.
|
|
576
|
+
`'ERR_AVVIO_PLUGIN_TIMEOUT'`. When set to `0`, disables this check. This
|
|
577
|
+
controls [avvio](https://www.npmjs.com/package/avvio) 's `timeout` parameter.
|
|
567
578
|
|
|
568
579
|
+ Default: `10000`
|
|
569
580
|
|
|
@@ -648,7 +659,8 @@ the incoming request as usual.
|
|
|
648
659
|
<a id="factory-ajv"></a>
|
|
649
660
|
|
|
650
661
|
Configure the Ajv v8 instance used by Fastify without providing a custom one.
|
|
651
|
-
The default configuration is explained in the
|
|
662
|
+
The default configuration is explained in the
|
|
663
|
+
[#schema-validator](Validation-and-Serialization.md#schema-validator) section.
|
|
652
664
|
|
|
653
665
|
```js
|
|
654
666
|
const fastify = require('fastify')({
|
|
@@ -743,7 +755,11 @@ function defaultClientErrorHandler (err, socket) {
|
|
|
743
755
|
this.log.trace({ err }, 'client error')
|
|
744
756
|
|
|
745
757
|
if (socket.writable) {
|
|
746
|
-
socket.end(
|
|
758
|
+
socket.end([
|
|
759
|
+
'HTTP/1.1 400 Bad Request',
|
|
760
|
+
`Content-Length: ${body.length}`,
|
|
761
|
+
`Content-Type: application/json\r\n\r\n${body}`
|
|
762
|
+
].join('\r\n'))
|
|
747
763
|
}
|
|
748
764
|
}
|
|
749
765
|
```
|
|
@@ -767,7 +783,11 @@ const fastify = require('fastify')({
|
|
|
767
783
|
this.log.trace({ err }, 'client error')
|
|
768
784
|
|
|
769
785
|
// the handler is responsible for generating a valid HTTP response
|
|
770
|
-
socket.end(
|
|
786
|
+
socket.end([
|
|
787
|
+
'HTTP/1.1 400 Bad Request',
|
|
788
|
+
`Content-Length: ${body.length}`,
|
|
789
|
+
`Content-Type: application/json\r\n\r\n${body}`
|
|
790
|
+
].join('\r\n'))
|
|
771
791
|
}
|
|
772
792
|
})
|
|
773
793
|
```
|
|
@@ -867,17 +887,17 @@ fastify.ready().then(() => {
|
|
|
867
887
|
#### listen
|
|
868
888
|
<a id="listen"></a>
|
|
869
889
|
|
|
870
|
-
Starts the server and internally waits for the `.ready()` event. The
|
|
871
|
-
|
|
890
|
+
Starts the server and internally waits for the `.ready()` event. The signature
|
|
891
|
+
is `.listen([options][, callback])`. Both the `options` object and the
|
|
872
892
|
`callback` parameters follow the [Node.js
|
|
873
893
|
core][https://nodejs.org/api/net.html#serverlistenoptions-callback] parameter
|
|
874
894
|
definitions.
|
|
875
895
|
|
|
876
|
-
By default, the server will listen on the address(es) resolved by `localhost`
|
|
877
|
-
specific host is provided. If listening on any available interface is
|
|
878
|
-
then specifying `0.0.0.0` for the address will listen on all IPv4
|
|
879
|
-
The following table details the possible values for `host` when
|
|
880
|
-
`localhost`, and what the result of those values for `host` will be.
|
|
896
|
+
By default, the server will listen on the address(es) resolved by `localhost`
|
|
897
|
+
when no specific host is provided. If listening on any available interface is
|
|
898
|
+
desired, then specifying `0.0.0.0` for the address will listen on all IPv4
|
|
899
|
+
addresses. The following table details the possible values for `host` when
|
|
900
|
+
targeting `localhost`, and what the result of those values for `host` will be.
|
|
881
901
|
|
|
882
902
|
Host | IPv4 | IPv6
|
|
883
903
|
--------------|------|-------
|
|
@@ -962,9 +982,9 @@ fastify.listen({
|
|
|
962
982
|
#### addresses
|
|
963
983
|
<a id="addresses"></a>
|
|
964
984
|
|
|
965
|
-
This method returns an array of addresses that the server is listening on.
|
|
966
|
-
|
|
967
|
-
|
|
985
|
+
This method returns an array of addresses that the server is listening on. If
|
|
986
|
+
you call it before `listen()` is called or after the `close()` function, it will
|
|
987
|
+
return an empty array.
|
|
968
988
|
|
|
969
989
|
```js
|
|
970
990
|
await fastify.listen({ port: 8080 })
|
|
@@ -980,9 +1000,10 @@ Note that the array contains the `fastify.server.address()` too.
|
|
|
980
1000
|
#### getDefaultRoute
|
|
981
1001
|
<a id="getDefaultRoute"></a>
|
|
982
1002
|
|
|
983
|
-
The `defaultRoute` handler handles requests that do not match any URL specified
|
|
984
|
-
This defaults to the 404 handler, but can be
|
|
985
|
-
Method to get the
|
|
1003
|
+
The `defaultRoute` handler handles requests that do not match any URL specified
|
|
1004
|
+
by your Fastify application. This defaults to the 404 handler, but can be
|
|
1005
|
+
overridden with [setDefaultRoute](#setdefaultroute). Method to get the
|
|
1006
|
+
`defaultRoute` for the server:
|
|
986
1007
|
|
|
987
1008
|
```js
|
|
988
1009
|
const defaultRoute = fastify.getDefaultRoute()
|
|
@@ -991,9 +1012,10 @@ const defaultRoute = fastify.getDefaultRoute()
|
|
|
991
1012
|
#### setDefaultRoute
|
|
992
1013
|
<a id="setDefaultRoute"></a>
|
|
993
1014
|
|
|
994
|
-
**Note**: The default 404 handler, or one set using `setNotFoundHandler`, will
|
|
995
|
-
|
|
996
|
-
|
|
1015
|
+
**Note**: The default 404 handler, or one set using `setNotFoundHandler`, will
|
|
1016
|
+
never trigger if the default route is overridden. Use
|
|
1017
|
+
[setNotFoundHandler](#setnotfoundhandler) if you want to customize 404 handling
|
|
1018
|
+
instead. Method to set the `defaultRoute` for the server:
|
|
997
1019
|
|
|
998
1020
|
```js
|
|
999
1021
|
const defaultRoute = function (req, res) {
|
|
@@ -1089,8 +1111,8 @@ fastify.register(function (instance, opts, done) {
|
|
|
1089
1111
|
#### pluginName
|
|
1090
1112
|
<a id="pluginName"></a>
|
|
1091
1113
|
|
|
1092
|
-
Name of the current plugin. The root plugin is called `'fastify'`.
|
|
1093
|
-
|
|
1114
|
+
Name of the current plugin. The root plugin is called `'fastify'`. There are
|
|
1115
|
+
three ways to define a name (in order).
|
|
1094
1116
|
|
|
1095
1117
|
1. If you use [fastify-plugin](https://github.com/fastify/fastify-plugin) the
|
|
1096
1118
|
metadata `name` is used.
|
|
@@ -1112,10 +1134,9 @@ involved plugins in the format of `fastify -> plugin-A -> plugin-B`.
|
|
|
1112
1134
|
#### hasPlugin
|
|
1113
1135
|
<a id="hasPlugin"></a>
|
|
1114
1136
|
|
|
1115
|
-
Method to check if a specific plugin has been registered.
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
Otherwise, returns `false`.
|
|
1137
|
+
Method to check if a specific plugin has been registered. Relies on the plugin
|
|
1138
|
+
metadata name. Returns `true` if the plugin is registered. Otherwise, returns
|
|
1139
|
+
`false`.
|
|
1119
1140
|
|
|
1120
1141
|
```js
|
|
1121
1142
|
const fastify = require('fastify')()
|
|
@@ -1144,7 +1165,8 @@ used by plugins.
|
|
|
1144
1165
|
#### inject
|
|
1145
1166
|
<a id="inject"></a>
|
|
1146
1167
|
|
|
1147
|
-
Fake HTTP injection (for testing purposes)
|
|
1168
|
+
Fake HTTP injection (for testing purposes)
|
|
1169
|
+
[here](../Guides/Testing.md#benefits-of-using-fastifyinject).
|
|
1148
1170
|
|
|
1149
1171
|
#### addSchema
|
|
1150
1172
|
<a id="add-schema"></a>
|
|
@@ -1240,10 +1262,10 @@ unknown to Fastify. See [issue
|
|
|
1240
1262
|
#2446](https://github.com/fastify/fastify/issues/2446) for an example of what
|
|
1241
1263
|
this property helps to resolve.
|
|
1242
1264
|
|
|
1243
|
-
Another use case is to tweak all the schemas processing.
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1265
|
+
Another use case is to tweak all the schemas processing. Doing so it is possible
|
|
1266
|
+
to use Ajv v8 JTD or Standalone feature. To use such as JTD or the Standalone
|
|
1267
|
+
mode, refers to the [`@fastify/ajv-compiler`
|
|
1268
|
+
documentation](https://github.com/fastify/ajv-compiler#usage).
|
|
1247
1269
|
|
|
1248
1270
|
```js
|
|
1249
1271
|
const fastify = Fastify({
|
|
@@ -1251,7 +1273,8 @@ const fastify = Fastify({
|
|
|
1251
1273
|
/**
|
|
1252
1274
|
* This factory is called whenever `fastify.register()` is called.
|
|
1253
1275
|
* It may receive as input the schemas of the parent context if some schemas have been added.
|
|
1254
|
-
* @param {object} parentSchemas these schemas will be returned by the
|
|
1276
|
+
* @param {object} parentSchemas these schemas will be returned by the
|
|
1277
|
+
* `getSchemas()` method function of the returned `bucket`.
|
|
1255
1278
|
*/
|
|
1256
1279
|
bucket: function factory (parentSchemas) {
|
|
1257
1280
|
return {
|
|
@@ -1286,7 +1309,8 @@ const fastify = Fastify({
|
|
|
1286
1309
|
* It may be called whenever `fastify.register()` is called only if new schemas have been added to the
|
|
1287
1310
|
* encapsulation context.
|
|
1288
1311
|
* It may receive as input the schemas of the parent context if some schemas have been added.
|
|
1289
|
-
* @param {object} externalSchemas these schemas will be returned by the
|
|
1312
|
+
* @param {object} externalSchemas these schemas will be returned by the
|
|
1313
|
+
* `bucket.getSchemas()`. Needed to resolve the external references $ref.
|
|
1290
1314
|
* @param {object} ajvServerOption the server `ajv` options to build your compilers accordingly
|
|
1291
1315
|
*/
|
|
1292
1316
|
buildValidator: function factory (externalSchemas, ajvServerOption) {
|
|
@@ -1303,8 +1327,10 @@ const fastify = Fastify({
|
|
|
1303
1327
|
* It may be called whenever `fastify.register()` is called only if new schemas have been added to the
|
|
1304
1328
|
* encapsulation context.
|
|
1305
1329
|
* It may receive as input the schemas of the parent context if some schemas have been added.
|
|
1306
|
-
* @param {object} externalSchemas these schemas will be returned by the
|
|
1307
|
-
*
|
|
1330
|
+
* @param {object} externalSchemas these schemas will be returned by the
|
|
1331
|
+
* `bucket.getSchemas()`. Needed to resolve the external references $ref.
|
|
1332
|
+
* @param {object} serializerOptsServerOption the server `serializerOpts`
|
|
1333
|
+
* options to build your compilers accordingly
|
|
1308
1334
|
*/
|
|
1309
1335
|
buildSerializer: function factory (externalSchemas, serializerOptsServerOption) {
|
|
1310
1336
|
// This factory function must return a schema serializer compiler.
|
|
@@ -1328,10 +1354,8 @@ is passed to `fastify.register()`. The handler is treated as a regular route
|
|
|
1328
1354
|
handler so requests will go through the full [Fastify
|
|
1329
1355
|
lifecycle](./Lifecycle.md#lifecycle). *async-await* is supported as well.
|
|
1330
1356
|
|
|
1331
|
-
You can also register
|
|
1332
|
-
[`
|
|
1333
|
-
[`preHandler`](./Hooks.md#route-hooks) hooks for
|
|
1334
|
-
the 404 handler.
|
|
1357
|
+
You can also register [`preValidation`](./Hooks.md#route-hooks) and
|
|
1358
|
+
[`preHandler`](./Hooks.md#route-hooks) hooks for the 404 handler.
|
|
1335
1359
|
|
|
1336
1360
|
_Note: The `preValidation` hook registered using this method will run for a
|
|
1337
1361
|
route that Fastify does not recognize and **not** when a route handler manually
|
|
@@ -1405,9 +1429,13 @@ if (statusCode >= 500) {
|
|
|
1405
1429
|
#### addConstraintStrategy
|
|
1406
1430
|
<a id="addConstraintStrategy"></a>
|
|
1407
1431
|
|
|
1408
|
-
Function to add a custom constraint strategy. To register a new type of
|
|
1432
|
+
Function to add a custom constraint strategy. To register a new type of
|
|
1433
|
+
constraint, you must add a new constraint strategy that knows how to match
|
|
1434
|
+
values to handlers, and that knows how to get the constraint value from a
|
|
1435
|
+
request.
|
|
1409
1436
|
|
|
1410
|
-
Add a custom constraint strategy using the `fastify.addConstraintStrategy`
|
|
1437
|
+
Add a custom constraint strategy using the `fastify.addConstraintStrategy`
|
|
1438
|
+
method:
|
|
1411
1439
|
|
|
1412
1440
|
```js
|
|
1413
1441
|
const customResponseTypeStrategy = {
|
|
@@ -1436,7 +1464,8 @@ router.addConstraintStrategy(customResponseTypeStrategy);
|
|
|
1436
1464
|
#### hasConstraintStrategy
|
|
1437
1465
|
<a id="hasConstraintStrategy"></a>
|
|
1438
1466
|
|
|
1439
|
-
The `fastify.hasConstraintStrategy(strategyName)` checks if there already exists
|
|
1467
|
+
The `fastify.hasConstraintStrategy(strategyName)` checks if there already exists
|
|
1468
|
+
a custom constraint strategy with the same name.
|
|
1440
1469
|
|
|
1441
1470
|
#### printRoutes
|
|
1442
1471
|
<a id="print-routes"></a>
|
|
@@ -1534,7 +1563,11 @@ content types, e.g. `text/json, application/vnd.oasis.opendocument.text`.
|
|
|
1534
1563
|
`content-type` can be a string, string array or RegExp.
|
|
1535
1564
|
|
|
1536
1565
|
```js
|
|
1537
|
-
// The two arguments passed to getDefaultJsonParser are for ProtoType poisoning
|
|
1566
|
+
// The two arguments passed to getDefaultJsonParser are for ProtoType poisoning
|
|
1567
|
+
// and Constructor Poisoning configuration respectively. The possible values are
|
|
1568
|
+
// 'ignore', 'remove', 'error'. ignore skips all validations and it is similar
|
|
1569
|
+
// to calling JSON.parse() directly. See the
|
|
1570
|
+
// [`secure-json-parse` documentation](https://github.com/fastify/secure-json-parse#api) for more information.
|
|
1538
1571
|
|
|
1539
1572
|
fastify.addContentTypeParser('text/json', { asString: true }, fastify.getDefaultJsonParser('ignore', 'ignore'))
|
|
1540
1573
|
```
|
|
@@ -1542,8 +1575,8 @@ fastify.addContentTypeParser('text/json', { asString: true }, fastify.getDefault
|
|
|
1542
1575
|
#### hasContentTypeParser
|
|
1543
1576
|
<a id="hasContentTypeParser"></a>
|
|
1544
1577
|
|
|
1545
|
-
`fastify.hasContentTypeParser(contentType)` is used to check whether there is a
|
|
1546
|
-
context for the specified content type.
|
|
1578
|
+
`fastify.hasContentTypeParser(contentType)` is used to check whether there is a
|
|
1579
|
+
content type parser in the current context for the specified content type.
|
|
1547
1580
|
|
|
1548
1581
|
```js
|
|
1549
1582
|
fastify.hasContentTypeParser('text/json')
|
|
@@ -1554,8 +1587,9 @@ fastify.hasContentTypeParser(/^.+\/json$/)
|
|
|
1554
1587
|
#### removeContentTypeParser
|
|
1555
1588
|
<a id="removeContentTypeParser"></a>
|
|
1556
1589
|
|
|
1557
|
-
`fastify.removeContentTypeParser(contentType)` is used to remove content type
|
|
1558
|
-
method allows for example to remove the
|
|
1590
|
+
`fastify.removeContentTypeParser(contentType)` is used to remove content type
|
|
1591
|
+
parsers in the current context. This method allows for example to remove the
|
|
1592
|
+
both built-in parsers for `application/json` and `text/plain`.
|
|
1559
1593
|
|
|
1560
1594
|
```js
|
|
1561
1595
|
fastify.removeContentTypeParser('application/json')
|
|
@@ -1566,11 +1600,14 @@ fastify.removeContentTypeParser(['application/json', 'text/plain'])
|
|
|
1566
1600
|
#### removeAllContentTypeParsers
|
|
1567
1601
|
<a id="removeAllContentTypeParsers"></a>
|
|
1568
1602
|
|
|
1569
|
-
The `fastify.removeAllContentTypeParsers()` method allows all content type
|
|
1570
|
-
A use case of this method is the
|
|
1571
|
-
|
|
1603
|
+
The `fastify.removeAllContentTypeParsers()` method allows all content type
|
|
1604
|
+
parsers in the current context to be removed. A use case of this method is the
|
|
1605
|
+
implementation of catch-all content type parser. Before adding this parser with
|
|
1606
|
+
`fastify.addContentTypeParser()` one could call the
|
|
1607
|
+
`removeAllContentTypeParsers` method.
|
|
1572
1608
|
|
|
1573
|
-
For more details about the usage of the different content type parser APIs see
|
|
1609
|
+
For more details about the usage of the different content type parser APIs see
|
|
1610
|
+
[here](./ContentTypeParser.md#usage).
|
|
1574
1611
|
|
|
1575
1612
|
#### getDefaultJsonParser
|
|
1576
1613
|
<a id="getDefaultJsonParser"></a>
|
|
@@ -2,15 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
## Type Providers
|
|
4
4
|
|
|
5
|
-
Type Providers are a TypeScript only feature that enables Fastify to statically
|
|
5
|
+
Type Providers are a TypeScript only feature that enables Fastify to statically
|
|
6
|
+
infer type information directly from inline JSON Schema. They are an alternative
|
|
7
|
+
to specifying generic arguments on routes; and can greatly reduce the need to
|
|
8
|
+
keep associated types for each schema defined in your project.
|
|
6
9
|
|
|
7
10
|
### Providers
|
|
8
11
|
|
|
9
|
-
Type Providers are offered as additional packages you will need to install into
|
|
12
|
+
Type Providers are offered as additional packages you will need to install into
|
|
13
|
+
your project. Each provider uses a different inference library under the hood;
|
|
14
|
+
allowing you to select the library most appropriate for your needs. Type
|
|
15
|
+
Provider packages follow a `@fastify/type-provider-{provider-name}` naming
|
|
16
|
+
convention.
|
|
10
17
|
|
|
11
18
|
The following inference packages are supported:
|
|
12
19
|
|
|
13
|
-
- `json-schema-to-ts` -
|
|
20
|
+
- `json-schema-to-ts` -
|
|
21
|
+
[github](https://github.com/ThomasAribart/json-schema-to-ts)
|
|
14
22
|
- `typebox` - [github](https://github.com/sinclairzx81/typebox)
|
|
15
23
|
|
|
16
24
|
### Json Schema to Ts
|
|
@@ -85,13 +93,22 @@ server.get('/route', {
|
|
|
85
93
|
})
|
|
86
94
|
```
|
|
87
95
|
|
|
88
|
-
TypeBox uses the properties `kind` and `modifier` internally. These properties
|
|
96
|
+
TypeBox uses the properties `kind` and `modifier` internally. These properties
|
|
97
|
+
are not strictly valid JSON schema which will cause `AJV@7` and newer versions
|
|
98
|
+
to throw an invalid schema error. To remove the error it's either necessary to
|
|
99
|
+
omit the properties by using
|
|
100
|
+
[`Type.Strict()`](https://github.com/sinclairzx81/typebox#strict) or use the AJV
|
|
101
|
+
options for adding custom keywords.
|
|
89
102
|
|
|
90
|
-
See also the [TypeBox
|
|
103
|
+
See also the [TypeBox
|
|
104
|
+
documentation](https://github.com/sinclairzx81/typebox#validation) on how to set
|
|
105
|
+
up AJV to work with TypeBox.
|
|
91
106
|
|
|
92
107
|
### Scoped Type-Provider
|
|
93
108
|
|
|
94
|
-
The provider types don't propagate globally. In encapsulated usage, one can
|
|
109
|
+
The provider types don't propagate globally. In encapsulated usage, one can
|
|
110
|
+
remap the context to use one or more providers (for example, `typebox` and
|
|
111
|
+
`json-schema-to-ts` can be used in the same application).
|
|
95
112
|
|
|
96
113
|
Example:
|
|
97
114
|
|
|
@@ -142,7 +159,9 @@ fastify.register(pluginWithJsonSchema)
|
|
|
142
159
|
fastify.register(pluginWithTypebox)
|
|
143
160
|
```
|
|
144
161
|
|
|
145
|
-
It's also important to mention that once the types don't propagate globally,
|
|
162
|
+
It's also important to mention that once the types don't propagate globally,
|
|
163
|
+
_currently_ is not possible to avoid multiple registrations on routes when
|
|
164
|
+
dealing with several scopes, see bellow:
|
|
146
165
|
|
|
147
166
|
```ts
|
|
148
167
|
import Fastify from 'fastify'
|
|
@@ -198,7 +217,8 @@ function plugin2(fastify: FastifyInstance, _opts, done): void {
|
|
|
198
217
|
|
|
199
218
|
### Type Definition of FastifyInstance + TypeProvider
|
|
200
219
|
|
|
201
|
-
When working with modules one has to make use of `FastifyInstance` with Type
|
|
220
|
+
When working with modules one has to make use of `FastifyInstance` with Type
|
|
221
|
+
Provider generics. See the example below:
|
|
202
222
|
|
|
203
223
|
```ts
|
|
204
224
|
// index.ts
|
|
@@ -190,9 +190,11 @@ Fastify offers two packages wrapping `json-schema-to-ts` and `typebox`:
|
|
|
190
190
|
- `@fastify/type-provider-json-schema-to-ts`
|
|
191
191
|
- `@fastify/type-provider-typebox`
|
|
192
192
|
|
|
193
|
-
They simplify schema validation setup and you can read more about them in [Type
|
|
193
|
+
They simplify schema validation setup and you can read more about them in [Type
|
|
194
|
+
Providers](./Type-Providers.md) page.
|
|
194
195
|
|
|
195
|
-
Below is how to setup schema validation using vanilla `typebox` and
|
|
196
|
+
Below is how to setup schema validation using vanilla `typebox` and
|
|
197
|
+
`json-schema-to-ts` packages.
|
|
196
198
|
|
|
197
199
|
#### typebox
|
|
198
200
|
|
|
@@ -1497,7 +1499,8 @@ database.
|
|
|
1497
1499
|
|
|
1498
1500
|
<!-- Links -->
|
|
1499
1501
|
|
|
1500
|
-
[Fastify]:
|
|
1502
|
+
[Fastify]:
|
|
1503
|
+
#fastifyrawserver-rawrequest-rawreply-loggeropts-fastifyserveroptions-fastifyinstance
|
|
1501
1504
|
[RawServerGeneric]: #rawserver
|
|
1502
1505
|
[RawRequestGeneric]: #rawrequest
|
|
1503
1506
|
[RawReplyGeneric]: #rawreply
|
|
@@ -1515,12 +1518,15 @@ database.
|
|
|
1515
1518
|
[FastifyInstance]: #fastifyfastifyinstance
|
|
1516
1519
|
[FastifyLoggerOptions]: #fastifyfastifyloggeroptions
|
|
1517
1520
|
[ContextConfigGeneric]: #ContextConfigGeneric
|
|
1518
|
-
[FastifyPlugin]:
|
|
1521
|
+
[FastifyPlugin]:
|
|
1522
|
+
#fastifyfastifypluginoptions-rawserver-rawrequest-requestgeneric
|
|
1519
1523
|
[FastifyPluginCallback]: #fastifyfastifyplugincallbackoptions
|
|
1520
1524
|
[FastifyPluginAsync]: #fastifyfastifypluginasyncoptions
|
|
1521
1525
|
[FastifyPluginOptions]: #fastifyfastifypluginoptions
|
|
1522
|
-
[FastifyRegister]:
|
|
1526
|
+
[FastifyRegister]:
|
|
1527
|
+
#fastifyfastifyregisterrawserver-rawrequest-requestgenericplugin-fastifyplugin-opts-fastifyregisteroptions
|
|
1523
1528
|
[FastifyRegisterOptions]: #fastifyfastifytregisteroptions
|
|
1524
1529
|
[LogLevel]: #fastifyloglevel
|
|
1525
1530
|
[FastifyError]: #fastifyfastifyerror
|
|
1526
|
-
[RouteOptions]:
|
|
1531
|
+
[RouteOptions]:
|
|
1532
|
+
#fastifyrouteoptionsrawserver-rawrequest-rawreply-requestgeneric-contextconfig
|