fastify 5.6.0 → 5.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +22 -0
- package/SECURITY.md +12 -0
- package/SPONSORS.md +1 -0
- package/build/build-validation.js +1 -1
- package/build/sync-version.js +1 -0
- package/docs/Guides/Ecosystem.md +5 -0
- package/docs/Guides/Fluent-Schema.md +2 -2
- package/docs/Reference/Decorators.md +36 -169
- package/docs/Reference/Encapsulation.md +5 -1
- package/docs/Reference/Plugins.md +11 -2
- package/docs/Reference/Reply.md +4 -1
- package/docs/Reference/Server.md +39 -1
- package/docs/Reference/Type-Providers.md +2 -2
- package/docs/Reference/TypeScript.md +136 -0
- package/eslint.config.js +18 -2
- package/fastify.d.ts +1 -1
- package/fastify.js +183 -168
- package/lib/{contentTypeParser.js → content-type-parser.js} +2 -1
- package/lib/error-handler.js +2 -2
- package/lib/{fourOhFour.js → four-oh-four.js} +4 -2
- package/lib/{handleRequest.js → handle-request.js} +1 -1
- 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 +5 -3
- package/lib/request.js +5 -0
- package/lib/route.js +20 -9
- package/lib/server.js +94 -8
- package/lib/symbols.js +1 -0
- package/lib/validation.js +9 -1
- package/lib/warnings.js +1 -1
- package/package.json +8 -8
- 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/decorator-namespace.test._js_ +1 -1
- package/test/diagnostics-channel/error-before-handler.test.js +1 -1
- package/test/http2/closing.test.js +88 -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/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/schema-validation.test.js +27 -4
- package/test/server.test.js +22 -4
- package/test/set-error-handler.test.js +1 -1
- package/test/skip-reply-send.test.js +2 -2
- package/test/stream.5.test.js +3 -3
- package/test/types/fastify.test-d.ts +70 -18
- package/test/types/hooks.test-d.ts +6 -1
- package/test/types/instance.test-d.ts +35 -15
- 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 +23 -6
- package/test/types/request.test-d.ts +25 -6
- package/test/types/route.test-d.ts +10 -1
- package/test/types/schema.test-d.ts +21 -0
- package/test/validation-error-handling.test.js +68 -1
- package/test/wrap-thenable.test.js +1 -1
- package/types/instance.d.ts +2 -2
- package/types/schema.d.ts +1 -1
- package/test/check.test.js +0 -219
- /package/lib/{configValidator.js → config-validator.js} +0 -0
- /package/lib/{reqIdGenFactory.js → req-id-gen-factory.js} +0 -0
- /package/lib/{wrapThenable.js → wrap-thenable.js} +0 -0
- /package/types/{serverFactory.d.ts → server-factory.d.ts} +0 -0
|
@@ -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<https.Server, http.IncomingMessage, http.ServerResponse> &
|
|
69
|
+
SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
|
|
70
|
+
>(fastify({ schemaController: {} }))
|
|
71
|
+
expectType<
|
|
72
|
+
FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
|
|
73
|
+
SafePromiseLike<FastifyInstance<https.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 }))
|
|
@@ -81,11 +119,17 @@ expectAssignable<FastifyInstance>(fastify({ requestIdLogLabel: 'request-id' }))
|
|
|
81
119
|
expectAssignable<FastifyInstance>(fastify({ onProtoPoisoning: 'error' }))
|
|
82
120
|
expectAssignable<FastifyInstance>(fastify({ onConstructorPoisoning: 'error' }))
|
|
83
121
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { rounding: 'ceil' } }))
|
|
84
|
-
expectAssignable<FastifyInstance>(
|
|
122
|
+
expectAssignable<FastifyInstance>(
|
|
123
|
+
fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } })
|
|
124
|
+
)
|
|
85
125
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { schema: {} } }))
|
|
86
126
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { otherProp: {} } }))
|
|
87
|
-
expectAssignable<
|
|
88
|
-
|
|
127
|
+
expectAssignable<
|
|
128
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
|
|
129
|
+
>(fastify({ logger: true }))
|
|
130
|
+
expectAssignable<
|
|
131
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
|
|
132
|
+
>(fastify({ logger: true }))
|
|
89
133
|
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({
|
|
90
134
|
logger: {
|
|
91
135
|
level: 'info',
|
|
@@ -126,7 +170,9 @@ const customLogger = {
|
|
|
126
170
|
debug: () => { },
|
|
127
171
|
child: () => customLogger
|
|
128
172
|
}
|
|
129
|
-
expectAssignable<
|
|
173
|
+
expectAssignable<
|
|
174
|
+
FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
|
|
175
|
+
>(fastify({ logger: customLogger }))
|
|
130
176
|
expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServer() }))
|
|
131
177
|
expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
|
|
132
178
|
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
|
|
@@ -221,7 +267,13 @@ expectAssignable<FastifyInstance>(fastify({
|
|
|
221
267
|
}))
|
|
222
268
|
|
|
223
269
|
expectAssignable<FastifyInstance>(fastify({
|
|
224
|
-
childLoggerFactory: function (
|
|
270
|
+
childLoggerFactory: function (
|
|
271
|
+
this: FastifyInstance,
|
|
272
|
+
logger: FastifyBaseLogger,
|
|
273
|
+
bindings: Bindings,
|
|
274
|
+
opts: ChildLoggerOptions,
|
|
275
|
+
req: RawRequestDefaultExpression
|
|
276
|
+
) {
|
|
225
277
|
expectType<FastifyBaseLogger>(logger)
|
|
226
278
|
expectType<Bindings>(bindings)
|
|
227
279
|
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)
|
|
@@ -42,7 +42,7 @@ expectType<string[]>(server.supportedMethods)
|
|
|
42
42
|
|
|
43
43
|
expectAssignable<FastifyInstance>(
|
|
44
44
|
server.setErrorHandler(function (error, request, reply) {
|
|
45
|
-
expectType<
|
|
45
|
+
expectType<unknown>(error)
|
|
46
46
|
expectAssignable<FastifyInstance>(this)
|
|
47
47
|
})
|
|
48
48
|
)
|
|
@@ -127,35 +127,49 @@ server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply)
|
|
|
127
127
|
|
|
128
128
|
function notFoundHandler (request: FastifyRequest, reply: FastifyReply) {}
|
|
129
129
|
async function notFoundAsyncHandler (request: FastifyRequest, reply: FastifyReply) {}
|
|
130
|
-
function notFoundpreHandlerHandler (
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
130
|
+
function notFoundpreHandlerHandler (
|
|
131
|
+
request: FastifyRequest,
|
|
132
|
+
reply: FastifyReply,
|
|
133
|
+
done: HookHandlerDoneFunction
|
|
134
|
+
) { done() }
|
|
135
|
+
async function notFoundpreHandlerAsyncHandler (
|
|
136
|
+
request: FastifyRequest,
|
|
137
|
+
reply: FastifyReply
|
|
138
|
+
) {}
|
|
139
|
+
function notFoundpreValidationHandler (
|
|
140
|
+
request: FastifyRequest,
|
|
141
|
+
reply: FastifyReply,
|
|
142
|
+
done: HookHandlerDoneFunction
|
|
143
|
+
) { done() }
|
|
144
|
+
async function notFoundpreValidationAsyncHandler (
|
|
145
|
+
request: FastifyRequest,
|
|
146
|
+
reply: FastifyReply
|
|
147
|
+
) {}
|
|
134
148
|
|
|
135
149
|
server.setNotFoundHandler(notFoundHandler)
|
|
136
150
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler }, notFoundHandler)
|
|
137
151
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerAsyncHandler }, notFoundHandler)
|
|
138
152
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationHandler }, notFoundHandler)
|
|
139
153
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationAsyncHandler }, notFoundHandler)
|
|
140
|
-
server.setNotFoundHandler(
|
|
154
|
+
server.setNotFoundHandler(
|
|
155
|
+
{ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler },
|
|
156
|
+
notFoundHandler
|
|
157
|
+
)
|
|
141
158
|
|
|
142
159
|
server.setNotFoundHandler(notFoundAsyncHandler)
|
|
143
160
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler }, notFoundAsyncHandler)
|
|
144
161
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerAsyncHandler }, notFoundAsyncHandler)
|
|
145
162
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationHandler }, notFoundAsyncHandler)
|
|
146
163
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationAsyncHandler }, notFoundAsyncHandler)
|
|
147
|
-
server.setNotFoundHandler(
|
|
164
|
+
server.setNotFoundHandler(
|
|
165
|
+
{ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler },
|
|
166
|
+
notFoundAsyncHandler
|
|
167
|
+
)
|
|
148
168
|
|
|
149
169
|
server.setNotFoundHandler(function (_, reply) {
|
|
150
170
|
return reply.send('')
|
|
151
171
|
})
|
|
152
172
|
|
|
153
|
-
function invalidErrorHandler (error: number) {
|
|
154
|
-
if (error) throw error
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
expectError(server.setErrorHandler(invalidErrorHandler))
|
|
158
|
-
|
|
159
173
|
server.setSchemaController({
|
|
160
174
|
bucket: (parentSchemas: unknown) => {
|
|
161
175
|
return {
|
|
@@ -253,7 +267,7 @@ expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as R
|
|
|
253
267
|
expectType<FastifyInstance>(fastify().get<RouteGenericInterface, { contextKey: string }>('/', {
|
|
254
268
|
handler: () => {},
|
|
255
269
|
errorHandler: (error, request, reply) => {
|
|
256
|
-
expectAssignable<
|
|
270
|
+
expectAssignable<unknown>(error)
|
|
257
271
|
expectAssignable<FastifyRequest>(request)
|
|
258
272
|
expectAssignable<{ contextKey: string }>(request.routeOptions.config)
|
|
259
273
|
expectAssignable<FastifyReply>(reply)
|
|
@@ -286,7 +300,13 @@ expectAssignable<FastifyInstance>(
|
|
|
286
300
|
})
|
|
287
301
|
)
|
|
288
302
|
|
|
289
|
-
function childLoggerFactory (
|
|
303
|
+
function childLoggerFactory (
|
|
304
|
+
this: FastifyInstance,
|
|
305
|
+
logger: FastifyBaseLogger,
|
|
306
|
+
bindings: Bindings,
|
|
307
|
+
opts: ChildLoggerOptions,
|
|
308
|
+
req: RawRequestDefaultExpression
|
|
309
|
+
) {
|
|
290
310
|
return logger.child(bindings, opts)
|
|
291
311
|
}
|
|
292
312
|
server.setChildLoggerFactory(childLoggerFactory)
|
|
@@ -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
|
+
)
|
|
@@ -20,7 +20,9 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
|
20
20
|
expectType<number>(reply.elapsedTime)
|
|
21
21
|
expectType<number>(reply.statusCode)
|
|
22
22
|
expectType<boolean>(reply.sent)
|
|
23
|
-
expectType<
|
|
23
|
+
expectType<
|
|
24
|
+
(hints: Record<string, string | string[]>, callback?: (() => void) | undefined) => void
|
|
25
|
+
>(reply.writeEarlyHints)
|
|
24
26
|
expectType<((payload?: unknown) => FastifyReply)>(reply.send)
|
|
25
27
|
expectAssignable<(key: string, value: any) => FastifyReply>(reply.header)
|
|
26
28
|
expectAssignable<(values: { [key: string]: any }) => FastifyReply>(reply.headers)
|
|
@@ -35,14 +37,29 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
|
35
37
|
expectType<(fn: (payload: any) => string) => FastifyReply>(reply.serializer)
|
|
36
38
|
expectType<(payload: any) => string | ArrayBuffer | Buffer>(reply.serialize)
|
|
37
39
|
expectType<(fulfilled: () => void, rejected: (err: Error) => void) => void>(reply.then)
|
|
38
|
-
expectType<
|
|
40
|
+
expectType<
|
|
41
|
+
(
|
|
42
|
+
key: string,
|
|
43
|
+
fn: ((reply: FastifyReply, payload: string | Buffer | null) => Promise<string>) |
|
|
44
|
+
((reply: FastifyReply, payload: string | Buffer | null,
|
|
45
|
+
done: (err: Error | null, value?: string) => void) => void)
|
|
46
|
+
) => FastifyReply
|
|
47
|
+
>(reply.trailer)
|
|
39
48
|
expectType<(key: string) => boolean>(reply.hasTrailer)
|
|
40
49
|
expectType<(key: string) => FastifyReply>(reply.removeTrailer)
|
|
41
50
|
expectType<FastifyInstance>(reply.server)
|
|
42
|
-
expectAssignable<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
expectAssignable<
|
|
51
|
+
expectAssignable<
|
|
52
|
+
((httpStatus: string) => DefaultSerializationFunction | undefined)
|
|
53
|
+
>(reply.getSerializationFunction)
|
|
54
|
+
expectAssignable<
|
|
55
|
+
((schema: { [key: string]: unknown }) => DefaultSerializationFunction | undefined)
|
|
56
|
+
>(reply.getSerializationFunction)
|
|
57
|
+
expectAssignable<
|
|
58
|
+
((schema: { [key: string]: unknown }, httpStatus?: string) => DefaultSerializationFunction)
|
|
59
|
+
>(reply.compileSerializationSchema)
|
|
60
|
+
expectAssignable<
|
|
61
|
+
((input: { [key: string]: unknown }, schema: { [key: string]: unknown }, httpStatus?: string) => unknown)
|
|
62
|
+
>(reply.serializeInput)
|
|
46
63
|
expectAssignable<((input: { [key: string]: unknown }, httpStatus: string) => unknown)>(reply.serializeInput)
|
|
47
64
|
expectType<ContextConfigDefault & FastifyRouteConfig & FastifyContextConfig>(reply.routeOptions.config)
|
|
48
65
|
expectType<string>(reply.getDecorator<string>('foo'))
|
|
@@ -91,7 +91,9 @@ const getHandler: RouteHandler = function (request, _reply) {
|
|
|
91
91
|
expectType<FastifyInstance>(request.server)
|
|
92
92
|
expectAssignable<(httpPart: HTTPRequestPart) => ExpectedGetValidationFunction>(request.getValidationFunction)
|
|
93
93
|
expectAssignable<(schema: { [key: string]: unknown }) => ExpectedGetValidationFunction>(request.getValidationFunction)
|
|
94
|
-
expectAssignable<
|
|
94
|
+
expectAssignable<
|
|
95
|
+
(input: { [key: string]: unknown }, schema: { [key: string]: unknown }, httpPart?: HTTPRequestPart) => boolean
|
|
96
|
+
>(request.validateInput)
|
|
95
97
|
expectAssignable<(input: { [key: string]: unknown }, httpPart?: HTTPRequestPart) => boolean>(request.validateInput)
|
|
96
98
|
expectType<string>(request.getDecorator<string>('foo'))
|
|
97
99
|
expectType<void>(request.setDecorator('foo', 'hello'))
|
|
@@ -99,14 +101,25 @@ const getHandler: RouteHandler = function (request, _reply) {
|
|
|
99
101
|
expectError(request.setDecorator<string>('foo', true))
|
|
100
102
|
}
|
|
101
103
|
|
|
102
|
-
const getHandlerWithCustomLogger: RouteHandlerMethod<
|
|
104
|
+
const getHandlerWithCustomLogger: RouteHandlerMethod<
|
|
105
|
+
RawServerDefault,
|
|
106
|
+
RawRequestDefaultExpression,
|
|
107
|
+
RawReplyDefaultExpression,
|
|
108
|
+
RouteGenericInterface,
|
|
109
|
+
ContextConfigDefault,
|
|
110
|
+
FastifySchema,
|
|
111
|
+
FastifyTypeProviderDefault,
|
|
112
|
+
CustomLoggerInterface
|
|
113
|
+
> = function (request, _reply) {
|
|
103
114
|
expectType<CustomLoggerInterface>(request.log)
|
|
104
115
|
}
|
|
105
116
|
|
|
106
117
|
const postHandler: Handler = function (request) {
|
|
107
118
|
expectType<RequestBody>(request.body)
|
|
108
119
|
expectType<RequestParams>(request.params)
|
|
109
|
-
expectType<RequestHeaders & RawRequestDefaultExpression['headers']>(
|
|
120
|
+
expectType<RequestHeaders & RawRequestDefaultExpression['headers']>(
|
|
121
|
+
request.headers
|
|
122
|
+
)
|
|
110
123
|
expectType<RequestQuerystring>(request.query)
|
|
111
124
|
expectType<string>(request.body.content)
|
|
112
125
|
expectType<string>(request.query.from)
|
|
@@ -154,15 +167,21 @@ const customLogger: CustomLoggerInterface = {
|
|
|
154
167
|
const serverWithCustomLogger = fastify({ loggerInstance: customLogger })
|
|
155
168
|
expectError<
|
|
156
169
|
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
|
|
157
|
-
& Promise<
|
|
170
|
+
& Promise<
|
|
171
|
+
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
|
|
172
|
+
>
|
|
158
173
|
>(serverWithCustomLogger)
|
|
159
174
|
expectAssignable<
|
|
160
175
|
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
|
|
161
|
-
& PromiseLike<
|
|
176
|
+
& PromiseLike<
|
|
177
|
+
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
|
|
178
|
+
>
|
|
162
179
|
>(serverWithCustomLogger)
|
|
163
180
|
expectType<
|
|
164
181
|
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
|
|
165
|
-
& SafePromiseLike<
|
|
182
|
+
& SafePromiseLike<
|
|
183
|
+
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
|
|
184
|
+
>
|
|
166
185
|
>(serverWithCustomLogger)
|
|
167
186
|
|
|
168
187
|
serverWithCustomLogger.get('/get', getHandlerWithCustomLogger)
|
|
@@ -23,7 +23,16 @@ declare module '../../fastify' {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
26
|
-
interface FastifyRequest<
|
|
26
|
+
interface FastifyRequest<
|
|
27
|
+
RouteGeneric,
|
|
28
|
+
RawServer,
|
|
29
|
+
RawRequest,
|
|
30
|
+
SchemaCompiler,
|
|
31
|
+
TypeProvider,
|
|
32
|
+
ContextConfig,
|
|
33
|
+
Logger,
|
|
34
|
+
RequestType
|
|
35
|
+
> {
|
|
27
36
|
message: ContextConfig extends { includeMessage: true }
|
|
28
37
|
? string
|
|
29
38
|
: null;
|