fastify 3.26.0 → 4.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/README.md +5 -4
  2. package/build/build-error-serializer.js +27 -0
  3. package/build/build-validation.js +49 -35
  4. package/docs/Guides/Ecosystem.md +2 -1
  5. package/docs/Guides/Prototype-Poisoning.md +3 -3
  6. package/docs/Migration-Guide-V4.md +12 -0
  7. package/docs/Reference/ContentTypeParser.md +8 -1
  8. package/docs/Reference/Errors.md +51 -6
  9. package/docs/Reference/Hooks.md +4 -7
  10. package/docs/Reference/LTS.md +5 -4
  11. package/docs/Reference/Reply.md +23 -22
  12. package/docs/Reference/Request.md +1 -3
  13. package/docs/Reference/Routes.md +17 -10
  14. package/docs/Reference/Server.md +98 -63
  15. package/docs/Reference/TypeScript.md +11 -13
  16. package/docs/Reference/Validation-and-Serialization.md +32 -54
  17. package/docs/Type-Providers.md +257 -0
  18. package/examples/hooks.js +1 -1
  19. package/examples/simple-stream.js +18 -0
  20. package/fastify.d.ts +36 -22
  21. package/fastify.js +72 -53
  22. package/lib/configValidator.js +902 -1023
  23. package/lib/contentTypeParser.js +6 -16
  24. package/lib/context.js +36 -10
  25. package/lib/decorate.js +5 -3
  26. package/lib/error-handler.js +158 -0
  27. package/lib/error-serializer.js +257 -0
  28. package/lib/errors.js +49 -10
  29. package/lib/fourOhFour.js +31 -20
  30. package/lib/handleRequest.js +10 -13
  31. package/lib/hooks.js +14 -9
  32. package/lib/noop-set.js +10 -0
  33. package/lib/pluginOverride.js +0 -3
  34. package/lib/pluginUtils.js +3 -2
  35. package/lib/reply.js +44 -163
  36. package/lib/request.js +13 -10
  37. package/lib/route.js +158 -139
  38. package/lib/schema-controller.js +3 -3
  39. package/lib/schemas.js +27 -1
  40. package/lib/server.js +219 -116
  41. package/lib/symbols.js +6 -4
  42. package/lib/validation.js +2 -1
  43. package/lib/warnings.js +2 -12
  44. package/lib/wrapThenable.js +4 -11
  45. package/package.json +40 -45
  46. package/test/404s.test.js +265 -108
  47. package/test/500s.test.js +2 -2
  48. package/test/async-await.test.js +15 -71
  49. package/test/close.test.js +39 -1
  50. package/test/content-parser.test.js +32 -0
  51. package/test/context-config.test.js +56 -4
  52. package/test/custom-http-server.test.js +14 -7
  53. package/test/custom-parser-async.test.js +0 -65
  54. package/test/custom-parser.test.js +54 -121
  55. package/test/decorator.test.js +1 -3
  56. package/test/delete.test.js +5 -5
  57. package/test/encapsulated-error-handler.test.js +50 -0
  58. package/test/esm/index.test.js +0 -14
  59. package/test/fastify-instance.test.js +4 -4
  60. package/test/fluent-schema.test.js +4 -4
  61. package/test/get.test.js +3 -3
  62. package/test/helper.js +18 -3
  63. package/test/hooks-async.test.js +14 -47
  64. package/test/hooks.on-ready.test.js +9 -4
  65. package/test/hooks.test.js +58 -99
  66. package/test/http2/closing.test.js +5 -11
  67. package/test/http2/unknown-http-method.test.js +3 -9
  68. package/test/https/custom-https-server.test.js +12 -6
  69. package/test/inject.test.js +1 -1
  70. package/test/input-validation.js +2 -2
  71. package/test/internals/all.test.js +2 -2
  72. package/test/internals/contentTypeParser.test.js +4 -4
  73. package/test/internals/handleRequest.test.js +9 -46
  74. package/test/internals/initialConfig.test.js +33 -12
  75. package/test/internals/logger.test.js +1 -1
  76. package/test/internals/reply.test.js +245 -3
  77. package/test/internals/request.test.js +13 -7
  78. package/test/internals/server.test.js +88 -0
  79. package/test/listen.test.js +84 -1
  80. package/test/logger.test.js +98 -58
  81. package/test/maxRequestsPerSocket.test.js +8 -6
  82. package/test/middleware.test.js +2 -25
  83. package/test/noop-set.test.js +19 -0
  84. package/test/nullable-validation.test.js +51 -14
  85. package/test/plugin.test.js +31 -5
  86. package/test/pretty-print.test.js +22 -10
  87. package/test/reply-error.test.js +123 -12
  88. package/test/request-error.test.js +2 -5
  89. package/test/route-hooks.test.js +17 -17
  90. package/test/route-prefix.test.js +2 -1
  91. package/test/route.test.js +216 -20
  92. package/test/router-options.test.js +1 -1
  93. package/test/schema-examples.test.js +11 -5
  94. package/test/schema-feature.test.js +24 -19
  95. package/test/schema-serialization.test.js +50 -9
  96. package/test/schema-special-usage.test.js +14 -81
  97. package/test/schema-validation.test.js +9 -9
  98. package/test/skip-reply-send.test.js +8 -8
  99. package/test/stream.test.js +23 -12
  100. package/test/throw.test.js +8 -5
  101. package/test/trust-proxy.test.js +1 -1
  102. package/test/type-provider.test.js +20 -0
  103. package/test/types/fastify.test-d.ts +12 -18
  104. package/test/types/hooks.test-d.ts +7 -3
  105. package/test/types/import.js +2 -0
  106. package/test/types/import.ts +1 -0
  107. package/test/types/instance.test-d.ts +61 -15
  108. package/test/types/logger.test-d.ts +44 -15
  109. package/test/types/route.test-d.ts +8 -2
  110. package/test/types/schema.test-d.ts +2 -39
  111. package/test/types/type-provider.test-d.ts +417 -0
  112. package/test/validation-error-handling.test.js +9 -9
  113. package/test/versioned-routes.test.js +29 -17
  114. package/test/wrapThenable.test.js +7 -6
  115. package/types/.eslintrc.json +1 -1
  116. package/types/content-type-parser.d.ts +17 -8
  117. package/types/hooks.d.ts +107 -60
  118. package/types/instance.d.ts +137 -105
  119. package/types/logger.d.ts +18 -104
  120. package/types/plugin.d.ts +10 -4
  121. package/types/register.d.ts +1 -1
  122. package/types/reply.d.ts +16 -11
  123. package/types/request.d.ts +10 -5
  124. package/types/route.d.ts +42 -31
  125. package/types/schema.d.ts +15 -1
  126. package/types/type-provider.d.ts +99 -0
  127. package/types/utils.d.ts +1 -1
  128. package/lib/schema-compilers.js +0 -12
  129. package/test/emit-warning.test.js +0 -166
