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.
Files changed (74) hide show
  1. package/.vscode/settings.json +22 -0
  2. package/SECURITY.md +12 -0
  3. package/SPONSORS.md +1 -0
  4. package/build/build-validation.js +1 -1
  5. package/build/sync-version.js +1 -0
  6. package/docs/Guides/Ecosystem.md +5 -0
  7. package/docs/Guides/Fluent-Schema.md +2 -2
  8. package/docs/Reference/Decorators.md +36 -169
  9. package/docs/Reference/Encapsulation.md +5 -1
  10. package/docs/Reference/Plugins.md +11 -2
  11. package/docs/Reference/Reply.md +4 -1
  12. package/docs/Reference/Server.md +39 -1
  13. package/docs/Reference/Type-Providers.md +2 -2
  14. package/docs/Reference/TypeScript.md +136 -0
  15. package/eslint.config.js +18 -2
  16. package/fastify.d.ts +1 -1
  17. package/fastify.js +183 -168
  18. package/lib/{contentTypeParser.js → content-type-parser.js} +2 -1
  19. package/lib/error-handler.js +2 -2
  20. package/lib/{fourOhFour.js → four-oh-four.js} +4 -2
  21. package/lib/{handleRequest.js → handle-request.js} +1 -1
  22. package/lib/{headRoute.js → head-route.js} +13 -1
  23. package/lib/{initialConfigValidation.js → initial-config-validation.js} +1 -1
  24. package/lib/{pluginOverride.js → plugin-override.js} +2 -2
  25. package/lib/{pluginUtils.js → plugin-utils.js} +2 -2
  26. package/lib/reply.js +5 -3
  27. package/lib/request.js +5 -0
  28. package/lib/route.js +20 -9
  29. package/lib/server.js +94 -8
  30. package/lib/symbols.js +1 -0
  31. package/lib/validation.js +9 -1
  32. package/lib/warnings.js +1 -1
  33. package/package.json +8 -8
  34. package/test/500s.test.js +191 -0
  35. package/test/child-logger-factory.test.js +3 -3
  36. package/test/content-parser.test.js +2 -1
  37. package/test/decorator-namespace.test._js_ +1 -1
  38. package/test/diagnostics-channel/error-before-handler.test.js +1 -1
  39. package/test/http2/closing.test.js +88 -0
  40. package/test/internals/content-type-parser.test.js +2 -2
  41. package/test/internals/handle-request.test.js +2 -2
  42. package/test/internals/initial-config.test.js +1 -1
  43. package/test/internals/plugin.test.js +2 -2
  44. package/test/internals/reply.test.js +22 -3
  45. package/test/internals/req-id-gen-factory.test.js +1 -1
  46. package/test/promises.test.js +3 -3
  47. package/test/reply-web-stream-locked.test.js +37 -0
  48. package/test/request-error.test.js +116 -0
  49. package/test/route.6.test.js +20 -1
  50. package/test/route.7.test.js +49 -0
  51. package/test/schema-validation.test.js +27 -4
  52. package/test/server.test.js +22 -4
  53. package/test/set-error-handler.test.js +1 -1
  54. package/test/skip-reply-send.test.js +2 -2
  55. package/test/stream.5.test.js +3 -3
  56. package/test/types/fastify.test-d.ts +70 -18
  57. package/test/types/hooks.test-d.ts +6 -1
  58. package/test/types/instance.test-d.ts +35 -15
  59. package/test/types/logger.test-d.ts +18 -6
  60. package/test/types/plugin.test-d.ts +24 -6
  61. package/test/types/register.test-d.ts +108 -33
  62. package/test/types/reply.test-d.ts +23 -6
  63. package/test/types/request.test-d.ts +25 -6
  64. package/test/types/route.test-d.ts +10 -1
  65. package/test/types/schema.test-d.ts +21 -0
  66. package/test/validation-error-handling.test.js +68 -1
  67. package/test/wrap-thenable.test.js +1 -1
  68. package/types/instance.d.ts +2 -2
  69. package/types/schema.d.ts +1 -1
  70. package/test/check.test.js +0 -219
  71. /package/lib/{configValidator.js → config-validator.js} +0 -0
  72. /package/lib/{reqIdGenFactory.js → req-id-gen-factory.js} +0 -0
  73. /package/lib/{wrapThenable.js → wrap-thenable.js} +0 -0
  74. /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<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> & Promise<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>>(fastify())
