fastify 5.12.0 → 6.0.0-alpha.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/README.md +2 -2
- package/build/build-validation.js +20 -34
- package/docs/Guides/Getting-Started.md +2 -2
- package/docs/Reference/Errors.md +5 -3
- package/docs/Reference/Hooks.md +0 -4
- package/docs/Reference/LTS.md +6 -5
- package/docs/Reference/Server.md +8 -88
- package/docs/Reference/TypeScript.md +3 -49
- package/docs/Reference/Warnings.md +4 -11
- package/fastify.d.ts +10 -24
- package/fastify.js +17 -34
- package/lib/config-validator.js +368 -475
- package/lib/errors.js +9 -3
- package/lib/log-controller.js +2 -4
- package/lib/logger-factory.js +1 -1
- package/lib/route.js +4 -9
- package/lib/server.js +2 -3
- package/lib/warnings.js +2 -46
- package/package.json +2 -2
- package/test/404s.test.js +0 -49
- package/test/allow-unsafe-regex.test.js +12 -4
- package/test/case-insensitive.test.js +12 -4
- package/test/custom-querystring-parser.test.js +0 -16
- package/test/hooks.test.js +6 -1
- package/test/http-methods/custom-http-methods.test.js +12 -17
- package/test/http-methods/trace.test.js +1 -1
- package/test/https/custom-https-server.test.js +1 -1
- package/test/internals/errors.test.js +15 -5
- package/test/internals/initial-config.test.js +56 -38
- package/test/internals/logger.test.js +17 -4
- package/test/internals/reply.test.js +1 -1
- package/test/internals/request.test.js +8 -24
- package/test/logger/logging.test.js +2 -121
- package/test/logger/request.test.js +1 -36
- package/test/route-prefix.test.js +46 -18
- package/test/route.1.test.js +2 -2
- package/test/router-options.test.js +25 -523
- package/test/set-error-handler.test.js +7 -9
- package/test/types/errors.tst.ts +2 -1
- package/test/types/fastify.tst.ts +44 -48
- package/test/types/instance.tst.ts +4 -5
- package/test/types/logger.tst.ts +3 -7
- package/test/use-semicolon-delimiter.test.js +0 -60
- package/types/context.d.ts +0 -20
- package/types/errors.d.ts +2 -1
- package/types/instance.d.ts +0 -4
- package/types/logger.d.ts +1 -16
- package/types/plugin.d.ts +0 -9
- package/types/request.d.ts +0 -4
- package/lib/promise.js +0 -23
- package/test/internals/promise.test.js +0 -63
package/README.md
CHANGED
|
@@ -46,8 +46,8 @@ developer experience with the least overhead and a powerful plugin architecture.
|
|
|
46
46
|
It is inspired by Hapi and Express and as far as we know, it is one of the
|
|
47
47
|
fastest web frameworks in town.
|
|
48
48
|
|
|
49
|
-
The `main` branch refers to the Fastify `
|
|
50
|
-
Check out the [`
|
|
49
|
+
The `main` branch refers to the Fastify `v6` release.
|
|
50
|
+
Check out the [`5.x` branch](https://github.com/fastify/fastify/tree/5.x) for `v5`.
|
|
51
51
|
|
|
52
52
|
### Table of Contents
|
|
53
53
|
|
|
@@ -30,26 +30,19 @@ const defaultInitOptions = {
|
|
|
30
30
|
requestTimeout: 0, // no limit
|
|
31
31
|
handlerTimeout: 0, // no timeout (disabled by default)
|
|
32
32
|
bodyLimit: 1024 * 1024, // 1 MiB
|
|
33
|
-
caseSensitive: true,
|
|
34
|
-
allowUnsafeRegex: false,
|
|
35
|
-
disableRequestLogging: false, // TODO: remove it in v6
|
|
36
|
-
ignoreTrailingSlash: false,
|
|
37
|
-
ignoreDuplicateSlashes: false,
|
|
38
|
-
maxParamLength: 100,
|
|
39
33
|
onProtoPoisoning: 'error',
|
|
40
34
|
onConstructorPoisoning: 'error',
|
|
41
35
|
pluginTimeout: 10000,
|
|
42
36
|
requestIdHeader: false,
|
|
43
|
-
requestIdLogLabel: 'reqId', // TODO: remove it in v6
|
|
44
37
|
http2SessionTimeout: 72000, // 72 seconds
|
|
45
38
|
exposeHeadRoutes: true,
|
|
46
|
-
|
|
47
|
-
allowErrorHandlerOverride: true, // TODO: set to false in v6
|
|
39
|
+
allowErrorHandlerOverride: false,
|
|
48
40
|
routerOptions: {
|
|
41
|
+
allowUnsafeRegex: false,
|
|
42
|
+
caseSensitive: true,
|
|
49
43
|
ignoreTrailingSlash: false,
|
|
50
44
|
ignoreDuplicateSlashes: false,
|
|
51
45
|
maxParamLength: 100,
|
|
52
|
-
allowUnsafeRegex: false,
|
|
53
46
|
useSemicolonDelimiter: false
|
|
54
47
|
}
|
|
55
48
|
}
|
|
@@ -75,8 +68,6 @@ const schema = {
|
|
|
75
68
|
requestTimeout: { type: 'integer', default: defaultInitOptions.requestTimeout },
|
|
76
69
|
handlerTimeout: { type: 'integer', default: defaultInitOptions.handlerTimeout },
|
|
77
70
|
bodyLimit: { type: 'integer', default: defaultInitOptions.bodyLimit },
|
|
78
|
-
caseSensitive: { type: 'boolean', default: defaultInitOptions.caseSensitive },
|
|
79
|
-
allowUnsafeRegex: { type: 'boolean', default: defaultInitOptions.allowUnsafeRegex },
|
|
80
71
|
http2: { type: 'boolean' },
|
|
81
72
|
https: {
|
|
82
73
|
if: {
|
|
@@ -99,42 +90,37 @@ const schema = {
|
|
|
99
90
|
},
|
|
100
91
|
ignoreTrailingSlash: { type: 'boolean', default: defaultInitOptions.ignoreTrailingSlash },
|
|
101
92
|
ignoreDuplicateSlashes: { type: 'boolean', default: defaultInitOptions.ignoreDuplicateSlashes },
|
|
102
|
-
disableRequestLogging: {
|
|
103
|
-
default: false
|
|
104
|
-
},
|
|
105
|
-
maxParamLength: { type: 'integer', default: defaultInitOptions.maxParamLength },
|
|
106
93
|
onProtoPoisoning: { type: 'string', default: defaultInitOptions.onProtoPoisoning },
|
|
107
94
|
onConstructorPoisoning: { type: 'string', default: defaultInitOptions.onConstructorPoisoning },
|
|
108
95
|
pluginTimeout: { type: 'integer', default: defaultInitOptions.pluginTimeout },
|
|
109
96
|
requestIdHeader: { anyOf: [{ type: 'boolean' }, { type: 'string' }], default: defaultInitOptions.requestIdHeader },
|
|
110
|
-
requestIdLogLabel: { type: 'string', default: defaultInitOptions.requestIdLogLabel },
|
|
111
97
|
http2SessionTimeout: { type: 'integer', default: defaultInitOptions.http2SessionTimeout },
|
|
112
98
|
exposeHeadRoutes: { type: 'boolean', default: defaultInitOptions.exposeHeadRoutes },
|
|
113
|
-
useSemicolonDelimiter: { type: 'boolean', default: defaultInitOptions.useSemicolonDelimiter },
|
|
114
99
|
routerOptions: {
|
|
115
100
|
type: 'object',
|
|
116
|
-
additionalProperties:
|
|
101
|
+
additionalProperties: false,
|
|
117
102
|
properties: {
|
|
103
|
+
allowUnsafeRegex: { type: 'boolean', default: defaultInitOptions.routerOptions.allowUnsafeRegex },
|
|
104
|
+
caseSensitive: { type: 'boolean', default: defaultInitOptions.routerOptions.caseSensitive },
|
|
105
|
+
constraints: {
|
|
106
|
+
type: 'object',
|
|
107
|
+
additionalProperties: {
|
|
108
|
+
type: 'object',
|
|
109
|
+
required: ['name', 'storage', 'validate', 'deriveConstraint'],
|
|
110
|
+
additionalProperties: true,
|
|
111
|
+
properties: {
|
|
112
|
+
name: { type: 'string' },
|
|
113
|
+
storage: {},
|
|
114
|
+
validate: {},
|
|
115
|
+
deriveConstraint: {}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
118
119
|
ignoreTrailingSlash: { type: 'boolean', default: defaultInitOptions.routerOptions.ignoreTrailingSlash },
|
|
119
120
|
ignoreDuplicateSlashes: { type: 'boolean', default: defaultInitOptions.routerOptions.ignoreDuplicateSlashes },
|
|
120
121
|
maxParamLength: { type: 'integer', default: defaultInitOptions.routerOptions.maxParamLength },
|
|
121
|
-
allowUnsafeRegex: { type: 'boolean', default: defaultInitOptions.routerOptions.allowUnsafeRegex },
|
|
122
122
|
useSemicolonDelimiter: { type: 'boolean', default: defaultInitOptions.routerOptions.useSemicolonDelimiter }
|
|
123
123
|
}
|
|
124
|
-
},
|
|
125
|
-
constraints: {
|
|
126
|
-
type: 'object',
|
|
127
|
-
additionalProperties: {
|
|
128
|
-
type: 'object',
|
|
129
|
-
required: ['name', 'storage', 'validate', 'deriveConstraint'],
|
|
130
|
-
additionalProperties: true,
|
|
131
|
-
properties: {
|
|
132
|
-
name: { type: 'string' },
|
|
133
|
-
storage: {},
|
|
134
|
-
validate: {},
|
|
135
|
-
deriveConstraint: {}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
124
|
}
|
|
139
125
|
}
|
|
140
126
|
}
|
|
@@ -303,7 +303,7 @@ export default fastifyPlugin(dbConnector)
|
|
|
303
303
|
```js
|
|
304
304
|
// CommonJs
|
|
305
305
|
/**
|
|
306
|
-
* @type {import('fastify-plugin').
|
|
306
|
+
* @type {import('fastify-plugin').FastifyPluginAsync}
|
|
307
307
|
*/
|
|
308
308
|
const fastifyPlugin = require('fastify-plugin')
|
|
309
309
|
|
|
@@ -616,5 +616,5 @@ npm start
|
|
|
616
616
|
speed](https://www.youtube.com/watch?v=5z46jJZNe8k) by
|
|
617
617
|
[@mcollina](https://github.com/mcollina)
|
|
618
618
|
- [What if I told you that HTTP can be
|
|
619
|
-
fast](https://www.webexpo.net/prague2017/talk/what-if-i-told-you-that-http-can-be-fast/)
|
|
619
|
+
fast](https://web.archive.org/web/20251006142253/https://www.webexpo.net/prague2017/talk/what-if-i-told-you-that-http-can-be-fast/)
|
|
620
620
|
by [@delvedor](https://github.com/delvedor)
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
- [FST_ERR_CTP_EMPTY_JSON_BODY](#fst_err_ctp_empty_json_body)
|
|
33
33
|
- [FST_ERR_CTP_INVALID_JSON_BODY](#fst_err_ctp_invalid_json_body)
|
|
34
34
|
- [FST_ERR_CTP_INSTANCE_ALREADY_STARTED](#fst_err_ctp_instance_already_started)
|
|
35
|
-
- [
|
|
35
|
+
- [FST_ERR_INSTANCE_ALREADY_STARTED](#fst_err_instance_already_started)
|
|
36
36
|
- [FST_ERR_DEC_ALREADY_PRESENT](#fst_err_dec_already_present)
|
|
37
37
|
- [FST_ERR_DEC_DEPENDENCY_INVALID_TYPE](#fst_err_dec_dependency_invalid_type)
|
|
38
38
|
- [FST_ERR_DEC_MISSING_DEPENDENCY](#fst_err_dec_missing_dependency)
|
|
@@ -84,6 +84,7 @@
|
|
|
84
84
|
- [FST_ERR_ROUTE_HANDLER_NOT_FN](#fst_err_route_handler_not_fn)
|
|
85
85
|
- [FST_ERR_ROUTE_MISSING_HANDLER](#fst_err_route_missing_handler)
|
|
86
86
|
- [FST_ERR_ROUTE_METHOD_INVALID](#fst_err_route_method_invalid)
|
|
87
|
+
- [FST_ERR_ROUTE_METHOD_ALREADY_SUPPORTED](#fst_err_route_method_already_supported)
|
|
87
88
|
- [FST_ERR_ROUTE_METHOD_NOT_SUPPORTED](#fst_err_route_method_not_supported)
|
|
88
89
|
- [FST_ERR_ROUTE_LOG_LEVEL_INVALID](#fst_err_route_log_level_invalid)
|
|
89
90
|
- [FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED](#fst_err_route_body_validation_schema_not_supported)
|
|
@@ -385,7 +386,7 @@ Below is a table with all the error codes used by Fastify.
|
|
|
385
386
|
| <a id="fst_err_ctp_empty_json_body">FST_ERR_CTP_EMPTY_JSON_BODY</a> | Body is not valid JSON but content-type is set to <code>application/json</code>. | Check if the request body is valid JSON. | [#5925](https://github.com/fastify/fastify/pull/5925) |
|
|
386
387
|
| <a id="fst_err_ctp_invalid_json_body">FST_ERR_CTP_INVALID_JSON_BODY</a> | Body cannot be empty when content-type is set to <code>application/json</code>. | Check the request body. | [#1253](https://github.com/fastify/fastify/pull/1253) |
|
|
387
388
|
| <a id="fst_err_ctp_instance_already_started">FST_ERR_CTP_INSTANCE_ALREADY_STARTED</a> | Fastify is already started. | - | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
388
|
-
| <a id="
|
|
389
|
+
| <a id="fst_err_instance_already_started">FST_ERR_INSTANCE_ALREADY_STARTED</a> | Fastify instance is already started. | - | [#6098](https://github.com/fastify/fastify/pull/6098) |
|
|
389
390
|
| <a id="fst_err_dec_already_present">FST_ERR_DEC_ALREADY_PRESENT</a> | A decorator with the same name is already registered. | Use a different decorator name. | [#1168](https://github.com/fastify/fastify/pull/1168) |
|
|
390
391
|
| <a id="fst_err_dec_dependency_invalid_type">FST_ERR_DEC_DEPENDENCY_INVALID_TYPE</a> | The dependencies of decorator must be of type `Array`. | Use an array for the dependencies. | [#3090](https://github.com/fastify/fastify/pull/3090) |
|
|
391
392
|
| <a id="fst_err_dec_missing_dependency">FST_ERR_DEC_MISSING_DEPENDENCY</a> | The decorator cannot be registered due to a missing dependency. | Register the missing dependency. | [#1168](https://github.com/fastify/fastify/pull/1168) |
|
|
@@ -437,6 +438,7 @@ Below is a table with all the error codes used by Fastify.
|
|
|
437
438
|
| <a id="fst_err_route_handler_not_fn">FST_ERR_ROUTE_HANDLER_NOT_FN</a> | Handler for the route must be a function. | Use a function for the handler. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
438
439
|
| <a id="fst_err_route_missing_handler">FST_ERR_ROUTE_MISSING_HANDLER</a> | Missing handler function for the route. | Add a handler function. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
439
440
|
| <a id="fst_err_route_method_invalid">FST_ERR_ROUTE_METHOD_INVALID</a> | Method is not a valid value. | Use a valid value for the method. | [#4750](https://github.com/fastify/fastify/pull/4750) |
|
|
441
|
+
| <a id="fst_err_route_method_already_supported">FST_ERR_ROUTE_METHOD_ALREADY_SUPPORTED</a> | The HTTP method is already supported. | Pass `{ overrideExisting: true }` to explicitly override it. | [#6909](https://github.com/fastify/fastify/pull/6909) |
|
|
440
442
|
| <a id="fst_err_route_method_not_supported">FST_ERR_ROUTE_METHOD_NOT_SUPPORTED</a> | Method is not supported for the route. | Use a supported method. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
441
443
|
| <a id="fst_err_route_log_level_invalid">FST_ERR_ROUTE_LOG_LEVEL_INVALID</a> | `logLevel` must match a configured logger level. | Use one of the configured logger levels for the route. | [#6523](https://github.com/fastify/fastify/pull/6523) |
|
|
442
444
|
| <a id="fst_err_route_body_validation_schema_not_supported">FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED</a> | Body validation schema route is not supported. | Use a different different method for the route. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
@@ -447,7 +449,7 @@ Below is a table with all the error codes used by Fastify.
|
|
|
447
449
|
| <a id="fst_err_route_missing_content_type">FST_ERR_ROUTE_MISSING_CONTENT_TYPE</a> | `Content-Type` header is required for the request. | Send request with `Content-Type` header. | [#6832](https://github.com/fastify/fastify/pull/6832) |
|
|
448
450
|
| <a id="fst_err_route_missing_content">FST_ERR_ROUTE_MISSING_CONTENT</a> | Body is required for the request. | Send request with payload. | [#6832](https://github.com/fastify/fastify/pull/6832) |
|
|
449
451
|
| <a id="fst_err_reopened_close_server">FST_ERR_REOPENED_CLOSE_SERVER</a> | Fastify has already been closed and cannot be reopened. | - | [#2415](https://github.com/fastify/fastify/pull/2415) |
|
|
450
|
-
| <a id="fst_err_reopened_server">FST_ERR_REOPENED_SERVER</a> | Fastify is already
|
|
452
|
+
| <a id="fst_err_reopened_server">FST_ERR_REOPENED_SERVER</a> | Fastify is already started. | - | [#2415](https://github.com/fastify/fastify/pull/2415) |
|
|
451
453
|
| <a id="fst_err_plugin_version_mismatch">FST_ERR_PLUGIN_VERSION_MISMATCH</a> | Installed Fastify plugin mismatched expected version. | Use a compatible version of the plugin. | [#2549](https://github.com/fastify/fastify/pull/2549) |
|
|
452
454
|
| <a id="fst_err_plugin_callback_not_fn">FST_ERR_PLUGIN_CALLBACK_NOT_FN</a> | Callback for a hook is not a function. | Use a function for the callback. | [#3106](https://github.com/fastify/fastify/pull/3106) |
|
|
453
455
|
| <a id="fst_err_plugin_not_valid">FST_ERR_PLUGIN_NOT_VALID</a> | Plugin must be a function or a promise. | Use a function or a promise for the plugin. | [#3106](https://github.com/fastify/fastify/pull/3106) |
|
package/docs/Reference/Hooks.md
CHANGED
|
@@ -260,10 +260,6 @@ The `onResponse` hook is executed when a response has been sent, so you will not
|
|
|
260
260
|
be able to send more data to the client. It can however be useful for sending
|
|
261
261
|
data to external services, for example, to gather statistics.
|
|
262
262
|
|
|
263
|
-
> ℹ️ Note:
|
|
264
|
-
> Setting `disableRequestLogging` to `true` will disable any error log
|
|
265
|
-
> inside the `onResponse` hook. In this case use `try - catch` to log errors.
|
|
266
|
-
|
|
267
263
|
### onTimeout
|
|
268
264
|
|
|
269
265
|
```js
|
package/docs/Reference/LTS.md
CHANGED
|
@@ -60,7 +60,8 @@ For more information, see the [Never Ending Support][hd-link] service offered.
|
|
|
60
60
|
| 2.0.0 | 2019-02-25 | 2021-01-31 | 6, 8, 10, 12, 14 | |
|
|
61
61
|
| 3.0.0 | 2020-07-07 | 2023-06-30 | 10, 12, 14, 16, 18 | v5(18) |
|
|
62
62
|
| 4.0.0 | 2022-06-08 | 2025-06-30 | 14, 16, 18, 20, 22 | v5(18), v5(20) |
|
|
63
|
-
| 5.0.0 | 2024-09-17 | TBD | 20, 22
|
|
63
|
+
| 5.0.0 | 2024-09-17 | TBD | 20, 22, 24, 26 | v5(20) |
|
|
64
|
+
| 6.0.0 | TDB | TBD | 24, 26 | v6(24) |
|
|
64
65
|
|
|
65
66
|
### CI Tested Operating Systems
|
|
66
67
|
<a id="supported-os"></a>
|
|
@@ -73,10 +74,10 @@ YAML workflow labels below:
|
|
|
73
74
|
|
|
74
75
|
| OS | YAML Workflow Label | Package Manager | Node.js | Nsolid(Node) |
|
|
75
76
|
| ------- | ------------------- | --------------- | ----------- | ------------- |
|
|
76
|
-
| Linux | `ubuntu-latest` | npm |
|
|
77
|
-
| Linux | `ubuntu-latest` | yarn,pnpm |
|
|
78
|
-
| Windows | `windows-latest` | npm |
|
|
79
|
-
| MacOS | `macos-latest` | npm |
|
|
77
|
+
| Linux | `ubuntu-latest` | npm | 24 | v6(24) |
|
|
78
|
+
| Linux | `ubuntu-latest` | yarn,pnpm | 24 | v6(24) |
|
|
79
|
+
| Windows | `windows-latest` | npm | 24 | v6(24) |
|
|
80
|
+
| MacOS | `macos-latest` | npm | 24 | v6(24) |
|
|
80
81
|
|
|
81
82
|
When using [yarn](https://yarnpkg.com/), the `--ignore-engines` flag may be required.
|
|
82
83
|
|
package/docs/Reference/Server.md
CHANGED
|
@@ -22,11 +22,9 @@ describes the properties available in that options object.
|
|
|
22
22
|
- [`onConstructorPoisoning`](#onconstructorpoisoning)
|
|
23
23
|
- [`logger`](#logger)
|
|
24
24
|
- [`loggerInstance`](#loggerinstance)
|
|
25
|
-
- [`disableRequestLogging`](#disablerequestlogging)
|
|
26
25
|
- [`logController`](#logcontroller)
|
|
27
26
|
- [`serverFactory`](#serverfactory)
|
|
28
27
|
- [`requestIdHeader`](#requestidheader)
|
|
29
|
-
- [`requestIdLogLabel`](#requestidloglabel)
|
|
30
28
|
- [`genReqId`](#genreqid)
|
|
31
29
|
- [`trustProxy`](#trustproxy)
|
|
32
30
|
- [`pluginTimeout`](#plugintimeout)
|
|
@@ -387,71 +385,6 @@ Pino interface by having the following methods: `info`, `error`, `debug`,
|
|
|
387
385
|
const fastify = require('fastify')({ loggerInstance: customLogger });
|
|
388
386
|
```
|
|
389
387
|
|
|
390
|
-
### `disableRequestLogging`
|
|
391
|
-
<a id="factory-disable-request-logging"></a>
|
|
392
|
-
|
|
393
|
-
> **Deprecated:** Use the [`logController`](#factory-log-controller)
|
|
394
|
-
> option with `disableRequestLogging` or `isLogDisabled` override instead.
|
|
395
|
-
> This top-level option will be removed in `fastify@6`.
|
|
396
|
-
|
|
397
|
-
+ Default: `false`
|
|
398
|
-
|
|
399
|
-
When logging is enabled, Fastify will issue an `info` level log
|
|
400
|
-
message when a request is received and when the response for that request has
|
|
401
|
-
been sent. By setting this option to `true`, these log messages will be
|
|
402
|
-
disabled. This allows for more flexible request start and end logging by
|
|
403
|
-
attaching custom `onRequest` and `onResponse` hooks.
|
|
404
|
-
|
|
405
|
-
This option can also be a function that receives the Fastify request object
|
|
406
|
-
and returns a boolean. This allows for conditional request logging based on the
|
|
407
|
-
request properties (e.g., URL, headers, decorations).
|
|
408
|
-
|
|
409
|
-
```js
|
|
410
|
-
const { LogController } = require('fastify')
|
|
411
|
-
|
|
412
|
-
// Deprecated
|
|
413
|
-
const fastify = require('fastify')({
|
|
414
|
-
logger: true,
|
|
415
|
-
disableRequestLogging: (request) => {
|
|
416
|
-
return request.url === '/health' || request.url === '/ready'
|
|
417
|
-
}
|
|
418
|
-
})
|
|
419
|
-
|
|
420
|
-
// Recommended: use logController instead
|
|
421
|
-
const fastify = require('fastify')({
|
|
422
|
-
logger: true,
|
|
423
|
-
logController: new LogController({
|
|
424
|
-
disableRequestLogging: (request) => {
|
|
425
|
-
return request.url === '/health' || request.url === '/ready'
|
|
426
|
-
}
|
|
427
|
-
})
|
|
428
|
-
})
|
|
429
|
-
```
|
|
430
|
-
|
|
431
|
-
The other log entries that will be disabled are:
|
|
432
|
-
- an error log written by the default `onResponse` hook on reply callback errors
|
|
433
|
-
- the error and info logs written by the `defaultErrorHandler`
|
|
434
|
-
on error management
|
|
435
|
-
- the info log written by the `fourOhFour` handler when a
|
|
436
|
-
non existent route is requested
|
|
437
|
-
|
|
438
|
-
Other log messages emitted by Fastify will stay enabled,
|
|
439
|
-
like deprecation warnings and messages
|
|
440
|
-
emitted when requests are received while the server is closing.
|
|
441
|
-
|
|
442
|
-
```js
|
|
443
|
-
// Examples of hooks to replicate the disabled functionality.
|
|
444
|
-
fastify.addHook('onRequest', (req, reply, done) => {
|
|
445
|
-
req.log.info({ url: req.raw.url, id: req.id }, 'received request')
|
|
446
|
-
done()
|
|
447
|
-
})
|
|
448
|
-
|
|
449
|
-
fastify.addHook('onResponse', (req, reply, done) => {
|
|
450
|
-
req.log.info({ url: req.raw.originalUrl, statusCode: reply.raw.statusCode }, 'request completed')
|
|
451
|
-
done()
|
|
452
|
-
})
|
|
453
|
-
```
|
|
454
|
-
|
|
455
388
|
### `logController`
|
|
456
389
|
<a id="factory-log-controller"></a>
|
|
457
390
|
|
|
@@ -593,17 +526,6 @@ const fastify = require('fastify')({
|
|
|
593
526
|
> value of their choosing.
|
|
594
527
|
> No validation is performed on `requestIdHeader`.
|
|
595
528
|
|
|
596
|
-
### `requestIdLogLabel`
|
|
597
|
-
<a id="factory-request-id-log-label"></a>
|
|
598
|
-
|
|
599
|
-
> **Deprecated:** Use the [`logController`](#factory-log-controller)
|
|
600
|
-
> option with `requestIdLogLabel` instead. This top-level option will be
|
|
601
|
-
> removed in `fastify@6`.
|
|
602
|
-
|
|
603
|
-
+ Default: `'reqId'`
|
|
604
|
-
|
|
605
|
-
Defines the label used for the request identifier when logging the request.
|
|
606
|
-
|
|
607
529
|
### `genReqId`
|
|
608
530
|
<a id="factory-gen-request-id"></a>
|
|
609
531
|
|
|
@@ -1227,16 +1149,15 @@ fastify.get('/dev', async (request, reply) => {
|
|
|
1227
1149
|
### `allowErrorHandlerOverride`
|
|
1228
1150
|
<a id="allow-error-handler-override"></a>
|
|
1229
1151
|
|
|
1230
|
-
* **Default:** `
|
|
1231
|
-
|
|
1232
|
-
> ⚠ Warning:
|
|
1233
|
-
> This option will be set to `false` by default
|
|
1234
|
-
> in the next major release.
|
|
1152
|
+
* **Default:** `false`
|
|
1235
1153
|
|
|
1236
1154
|
When set to `false`, it prevents `setErrorHandler` from being called
|
|
1237
1155
|
multiple times within the same scope, ensuring that the previous error
|
|
1238
1156
|
handler is not unintentionally overridden.
|
|
1239
1157
|
|
|
1158
|
+
Set this option to `true` to allow an error handler to be overridden within
|
|
1159
|
+
the same scope.
|
|
1160
|
+
|
|
1240
1161
|
#### Example of incorrect usage:
|
|
1241
1162
|
|
|
1242
1163
|
```js
|
|
@@ -1732,7 +1653,8 @@ fastify.mkcol('/', (req, reply) => {
|
|
|
1732
1653
|
})
|
|
1733
1654
|
```
|
|
1734
1655
|
|
|
1735
|
-
Calling `addHttpMethod` for an existing method
|
|
1656
|
+
Calling `addHttpMethod` for an existing method requires `overrideExisting: true`
|
|
1657
|
+
and overrides its body behavior.
|
|
1736
1658
|
|
|
1737
1659
|
```js
|
|
1738
1660
|
fastify.addHttpMethod('GET', {
|
|
@@ -1741,8 +1663,8 @@ fastify.addHttpMethod('GET', {
|
|
|
1741
1663
|
})
|
|
1742
1664
|
```
|
|
1743
1665
|
|
|
1744
|
-
|
|
1745
|
-
|
|
1666
|
+
Omitting `overrideExisting: true` when the method already exists throws
|
|
1667
|
+
`FST_ERR_ROUTE_METHOD_ALREADY_SUPPORTED`.
|
|
1746
1668
|
|
|
1747
1669
|
#### addSchema
|
|
1748
1670
|
<a id="add-schema"></a>
|
|
@@ -2446,12 +2368,10 @@ The properties that can currently be exposed are:
|
|
|
2446
2368
|
- http2
|
|
2447
2369
|
- https (it will return `false`/`true` or `{ allowHTTP1: true/false }` if
|
|
2448
2370
|
explicitly passed)
|
|
2449
|
-
- disableRequestLogging
|
|
2450
2371
|
- onProtoPoisoning
|
|
2451
2372
|
- onConstructorPoisoning
|
|
2452
2373
|
- pluginTimeout
|
|
2453
2374
|
- requestIdHeader
|
|
2454
|
-
- requestIdLogLabel
|
|
2455
2375
|
- http2SessionTimeout
|
|
2456
2376
|
- routerOptions
|
|
2457
2377
|
- allowUnsafeRegex
|
|
@@ -1334,14 +1334,6 @@ Interface method definition used within the
|
|
|
1334
1334
|
Interface method definition used within the
|
|
1335
1335
|
[`fastify.register()`][FastifyRegister] method.
|
|
1336
1336
|
|
|
1337
|
-
##### fastify.FastifyPlugin< [Options][FastifyPluginOptions]>
|
|
1338
|
-
[src](https://github.com/fastify/fastify/blob/main/types/plugin.d.ts#L29)
|
|
1339
|
-
|
|
1340
|
-
Interface method definition used within the
|
|
1341
|
-
[`fastify.register()`][FastifyRegister] method. Document deprecated in favor of
|
|
1342
|
-
`FastifyPluginCallback` and `FastifyPluginAsync` since general `FastifyPlugin`
|
|
1343
|
-
doesn't properly infer types for async functions.
|
|
1344
|
-
|
|
1345
1337
|
##### fastify.FastifyPluginOptions
|
|
1346
1338
|
[src](https://github.com/fastify/fastify/blob/main/types/plugin.d.ts#L31)
|
|
1347
1339
|
|
|
@@ -1358,8 +1350,6 @@ extends FastifyPluginOptions`) so they can be passed to the register method.
|
|
|
1358
1350
|
[src](https://github.com/fastify/fastify/blob/main/types/register.d.ts#L9)
|
|
1359
1351
|
##### fastify.FastifyRegister(plugin: [FastifyPluginAsync][FastifyPluginAsync], opts: [FastifyRegisterOptions][FastifyRegisterOptions])
|
|
1360
1352
|
[src](https://github.com/fastify/fastify/blob/main/types/register.d.ts#L9)
|
|
1361
|
-
##### fastify.FastifyRegister(plugin: [FastifyPlugin][FastifyPlugin], opts: [FastifyRegisterOptions][FastifyRegisterOptions])
|
|
1362
|
-
[src](https://github.com/fastify/fastify/blob/main/types/register.d.ts#L9)
|
|
1363
1353
|
|
|
1364
1354
|
This type interface specifies the type for the
|
|
1365
1355
|
[`fastify.register()`](./Server.md#register) method. The type interface returns
|
|
@@ -1368,8 +1358,7 @@ a function signature with an underlying generic `Options` which is defaulted to
|
|
|
1368
1358
|
FastifyPlugin parameter when calling this function so there is no need to
|
|
1369
1359
|
specify the underlying generic. The options parameter is the intersection of the
|
|
1370
1360
|
plugin's options and two additional optional properties: `prefix: string` and
|
|
1371
|
-
`logLevel`: [LogLevel][LogLevel].
|
|
1372
|
-
`FastifyPluginCallback` and `FastifyPluginAsync` instead.
|
|
1361
|
+
`logLevel`: [LogLevel][LogLevel].
|
|
1373
1362
|
|
|
1374
1363
|
Below is an example of the options inference in action:
|
|
1375
1364
|
|
|
@@ -1427,39 +1416,6 @@ Union type of: `'info' | 'error' | 'debug' | 'fatal' | 'warn' | 'trace'`
|
|
|
1427
1416
|
|
|
1428
1417
|
---
|
|
1429
1418
|
|
|
1430
|
-
#### Context
|
|
1431
|
-
|
|
1432
|
-
The context type definition is similar to the other highly dynamic pieces of the
|
|
1433
|
-
type system. Route context is available in the route handler method.
|
|
1434
|
-
|
|
1435
|
-
##### fastify.FastifyRequestContext
|
|
1436
|
-
|
|
1437
|
-
[src](https://github.com/fastify/fastify/blob/main/types/context.d.ts#L11)
|
|
1438
|
-
|
|
1439
|
-
An interface with a single required property `config` that is set by default to
|
|
1440
|
-
`unknown`. Can be specified either using a generic or an overload.
|
|
1441
|
-
|
|
1442
|
-
This type definition is potentially incomplete. If you are using it and can
|
|
1443
|
-
provide more details on how to improve the definition, we strongly encourage you
|
|
1444
|
-
to open an issue in the main
|
|
1445
|
-
[fastify/fastify](https://github.com/fastify/fastify) repository. Thank you in
|
|
1446
|
-
advanced!
|
|
1447
|
-
|
|
1448
|
-
##### fastify.FastifyReplyContext
|
|
1449
|
-
|
|
1450
|
-
[src](https://github.com/fastify/fastify/blob/main/types/context.d.ts#L11)
|
|
1451
|
-
|
|
1452
|
-
An interface with a single required property `config` that is set by default to
|
|
1453
|
-
`unknown`. Can be specified either using a generic or an overload.
|
|
1454
|
-
|
|
1455
|
-
This type definition is potentially incomplete. If you are using it and can
|
|
1456
|
-
provide more details on how to improve the definition, we strongly encourage you
|
|
1457
|
-
to open an issue in the main
|
|
1458
|
-
[fastify/fastify](https://github.com/fastify/fastify) repository. Thank you in
|
|
1459
|
-
advanced!
|
|
1460
|
-
|
|
1461
|
-
---
|
|
1462
|
-
|
|
1463
1419
|
#### Routing
|
|
1464
1420
|
|
|
1465
1421
|
One of the core principles in Fastify is its routing capabilities. Most of the
|
|
@@ -1559,9 +1515,9 @@ FastifyError is a custom error object that includes status code and validation
|
|
|
1559
1515
|
results.
|
|
1560
1516
|
|
|
1561
1517
|
It extends the Node.js `Error` type, and adds two additional, optional
|
|
1562
|
-
properties: `statusCode: number` and `validation:
|
|
1518
|
+
properties: `statusCode: number` and `validation: FastifySchemaValidationError[]`.
|
|
1563
1519
|
|
|
1564
|
-
##### fastify.
|
|
1520
|
+
##### fastify.FastifySchemaValidationError
|
|
1565
1521
|
|
|
1566
1522
|
[src](https://github.com/fastify/fastify/blob/main/fastify.d.ts#L184)
|
|
1567
1523
|
|
|
@@ -1713,8 +1669,6 @@ database.
|
|
|
1713
1669
|
[FastifyInstance]: #fastifyfastifyinstance
|
|
1714
1670
|
[FastifyLoggerOptions]: #fastifyfastifyloggeroptions
|
|
1715
1671
|
[ContextConfigGeneric]: #ContextConfigGeneric
|
|
1716
|
-
[FastifyPlugin]:
|
|
1717
|
-
#fastifyfastifypluginoptions-rawserver-rawrequest-requestgeneric
|
|
1718
1672
|
[FastifyPluginCallback]: #fastifyfastifyplugincallbackoptions
|
|
1719
1673
|
[FastifyPluginAsync]: #fastifyfastifypluginasyncoptions
|
|
1720
1674
|
[FastifyPluginOptions]: #fastifyfastifypluginoptions
|
|
@@ -7,12 +7,7 @@
|
|
|
7
7
|
- [Fastify Warning Codes](#fastify-warning-codes)
|
|
8
8
|
- [FSTWRN001](#FSTWRN001)
|
|
9
9
|
- [FSTWRN003](#FSTWRN003)
|
|
10
|
-
- [FSTWRN004](#FSTWRN004)
|
|
11
10
|
- [Fastify Deprecation Codes](#fastify-deprecation-codes)
|
|
12
|
-
- [FSTDEP022](#FSTDEP022)
|
|
13
|
-
- [FSTDEP023](#FSTDEP023)
|
|
14
|
-
- [FSTDEP024](#FSTDEP024)
|
|
15
|
-
- [FSTDEP025](#FSTDEP025)
|
|
16
11
|
|
|
17
12
|
## Warnings
|
|
18
13
|
|
|
@@ -34,7 +29,7 @@ one of the following methods:
|
|
|
34
29
|
- Set the `NODE_NO_WARNINGS` environment variable to `1`
|
|
35
30
|
- Pass the `--no-warnings` flag to the node process
|
|
36
31
|
- Set `no-warnings` in the `NODE_OPTIONS` environment variable
|
|
37
|
-
- Pass `--disable-warning=
|
|
32
|
+
- Pass `--disable-warning=FSTWRN003` to disable a specific warning
|
|
38
33
|
|
|
39
34
|
For more information on disabling warnings, see [Node's documentation](https://nodejs.org/api/cli.html).
|
|
40
35
|
|
|
@@ -46,7 +41,7 @@ Disabling warnings is not recommended and may cause unexpected behavior.
|
|
|
46
41
|
| ---- | ----------- | ------------ | ---------- |
|
|
47
42
|
| <a id="FSTWRN001">FSTWRN001</a> | The specified schema for a route is missing. This may indicate the schema is not well specified. | Check the schema for the route. | [#4647](https://github.com/fastify/fastify/pull/4647) |
|
|
48
43
|
| <a id="FSTWRN003">FSTWRN003</a> | The `%s` plugin mixes async and callback styles, which may lead to unhandled rejections. | Do not mix async and callback style. | [#6011](https://github.com/fastify/fastify/pull/6011) |
|
|
49
|
-
|
|
44
|
+
|
|
50
45
|
### Fastify Deprecation Codes
|
|
51
46
|
|
|
52
47
|
Deprecation codes are supported by the Node.js CLI options:
|
|
@@ -55,10 +50,8 @@ Deprecation codes are supported by the Node.js CLI options:
|
|
|
55
50
|
- [--throw-deprecation](https://nodejs.org/api/cli.html#throw-deprecation)
|
|
56
51
|
- [--trace-deprecation](https://nodejs.org/api/cli.html#trace-deprecation)
|
|
57
52
|
|
|
53
|
+
There are currently no active deprecation codes because the APIs deprecated in
|
|
54
|
+
Fastify v5 have been removed in Fastify v6.
|
|
58
55
|
|
|
59
56
|
| Code | Description | How to solve | Discussion |
|
|
60
57
|
| ---- | ----------- | ------------ | ---------- |
|
|
61
|
-
| <a id="FSTDEP022">FSTDEP022</a> | You are trying to access the deprecated router options on top option properties. | Use `options.routerOptions`. | [#5985](https://github.com/fastify/fastify/pull/5985)
|
|
62
|
-
| <a id="FSTDEP023">FSTDEP023</a> | `disableRequestLogging` top-level option is deprecated. | Pass a `LogController` instance via the `logController` option with `disableRequestLogging` in its constructor instead. |
|
|
63
|
-
| <a id="FSTDEP024">FSTDEP024</a> | `requestIdLogLabel` top-level option is deprecated. | Pass a `LogController` instance via the `logController` option with `requestIdLogLabel` in its constructor instead. |
|
|
64
|
-
| <a id="FSTDEP025">FSTDEP025</a> | Calling `addHttpMethod` for an existing HTTP method without `{ overrideExisting: true }` is deprecated. | Pass `{ overrideExisting: true }` to make the override explicit. | [#6879](https://github.com/fastify/fastify/pull/6879) |
|
package/fastify.d.ts
CHANGED
|
@@ -10,21 +10,20 @@ import { ConstraintStrategy, Config as FindMyWayConfig, HTTPVersion } from 'find
|
|
|
10
10
|
import { InjectOptions, CallbackFunc as LightMyRequestCallback, Chain as LightMyRequestChain, Response as LightMyRequestResponse } from 'light-my-request'
|
|
11
11
|
|
|
12
12
|
import { AddContentTypeParser, ConstructorAction, FastifyBodyParser, FastifyContentTypeParser, getDefaultJsonParser, hasContentTypeParser, ProtoAction } from './types/content-type-parser'
|
|
13
|
-
import { FastifyContextConfig
|
|
13
|
+
import { FastifyContextConfig } from './types/context'
|
|
14
14
|
import { FastifyErrorCodes } from './types/errors'
|
|
15
15
|
import { DoneFuncWithErrOrRes, HookHandlerDoneFunction, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onListenAsyncHookHandler, onListenHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAbortAsyncHookHandler, onRequestAbortHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preCloseAsyncHookHandler, preCloseHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, RequestPayload } from './types/hooks'
|
|
16
16
|
import { FastifyInstance, FastifyListenOptions, PrintRoutesOptions } from './types/instance'
|
|
17
17
|
import {
|
|
18
18
|
FastifyBaseLogger,
|
|
19
19
|
FastifyChildLoggerFactory,
|
|
20
|
-
LogController as LogControllerClass,
|
|
21
20
|
FastifyLogFn,
|
|
22
|
-
FastifyLoggerInstance,
|
|
23
21
|
FastifyLoggerOptions,
|
|
22
|
+
LogController as LogControllerClass,
|
|
24
23
|
LogLevel,
|
|
25
24
|
PinoLoggerOptions
|
|
26
25
|
} from './types/logger'
|
|
27
|
-
import {
|
|
26
|
+
import { FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions } from './types/plugin'
|
|
28
27
|
import { FastifyRegister, FastifyRegisterOptions, RegisterOptions } from './types/register'
|
|
29
28
|
import { FastifyReply } from './types/reply'
|
|
30
29
|
import { FastifyRequest, RequestGenericInterface } from './types/request'
|
|
@@ -96,7 +95,10 @@ declare namespace fastify {
|
|
|
96
95
|
|
|
97
96
|
type TrustProxyFunction = (address: string, hop: number) => boolean
|
|
98
97
|
|
|
99
|
-
export type FastifyRouterOptions<RawServer extends RawServerBase> = Omit<FindMyWayConfigForServer<RawServer>, 'defaultRoute' | 'onBadUrl' | 'onMaxParamLength' | 'querystringParser'> & {
|
|
98
|
+
export type FastifyRouterOptions<RawServer extends RawServerBase> = Omit<FindMyWayConfigForServer<RawServer>, 'defaultRoute' | 'onBadUrl' | 'onMaxParamLength' | 'querystringParser' | 'constraints'> & {
|
|
99
|
+
constraints?: {
|
|
100
|
+
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServer>, unknown>,
|
|
101
|
+
},
|
|
100
102
|
defaultRoute?: (
|
|
101
103
|
req: RawRequestDefaultExpression<RawServer>,
|
|
102
104
|
res: RawReplyDefaultExpression<RawServer>
|
|
@@ -132,8 +134,6 @@ declare namespace fastify {
|
|
|
132
134
|
bodyLimit?: number,
|
|
133
135
|
handlerTimeout?: number,
|
|
134
136
|
maxParamLength?: number,
|
|
135
|
-
/** @deprecated Use the `logController` option with `disableRequestLogging` or `isLogDisabled` override instead. Will be removed in `fastify@6`. */
|
|
136
|
-
disableRequestLogging?: boolean | ((req: FastifyRequest) => boolean),
|
|
137
137
|
logController?: LogControllerClass,
|
|
138
138
|
exposeHeadRoutes?: boolean,
|
|
139
139
|
onProtoPoisoning?: ProtoAction,
|
|
@@ -142,18 +142,9 @@ declare namespace fastify {
|
|
|
142
142
|
loggerInstance?: Logger
|
|
143
143
|
serializerOpts?: FJSOptions | Record<string, unknown>,
|
|
144
144
|
serverFactory?: FastifyServerFactory<RawServer>,
|
|
145
|
-
caseSensitive?: boolean,
|
|
146
|
-
allowUnsafeRegex?: boolean,
|
|
147
145
|
requestIdHeader?: string | false,
|
|
148
|
-
/** @deprecated Use the `logController` option with `requestIdLogLabel` instead. Will be removed in `fastify@6`. */
|
|
149
|
-
requestIdLogLabel?: string;
|
|
150
|
-
useSemicolonDelimiter?: boolean,
|
|
151
146
|
genReqId?: (req: RawRequestDefaultExpression<RawServer>) => string,
|
|
152
147
|
trustProxy?: boolean | string | string[] | number | TrustProxyFunction,
|
|
153
|
-
querystringParser?: (str: string) => { [key: string]: unknown },
|
|
154
|
-
constraints?: {
|
|
155
|
-
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServer>, unknown>,
|
|
156
|
-
},
|
|
157
148
|
schemaController?: {
|
|
158
149
|
bucket?: (parentSchemas?: unknown) => {
|
|
159
150
|
add(schema: unknown): FastifyInstance;
|
|
@@ -212,20 +203,15 @@ declare namespace fastify {
|
|
|
212
203
|
routerOptions?: FastifyRouterOptions<RawServer>,
|
|
213
204
|
}
|
|
214
205
|
|
|
215
|
-
/**
|
|
216
|
-
* @deprecated use {@link FastifySchemaValidationError}
|
|
217
|
-
*/
|
|
218
|
-
export type ValidationResult = FastifySchemaValidationError
|
|
219
|
-
|
|
220
206
|
/* Export additional types */
|
|
221
207
|
export type {
|
|
222
208
|
LightMyRequestChain, InjectOptions, LightMyRequestResponse, LightMyRequestCallback, // 'light-my-request'
|
|
223
209
|
FastifyRequest, RequestGenericInterface, // './types/request'
|
|
224
210
|
FastifyReply, // './types/reply'
|
|
225
|
-
FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions,
|
|
211
|
+
FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, // './types/plugin'
|
|
226
212
|
FastifyListenOptions, FastifyInstance, PrintRoutesOptions, // './types/instance'
|
|
227
|
-
FastifyLoggerOptions, FastifyBaseLogger,
|
|
228
|
-
|
|
213
|
+
FastifyLoggerOptions, FastifyBaseLogger, FastifyLogFn, LogLevel, // './types/logger'
|
|
214
|
+
FastifyContextConfig, // './types/context'
|
|
229
215
|
RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions,
|
|
230
216
|
RouteShorthandOptionsWithHandler, RouteGenericInterface, // './types/route'
|
|
231
217
|
FastifyRegister, FastifyRegisterOptions, RegisterOptions, // './types/register'
|