fastify 5.6.1 → 5.7.0
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/.claude/settings.local.json +11 -0
- package/README.md +34 -33
- package/SECURITY.md +45 -7
- package/SPONSORS.md +1 -1
- package/build/build-validation.js +2 -3
- package/docs/Guides/Detecting-When-Clients-Abort.md +1 -1
- package/docs/Guides/Ecosystem.md +17 -15
- package/docs/Guides/Fluent-Schema.md +2 -2
- package/docs/Guides/Migration-Guide-V4.md +2 -1
- package/docs/Guides/Migration-Guide-V5.md +9 -0
- package/docs/Guides/Serverless.md +25 -7
- package/docs/Guides/Testing.md +2 -2
- package/docs/Reference/Decorators.md +4 -3
- package/docs/Reference/Encapsulation.md +5 -1
- package/docs/Reference/Logging.md +4 -0
- package/docs/Reference/Plugins.md +13 -4
- package/docs/Reference/Principles.md +2 -2
- package/docs/Reference/Reply.md +6 -2
- package/docs/Reference/Server.md +67 -4
- package/docs/Reference/Type-Providers.md +2 -2
- package/docs/Reference/TypeScript.md +2 -1
- package/docs/Reference/Validation-and-Serialization.md +7 -1
- package/docs/fastify-fastify-pr-6425-run-20528050272-Lint_Docs.log +180 -0
- package/docs/fastify-fastify-pr-6425-run-20528054188-PR_#6425.log +4006 -0
- package/docs/fastify-fastify-pr-6425-run-20528054403-Pull_Request_Labeler.log +42 -0
- package/docs/fastify-fastify-pr-6425-run-20528054421-Lint_Docs.log +196 -0
- package/docs/fastify-fastify-pr-6425-run-20528054423-Internal_Links_Check.log +1864 -0
- package/docs/fastify-fastify-pr-6425-run-20528054430-Test_compare.log +5 -0
- package/docs/fastify-fastify-pr-6425-run-20528054438-pull_request_title_check.log +41 -0
- package/eslint.config.js +18 -2
- package/examples/benchmark/webstream.js +27 -0
- package/fastify.d.ts +17 -22
- package/fastify.js +192 -177
- package/lib/{configValidator.js → config-validator.js} +189 -223
- package/lib/{contentTypeParser.js → content-type-parser.js} +2 -1
- package/lib/error-handler.js +3 -6
- package/lib/error-status.js +14 -0
- package/lib/{fourOhFour.js → four-oh-four.js} +6 -3
- package/lib/{handleRequest.js → handle-request.js} +7 -2
- package/lib/{headRoute.js → head-route.js} +13 -1
- package/lib/{initialConfigValidation.js → initial-config-validation.js} +1 -1
- package/lib/{pluginOverride.js → plugin-override.js} +2 -2
- package/lib/{pluginUtils.js → plugin-utils.js} +2 -2
- package/lib/reply.js +58 -6
- package/lib/request.js +5 -0
- package/lib/route.js +39 -14
- package/lib/schema-controller.js +2 -2
- package/lib/server.js +38 -5
- package/lib/validation.js +9 -1
- package/lib/{wrapThenable.js → wrap-thenable.js} +3 -0
- package/package.json +11 -11
- package/test/404s.test.js +69 -0
- package/test/500s.test.js +191 -0
- package/test/child-logger-factory.test.js +3 -3
- package/test/content-parser.test.js +2 -1
- package/test/diagnostics-channel/error-before-handler.test.js +1 -1
- package/test/diagnostics-channel/error-status.test.js +84 -0
- package/test/internals/content-type-parser.test.js +2 -2
- package/test/internals/handle-request.test.js +2 -2
- package/test/internals/initial-config.test.js +1 -1
- package/test/internals/plugin.test.js +2 -2
- package/test/internals/reply.test.js +22 -3
- package/test/internals/req-id-gen-factory.test.js +1 -1
- package/test/internals/schema-controller-perf.test.js +40 -0
- package/test/issue-4959.test.js +34 -9
- package/test/listen.1.test.js +9 -1
- package/test/logger/logging.test.js +38 -1
- package/test/promises.test.js +3 -3
- package/test/reply-web-stream-locked.test.js +37 -0
- package/test/request-error.test.js +116 -0
- package/test/route.6.test.js +20 -1
- package/test/route.7.test.js +49 -0
- package/test/router-options.test.js +169 -0
- package/test/schema-validation.test.js +27 -4
- package/test/server.test.js +25 -4
- package/test/stream.5.test.js +3 -3
- package/test/types/fastify.test-d.ts +94 -21
- package/test/types/hooks.test-d.ts +6 -1
- package/test/types/instance.test-d.ts +64 -36
- package/test/types/logger.test-d.ts +18 -6
- package/test/types/plugin.test-d.ts +24 -6
- package/test/types/register.test-d.ts +108 -33
- package/test/types/reply.test-d.ts +78 -10
- package/test/types/request.test-d.ts +25 -6
- package/test/types/route.test-d.ts +10 -1
- package/test/types/type-provider.test-d.ts +6 -6
- package/test/validation-error-handling.test.js +68 -1
- package/test/web-api.test.js +136 -0
- package/test/wrap-thenable.test.js +1 -1
- package/types/instance.d.ts +3 -3
- package/types/reply.d.ts +2 -2
- package/types/type-provider.d.ts +16 -0
- package/.vscode/settings.json +0 -22
- package/test/check.test.js +0 -219
- package/test/decorator-namespace.test._js_ +0 -30
- /package/lib/{reqIdGenFactory.js → req-id-gen-factory.js} +0 -0
- /package/types/{serverFactory.d.ts → server-factory.d.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorObject as AjvErrorObject } from 'ajv'
|
|
1
|
+
import Ajv, { ErrorObject as AjvErrorObject } from 'ajv'
|
|
2
2
|
import * as http from 'node:http'
|
|
3
3
|
import * as http2 from 'node:http2'
|
|
4
4
|
import * as https from 'node:https'
|
|
@@ -25,20 +25,53 @@ import { Bindings, ChildLoggerOptions } from '../../types/logger'
|
|
|
25
25
|
|
|
26
26
|
// FastifyInstance
|
|
27
27
|
// http server
|
|
28
|
-
expectError<
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
expectError<
|
|
29
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
|
|
30
|
+
Promise<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
|
|
31
|
+
>(fastify())
|
|
32
|
+
expectAssignable<
|
|
33
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
|
|
34
|
+
PromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
|
|
35
|
+
>(fastify())
|
|
36
|
+
expectType<
|
|
37
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
|
|
38
|
+
SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
|
|
39
|
+
>(fastify())
|
|
40
|
+
expectType<
|
|
41
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
|
|
42
|
+
SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
|
|
43
|
+
>(fastify({}))
|
|
44
|
+
expectType<
|
|
45
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
|
|
46
|
+
SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
|
|
47
|
+
>(fastify({ http: {} }))
|
|
33
48
|
// https server
|
|
34
|
-
expectType<
|
|
35
|
-
|
|
49
|
+
expectType<
|
|
50
|
+
FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
|
|
51
|
+
SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
|
|
52
|
+
>(fastify({ https: {} }))
|
|
53
|
+
expectType<
|
|
54
|
+
FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
|
|
55
|
+
SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
|
|
56
|
+
>(fastify({ https: null }))
|
|
36
57
|
// http2 server
|
|
37
|
-
expectType<
|
|
38
|
-
|
|
58
|
+
expectType<
|
|
59
|
+
FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse> &
|
|
60
|
+
SafePromiseLike<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>>
|
|
61
|
+
>(fastify({ http2: true, http2SessionTimeout: 1000 }))
|
|
62
|
+
expectType<
|
|
63
|
+
FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse> &
|
|
64
|
+
SafePromiseLike<FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse>>
|
|
65
|
+
>(fastify({ http2: true, https: {}, http2SessionTimeout: 1000 }))
|
|
39
66
|
expectType<LightMyRequestChain>(fastify({ http2: true, https: {} }).inject())
|
|
40
|
-
expectType<
|
|
41
|
-
|
|
67
|
+
expectType<
|
|
68
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
|
|
69
|
+
SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
|
|
70
|
+
>(fastify({ schemaController: {} }))
|
|
71
|
+
expectType<
|
|
72
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
|
|
73
|
+
SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
|
|
74
|
+
>(
|
|
42
75
|
fastify({
|
|
43
76
|
schemaController: {
|
|
44
77
|
compilersFactory: {}
|
|
@@ -61,13 +94,18 @@ expectAssignable<InjectOptions>({ query: '' })
|
|
|
61
94
|
fastify({ http2: true, https: {} }).inject().then((resp) => {
|
|
62
95
|
expectAssignable<LightMyRequestResponse>(resp)
|
|
63
96
|
})
|
|
64
|
-
const lightMyRequestCallback: LightMyRequestCallback = (
|
|
97
|
+
const lightMyRequestCallback: LightMyRequestCallback = (
|
|
98
|
+
err: Error | undefined,
|
|
99
|
+
response: LightMyRequestResponse | undefined
|
|
100
|
+
) => {
|
|
65
101
|
if (err) throw err
|
|
66
102
|
}
|
|
67
103
|
fastify({ http2: true, https: {} }).inject({}, lightMyRequestCallback)
|
|
68
104
|
|
|
69
105
|
// server options
|
|
70
|
-
expectAssignable<
|
|
106
|
+
expectAssignable<
|
|
107
|
+
FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
|
|
108
|
+
>(fastify({ http2: true }))
|
|
71
109
|
expectAssignable<FastifyInstance>(fastify({ ignoreTrailingSlash: true }))
|
|
72
110
|
expectAssignable<FastifyInstance>(fastify({ ignoreDuplicateSlashes: true }))
|
|
73
111
|
expectAssignable<FastifyInstance>(fastify({ connectionTimeout: 1000 }))
|
|
@@ -77,15 +115,22 @@ expectAssignable<FastifyInstance>(fastify({ pluginTimeout: 1000 }))
|
|
|
77
115
|
expectAssignable<FastifyInstance>(fastify({ bodyLimit: 100 }))
|
|
78
116
|
expectAssignable<FastifyInstance>(fastify({ maxParamLength: 100 }))
|
|
79
117
|
expectAssignable<FastifyInstance>(fastify({ disableRequestLogging: true }))
|
|
118
|
+
expectAssignable<FastifyInstance>(fastify({ disableRequestLogging: (req) => req.url?.includes('/health') ?? false }))
|
|
80
119
|
expectAssignable<FastifyInstance>(fastify({ requestIdLogLabel: 'request-id' }))
|
|
81
120
|
expectAssignable<FastifyInstance>(fastify({ onProtoPoisoning: 'error' }))
|
|
82
121
|
expectAssignable<FastifyInstance>(fastify({ onConstructorPoisoning: 'error' }))
|
|
83
122
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { rounding: 'ceil' } }))
|
|
84
|
-
expectAssignable<FastifyInstance>(
|
|
123
|
+
expectAssignable<FastifyInstance>(
|
|
124
|
+
fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } })
|
|
125
|
+
)
|
|
85
126
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { schema: {} } }))
|
|
86
127
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { otherProp: {} } }))
|
|
87
|
-
expectAssignable<
|
|
88
|
-
|
|
128
|
+
expectAssignable<
|
|
129
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
|
|
130
|
+
>(fastify({ logger: true }))
|
|
131
|
+
expectAssignable<
|
|
132
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
|
|
133
|
+
>(fastify({ logger: true }))
|
|
89
134
|
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({
|
|
90
135
|
logger: {
|
|
91
136
|
level: 'info',
|
|
@@ -126,7 +171,9 @@ const customLogger = {
|
|
|
126
171
|
debug: () => { },
|
|
127
172
|
child: () => customLogger
|
|
128
173
|
}
|
|
129
|
-
expectAssignable<
|
|
174
|
+
expectAssignable<
|
|
175
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
|
|
176
|
+
>(fastify({ logger: customLogger }))
|
|
130
177
|
expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServer() }))
|
|
131
178
|
expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
|
|
132
179
|
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
|
|
@@ -186,12 +233,32 @@ expectAssignable<FastifyInstance>(fastify({
|
|
|
186
233
|
customOptions: {
|
|
187
234
|
removeAdditional: 'all'
|
|
188
235
|
},
|
|
189
|
-
plugins: [() =>
|
|
236
|
+
plugins: [(ajv: Ajv): Ajv => ajv]
|
|
190
237
|
}
|
|
191
238
|
}))
|
|
192
239
|
expectAssignable<FastifyInstance>(fastify({
|
|
193
240
|
ajv: {
|
|
194
|
-
plugins: [[() =>
|
|
241
|
+
plugins: [[(ajv: Ajv): Ajv => ajv, ['keyword1', 'keyword2']]]
|
|
242
|
+
}
|
|
243
|
+
}))
|
|
244
|
+
expectError(fastify({
|
|
245
|
+
ajv: {
|
|
246
|
+
customOptions: {
|
|
247
|
+
removeAdditional: 'all'
|
|
248
|
+
},
|
|
249
|
+
plugins: [
|
|
250
|
+
() => {
|
|
251
|
+
// error, plugins always return the Ajv instance fluently
|
|
252
|
+
}
|
|
253
|
+
]
|
|
254
|
+
}
|
|
255
|
+
}))
|
|
256
|
+
expectAssignable<FastifyInstance>(fastify({
|
|
257
|
+
ajv: {
|
|
258
|
+
onCreate: (ajvInstance) => {
|
|
259
|
+
expectType<Ajv>(ajvInstance)
|
|
260
|
+
return ajvInstance
|
|
261
|
+
}
|
|
195
262
|
}
|
|
196
263
|
}))
|
|
197
264
|
expectAssignable<FastifyInstance>(fastify({ frameworkErrors: () => { } }))
|
|
@@ -221,7 +288,13 @@ expectAssignable<FastifyInstance>(fastify({
|
|
|
221
288
|
}))
|
|
222
289
|
|
|
223
290
|
expectAssignable<FastifyInstance>(fastify({
|
|
224
|
-
childLoggerFactory: function (
|
|
291
|
+
childLoggerFactory: function (
|
|
292
|
+
this: FastifyInstance,
|
|
293
|
+
logger: FastifyBaseLogger,
|
|
294
|
+
bindings: Bindings,
|
|
295
|
+
opts: ChildLoggerOptions,
|
|
296
|
+
req: RawRequestDefaultExpression
|
|
297
|
+
) {
|
|
225
298
|
expectType<FastifyBaseLogger>(logger)
|
|
226
299
|
expectType<Bindings>(bindings)
|
|
227
300
|
expectType<ChildLoggerOptions>(opts)
|
|
@@ -416,7 +416,12 @@ server.route({
|
|
|
416
416
|
expectType<FastifyRequest>(request)
|
|
417
417
|
expectType<FastifyReply>(reply)
|
|
418
418
|
expectType<RequestPayload>(payload)
|
|
419
|
-
expectType
|
|
419
|
+
expectType<
|
|
420
|
+
<TError extends Error = FastifyError>(
|
|
421
|
+
err?: TError | null | undefined,
|
|
422
|
+
res?: RequestPayload | undefined
|
|
423
|
+
) => void
|
|
424
|
+
>(done)
|
|
420
425
|
},
|
|
421
426
|
preValidation: (request, reply, done) => {
|
|
422
427
|
expectType<FastifyRequest>(request)
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { expectAssignable, expectError, expectNotDeprecated, expectType } from 'tsd'
|
|
1
|
+
import { expectAssignable, expectError, expectNotAssignable, expectNotDeprecated, expectType } from 'tsd'
|
|
2
2
|
import fastify, {
|
|
3
3
|
FastifyBaseLogger,
|
|
4
4
|
FastifyBodyParser,
|
|
5
5
|
FastifyError,
|
|
6
6
|
FastifyInstance,
|
|
7
|
+
FastifyRouterOptions,
|
|
7
8
|
RawReplyDefaultExpression,
|
|
8
9
|
RawRequestDefaultExpression,
|
|
9
10
|
RawServerDefault,
|
|
@@ -15,7 +16,7 @@ import { FastifyRequest } from '../../types/request'
|
|
|
15
16
|
import { FastifySchemaControllerOptions, FastifySchemaCompiler, FastifySerializerCompiler } from '../../types/schema'
|
|
16
17
|
import { AddressInfo } from 'node:net'
|
|
17
18
|
import { Bindings, ChildLoggerOptions } from '../../types/logger'
|
|
18
|
-
import { ConstraintStrategy } from 'find-my-way'
|
|
19
|
+
import { Config as FindMyWayConfig, ConstraintStrategy } from 'find-my-way'
|
|
19
20
|
import { FindMyWayVersion } from '../../types/instance'
|
|
20
21
|
|
|
21
22
|
const server = fastify()
|
|
@@ -42,7 +43,7 @@ expectType<string[]>(server.supportedMethods)
|
|
|
42
43
|
|
|
43
44
|
expectAssignable<FastifyInstance>(
|
|
44
45
|
server.setErrorHandler(function (error, request, reply) {
|
|
45
|
-
expectType<
|
|
46
|
+
expectType<unknown>(error)
|
|
46
47
|
expectAssignable<FastifyInstance>(this)
|
|
47
48
|
})
|
|
48
49
|
)
|
|
@@ -92,12 +93,12 @@ interface ReplyPayload {
|
|
|
92
93
|
// typed sync error handler
|
|
93
94
|
server.setErrorHandler<CustomError, ReplyPayload>((error, request, reply) => {
|
|
94
95
|
expectType<CustomError>(error)
|
|
95
|
-
expectType<((payload
|
|
96
|
+
expectType<((...args: [payload: ReplyPayload['Reply']]) => FastifyReply<ReplyPayload, RawServerDefault, RawRequestDefaultExpression<RawServerDefault>, RawReplyDefaultExpression<RawServerDefault>>)>(reply.send)
|
|
96
97
|
})
|
|
97
98
|
// typed async error handler send
|
|
98
99
|
server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
|
|
99
100
|
expectType<CustomError>(error)
|
|
100
|
-
expectType<((payload
|
|
101
|
+
expectType<((...args: [payload: ReplyPayload['Reply']]) => FastifyReply<ReplyPayload, RawServerDefault, RawRequestDefaultExpression<RawServerDefault>, RawReplyDefaultExpression<RawServerDefault>>)>(reply.send)
|
|
101
102
|
})
|
|
102
103
|
// typed async error handler return
|
|
103
104
|
server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
|
|
@@ -127,35 +128,49 @@ server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply)
|
|
|
127
128
|
|
|
128
129
|
function notFoundHandler (request: FastifyRequest, reply: FastifyReply) {}
|
|
129
130
|
async function notFoundAsyncHandler (request: FastifyRequest, reply: FastifyReply) {}
|
|
130
|
-
function notFoundpreHandlerHandler (
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
function notFoundpreHandlerHandler (
|
|
132
|
+
request: FastifyRequest,
|
|
133
|
+
reply: FastifyReply,
|
|
134
|
+
done: HookHandlerDoneFunction
|
|
135
|
+
) { done() }
|
|
136
|
+
async function notFoundpreHandlerAsyncHandler (
|
|
137
|
+
request: FastifyRequest,
|
|
138
|
+
reply: FastifyReply
|
|
139
|
+
) {}
|
|
140
|
+
function notFoundpreValidationHandler (
|
|
141
|
+
request: FastifyRequest,
|
|
142
|
+
reply: FastifyReply,
|
|
143
|
+
done: HookHandlerDoneFunction
|
|
144
|
+
) { done() }
|
|
145
|
+
async function notFoundpreValidationAsyncHandler (
|
|
146
|
+
request: FastifyRequest,
|
|
147
|
+
reply: FastifyReply
|
|
148
|
+
) {}
|
|
134
149
|
|
|
135
150
|
server.setNotFoundHandler(notFoundHandler)
|
|
136
151
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler }, notFoundHandler)
|
|
137
152
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerAsyncHandler }, notFoundHandler)
|
|
138
153
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationHandler }, notFoundHandler)
|
|
139
154
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationAsyncHandler }, notFoundHandler)
|
|
140
|
-
server.setNotFoundHandler(
|
|
155
|
+
server.setNotFoundHandler(
|
|
156
|
+
{ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler },
|
|
157
|
+
notFoundHandler
|
|
158
|
+
)
|
|
141
159
|
|
|
142
160
|
server.setNotFoundHandler(notFoundAsyncHandler)
|
|
143
161
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler }, notFoundAsyncHandler)
|
|
144
162
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerAsyncHandler }, notFoundAsyncHandler)
|
|
145
163
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationHandler }, notFoundAsyncHandler)
|
|
146
164
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationAsyncHandler }, notFoundAsyncHandler)
|
|
147
|
-
server.setNotFoundHandler(
|
|
165
|
+
server.setNotFoundHandler(
|
|
166
|
+
{ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler },
|
|
167
|
+
notFoundAsyncHandler
|
|
168
|
+
)
|
|
148
169
|
|
|
149
170
|
server.setNotFoundHandler(function (_, reply) {
|
|
150
171
|
return reply.send('')
|
|
151
172
|
})
|
|
152
173
|
|
|
153
|
-
function invalidErrorHandler (error: number) {
|
|
154
|
-
if (error) throw error
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
expectError(server.setErrorHandler(invalidErrorHandler))
|
|
158
|
-
|
|
159
174
|
server.setSchemaController({
|
|
160
175
|
bucket: (parentSchemas: unknown) => {
|
|
161
176
|
return {
|
|
@@ -253,7 +268,7 @@ expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as R
|
|
|
253
268
|
expectType<FastifyInstance>(fastify().get<RouteGenericInterface, { contextKey: string }>('/', {
|
|
254
269
|
handler: () => {},
|
|
255
270
|
errorHandler: (error, request, reply) => {
|
|
256
|
-
expectAssignable<
|
|
271
|
+
expectAssignable<unknown>(error)
|
|
257
272
|
expectAssignable<FastifyRequest>(request)
|
|
258
273
|
expectAssignable<{ contextKey: string }>(request.routeOptions.config)
|
|
259
274
|
expectAssignable<FastifyReply>(reply)
|
|
@@ -286,7 +301,13 @@ expectAssignable<FastifyInstance>(
|
|
|
286
301
|
})
|
|
287
302
|
)
|
|
288
303
|
|
|
289
|
-
function childLoggerFactory (
|
|
304
|
+
function childLoggerFactory (
|
|
305
|
+
this: FastifyInstance,
|
|
306
|
+
logger: FastifyBaseLogger,
|
|
307
|
+
bindings: Bindings,
|
|
308
|
+
opts: ChildLoggerOptions,
|
|
309
|
+
req: RawRequestDefaultExpression
|
|
310
|
+
) {
|
|
290
311
|
return logger.child(bindings, opts)
|
|
291
312
|
}
|
|
292
313
|
server.setChildLoggerFactory(childLoggerFactory)
|
|
@@ -303,7 +324,7 @@ type InitialConfig = Readonly<{
|
|
|
303
324
|
https?: boolean | Readonly<{ allowHTTP1: boolean }>,
|
|
304
325
|
ignoreTrailingSlash?: boolean,
|
|
305
326
|
ignoreDuplicateSlashes?: boolean,
|
|
306
|
-
disableRequestLogging?: boolean,
|
|
327
|
+
disableRequestLogging?: boolean | ((req: FastifyRequest) => boolean),
|
|
307
328
|
maxParamLength?: number,
|
|
308
329
|
onProtoPoisoning?: 'error' | 'remove' | 'ignore',
|
|
309
330
|
onConstructorPoisoning?: 'error' | 'remove' | 'ignore',
|
|
@@ -312,25 +333,32 @@ type InitialConfig = Readonly<{
|
|
|
312
333
|
requestIdLogLabel?: string,
|
|
313
334
|
http2SessionTimeout?: number,
|
|
314
335
|
useSemicolonDelimiter?: boolean,
|
|
315
|
-
routerOptions?:
|
|
316
|
-
allowUnsafeRegex?: boolean,
|
|
317
|
-
buildPrettyMeta?: (route: { [k: string]: unknown, store: { [k: string]: unknown } }) => object,
|
|
318
|
-
caseSensitive?: boolean,
|
|
319
|
-
constraints?: {
|
|
320
|
-
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServerDefault>, unknown>
|
|
321
|
-
}
|
|
322
|
-
defaultRoute?: (req: FastifyRequest, res: FastifyReply) => void,
|
|
323
|
-
ignoreDuplicateSlashes?: boolean,
|
|
324
|
-
ignoreTrailingSlash?: boolean,
|
|
325
|
-
maxParamLength?: number,
|
|
326
|
-
onBadUrl?: (path: string, req: FastifyRequest, res: FastifyReply) => void,
|
|
327
|
-
querystringParser?: (str: string) => { [key: string]: unknown },
|
|
328
|
-
useSemicolonDelimiter?: boolean,
|
|
329
|
-
}
|
|
336
|
+
routerOptions?: FastifyRouterOptions<RawServerDefault>
|
|
330
337
|
}>
|
|
331
338
|
|
|
332
339
|
expectType<InitialConfig>(fastify().initialConfig)
|
|
333
340
|
|
|
341
|
+
const routerOptionsForFindMyWay = {} as FastifyRouterOptions<RawServerDefault>
|
|
342
|
+
expectAssignable<FindMyWayConfig<FindMyWayVersion<RawServerDefault>>>(routerOptionsForFindMyWay)
|
|
343
|
+
|
|
344
|
+
fastify({
|
|
345
|
+
routerOptions: {
|
|
346
|
+
defaultRoute: (req, res) => {
|
|
347
|
+
expectType<RawRequestDefaultExpression<RawServerDefault>>(req)
|
|
348
|
+
expectType<RawReplyDefaultExpression<RawServerDefault>>(res)
|
|
349
|
+
expectNotAssignable<FastifyReply>(res)
|
|
350
|
+
res.end('foo')
|
|
351
|
+
},
|
|
352
|
+
onBadUrl: (path, req, res) => {
|
|
353
|
+
expectType<string>(path)
|
|
354
|
+
expectType<RawRequestDefaultExpression<RawServerDefault>>(req)
|
|
355
|
+
expectType<RawReplyDefaultExpression<RawServerDefault>>(res)
|
|
356
|
+
expectNotAssignable<FastifyReply>(res)
|
|
357
|
+
res.end('foo')
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
})
|
|
361
|
+
|
|
334
362
|
expectType<FastifyBodyParser<string>>(server.defaultTextParser)
|
|
335
363
|
|
|
336
364
|
expectType<FastifyBodyParser<string>>(server.getDefaultJsonParser('ignore', 'error'))
|
|
@@ -542,7 +570,7 @@ expectError(server.decorateReply('typedTestReplyMethod', async function (x) {
|
|
|
542
570
|
const foo = server.getDecorator<string>('foo')
|
|
543
571
|
expectType<string>(foo)
|
|
544
572
|
|
|
545
|
-
const versionConstraintStrategy = {
|
|
573
|
+
const versionConstraintStrategy: ConstraintStrategy<FindMyWayVersion<RawServerDefault>> = {
|
|
546
574
|
name: 'version',
|
|
547
575
|
storage: () => ({
|
|
548
576
|
get: () => () => {},
|
|
@@ -17,12 +17,24 @@ expectType<FastifyLoggerInstance>(fastify().log)
|
|
|
17
17
|
class Foo {}
|
|
18
18
|
|
|
19
19
|
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(logLevel => {
|
|
20
|
-
expectType<FastifyLogFn>(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
expectType<void>(
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
expectType<FastifyLogFn>(
|
|
21
|
+
fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]
|
|
22
|
+
)
|
|
23
|
+
expectType<void>(
|
|
24
|
+
fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]('')
|
|
25
|
+
)
|
|
26
|
+
expectType<void>(
|
|
27
|
+
fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({})
|
|
28
|
+
)
|
|
29
|
+
expectType<void>(
|
|
30
|
+
fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({ foo: 'bar' })
|
|
31
|
+
)
|
|
32
|
+
expectType<void>(
|
|
33
|
+
fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Error())
|
|
34
|
+
)
|
|
35
|
+
expectType<void>(
|
|
36
|
+
fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Foo())
|
|
37
|
+
)
|
|
26
38
|
})
|
|
27
39
|
|
|
28
40
|
interface CustomLogger extends FastifyBaseLogger {
|
|
@@ -21,8 +21,15 @@ const testPluginOptsAsync: FastifyPluginAsync<TestOptions> = async function (ins
|
|
|
21
21
|
expectType<TestOptions>(opts)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
const testPluginOptsWithType = (
|
|
25
|
-
|
|
24
|
+
const testPluginOptsWithType = (
|
|
25
|
+
instance: FastifyInstance,
|
|
26
|
+
opts: FastifyPluginOptions,
|
|
27
|
+
done: (error?: FastifyError) => void
|
|
28
|
+
) => { }
|
|
29
|
+
const testPluginOptsWithTypeAsync = async (
|
|
30
|
+
instance: FastifyInstance,
|
|
31
|
+
opts: FastifyPluginOptions
|
|
32
|
+
) => { }
|
|
26
33
|
|
|
27
34
|
expectError(fastify().register(testPluginOpts, {})) // error because missing required options from generic declaration
|
|
28
35
|
expectError(fastify().register(testPluginOptsAsync, {})) // error because missing required options from generic declaration
|
|
@@ -43,16 +50,27 @@ expectAssignable<FastifyInstance>(fastify().register(testPluginCallback, {}))
|
|
|
43
50
|
const testPluginAsync: FastifyPluginAsync = async function (instance, opts) { }
|
|
44
51
|
expectAssignable<FastifyInstance>(fastify().register(testPluginAsync, {}))
|
|
45
52
|
|
|
46
|
-
expectAssignable<FastifyInstance>(
|
|
53
|
+
expectAssignable<FastifyInstance>(
|
|
54
|
+
fastify().register(function (instance, opts): Promise<void> { return Promise.resolve() })
|
|
55
|
+
)
|
|
47
56
|
expectAssignable<FastifyInstance>(fastify().register(async function (instance, opts) { }, () => { }))
|
|
48
57
|
expectAssignable<FastifyInstance>(fastify().register(async function (instance, opts) { }, { logLevel: 'info', prefix: 'foobar' }))
|
|
49
58
|
|
|
50
59
|
expectError(fastify().register(function (instance, opts, done) { }, { ...testOptions, logLevel: '' })) // must use a valid logLevel
|
|
51
60
|
|
|
52
61
|
const httpsServer = fastify({ https: {} })
|
|
53
|
-
expectError<
|
|
54
|
-
|
|
55
|
-
|
|
62
|
+
expectError<
|
|
63
|
+
FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
|
|
64
|
+
Promise<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
|
|
65
|
+
>(httpsServer)
|
|
66
|
+
expectAssignable<
|
|
67
|
+
FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
|
|
68
|
+
PromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
|
|
69
|
+
>(httpsServer)
|
|
70
|
+
expectType<
|
|
71
|
+
FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
|
|
72
|
+
SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
|
|
73
|
+
>(httpsServer)
|
|
56
74
|
|
|
57
75
|
// Chainable
|
|
58
76
|
httpsServer
|
|
@@ -9,7 +9,11 @@ const testPluginAsync: FastifyPluginAsync = async function (instance, opts) { }
|
|
|
9
9
|
const testPluginOpts: FastifyPluginCallback = function (instance, opts, done) { }
|
|
10
10
|
const testPluginOptsAsync: FastifyPluginAsync = async function (instance, opts) { }
|
|
11
11
|
|
|
12
|
-
const testPluginOptsWithType = (
|
|
12
|
+
const testPluginOptsWithType = (
|
|
13
|
+
instance: FastifyInstance,
|
|
14
|
+
opts: FastifyPluginOptions,
|
|
15
|
+
done: (error?: FastifyError) => void
|
|
16
|
+
) => { }
|
|
13
17
|
const testPluginOptsWithTypeAsync = async (instance: FastifyInstance, opts: FastifyPluginOptions) => { }
|
|
14
18
|
|
|
15
19
|
interface TestOptions extends FastifyPluginOptions {
|
|
@@ -46,8 +50,15 @@ const serverWithHttp2 = fastify({ http2: true })
|
|
|
46
50
|
type ServerWithHttp2 = FastifyInstance<Http2Server, Http2ServerRequest, Http2ServerResponse>
|
|
47
51
|
const testPluginWithHttp2: FastifyPluginCallback<TestOptions, Http2Server> = function (instance, opts, done) { }
|
|
48
52
|
const testPluginWithHttp2Async: FastifyPluginAsync<TestOptions, Http2Server> = async function (instance, opts) { }
|
|
49
|
-
const testPluginWithHttp2WithType = (
|
|
50
|
-
|
|
53
|
+
const testPluginWithHttp2WithType = (
|
|
54
|
+
instance: ServerWithHttp2,
|
|
55
|
+
opts: FastifyPluginOptions,
|
|
56
|
+
done: (error?: FastifyError) => void
|
|
57
|
+
) => { }
|
|
58
|
+
const testPluginWithHttp2WithTypeAsync = async (
|
|
59
|
+
instance: ServerWithHttp2,
|
|
60
|
+
opts: FastifyPluginOptions
|
|
61
|
+
) => { }
|
|
51
62
|
const testOptions: TestOptions = {
|
|
52
63
|
option1: 'a',
|
|
53
64
|
option2: false
|
|
@@ -82,11 +93,32 @@ expectAssignable<ServerWithHttp2>(serverWithHttp2.register(async (instance: Serv
|
|
|
82
93
|
// With Type Provider
|
|
83
94
|
type TestTypeProvider = { schema: 'test', validator: 'test', serializer: 'test' }
|
|
84
95
|
const serverWithTypeProvider = fastify().withTypeProvider<TestTypeProvider>()
|
|
85
|
-
type ServerWithTypeProvider = FastifyInstance<
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
type ServerWithTypeProvider = FastifyInstance<
|
|
97
|
+
Server,
|
|
98
|
+
IncomingMessage,
|
|
99
|
+
ServerResponse,
|
|
100
|
+
FastifyLoggerInstance,
|
|
101
|
+
TestTypeProvider
|
|
102
|
+
>
|
|
103
|
+
const testPluginWithTypeProvider: FastifyPluginCallback<
|
|
104
|
+
TestOptions,
|
|
105
|
+
RawServerDefault,
|
|
106
|
+
TestTypeProvider
|
|
107
|
+
> = function (instance, opts, done) { }
|
|
108
|
+
const testPluginWithTypeProviderAsync: FastifyPluginAsync<
|
|
109
|
+
TestOptions,
|
|
110
|
+
RawServerDefault,
|
|
111
|
+
TestTypeProvider
|
|
112
|
+
> = async function (instance, opts) { }
|
|
113
|
+
const testPluginWithTypeProviderWithType = (
|
|
114
|
+
instance: ServerWithTypeProvider,
|
|
115
|
+
opts: FastifyPluginOptions,
|
|
116
|
+
done: (error?: FastifyError) => void
|
|
117
|
+
) => { }
|
|
118
|
+
const testPluginWithTypeProviderWithTypeAsync = async (
|
|
119
|
+
instance: ServerWithTypeProvider,
|
|
120
|
+
opts: FastifyPluginOptions
|
|
121
|
+
) => { }
|
|
90
122
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginCallback))
|
|
91
123
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginAsync))
|
|
92
124
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginOpts))
|
|
@@ -129,34 +161,77 @@ const customLogger = {
|
|
|
129
161
|
const serverWithTypeProviderAndLogger = fastify({
|
|
130
162
|
loggerInstance: customLogger
|
|
131
163
|
}).withTypeProvider<TestTypeProvider>()
|
|
132
|
-
type ServerWithTypeProviderAndLogger = FastifyInstance<
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
164
|
+
type ServerWithTypeProviderAndLogger = FastifyInstance<
|
|
165
|
+
Server,
|
|
166
|
+
IncomingMessage,
|
|
167
|
+
ServerResponse,
|
|
168
|
+
typeof customLogger,
|
|
169
|
+
TestTypeProvider
|
|
170
|
+
>
|
|
171
|
+
const testPluginWithTypeProviderAndLogger: FastifyPluginCallback<
|
|
172
|
+
TestOptions,
|
|
173
|
+
RawServerDefault,
|
|
174
|
+
TestTypeProvider,
|
|
175
|
+
typeof customLogger
|
|
176
|
+
> = function (instance, opts, done) { }
|
|
177
|
+
const testPluginWithTypeProviderAndLoggerAsync: FastifyPluginAsync<
|
|
178
|
+
TestOptions,
|
|
179
|
+
RawServerDefault,
|
|
180
|
+
TestTypeProvider,
|
|
181
|
+
typeof customLogger
|
|
182
|
+
> = async function (instance, opts) { }
|
|
183
|
+
const testPluginWithTypeProviderAndLoggerWithType = (
|
|
184
|
+
instance: ServerWithTypeProviderAndLogger,
|
|
185
|
+
opts: FastifyPluginOptions,
|
|
186
|
+
done: (error?: FastifyError) => void
|
|
187
|
+
) => { }
|
|
188
|
+
const testPluginWithTypeProviderAndLoggerWithTypeAsync = async (
|
|
189
|
+
instance: ServerWithTypeProviderAndLogger,
|
|
190
|
+
opts: FastifyPluginOptions
|
|
191
|
+
) => { }
|
|
137
192
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginCallback))
|
|
138
193
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginAsync))
|
|
139
194
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOpts))
|
|
140
195
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsAsync))
|
|
141
196
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsWithType))
|
|
142
197
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsWithTypeAsync))
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
161
|
-
|
|
162
|
-
|
|
198
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
199
|
+
// @ts-expect-error
|
|
200
|
+
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger)
|
|
201
|
+
)
|
|
202
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
203
|
+
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger, testOptions)
|
|
204
|
+
)
|
|
205
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
206
|
+
// @ts-expect-error
|
|
207
|
+
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync)
|
|
208
|
+
)
|
|
209
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
210
|
+
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync, testOptions)
|
|
211
|
+
)
|
|
212
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
213
|
+
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithType)
|
|
214
|
+
)
|
|
215
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
216
|
+
serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithTypeAsync)
|
|
217
|
+
)
|
|
218
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
219
|
+
serverWithTypeProviderAndLogger.register((instance) => {
|
|
220
|
+
expectAssignable<FastifyInstance>(instance)
|
|
221
|
+
})
|
|
222
|
+
)
|
|
223
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
224
|
+
serverWithTypeProviderAndLogger.register((instance: ServerWithTypeProviderAndLogger) => {
|
|
225
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(instance)
|
|
226
|
+
})
|
|
227
|
+
)
|
|
228
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
229
|
+
serverWithTypeProviderAndLogger.register(async (instance) => {
|
|
230
|
+
expectAssignable<FastifyInstance>(instance)
|
|
231
|
+
})
|
|
232
|
+
)
|
|
233
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(
|
|
234
|
+
serverWithTypeProviderAndLogger.register(async (instance: ServerWithTypeProviderAndLogger) => {
|
|
235
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(instance)
|
|
236
|
+
})
|
|
237
|
+
)
|