@@ -1,6 +1,12 @@
1
1
  import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
2
2
  import { RouteOptions, RouteShorthandMethod, RouteGenericInterface, DefaultRoute } from './route'
3
- import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError, FastifySerializerCompiler } from './schema'
3
+ import {
4
+ FastifySchema,
5
+ FastifySchemaCompiler,
6
+ FastifySchemaValidationError,
7
+ FastifySerializerCompiler,
8
+ FastifySchemaControllerOptions
9
+ } from './schema'
4
10
  import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
5
11
  import { FastifyLoggerInstance } from './logger'
6
12
  import { FastifyRegister } from './register'
@@ -9,6 +15,7 @@ import { FastifyRequest } from './request'
9
15
  import { FastifyReply } from './reply'
10
16
  import { FastifyError } from 'fastify-error'
11
17
  import { AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction, FastifyBodyParser, removeContentTypeParser, removeAllContentTypeParsers } from './content-type-parser'
18
+ import { FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
12
19
 
13
20
  export interface PrintRoutesOptions {
14
21
  includeMeta?: boolean | (string | symbol)[]
@@ -16,6 +23,8 @@ export interface PrintRoutesOptions {
16
23
  includeHooks?: boolean
17
24
  }
18
25
 
26
+ type NotInInterface<Key, _Interface> = Key extends keyof _Interface ? never : Key
27
+
19
28
  /**
20
29
  * Fastify server instance. Returned by the core `fastify()` method.
21
30
  */
@@ -23,19 +32,22 @@ export interface FastifyInstance<
23
32
  RawServer extends RawServerBase = RawServerDefault,
24
33
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
25
34
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
26
- Logger = FastifyLoggerInstance
35
+ Logger = FastifyLoggerInstance,
36
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
27
37
  > {
28
38
  server: RawServer;
29
39
  prefix: string;
30
40
  version: string;
31
41
  log: Logger;
32
42
 
33
- addSchema(schema: unknown): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
43
+ withTypeProvider<Provider extends FastifyTypeProvider>(): FastifyInstance<RawServer, RawRequest, RawReply, Logger, Provider>;
44
+
45
+ addSchema(schema: unknown): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
34
46
  getSchema(schemaId: string): unknown;
35
47
  getSchemas(): Record<string, unknown>;
36
48
 
37
- after(): FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>;
38
- after(afterListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
49
+ after(): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> & PromiseLike<undefined>;
50
+ after(afterListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
39
51
 
40
52
  close(): Promise<undefined>;
41
53
  close(closeListener: () => void): undefined;
@@ -43,24 +55,24 @@ export interface FastifyInstance<
43
55
  // should be able to define something useful with the decorator getter/setter pattern using Generics to enforce the users function returns what they expect it to
44
56
  decorate<T>(property: string | symbol,
45
57
  value: T extends (...args: any[]) => any
46
- ? (this: FastifyInstance<RawServer, RawRequest, RawReply, Logger>, ...args: Parameters<T>) => ReturnType<T>
58
+ ? (this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>, ...args: Parameters<T>) => ReturnType<T>
47
59
  : T,
48
60
  dependencies?: string[]
49
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
61
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
50
62
 
51
63
  decorateRequest<T>(property: string | symbol,
52
64
  value: T extends (...args: any[]) => any
53
65
  ? (this: FastifyRequest, ...args: Parameters<T>) => ReturnType<T>
54
66
  : T,
55
67
  dependencies?: string[]
56
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
68
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
57
69
 
58
70
  decorateReply<T>(property: string | symbol,
59
71
  value: T extends (...args: any[]) => any
60
72
  ? (this: FastifyReply, ...args: Parameters<T>) => ReturnType<T>
61
73
  : T,
62
74
  dependencies?: string[]
63
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
75
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
64
76
 
65
77
  hasDecorator(decorator: string | symbol): boolean;
66
78
  hasRequestDecorator(decorator: string | symbol): boolean;
@@ -77,10 +89,10 @@ export interface FastifyInstance<
77
89
  listen(opts: { port: number; host?: string; backlog?: number }, callback: (err: Error|null, address: string) => void): void;
78
90
  listen(opts: { port: number; host?: string; backlog?: number }): Promise<string>;
79
91
 
80
- ready(): FastifyInstance<RawServer, RawRequest, RawReply> & PromiseLike<undefined>;
81
- ready(readyListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
92
+ ready(): FastifyInstance<RawServer, RawRequest, RawReply, FastifyLoggerInstance, TypeProvider> & PromiseLike<undefined>;
93
+ ready(readyListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
82
94
 
83
- register: FastifyRegister<FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>>;
95
+ register: FastifyRegister<FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> & PromiseLike<undefined>>;
84
96
 
85
97
  routing(req: RawRequest, res: RawReply): void;
86
98
  getDefaultRoute: DefaultRoute<RawRequest, RawReply>;
@@ -90,16 +102,16 @@ export interface FastifyInstance<
90
102
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
91
103
  ContextConfig = ContextConfigDefault,
92
104
  SchemaCompiler = FastifySchema,
93
- >(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
105
+ >(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
94
106
 
95
- get: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
96
- head: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
97
- post: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
98
- put: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
99
- delete: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
100
- options: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
101
- patch: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
102
- all: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
107
+ get: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
108
+ head: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
109
+ post: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
110
+ put: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
111
+ delete: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
112
+ options: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
113
+ patch: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
114
+ all: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
103
115
 
104
116
  // addHook: overloads
105
117
 
@@ -111,19 +123,21 @@ export interface FastifyInstance<
111
123
  */
112
124
  addHook<
113
125
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
114
- ContextConfig = ContextConfigDefault
126
+ ContextConfig = ContextConfigDefault,
127
+ SchemaCompiler extends FastifySchema = FastifySchema
115
128
  >(
116
129
  name: 'onRequest',
117
- hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
118
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
130
+ hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
131
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
119
132
 
120
133
  addHook<
121
134
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
122
- ContextConfig = ContextConfigDefault
135
+ ContextConfig = ContextConfigDefault,
136
+ SchemaCompiler extends FastifySchema = FastifySchema
123
137
  >(
124
138
  name: 'onRequest',
125
- hook: onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
126
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
139
+ hook: onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
140
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
127
141
 
128
142
  /**
129
143
  * `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.
@@ -131,57 +145,63 @@ export interface FastifyInstance<
131
145
  */
132
146
  addHook<
133
147
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
134
- ContextConfig = ContextConfigDefault
148
+ ContextConfig = ContextConfigDefault,
149
+ SchemaCompiler extends FastifySchema = FastifySchema
135
150
  >(
136
151
  name: 'preParsing',
137
- hook: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
138
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
152
+ hook: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
153
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
139
154
 
140
155
  addHook<
141
156
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
142
- ContextConfig = ContextConfigDefault
157
+ ContextConfig = ContextConfigDefault,
158
+ SchemaCompiler extends FastifySchema = FastifySchema
143
159
  >(
144
160
  name: 'preParsing',
145
- hook: preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
146
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
161
+ hook: preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
162
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
147
163
 
148
164
  /**
149
165
  * `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
150
166
  */
151
167
  addHook<
152
168
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
153
- ContextConfig = ContextConfigDefault
169
+ ContextConfig = ContextConfigDefault,
170
+ SchemaCompiler extends FastifySchema = FastifySchema
154
171
  >(
155
172
  name: 'preValidation',
156
- hook: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
157
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
173
+ hook: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
174
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
158
175
 
159
176
  addHook<
160
177
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
161
- ContextConfig = ContextConfigDefault
178
+ ContextConfig = ContextConfigDefault,
179
+ SchemaCompiler extends FastifySchema = FastifySchema
162
180
  >(
163
181
  name: 'preValidation',
164
- hook: preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
165
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
182
+ hook: preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
183
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
166
184
 
167
185
  /**
168
186
  * `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
169
187
  */
170
188
  addHook<
171
189
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
172
- ContextConfig = ContextConfigDefault
190
+ ContextConfig = ContextConfigDefault,
191
+ SchemaCompiler extends FastifySchema = FastifySchema
173
192
  >(
174
193
  name: 'preHandler',
175
- hook: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
176
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
194
+ hook: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
195
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
177
196
 
178
197
  addHook<
179
198
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
180
- ContextConfig = ContextConfigDefault
199
+ ContextConfig = ContextConfigDefault,
200
+ SchemaCompiler extends FastifySchema = FastifySchema
181
201
  >(
182
202
  name: 'preHandler',
183
- hook: preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
184
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
203
+ hook: preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
204
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
185
205
 
186
206
  /**
187
207
  * `preSerialization` is the fifth hook to be executed in the request lifecycle. The previous hook was `preHandler`, the next hook will be `onSend`.
@@ -190,20 +210,22 @@ export interface FastifyInstance<
190
210
  addHook<
191
211
  PreSerializationPayload = unknown,
192
212
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
193
- ContextConfig = ContextConfigDefault
213
+ ContextConfig = ContextConfigDefault,
214
+ SchemaCompiler extends FastifySchema = FastifySchema
194
215
  >(
195
216
  name: 'preSerialization',
196
- hook: preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
197
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
217
+ hook: preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
218
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
198
219
 
199
220
  addHook<
200
221
  PreSerializationPayload = unknown,
201
222
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
202
- ContextConfig = ContextConfigDefault
223
+ ContextConfig = ContextConfigDefault,
224
+ SchemaCompiler extends FastifySchema = FastifySchema
203
225
  >(
204
226
  name: 'preSerialization',
205
- hook: preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
206
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
227
+ hook: preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
228
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
207
229
 
208
230
  /**
209
231
  * You can change the payload with the `onSend` hook. It is the sixth hook to be executed in the request lifecycle. The previous hook was `preSerialization`, the next hook will be `onResponse`.
@@ -212,20 +234,22 @@ export interface FastifyInstance<
212
234
  addHook<
213
235
  OnSendPayload = unknown,
214
236
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
215
- ContextConfig = ContextConfigDefault
237
+ ContextConfig = ContextConfigDefault,
238
+ SchemaCompiler extends FastifySchema = FastifySchema
216
239
  >(
217
240
  name: 'onSend',
218
- hook: onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
219
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
241
+ hook: onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
242
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
220
243
 
221
244
  addHook<
222
245
  OnSendPayload = unknown,
223
246
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
224
- ContextConfig = ContextConfigDefault
247
+ ContextConfig = ContextConfigDefault,
248
+ SchemaCompiler extends FastifySchema = FastifySchema
225
249
  >(
226
250
  name: 'onSend',
227
- hook: onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
228
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
251
+ hook: onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
252
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
229
253
 
230
254
  /**
231
255
  * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook.
@@ -233,19 +257,21 @@ export interface FastifyInstance<
233
257
  */
234
258
  addHook<
235
259
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
236
- ContextConfig = ContextConfigDefault
260
+ ContextConfig = ContextConfigDefault,
261
+ SchemaCompiler extends FastifySchema = FastifySchema
237
262
  >(
238
263
  name: 'onResponse',
239
- hook: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
240
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
264
+ hook: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
265
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
241
266
 
242
267
  addHook<
243
268
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
244
- ContextConfig = ContextConfigDefault
269
+ ContextConfig = ContextConfigDefault,
270
+ SchemaCompiler extends FastifySchema = FastifySchema
245
271
  >(
246
272
  name: 'onResponse',
247
- hook: onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
248
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
273
+ hook: onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
274
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
249
275
 
250
276
  /**
251
277
  * `onTimeout` is useful if you need to monitor the request timed out in your service. (if the `connectionTimeout` property is set on the fastify instance)
@@ -253,19 +279,21 @@ export interface FastifyInstance<
253
279
  */
254
280
  addHook<
255
281
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
256
- ContextConfig = ContextConfigDefault
282
+ ContextConfig = ContextConfigDefault,
283
+ SchemaCompiler extends FastifySchema = FastifySchema
257
284
  >(
258
285
  name: 'onTimeout',
259
- hook: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
260
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
286
+ hook: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
287
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
261
288
 
262
289
  addHook<
263
290
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
264
- ContextConfig = ContextConfigDefault
291
+ ContextConfig = ContextConfigDefault,
292
+ SchemaCompiler extends FastifySchema = FastifySchema
265
293
  >(
266
294
  name: 'onTimeout',
267
- hook: onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
268
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
295
+ hook: onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
296
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
269
297
 
270
298
  /**
271
299
  * This hook is useful if you need to do some custom error logging or add some specific header in case of error.
@@ -275,19 +303,21 @@ export interface FastifyInstance<
275
303
  */
276
304
  addHook<
277
305
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
278
- ContextConfig = ContextConfigDefault
306
+ ContextConfig = ContextConfigDefault,
307
+ SchemaCompiler extends FastifySchema = FastifySchema
279
308
  >(
280
309
  name: 'onError',
281
- hook: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
282
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
310
+ hook: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider>
311
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
283
312
 
284
313
  addHook<
285
314
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
286
- ContextConfig = ContextConfigDefault
315
+ ContextConfig = ContextConfigDefault,
316
+ SchemaCompiler extends FastifySchema = FastifySchema
287
317
  >(
288
318
  name: 'onError',
289
- hook: onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
290
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
319
+ hook: onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider>
320
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
291
321
 
292
322
  // Application addHooks
293
323
 
@@ -296,11 +326,12 @@ export interface FastifyInstance<
296
326
  */
297
327
  addHook<
298
328
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
299
- ContextConfig = ContextConfigDefault
329
+ ContextConfig = ContextConfigDefault,
330
+ SchemaCompiler extends FastifySchema = FastifySchema
300
331
  >(
301
332
  name: 'onRoute',
302
- hook: onRouteHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
303
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
333
+ hook: onRouteHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
334
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
304
335
 
305
336
  /**
306
337
  * Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed before the registered code.
@@ -309,8 +340,8 @@ export interface FastifyInstance<
309
340
  */
310
341
  addHook(
311
342
  name: 'onRegister',
312
- hook: onRegisterHookHandler<RawServer, RawRequest, RawReply, Logger>
313
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
343
+ hook: onRegisterHookHandler<RawServer, RawRequest, RawReply, Logger, TypeProvider>
344
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
314
345
 
315
346
  /**
316
347
  * Triggered when fastify.listen() or fastify.ready() is invoked to start the server. It is useful when plugins need a "ready" event, for example to load data before the server start listening for requests.
@@ -318,20 +349,20 @@ export interface FastifyInstance<
318
349
  addHook(
319
350
  name: 'onReady',
320
351
  hook: onReadyHookHandler
321
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
352
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
322
353
 
323
354
  addHook(
324
355
  name: 'onReady',
325
356
  hook: onReadyAsyncHookHandler,
326
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
357
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
327
358
 
328
359
  /**
329
360
  * Triggered when fastify.close() is invoked to stop the server. It is useful when plugins need a "shutdown" event, for example to close an open connection to a database.
330
361
  */
331
362
  addHook(
332
363
  name: 'onClose',
333
- hook: onCloseHookHandler<RawServer, RawRequest, RawReply, Logger>
334
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
364
+ hook: onCloseHookHandler<RawServer, RawRequest, RawReply, Logger, TypeProvider>
365
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
335
366
 
336
367
  addHook(
337
368
  name: 'onClose',
@@ -341,17 +372,17 @@ export interface FastifyInstance<
341
372
  /**
342
373
  * Set the 404 handler
343
374
  */
344
- setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface> (
345
- handler: (request: FastifyRequest<RouteGeneric, RawServer, RawRequest>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric>) => void
346
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
375
+ setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema> (
376
+ handler: (request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfigDefault, SchemaCompiler, TypeProvider>) => void
377
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
347
378
 
348
- setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig extends ContextConfigDefault = ContextConfigDefault> (
379
+ setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig extends ContextConfigDefault = ContextConfigDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema> (
349
380
  opts: {
350
- preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
351
- preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
381
+ preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
382
+ preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
352
383
  },
353
- handler: (request: FastifyRequest<RouteGeneric, RawServer, RawRequest>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric>) => void
354
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>
384
+ handler: (request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfigDefault, SchemaCompiler, TypeProvider>) => void
385
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>
355
386
 
356
387
  /**
357
388
  * Fastify default error handler
@@ -361,38 +392,38 @@ export interface FastifyInstance<
361
392
  /**
362
393
  * Set a function that will be called whenever an error happens
363
394
  */
364
- setErrorHandler<TError extends Error = FastifyError, RouteGeneric extends RouteGenericInterface = RouteGenericInterface>(
365
- handler: (
366
- this: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
367
- error: TError,
368
- request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
369
- reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric>
370
- ) => void | Promise<RouteGeneric['Reply'] | void>
371
- ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
395
+ setErrorHandler<TError extends Error = FastifyError, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, SchemaCompiler extends FastifySchema = FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault>(
396
+ handler: (this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>, error: TError, request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfigDefault, SchemaCompiler, TypeProvider>) => any | Promise<any>
397
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
372
398
 
373
399
  /**
374
400
  * Set the schema validator for all routes.
375
401
  */
376
- setValidatorCompiler<T = FastifySchema>(schemaCompiler: FastifySchemaCompiler<T>): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
402
+ setValidatorCompiler<T = FastifySchema>(schemaCompiler: FastifySchemaCompiler<T>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
377
403
 
378
404
  /**
379
405
  * Set the schema serializer for all routes.
380
406
  */
381
- setSerializerCompiler<T = FastifySchema>(schemaCompiler: FastifySerializerCompiler<T>): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
407
+ setSerializerCompiler<T = FastifySchema>(schemaCompiler: FastifySerializerCompiler<T>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
408
+
409
+ /**
410
+ * Set the schema controller for all routes.
411
+ */
412
+ setSchemaController(schemaControllerOpts: FastifySchemaControllerOptions): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
382
413
 
383
414
  /**
384
415
  * Set the reply serializer for all routes.
385
416
  */
386
- setReplySerializer(replySerializer: (payload: unknown, statusCode: number) => string): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
417
+ setReplySerializer(replySerializer: (payload: unknown, statusCode: number) => string): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
387
418
 
388
419
  /*
389
420
  * Set the schema error formatter for all routes.
390
421
  */
391
- setSchemaErrorFormatter(errorFormatter: (errors: FastifySchemaValidationError[], dataVar: string) => Error): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
422
+ setSchemaErrorFormatter(errorFormatter: (errors: FastifySchemaValidationError[], dataVar: string) => Error): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
392
423
  /**
393
424
  * Add a content type parser
394
425
  */
395
- addContentTypeParser: AddContentTypeParser<RawServer, RawRequest>;
426
+ addContentTypeParser: AddContentTypeParser<RawServer, RawRequest, RouteGenericInterface, FastifySchema, TypeProvider>;
396
427
  hasContentTypeParser: hasContentTypeParser;
397
428
  /**
398
429
  * Remove an existing content type parser
@@ -427,6 +458,7 @@ export interface FastifyInstance<
427
458
  initialConfig: Readonly<{
428
459
  connectionTimeout?: number,
429
460
  keepAliveTimeout?: number,
461
+ forceCloseConnections?: boolean,
430
462
  bodyLimit?: number,
431
463
  caseSensitive?: boolean,
432
464
  http2?: boolean,