29
- expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> & PromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>>(fastify())
30
- expectType<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>>(fastify())
31
- expectType<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({}))
32
- expectType<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({ http: {} }))
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<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({ https: {} }))
35
- expectType<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({ https: null }))
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<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse> & SafePromiseLike<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>>>(fastify({ http2: true, http2SessionTimeout: 1000 }))
38
- expectType<FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse> & SafePromiseLike<FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse>>>(fastify({ http2: true, https: {}, http2SessionTimeout: 1000 }))
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<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({ schemaController: {} }))
41
- expectType<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(
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 = (err: Error | undefined, response: LightMyRequestResponse | undefined) => {
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<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>>(fastify({ http2: true }))
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>(fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } }))
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<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
88
- expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
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<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: customLogger }))
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 (this: FastifyInstance, logger: FastifyBaseLogger, bindings: Bindings, opts: ChildLoggerOptions, req: RawRequestDefaultExpression) {
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<<TError extends Error = FastifyError>(err?: TError | null | undefined, res?: RequestPayload | undefined) => void>(done)
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<FastifyError>(error)
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 (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) { done() }
131
- async function notFoundpreHandlerAsyncHandler (request: FastifyRequest, reply: FastifyReply) {}
132
- function notFoundpreValidationHandler (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) { done() }
133
- async function notFoundpreValidationAsyncHandler (request: FastifyRequest, reply: FastifyReply) {}
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({ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler }, notFoundHandler)
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({ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler }, notFoundAsyncHandler)
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<FastifyError>(error)
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 (this: FastifyInstance, logger: FastifyBaseLogger, bindings: Bindings, opts: ChildLoggerOptions, req: RawRequestDefaultExpression) {
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>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel])
21
- expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](''))
22
- expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({}))
23
- expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({ foo: 'bar' }))
24
- expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Error()))
25
- expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Foo()))
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 = (instance: FastifyInstance, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
25
- const testPluginOptsWithTypeAsync = async (instance: FastifyInstance, opts: FastifyPluginOptions) => { }
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>(fastify().register(function (instance, opts): Promise<void> { return Promise.resolve() }))
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<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & Promise<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(httpsServer)
54
- expectAssignable<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & PromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(httpsServer)
55
- expectType<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(httpsServer)
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 = (instance: FastifyInstance, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
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 = (instance: ServerWithHttp2, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
50
- const testPluginWithHttp2WithTypeAsync = async (instance: ServerWithHttp2, opts: FastifyPluginOptions) => { }
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<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance, TestTypeProvider>
86
- const testPluginWithTypeProvider: FastifyPluginCallback<TestOptions, RawServerDefault, TestTypeProvider> = function (instance, opts, done) { }
87
- const testPluginWithTypeProviderAsync: FastifyPluginAsync<TestOptions, RawServerDefault, TestTypeProvider> = async function (instance, opts) { }
88
- const testPluginWithTypeProviderWithType = (instance: ServerWithTypeProvider, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
89
- const testPluginWithTypeProviderWithTypeAsync = async (instance: ServerWithTypeProvider, opts: FastifyPluginOptions) => { }
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<Server, IncomingMessage, ServerResponse, typeof customLogger, TestTypeProvider>
133
- const testPluginWithTypeProviderAndLogger: FastifyPluginCallback<TestOptions, RawServerDefault, TestTypeProvider, typeof customLogger> = function (instance, opts, done) { }
134
- const testPluginWithTypeProviderAndLoggerAsync: FastifyPluginAsync<TestOptions, RawServerDefault, TestTypeProvider, typeof customLogger> = async function (instance, opts) { }
135
- const testPluginWithTypeProviderAndLoggerWithType = (instance: ServerWithTypeProviderAndLogger, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
136
- const testPluginWithTypeProviderAndLoggerWithTypeAsync = async (instance: ServerWithTypeProviderAndLogger, opts: FastifyPluginOptions) => { }
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
- // @ts-expect-error
144
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger))
145
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger, testOptions))
146
- // @ts-expect-error
147
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync))
148
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync, testOptions))
149
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithType))
150
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithTypeAsync))
151
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register((instance) => {
152
- expectAssignable<FastifyInstance>(instance)
153
- }))
154
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register((instance: ServerWithTypeProviderAndLogger) => {
155
- expectAssignable<ServerWithTypeProviderAndLogger>(instance)
156
- }))
157
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(async (instance) => {
158
- expectAssignable<FastifyInstance>(instance)
159
- }))
160
- expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(async (instance: ServerWithTypeProviderAndLogger) => {
161
- expectAssignable<ServerWithTypeProviderAndLogger>(instance)
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<(hints: Record<string, string | string[]>, callback?: (() => void) | undefined) => void>(reply.writeEarlyHints)
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<(key: string, fn: ((reply: FastifyReply, payload: string | Buffer | null) => Promise<string>) | ((reply: FastifyReply, payload: string | Buffer | null, done: (err: Error | null, value?: string) => void) => void)) => FastifyReply>(reply.trailer)
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<((httpStatus: string) => DefaultSerializationFunction | undefined)>(reply.getSerializationFunction)
43
- expectAssignable<((schema: { [key: string]: unknown }) => DefaultSerializationFunction | undefined)>(reply.getSerializationFunction)
44
- expectAssignable<((schema: { [key: string]: unknown }, httpStatus?: string) => DefaultSerializationFunction)>(reply.compileSerializationSchema)
45
- expectAssignable<((input: { [key: string]: unknown }, schema: { [key: string]: unknown }, httpStatus?: string) => unknown)>(reply.serializeInput)
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<(input: { [key: string]: unknown }, schema: { [key: string]: unknown }, httpPart?: HTTPRequestPart) => boolean>(request.validateInput)
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<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RouteGenericInterface, ContextConfigDefault, FastifySchema, FastifyTypeProviderDefault, CustomLoggerInterface> = function (request, _reply) {
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']>(request.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<FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>>
170
+ & Promise<
171
+ FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
172
+ >
158
173
  >(serverWithCustomLogger)
159
174
  expectAssignable<
160
175
  FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
161
- & PromiseLike<FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>>
176
+ & PromiseLike<
177
+ FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
178
+ >
162
179
  >(serverWithCustomLogger)
163
180
  expectType<
164
181
  FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
165
- & SafePromiseLike<FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>>
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<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger, RequestType> {
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;