fastify 5.10.0 → 5.11.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/.pi/bash-confirm-whitelist.json +180 -0
- package/SPONSORS.md +1 -0
- package/docs/Guides/Ecosystem.md +2 -2
- package/docs/Guides/Getting-Started.md +8 -8
- package/docs/Reference/Errors.md +4 -0
- package/docs/Reference/Reply.md +2 -2
- package/docs/Reference/Request.md +11 -11
- package/docs/Reference/Server.md +34 -17
- package/docs/Reference/TypeScript.md +5 -7
- package/docs/Reference/Validation-and-Serialization.md +13 -2
- package/docs/Reference/Warnings.md +3 -2
- package/eslint.config.js +1 -1
- package/fastify.d.ts +57 -13
- package/fastify.js +17 -5
- package/lib/content-type.js +36 -18
- package/lib/errors.js +10 -0
- package/lib/handle-request.js +35 -7
- package/lib/hooks.js +5 -1
- package/lib/route.js +1 -1
- package/lib/schemas.js +3 -3
- package/lib/validation.js +1 -1
- package/lib/warnings.js +11 -2
- package/package.json +2 -2
- package/test/close.test.js +39 -0
- package/test/content-type.test.js +31 -4
- package/test/find-route.test.js +35 -0
- package/test/fix-6411.test.js +65 -0
- package/test/http-methods/custom-http-methods.test.js +61 -0
- package/test/internals/all.test.js +2 -1
- package/test/internals/errors.test.js +21 -1
- package/test/internals/reply.test.js +22 -0
- package/test/reply-error.test.js +35 -0
- package/test/rfc-10008.test.js +147 -0
- package/test/route-shorthand.test.js +14 -4
- package/test/schema-serialization.test.js +33 -0
- package/test/types/errors.tst.ts +2 -0
- package/test/types/fastify.tst.ts +11 -0
- package/test/types/instance.tst.ts +2 -0
- package/test/types/route.tst.ts +3 -3
- package/test/validation-error-handling.test.js +34 -0
- package/types/content-type-parser.d.ts +26 -6
- package/types/errors.d.ts +2 -0
- package/types/hooks.d.ts +137 -76
- package/types/instance.d.ts +153 -47
- package/types/logger.d.ts +36 -3
- package/types/plugin.d.ts +7 -3
- package/types/register.d.ts +53 -10
- package/types/reply.d.ts +62 -14
- package/types/route.d.ts +68 -34
- package/types/type-provider.d.ts +29 -8
- package/types/utils.d.ts +2 -2
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
{
|
|
2
|
+
"entries": [
|
|
3
|
+
{
|
|
4
|
+
"type": "exact",
|
|
5
|
+
"value": "git fetch --all --prune --quiet && echo FETCH_OK || echo FETCH_FAILED",
|
|
6
|
+
"addedAt": "2026-02-07T10:03:03.909Z",
|
|
7
|
+
"note": "Always accept",
|
|
8
|
+
"source": "user"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"type": "exact",
|
|
12
|
+
"value": "npm install --ignore-scripts",
|
|
13
|
+
"addedAt": "2026-02-07T10:21:13.104Z",
|
|
14
|
+
"note": "Always accept",
|
|
15
|
+
"source": "user"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"type": "exact",
|
|
19
|
+
"value": "npm run coverage:ci-check-coverage",
|
|
20
|
+
"addedAt": "2026-02-07T15:10:08.412Z",
|
|
21
|
+
"note": "Always accept",
|
|
22
|
+
"source": "user"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"type": "exact",
|
|
26
|
+
"value": "node --test test/web-api.test.js",
|
|
27
|
+
"addedAt": "2026-02-07T15:24:46.872Z",
|
|
28
|
+
"note": "Always accept",
|
|
29
|
+
"source": "user"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"type": "exact",
|
|
33
|
+
"value": "gpg --list-secret-keys --keyid-format=long || true && git config --get user.signingkey && git config --get commit.gpgsign",
|
|
34
|
+
"addedAt": "2026-02-11T04:42:44.716Z",
|
|
35
|
+
"note": "Always accept",
|
|
36
|
+
"source": "user"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"type": "exact",
|
|
40
|
+
"value": "cd /home/matteo/repositories/fastify && npm run test:typescript",
|
|
41
|
+
"addedAt": "2026-02-19T20:22:59.298Z",
|
|
42
|
+
"note": "Always accept",
|
|
43
|
+
"source": "user"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"type": "exact",
|
|
47
|
+
"value": "cd /home/matteo/repositories/fastify && git checkout -- types/route.d.ts",
|
|
48
|
+
"addedAt": "2026-02-19T20:23:45.912Z",
|
|
49
|
+
"note": "Always accept",
|
|
50
|
+
"source": "user"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"type": "exact",
|
|
54
|
+
"value": "cd /home/matteo/repositories/fastify && git status --short && git diff -- types/route.d.ts test/types/route.test-d.ts | sed -n '1,220p'",
|
|
55
|
+
"addedAt": "2026-02-19T20:25:10.175Z",
|
|
56
|
+
"note": "Always accept",
|
|
57
|
+
"source": "user"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"type": "exact",
|
|
61
|
+
"value": "cd /home/matteo/repositories/fastify && git checkout -- types/route.d.ts test/types/route.test-d.ts && npm run test:typescript",
|
|
62
|
+
"addedAt": "2026-02-19T20:25:56.421Z",
|
|
63
|
+
"note": "Always accept",
|
|
64
|
+
"source": "user"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"type": "exact",
|
|
68
|
+
"value": "cd /home/matteo/repositories/fastify && npm run lint:eslint -- types/route.d.ts test/types/route.test-d.ts",
|
|
69
|
+
"addedAt": "2026-02-19T20:27:12.547Z",
|
|
70
|
+
"note": "Always accept",
|
|
71
|
+
"source": "user"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"type": "exact",
|
|
75
|
+
"value": "cd /home/matteo/repositories/fastify && git checkout -b fix/6513-route-hook-meta-return",
|
|
76
|
+
"addedAt": "2026-02-19T20:27:31.569Z",
|
|
77
|
+
"note": "Always accept",
|
|
78
|
+
"source": "user"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"type": "exact",
|
|
82
|
+
"value": "cd /home/matteo/repositories/fastify && git add types/route.d.ts test/types/route.test-d.ts && git commit -m \"fix(types): allow async route hooks in shorthand options\"",
|
|
83
|
+
"addedAt": "2026-02-19T20:27:35.261Z",
|
|
84
|
+
"note": "Always accept",
|
|
85
|
+
"source": "user"
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"type": "exact",
|
|
89
|
+
"value": "cd /home/matteo/repositories/fastify && npx tsc /tmp/ctx-test2.ts --noEmit --strict",
|
|
90
|
+
"addedAt": "2026-02-19T20:44:45.451Z",
|
|
91
|
+
"note": "Always accept",
|
|
92
|
+
"source": "user"
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
"type": "exact",
|
|
96
|
+
"value": "cd /home/matteo/repositories/fastify && npx tsd --files test/types/hooks.test-d.ts",
|
|
97
|
+
"addedAt": "2026-02-19T20:47:16.815Z",
|
|
98
|
+
"note": "Always accept",
|
|
99
|
+
"source": "user"
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
"type": "exact",
|
|
103
|
+
"value": "cd /home/matteo/repositories/fastify && npx tsd --files test/types/route.test-d.ts",
|
|
104
|
+
"addedAt": "2026-02-19T20:47:37.607Z",
|
|
105
|
+
"note": "Always accept",
|
|
106
|
+
"source": "user"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"type": "exact",
|
|
110
|
+
"value": "cd /home/matteo/repositories/fastify && npx tsd",
|
|
111
|
+
"addedAt": "2026-02-19T20:48:17.812Z",
|
|
112
|
+
"note": "Always accept",
|
|
113
|
+
"source": "user"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"type": "exact",
|
|
117
|
+
"value": "cd /home/matteo/repositories/fastify && npm run lint:eslint -- types/route.d.ts && npx tsd --files test/types/hooks.test-d.ts && npx tsd --files test/types/route.test-d.ts",
|
|
118
|
+
"addedAt": "2026-02-19T20:51:05.173Z",
|
|
119
|
+
"note": "Always accept",
|
|
120
|
+
"source": "user"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"type": "exact",
|
|
124
|
+
"value": "which gh && gh --version && gh auth status",
|
|
125
|
+
"addedAt": "2026-02-20T07:03:43.023Z",
|
|
126
|
+
"note": "Always accept",
|
|
127
|
+
"source": "user"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"type": "exact",
|
|
131
|
+
"value": "gh api repos/fastify/fastify/rulesets",
|
|
132
|
+
"addedAt": "2026-02-22T22:29:22.020Z",
|
|
133
|
+
"note": "Always accept",
|
|
134
|
+
"source": "user"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"type": "exact",
|
|
138
|
+
"value": "node --test test/fastify-instance.test.js",
|
|
139
|
+
"addedAt": "2026-02-23T07:23:34.147Z",
|
|
140
|
+
"note": "Always accept",
|
|
141
|
+
"source": "user"
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
"type": "exact",
|
|
145
|
+
"value": "nl -ba scripts/validate-ecosystem-links.js | sed -n '1,220p'",
|
|
146
|
+
"addedAt": "2026-02-23T07:23:55.927Z",
|
|
147
|
+
"note": "Always accept",
|
|
148
|
+
"source": "user"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
"type": "pattern",
|
|
152
|
+
"value": "^npx\\s+eslint\\s+[\\w./~-]+\\s+[\\w./~-]+\\s+[\\w./~-]+\\s+[\\w./~-]+$",
|
|
153
|
+
"addedAt": "2026-03-09T12:41:11.065Z",
|
|
154
|
+
"note": "Always accept generic",
|
|
155
|
+
"source": "ai"
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
"type": "pattern",
|
|
159
|
+
"value": "^node\\s+--test\\s+[\\w./~-]+\\s+[\\w./~-]+$",
|
|
160
|
+
"addedAt": "2026-03-09T12:41:58.425Z",
|
|
161
|
+
"note": "Always accept generic",
|
|
162
|
+
"source": "ai"
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
"type": "pattern",
|
|
166
|
+
"value": "^npm\\s+run\\s+test:typescript$",
|
|
167
|
+
"addedAt": "2026-03-09T12:42:48.283Z",
|
|
168
|
+
"note": "Always accept generic",
|
|
169
|
+
"source": "ai"
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"type": "exact",
|
|
173
|
+
"value": "gh search code \"request.host org:fastify\" --limit 20 --json repository,path,url",
|
|
174
|
+
"addedAt": "2026-03-09T21:03:22.953Z",
|
|
175
|
+
"note": "Always accept exact",
|
|
176
|
+
"source": "user"
|
|
177
|
+
}
|
|
178
|
+
],
|
|
179
|
+
"version": 2
|
|
180
|
+
}
|
package/SPONSORS.md
CHANGED
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -208,8 +208,6 @@ section.
|
|
|
208
208
|
Type-safe dependency injection support for Fastify, powered by [InferDI](https://github.com/inferdi/inferdi).
|
|
209
209
|
- [`@jerome1337/fastify-enforce-routes-pattern`](https://github.com/Jerome1337/fastify-enforce-routes-pattern)
|
|
210
210
|
A Fastify plugin that enforces naming pattern for routes path.
|
|
211
|
-
- [`@joggr/fastify-prisma`](https://github.com/joggrdocs/fastify-prisma)
|
|
212
|
-
A plugin for accessing an instantiated PrismaClient on your server.
|
|
213
211
|
- [`@matths/fastify-svelte-view`](https://github.com/matths/fastify-svelte-view)
|
|
214
212
|
A Fastify plugin for rendering Svelte components with support for SSR
|
|
215
213
|
(Server-Side Rendering), CSR (Client-Side Rendering), and SSR with hydration.
|
|
@@ -242,6 +240,8 @@ section.
|
|
|
242
240
|
SeaweedFS for Fastify
|
|
243
241
|
- [`@yeliex/fastify-problem-details`](https://github.com/yeliex/fastify-problem-details)
|
|
244
242
|
RFC 9457 Problem Details implementation for Fastify, with typed HTTP errors.
|
|
243
|
+
- [`@zrosenbauer/fastify-prisma`](https://github.com/zrosenbauer/fastify-prisma)
|
|
244
|
+
A plugin for accessing an instantiated PrismaClient on your server.
|
|
245
245
|
- [`apitally`](https://github.com/apitally/apitally-js) Fastify plugin to
|
|
246
246
|
integrate with [Apitally](https://apitally.io/fastify), an API analytics,
|
|
247
247
|
logging and monitoring tool.
|
|
@@ -146,7 +146,7 @@ declaration](../Reference/Routes.md) docs).
|
|
|
146
146
|
```js
|
|
147
147
|
// ESM
|
|
148
148
|
import Fastify from 'fastify'
|
|
149
|
-
import
|
|
149
|
+
import routes from './our-first-routes.js'
|
|
150
150
|
/**
|
|
151
151
|
* @type {import('fastify').FastifyInstance} Instance of Fastify
|
|
152
152
|
*/
|
|
@@ -154,7 +154,7 @@ const fastify = Fastify({
|
|
|
154
154
|
logger: true
|
|
155
155
|
})
|
|
156
156
|
|
|
157
|
-
fastify.register(
|
|
157
|
+
fastify.register(routes)
|
|
158
158
|
|
|
159
159
|
fastify.listen({ port: 3000 }, function (err, address) {
|
|
160
160
|
if (err) {
|
|
@@ -174,7 +174,7 @@ const fastify = require('fastify')({
|
|
|
174
174
|
logger: true
|
|
175
175
|
})
|
|
176
176
|
|
|
177
|
-
fastify.register(require('./our-first-
|
|
177
|
+
fastify.register(require('./our-first-routes'))
|
|
178
178
|
|
|
179
179
|
fastify.listen({ port: 3000 }, function (err, address) {
|
|
180
180
|
if (err) {
|
|
@@ -187,7 +187,7 @@ fastify.listen({ port: 3000 }, function (err, address) {
|
|
|
187
187
|
|
|
188
188
|
|
|
189
189
|
```js
|
|
190
|
-
// our-first-
|
|
190
|
+
// our-first-routes.js
|
|
191
191
|
|
|
192
192
|
/**
|
|
193
193
|
* Encapsulates the routes
|
|
@@ -236,7 +236,7 @@ npm i fastify-plugin @fastify/mongodb
|
|
|
236
236
|
// ESM
|
|
237
237
|
import Fastify from 'fastify'
|
|
238
238
|
import dbConnector from './our-db-connector.js'
|
|
239
|
-
import
|
|
239
|
+
import routes from './our-first-routes.js'
|
|
240
240
|
|
|
241
241
|
/**
|
|
242
242
|
* @type {import('fastify').FastifyInstance} Instance of Fastify
|
|
@@ -245,7 +245,7 @@ const fastify = Fastify({
|
|
|
245
245
|
logger: true
|
|
246
246
|
})
|
|
247
247
|
fastify.register(dbConnector)
|
|
248
|
-
fastify.register(
|
|
248
|
+
fastify.register(routes)
|
|
249
249
|
|
|
250
250
|
fastify.listen({ port: 3000 }, function (err, address) {
|
|
251
251
|
if (err) {
|
|
@@ -266,7 +266,7 @@ const fastify = require('fastify')({
|
|
|
266
266
|
})
|
|
267
267
|
|
|
268
268
|
fastify.register(require('./our-db-connector'))
|
|
269
|
-
fastify.register(require('./our-first-
|
|
269
|
+
fastify.register(require('./our-first-routes'))
|
|
270
270
|
|
|
271
271
|
fastify.listen({ port: 3000 }, function (err, address) {
|
|
272
272
|
if (err) {
|
|
@@ -325,7 +325,7 @@ module.exports = fastifyPlugin(dbConnector)
|
|
|
325
325
|
|
|
326
326
|
```
|
|
327
327
|
|
|
328
|
-
**our-first-
|
|
328
|
+
**our-first-routes.js**
|
|
329
329
|
```js
|
|
330
330
|
/**
|
|
331
331
|
* A plugin that provide encapsulated routes
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -91,6 +91,8 @@
|
|
|
91
91
|
|
|
92
92
|
- [FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT](#fst_err_route_handler_timeout_option_not_int)
|
|
93
93
|
- [FST_ERR_ROUTE_REWRITE_NOT_STR](#fst_err_route_rewrite_not_str)
|
|
94
|
+
- [FST_ERR_ROUTE_MISSING_CONTENT_TYPE](#fst_err_route_missing_content_type)
|
|
95
|
+
- [FST_ERR_ROUTE_MISSING_CONTENT](#fst_err_route_missing_content)
|
|
94
96
|
- [FST_ERR_REOPENED_CLOSE_SERVER](#fst_err_reopened_close_server)
|
|
95
97
|
- [FST_ERR_REOPENED_SERVER](#fst_err_reopened_server)
|
|
96
98
|
- [FST_ERR_PLUGIN_VERSION_MISMATCH](#fst_err_plugin_version_mismatch)
|
|
@@ -369,6 +371,8 @@ Below is a table with all the error codes used by Fastify.
|
|
|
369
371
|
| <a id="fst_err_handler_timeout">FST_ERR_HANDLER_TIMEOUT</a> | Request timed out. | Increase the `handlerTimeout` option or optimize the handler. | - |
|
|
370
372
|
| <a id="fst_err_route_handler_timeout_option_not_int">FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT</a> | `handlerTimeout` option must be a positive integer. | Use a positive integer for the `handlerTimeout` option. | - |
|
|
371
373
|
| <a id="fst_err_route_rewrite_not_str">FST_ERR_ROUTE_REWRITE_NOT_STR</a> | `rewriteUrl` needs to be of type `string`. | Use a string for the `rewriteUrl`. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
374
|
+
| <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) |
|
|
375
|
+
| <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) |
|
|
372
376
|
| <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) |
|
|
373
377
|
| <a id="fst_err_reopened_server">FST_ERR_REOPENED_SERVER</a> | Fastify is already listening. | - | [#2415](https://github.com/fastify/fastify/pull/2415) |
|
|
374
378
|
| <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) |
|
package/docs/Reference/Reply.md
CHANGED
|
@@ -271,7 +271,7 @@ as soon as possible.
|
|
|
271
271
|
> in the error, you can turn on `debug` level logging.
|
|
272
272
|
|
|
273
273
|
```js
|
|
274
|
-
reply.trailer('server-timing', function() {
|
|
274
|
+
reply.trailer('server-timing', async function () {
|
|
275
275
|
return 'db;dur=53, app;dur=47.2'
|
|
276
276
|
})
|
|
277
277
|
|
|
@@ -304,7 +304,7 @@ Returns a boolean indicating if the specified trailer has been set.
|
|
|
304
304
|
|
|
305
305
|
Remove the value of a previously set trailer.
|
|
306
306
|
```js
|
|
307
|
-
reply.trailer('server-timing', function() {
|
|
307
|
+
reply.trailer('server-timing', async function () {
|
|
308
308
|
return 'db;dur=53, app;dur=47.2'
|
|
309
309
|
})
|
|
310
310
|
reply.removeTrailer('server-timing')
|
|
@@ -34,14 +34,14 @@ Request is a core Fastify object containing the following fields:
|
|
|
34
34
|
value comes from `socket.encrypted` (or `X-Forwarded-Proto` when
|
|
35
35
|
[`trustProxy`](./Server.md#factory-trust-proxy) is enabled).
|
|
36
36
|
|
|
37
|
-
> ⚠️ Security:
|
|
38
|
-
> `request.ip`, `request.ips`, `request.host`, `request.hostname`,
|
|
39
|
-
> `request.port`, and `request.protocol` come from request metadata
|
|
40
|
-
> (socket and/or forwarding headers) and should be treated as untrusted input.
|
|
41
|
-
> Fastify does not perform security validation for business logic.
|
|
42
|
-
> If these values are used in security-sensitive decisions, they must
|
|
43
|
-
> be validated explicitly (for example: trusted proxy configuration,
|
|
44
|
-
> allow-lists, strict parsing, and normalization).
|
|
37
|
+
> ⚠️ Security:
|
|
38
|
+
> `request.ip`, `request.ips`, `request.host`, `request.hostname`,
|
|
39
|
+
> `request.port`, and `request.protocol` come from request metadata
|
|
40
|
+
> (socket and/or forwarding headers) and should be treated as untrusted input.
|
|
41
|
+
> Fastify does not perform security validation for business logic.
|
|
42
|
+
> If these values are used in security-sensitive decisions, they must
|
|
43
|
+
> be validated explicitly (for example: trusted proxy configuration,
|
|
44
|
+
> allow-lists, strict parsing, and normalization).
|
|
45
45
|
|
|
46
46
|
- `method` - The method of the incoming request.
|
|
47
47
|
- `url` - The URL of the incoming request.
|
|
@@ -87,9 +87,9 @@ Request is a core Fastify object containing the following fields:
|
|
|
87
87
|
default (or customized) `ValidationCompiler`. The optional `httpPart` is
|
|
88
88
|
forwarded to the `ValidationCompiler` if provided, defaults to `null`.
|
|
89
89
|
- [.validateInput(data, schema | httpPart, [httpPart])](#validate) -
|
|
90
|
-
Validates the input using the specified schema and returns
|
|
91
|
-
|
|
92
|
-
that HTTP
|
|
90
|
+
Validates the input using the specified schema or HTTP part and returns
|
|
91
|
+
`true` if the input is valid, `false` otherwise. If `httpPart` is provided,
|
|
92
|
+
the function uses the validator for that HTTP part. Defaults to `null`.
|
|
93
93
|
|
|
94
94
|
### Headers
|
|
95
95
|
|
package/docs/Reference/Server.md
CHANGED
|
@@ -390,8 +390,8 @@ Pino interface by having the following methods: `info`, `error`, `debug`,
|
|
|
390
390
|
### `disableRequestLogging`
|
|
391
391
|
<a id="factory-disable-request-logging"></a>
|
|
392
392
|
|
|
393
|
-
> **Deprecated:** Use the [`logController`](#log-controller)
|
|
394
|
-
> `disableRequestLogging` or `isLogDisabled` override instead.
|
|
393
|
+
> **Deprecated:** Use the [`logController`](#factory-log-controller)
|
|
394
|
+
> option with `disableRequestLogging` or `isLogDisabled` override instead.
|
|
395
395
|
> This top-level option will be removed in `fastify@6`.
|
|
396
396
|
|
|
397
397
|
+ Default: `false`
|
|
@@ -407,6 +407,8 @@ and returns a boolean. This allows for conditional request logging based on the
|
|
|
407
407
|
request properties (e.g., URL, headers, decorations).
|
|
408
408
|
|
|
409
409
|
```js
|
|
410
|
+
const { LogController } = require('fastify')
|
|
411
|
+
|
|
410
412
|
// Deprecated
|
|
411
413
|
const fastify = require('fastify')({
|
|
412
414
|
logger: true,
|
|
@@ -418,11 +420,11 @@ const fastify = require('fastify')({
|
|
|
418
420
|
// Recommended: use logController instead
|
|
419
421
|
const fastify = require('fastify')({
|
|
420
422
|
logger: true,
|
|
421
|
-
logController: {
|
|
423
|
+
logController: new LogController({
|
|
422
424
|
disableRequestLogging: (request) => {
|
|
423
425
|
return request.url === '/health' || request.url === '/ready'
|
|
424
426
|
}
|
|
425
|
-
}
|
|
427
|
+
})
|
|
426
428
|
})
|
|
427
429
|
```
|
|
428
430
|
|
|
@@ -567,7 +569,7 @@ enhance the server instance inside the `serverFactory` function before the
|
|
|
567
569
|
### `requestIdHeader`
|
|
568
570
|
<a id="factory-request-id-header"></a>
|
|
569
571
|
|
|
570
|
-
+ Default: `
|
|
572
|
+
+ Default: `false`
|
|
571
573
|
|
|
572
574
|
The header name used to set the request-id. See [the
|
|
573
575
|
request-id](./Logging.md#logging-request-id) section.
|
|
@@ -579,8 +581,6 @@ By default `requestIdHeader` is set to `false` and will immediately use [genReqI
|
|
|
579
581
|
Setting `requestIdHeader` to an empty String (`""`) will set the
|
|
580
582
|
requestIdHeader to `false`.
|
|
581
583
|
|
|
582
|
-
+ Default: `false`
|
|
583
|
-
|
|
584
584
|
```js
|
|
585
585
|
const fastify = require('fastify')({
|
|
586
586
|
requestIdHeader: 'x-custom-id', // -> use 'X-Custom-Id' header if available
|
|
@@ -596,8 +596,9 @@ const fastify = require('fastify')({
|
|
|
596
596
|
### `requestIdLogLabel`
|
|
597
597
|
<a id="factory-request-id-log-label"></a>
|
|
598
598
|
|
|
599
|
-
> **Deprecated:** Use the [`logController`](#log-controller)
|
|
600
|
-
> `requestIdLogLabel` instead. This top-level option will be
|
|
599
|
+
> **Deprecated:** Use the [`logController`](#factory-log-controller)
|
|
600
|
+
> option with `requestIdLogLabel` instead. This top-level option will be
|
|
601
|
+
> removed in `fastify@6`.
|
|
601
602
|
|
|
602
603
|
+ Default: `'reqId'`
|
|
603
604
|
|
|
@@ -659,11 +660,11 @@ For more examples, refer to the
|
|
|
659
660
|
You may access the `ip`, `ips`, `host` and `protocol` values on the
|
|
660
661
|
[`request`](./Request.md) object.
|
|
661
662
|
|
|
662
|
-
> ⚠️ Security:
|
|
663
|
-
> These values are derived from socket/forwarding metadata and must be treated
|
|
664
|
-
> as untrusted input unless your proxy chain is explicitly trusted and
|
|
665
|
-
> validated. Do not use them directly for authorization or other
|
|
666
|
-
> security-sensitive decisions without explicit validation.
|
|
663
|
+
> ⚠️ Security:
|
|
664
|
+
> These values are derived from socket/forwarding metadata and must be treated
|
|
665
|
+
> as untrusted input unless your proxy chain is explicitly trusted and
|
|
666
|
+
> validated. Do not use them directly for authorization or other
|
|
667
|
+
> security-sensitive decisions without explicit validation.
|
|
667
668
|
|
|
668
669
|
```js
|
|
669
670
|
fastify.get('/', (request, reply) => {
|
|
@@ -1702,10 +1703,17 @@ Fake HTTP injection (for testing purposes)
|
|
|
1702
1703
|
<a id="addHttpMethod"></a>
|
|
1703
1704
|
|
|
1704
1705
|
Fastify supports the `GET`, `HEAD`, `TRACE`, `DELETE`, `OPTIONS`,
|
|
1705
|
-
`PATCH`, `PUT` and `
|
|
1706
|
+
`PATCH`, `PUT`, `POST` and `QUERY` HTTP methods by default.
|
|
1706
1707
|
The `addHttpMethod` method allows to add any non standard HTTP
|
|
1707
1708
|
methods to the server that are [supported by Node.js](https://nodejs.org/api/http.html#httpmethods).
|
|
1708
1709
|
|
|
1710
|
+
The method accepts an optional configuration object:
|
|
1711
|
+
|
|
1712
|
+
| Property | Type | Default | Description |
|
|
1713
|
+
| -------- | ---- | ------- | ----------- |
|
|
1714
|
+
| `hasBody` | `boolean` | `false` | Whether the method accepts a request body. |
|
|
1715
|
+
| `overrideExisting` | `boolean` | `false` | Whether to explicitly override an existing method. |
|
|
1716
|
+
|
|
1709
1717
|
```js
|
|
1710
1718
|
// Add a new HTTP method called 'MKCOL' that supports a request body
|
|
1711
1719
|
fastify.addHttpMethod('MKCOL', { hasBody: true, })
|
|
@@ -1724,8 +1732,17 @@ fastify.mkcol('/', (req, reply) => {
|
|
|
1724
1732
|
})
|
|
1725
1733
|
```
|
|
1726
1734
|
|
|
1727
|
-
|
|
1728
|
-
|
|
1735
|
+
Calling `addHttpMethod` for an existing method overrides its body behavior.
|
|
1736
|
+
|
|
1737
|
+
```js
|
|
1738
|
+
fastify.addHttpMethod('GET', {
|
|
1739
|
+
hasBody: true,
|
|
1740
|
+
overrideExisting: true
|
|
1741
|
+
})
|
|
1742
|
+
```
|
|
1743
|
+
|
|
1744
|
+
In Fastify v5, omitting `overrideExisting: true` emits `FSTDEP025`; in Fastify
|
|
1745
|
+
v6, it will throw an error.
|
|
1729
1746
|
|
|
1730
1747
|
#### addSchema
|
|
1731
1748
|
<a id="add-schema"></a>
|
|
@@ -15,7 +15,7 @@ fill in the gaps. Just make sure to read our
|
|
|
15
15
|
[`CONTRIBUTING.md`](https://github.com/fastify/fastify/blob/main/CONTRIBUTING.md)
|
|
16
16
|
file before getting started to make sure things go smoothly!
|
|
17
17
|
|
|
18
|
-
> The documentation in this section covers Fastify
|
|
18
|
+
> The documentation in this section covers Fastify typings
|
|
19
19
|
|
|
20
20
|
> Plugins may or may not include typings. See [Plugins](#plugins) for more
|
|
21
21
|
> information. We encourage users to send pull requests to improve typings
|
|
@@ -90,7 +90,7 @@ in a blank http Fastify server.
|
|
|
90
90
|
🏓
|
|
91
91
|
|
|
92
92
|
🎉 You now have a working Typescript Fastify server! This example demonstrates
|
|
93
|
-
the simplicity of the
|
|
93
|
+
the simplicity of the type system. By default, the type system
|
|
94
94
|
assumes you are using an `http` server. The later examples will demonstrate how
|
|
95
95
|
to create more complex servers such as `https` and `http2`, how to specify route
|
|
96
96
|
schemas, and more!
|
|
@@ -545,9 +545,9 @@ Fastify Plugin in a TypeScript Project.
|
|
|
545
545
|
}
|
|
546
546
|
|
|
547
547
|
// export plugin using fastify-plugin
|
|
548
|
-
export default fp(myPluginCallback
|
|
548
|
+
export default fp(myPluginCallback)
|
|
549
549
|
// or
|
|
550
|
-
// export default fp(myPluginAsync
|
|
550
|
+
// export default fp(myPluginAsync)
|
|
551
551
|
```
|
|
552
552
|
6. Run `npm run build` to compile the plugin code and produce both a JavaScript
|
|
553
553
|
source file and a type definition file.
|
|
@@ -595,7 +595,6 @@ your plugin.
|
|
|
595
595
|
}
|
|
596
596
|
|
|
597
597
|
module.exports = fp(myPlugin, {
|
|
598
|
-
fastify: '5.x',
|
|
599
598
|
name: 'my-plugin' // this is used by fastify-plugin to derive the property name
|
|
600
599
|
})
|
|
601
600
|
```
|
|
@@ -843,8 +842,7 @@ module.exports = async function (fastify, { optionA, optionB }) {
|
|
|
843
842
|
|
|
844
843
|
## API Type System Documentation
|
|
845
844
|
|
|
846
|
-
This section is a detailed account of all the types available to you in Fastify
|
|
847
|
-
version 3.x
|
|
845
|
+
This section is a detailed account of all the types available to you in Fastify.
|
|
848
846
|
|
|
849
847
|
All `http`, `https`, and `http2` types are inferred from `@types/node`
|
|
850
848
|
|
|
@@ -876,14 +876,25 @@ with the following payload:
|
|
|
876
876
|
|
|
877
877
|
To handle errors inside the route, specify the `attachValidation` option. If
|
|
878
878
|
there is a validation error, the `validationError` property of the request will
|
|
879
|
-
contain the `Error` object
|
|
879
|
+
contain the same `Error` object Fastify would have sent on its own, so no
|
|
880
|
+
message has to be rebuilt from the raw validation result:
|
|
881
|
+
|
|
882
|
+
- `message` is the formatted message, identical to the one in the response
|
|
883
|
+
payload above (for example `body must have required property 'name'`). It is
|
|
884
|
+
produced by [`schemaErrorFormatter`](#schemaerrorformatter), so a custom
|
|
885
|
+
formatter is reflected here too.
|
|
886
|
+
- `validation` is the raw validation result, as returned by the validator.
|
|
887
|
+
- `validationContext` is the part of the request that failed validation
|
|
888
|
+
(`body`, `params`, `querystring` or `headers`).
|
|
889
|
+
- `code` is `FST_ERR_VALIDATION` and `statusCode` is `400`.
|
|
880
890
|
|
|
881
891
|
```js
|
|
882
892
|
const fastify = Fastify()
|
|
883
893
|
|
|
884
894
|
fastify.post('/', { schema, attachValidation: true }, function (req, reply) {
|
|
885
895
|
if (req.validationError) {
|
|
886
|
-
// `req.validationError.
|
|
896
|
+
// `req.validationError.message` is the formatted message
|
|
897
|
+
// `req.validationError.validation` contains the raw validation result
|
|
887
898
|
reply.code(400).send(req.validationError)
|
|
888
899
|
}
|
|
889
900
|
})
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
- [FSTDEP022](#FSTDEP022)
|
|
13
13
|
- [FSTDEP023](#FSTDEP023)
|
|
14
14
|
- [FSTDEP024](#FSTDEP024)
|
|
15
|
+
- [FSTDEP025](#FSTDEP025)
|
|
15
16
|
|
|
16
17
|
## Warnings
|
|
17
18
|
|
|
@@ -33,6 +34,7 @@ one of the following methods:
|
|
|
33
34
|
- Set the `NODE_NO_WARNINGS` environment variable to `1`
|
|
34
35
|
- Pass the `--no-warnings` flag to the node process
|
|
35
36
|
- Set `no-warnings` in the `NODE_OPTIONS` environment variable
|
|
37
|
+
- Pass `--disable-warning=FSTWRN004` to disable a specific warning
|
|
36
38
|
|
|
37
39
|
For more information on disabling warnings, see [Node's documentation](https://nodejs.org/api/cli.html).
|
|
38
40
|
|
|
@@ -45,8 +47,6 @@ Disabling warnings is not recommended and may cause unexpected behavior.
|
|
|
45
47
|
| <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) |
|
|
46
48
|
| <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) |
|
|
47
49
|
| <a id="FSTWRN004">FSTWRN004</a> | An `errorHandler` is being overridden in the same scope, which can lead to subtle bugs. | Avoid calling `setErrorHandler` more than once in the same scope. For more information, see [Server documentation](https://fastify.dev/docs/latest/Reference/Server/#allowerrorhandleroverride). | [#6104](https://github.com/fastify/fastify/pull/6104) |
|
|
48
|
-
|
|
49
|
-
|
|
50
50
|
### Fastify Deprecation Codes
|
|
51
51
|
|
|
52
52
|
Deprecation codes are supported by the Node.js CLI options:
|
|
@@ -61,3 +61,4 @@ Deprecation codes are supported by the Node.js CLI options:
|
|
|
61
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
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
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) |
|