fastify 3.7.0 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/docs/Ecosystem.md +6 -1
  2. package/docs/Hooks.md +0 -5
  3. package/docs/Recommendations.md +17 -0
  4. package/docs/Reply.md +2 -2
  5. package/docs/Request.md +1 -1
  6. package/docs/Routes.md +19 -1
  7. package/docs/Server.md +2 -0
  8. package/docs/Style-Guide.md +180 -0
  9. package/docs/TypeScript.md +359 -359
  10. package/examples/parser.js +1 -1
  11. package/fastify.js +8 -8
  12. package/lib/contentTypeParser.js +12 -11
  13. package/lib/context.js +4 -3
  14. package/lib/decorate.js +1 -1
  15. package/lib/fourOhFour.js +4 -4
  16. package/lib/handleRequest.js +5 -5
  17. package/lib/hooks.js +4 -4
  18. package/lib/logger.js +6 -6
  19. package/lib/pluginUtils.js +1 -1
  20. package/lib/reply.js +24 -21
  21. package/lib/reqIdGenFactory.js +2 -2
  22. package/lib/request.js +8 -5
  23. package/lib/route.js +9 -8
  24. package/lib/schemas.js +1 -1
  25. package/lib/server.js +5 -5
  26. package/lib/validation.js +8 -8
  27. package/package.json +8 -8
  28. package/test/404s.test.js +15 -15
  29. package/test/async-await.test.js +7 -7
  30. package/test/custom-parser-async.test.js +2 -2
  31. package/test/custom-parser.test.js +8 -8
  32. package/test/helper.js +1 -1
  33. package/test/hooks.test.js +6 -6
  34. package/test/http2/head.test.js +1 -1
  35. package/test/http2/plain.test.js +1 -1
  36. package/test/http2/secure-with-fallback.test.js +1 -1
  37. package/test/http2/secure.test.js +1 -1
  38. package/test/http2/unknown-http-method.test.js +1 -1
  39. package/test/https/https.test.js +2 -1
  40. package/test/inject.test.js +2 -2
  41. package/test/internals/all.test.js +1 -1
  42. package/test/internals/hookRunner.test.js +1 -1
  43. package/test/internals/logger.test.js +1 -1
  44. package/test/internals/reply.test.js +62 -7
  45. package/test/internals/request.test.js +23 -0
  46. package/test/listen.test.js +12 -0
  47. package/test/logger.test.js +10 -10
  48. package/test/nullable-validation.test.js +108 -0
  49. package/test/pretty-print.test.js +9 -14
  50. package/test/reply-error.test.js +2 -2
  51. package/test/route-hooks.test.js +10 -10
  52. package/test/stream.test.js +11 -11
  53. package/test/types/fastify.test-d.ts +1 -2
  54. package/test/types/logger.test-d.ts +1 -1
  55. package/test/types/route.test-d.ts +5 -0
  56. package/test/versioned-routes.test.js +55 -0
  57. package/types/.eslintrc.json +4 -1
  58. package/types/content-type-parser.d.ts +0 -15
  59. package/types/hooks.d.ts +138 -16
  60. package/types/instance.d.ts +81 -2
  61. package/types/logger.d.ts +1 -1
  62. package/types/plugin.d.ts +5 -7
  63. package/types/register.d.ts +8 -8
  64. package/types/route.d.ts +38 -58
  65. package/types/schema.d.ts +4 -4
  66. package/types/serverFactory.d.ts +9 -9
package/types/hooks.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- /* eslint-disable @typescript-eslint/class-name-casing */
2
-
3
1
  import { Readable } from 'stream'
4
2
  import { FastifyInstance } from './instance'
5
3
  import { RouteOptions, RouteGenericInterface } from './route'
@@ -32,7 +30,20 @@ export interface onRequestHookHandler<
32
30
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
33
31
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
34
32
  done: HookHandlerDoneFunction
35
- ): Promise<unknown> | void;
33
+ ): void;
34
+ }
35
+
36
+ export interface onRequestAsyncHookHandler<
37
+ RawServer extends RawServerBase = RawServerDefault,
38
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
39
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
40
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
41
+ ContextConfig = ContextConfigDefault
42
+ > {
43
+ (
44
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
45
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
46
+ ): Promise<unknown>;
36
47
  }
37
48
 
38
49
  /**
@@ -51,7 +62,21 @@ export interface preParsingHookHandler<
51
62
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
52
63
  payload: RequestPayload,
53
64
  done: <TError extends Error = FastifyError>(err?: TError | null, res?: RequestPayload) => void
54
- ): Promise<RequestPayload | unknown> | void;
65
+ ): void;
66
+ }
67
+
68
+ export interface preParsingAsyncHookHandler<
69
+ RawServer extends RawServerBase = RawServerDefault,
70
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
71
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
72
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
73
+ ContextConfig = ContextConfigDefault
74
+ > {
75
+ (
76
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
77
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
78
+ payload: RequestPayload,
79
+ ): Promise<RequestPayload | unknown>;
55
80
  }
56
81
 
57
82
  /**
@@ -68,7 +93,20 @@ export interface preValidationHookHandler<
68
93
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
69
94
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
70
95
  done: HookHandlerDoneFunction
71
- ): Promise<unknown> | void;
96
+ ): void;
97
+ }
98
+
99
+ export interface preValidationAsyncHookHandler<
100
+ RawServer extends RawServerBase = RawServerDefault,
101
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
102
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
103
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
104
+ ContextConfig = ContextConfigDefault
105
+ > {
106
+ (
107
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
108
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
109
+ ): Promise<unknown>;
72
110
  }
73
111
 
74
112
  /**
@@ -85,7 +123,20 @@ export interface preHandlerHookHandler<
85
123
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
86
124
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
87
125
  done: HookHandlerDoneFunction
88
- ): Promise<unknown> | void;
126
+ ): void;
127
+ }
128
+
129
+ export interface preHandlerAsyncHookHandler<
130
+ RawServer extends RawServerBase = RawServerDefault,
131
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
132
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
133
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
134
+ ContextConfig = ContextConfigDefault
135
+ > {
136
+ (
137
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
138
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
139
+ ): Promise<unknown>;
89
140
  }
90
141
 
91
142
  // This is used within the `preSerialization` and `onSend` hook handlers
@@ -112,7 +163,22 @@ export interface preSerializationHookHandler<
112
163
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
113
164
  payload: PreSerializationPayload,
114
165
  done: DoneFuncWithErrOrRes
115
- ): Promise<unknown> | void;
166
+ ): void;
167
+ }
168
+
169
+ export interface preSerializationAsyncHookHandler<
170
+ PreSerializationPayload,
171
+ RawServer extends RawServerBase = RawServerDefault,
172
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
173
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
174
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
175
+ ContextConfig = ContextConfigDefault
176
+ > {
177
+ (
178
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
179
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
180
+ payload: PreSerializationPayload
181
+ ): Promise<unknown>;
116
182
  }
117
183
 
118
184
  /**
@@ -132,7 +198,22 @@ export interface onSendHookHandler<
132
198
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
133
199
  payload: OnSendPayload,
134
200
  done: DoneFuncWithErrOrRes
135
- ): Promise<unknown> | void;
201
+ ): void;
202
+ }
203
+
204
+ export interface onSendAsyncHookHandler<
205
+ OnSendPayload,
206
+ RawServer extends RawServerBase = RawServerDefault,
207
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
208
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
209
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
210
+ ContextConfig = ContextConfigDefault
211
+ > {
212
+ (
213
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
214
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
215
+ payload: OnSendPayload,
216
+ ): Promise<unknown>;
136
217
  }
137
218
 
138
219
  /**
@@ -150,7 +231,20 @@ export interface onResponseHookHandler<
150
231
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
151
232
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
152
233
  done: HookHandlerDoneFunction
153
- ): Promise<unknown> | void;
234
+ ): void;
235
+ }
236
+
237
+ export interface onResponseAsyncHookHandler<
238
+ RawServer extends RawServerBase = RawServerDefault,
239
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
240
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
241
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
242
+ ContextConfig = ContextConfigDefault
243
+ > {
244
+ (
245
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
246
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
247
+ ): Promise<unknown>;
154
248
  }
155
249
 
156
250
  /**
@@ -168,7 +262,20 @@ export interface onTimeoutHookHandler<
168
262
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
169
263
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
170
264
  done: HookHandlerDoneFunction
171
- ): Promise<unknown> | void;
265
+ ): void;
266
+ }
267
+
268
+ export interface onTimeoutAsyncHookHandler<
269
+ RawServer extends RawServerBase = RawServerDefault,
270
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
271
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
272
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
273
+ ContextConfig = ContextConfigDefault
274
+ > {
275
+ (
276
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
277
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
278
+ ): Promise<unknown>;
172
279
  }
173
280
 
174
281
  /**
@@ -190,7 +297,22 @@ export interface onErrorHookHandler<
190
297
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
191
298
  error: TError,
192
299
  done: () => void
193
- ): Promise<unknown> | void;
300
+ ): void;
301
+ }
302
+
303
+ export interface onErrorAsyncHookHandler<
304
+ RawServer extends RawServerBase = RawServerDefault,
305
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
306
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
307
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
308
+ ContextConfig = ContextConfigDefault,
309
+ TError extends Error = FastifyError
310
+ > {
311
+ (
312
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
313
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
314
+ error: TError
315
+ ): Promise<unknown>;
194
316
  }
195
317
 
196
318
  // Application Hooks
@@ -230,15 +352,15 @@ export interface onRegisterHookHandler<
230
352
  /**
231
353
  * 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.
232
354
  */
233
- export interface onReadyHookHandler<
234
- RawServer extends RawServerBase = RawServerDefault,
235
- Logger = FastifyLoggerInstance
236
- > {
355
+ export interface onReadyHookHandler {
237
356
  (
238
357
  done: HookHandlerDoneFunction
239
- ): Promise<unknown> | void;
358
+ ): void;
240
359
  }
241
360
 
361
+ export interface onReadyAsyncHookHandler {
362
+ (): Promise<unknown>;
363
+ }
242
364
  /**
243
365
  * 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.
244
366
  */
@@ -4,7 +4,7 @@ import { FastifySchemaCompiler, FastifySchemaValidationError } from './schema'
4
4
  import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
5
5
  import { FastifyLoggerInstance } from './logger'
6
6
  import { FastifyRegister } from './register'
7
- import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler, onTimeoutHookHandler } from './hooks'
7
+ import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler, onTimeoutHookHandler, preParsingAsyncHookHandler, preValidationAsyncHookHandler, preHandlerAsyncHookHandler, preSerializationAsyncHookHandler, onSendAsyncHookHandler, onResponseAsyncHookHandler, onTimeoutAsyncHookHandler, onErrorAsyncHookHandler, onReadyAsyncHookHandler, onRequestAsyncHookHandler } from './hooks'
8
8
  import { FastifyRequest } from './request'
9
9
  import { FastifyReply } from './reply'
10
10
  import { FastifyError } from 'fastify-error'
@@ -89,6 +89,14 @@ export interface FastifyInstance<
89
89
  hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
90
90
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
91
91
 
92
+ addHook<
93
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
94
+ ContextConfig = ContextConfigDefault
95
+ >(
96
+ name: 'onRequest',
97
+ hook: onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
98
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
99
+
92
100
  /**
93
101
  * `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.
94
102
  * Notice: in the `preParsing` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
@@ -101,6 +109,14 @@ export interface FastifyInstance<
101
109
  hook: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
102
110
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
103
111
 
112
+ addHook<
113
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
114
+ ContextConfig = ContextConfigDefault
115
+ >(
116
+ name: 'preParsing',
117
+ hook: preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
118
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
119
+
104
120
  /**
105
121
  * `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
106
122
  */
@@ -112,6 +128,14 @@ export interface FastifyInstance<
112
128
  hook: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
113
129
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
114
130
 
131
+ addHook<
132
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
133
+ ContextConfig = ContextConfigDefault
134
+ >(
135
+ name: 'preValidation',
136
+ hook: preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
137
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
138
+
115
139
  /**
116
140
  * `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
117
141
  */
@@ -123,6 +147,14 @@ export interface FastifyInstance<
123
147
  hook: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
124
148
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
125
149
 
150
+ addHook<
151
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
152
+ ContextConfig = ContextConfigDefault
153
+ >(
154
+ name: 'preHandler',
155
+ hook: preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
156
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
157
+
126
158
  /**
127
159
  * `preSerialization` is the fifth hook to be executed in the request lifecycle. The previous hook was `preHandler`, the next hook will be `onSend`.
128
160
  * Note: the hook is NOT called if the payload is a string, a Buffer, a stream or null.
@@ -136,6 +168,15 @@ export interface FastifyInstance<
136
168
  hook: preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
137
169
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
138
170
 
171
+ addHook<
172
+ PreSerializationPayload = unknown,
173
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
174
+ ContextConfig = ContextConfigDefault
175
+ >(
176
+ name: 'preSerialization',
177
+ hook: preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
178
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
179
+
139
180
  /**
140
181
  * 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`.
141
182
  * Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null.
@@ -149,6 +190,15 @@ export interface FastifyInstance<
149
190
  hook: onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
150
191
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
151
192
 
193
+ addHook<
194
+ OnSendPayload = unknown,
195
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
196
+ ContextConfig = ContextConfigDefault
197
+ >(
198
+ name: 'onSend',
199
+ hook: onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
200
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
201
+
152
202
  /**
153
203
  * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook.
154
204
  * The onResponse hook is executed when a response has been sent, so you will not be able to send more data to the client. It can however be useful for sending data to external services, for example to gather statistics.
@@ -161,6 +211,14 @@ export interface FastifyInstance<
161
211
  hook: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
162
212
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
163
213
 
214
+ addHook<
215
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
216
+ ContextConfig = ContextConfigDefault
217
+ >(
218
+ name: 'onResponse',
219
+ hook: onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
220
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
221
+
164
222
  /**
165
223
  * `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)
166
224
  * The onTimeout hook is executed when a request is timed out and the http socket has been hanged up. Therefore you will not be able to send data to the client.
@@ -173,6 +231,14 @@ export interface FastifyInstance<
173
231
  hook: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
174
232
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
175
233
 
234
+ addHook<
235
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
236
+ ContextConfig = ContextConfigDefault
237
+ >(
238
+ name: 'onTimeout',
239
+ hook: onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
240
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
241
+
176
242
  /**
177
243
  * This hook is useful if you need to do some custom error logging or add some specific header in case of error.
178
244
  * It is not intended for changing the error, and calling reply.send will throw an exception.
@@ -187,6 +253,14 @@ export interface FastifyInstance<
187
253
  hook: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
188
254
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
189
255
 
256
+ addHook<
257
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
258
+ ContextConfig = ContextConfigDefault
259
+ >(
260
+ name: 'onError',
261
+ hook: onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
262
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
263
+
190
264
  // Application addHooks
191
265
 
192
266
  /**
@@ -215,7 +289,12 @@ export interface FastifyInstance<
215
289
  */
216
290
  addHook(
217
291
  name: 'onReady',
218
- hook: onReadyHookHandler<RawServer, Logger>
292
+ hook: onReadyHookHandler
293
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
294
+
295
+ addHook(
296
+ name: 'onReady',
297
+ hook: onReadyAsyncHookHandler,
219
298
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
220
299
 
221
300
  /**
package/types/logger.d.ts CHANGED
@@ -27,7 +27,7 @@ import { FastifyRequest, RequestGenericInterface } from './request'
27
27
  */
28
28
  export interface FastifyLogFn {
29
29
  (msg: string, ...args: unknown[]): void;
30
- (obj: object, msg?: string, ...args: unknown[]): void;
30
+ (obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
31
31
  }
32
32
 
33
33
  export type LogLevel = 'info' | 'error' | 'debug' | 'fatal' | 'warn' | 'trace'
package/types/plugin.d.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { FastifyInstance } from './instance'
2
2
  import { RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault } from './utils'
3
3
 
4
+ export type FastifyPluginOptions = Record<string, any>
5
+
4
6
  /**
5
7
  * FastifyPluginCallback
6
8
  *
7
9
  * Fastify allows the user to extend its functionalities with plugins. A plugin can be a set of routes, a server decorator or whatever. To activate plugins, use the `fastify.register()` method.
8
10
  */
9
- export type FastifyPluginCallback<Options extends FastifyPluginOptions = {}, Server extends RawServerBase = RawServerDefault> = (
11
+ export type FastifyPluginCallback<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = (
10
12
  instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>>,
11
13
  opts: Options,
12
14
  next: (err?: Error) => void
@@ -17,7 +19,7 @@ export type FastifyPluginCallback<Options extends FastifyPluginOptions = {}, Ser
17
19
  *
18
20
  * Fastify allows the user to extend its functionalities with plugins. A plugin can be a set of routes, a server decorator or whatever. To activate plugins, use the `fastify.register()` method.
19
21
  */
20
- export type FastifyPluginAsync<Options extends FastifyPluginOptions = {}, Server extends RawServerBase = RawServerDefault> = (
22
+ export type FastifyPluginAsync<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = (
21
23
  instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>>,
22
24
  opts: Options
23
25
  ) => Promise<void>;
@@ -26,8 +28,4 @@ export type FastifyPluginAsync<Options extends FastifyPluginOptions = {}, Server
26
28
  * Generic plugin type.
27
29
  * @deprecated union type doesn't work well with type inference in TS and is therefore deprecated in favor of explicit types. See FastifyRegister.
28
30
  */
29
- export type FastifyPlugin<Options extends FastifyPluginOptions = {}> = FastifyPluginCallback<Options> | FastifyPluginAsync<Options>
30
-
31
- export interface FastifyPluginOptions {
32
- [key: string]: any;
33
- }
31
+ export type FastifyPlugin<Options extends FastifyPluginOptions = Record<never, never>> = FastifyPluginCallback<Options> | FastifyPluginAsync<Options>
@@ -1,6 +1,14 @@
1
1
  import { FastifyPluginOptions, FastifyPluginCallback, FastifyPluginAsync } from './plugin'
2
2
  import { LogLevel } from './logger'
3
3
 
4
+ interface RegisterOptions {
5
+ prefix?: string;
6
+ logLevel?: LogLevel;
7
+ logSerializers?: Record<string, (value: any) => string>;
8
+ }
9
+
10
+ export type FastifyRegisterOptions<Options> = (RegisterOptions & Options) | (() => RegisterOptions & Options)
11
+
4
12
  /**
5
13
  * FastifyRegister
6
14
  *
@@ -20,11 +28,3 @@ export interface FastifyRegister<T = void> {
20
28
  opts?: FastifyRegisterOptions<Options>
21
29
  ): T;
22
30
  }
23
-
24
- export type FastifyRegisterOptions<Options> = (RegisterOptions & Options) | (() => RegisterOptions & Options)
25
-
26
- interface RegisterOptions {
27
- prefix?: string;
28
- logLevel?: LogLevel;
29
- logSerializers?: Record<string, (value: any) => string>;
30
- }
package/types/route.d.ts CHANGED
@@ -9,49 +9,6 @@ import { FastifyError } from 'fastify-error'
9
9
 
10
10
  export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface {}
11
11
 
12
- /**
13
- * Fastify Router Shorthand method type that is similar to the Express/Restify approach
14
- */
15
- export interface RouteShorthandMethod<
16
- RawServer extends RawServerBase = RawServerDefault,
17
- RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
18
- RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
19
- > {
20
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
21
- path: string,
22
- opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
23
- handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
24
- ): FastifyInstance<RawServer, RawRequest, RawReply>;
25
- }
26
-
27
- /**
28
- * Fastify Router Shorthand method type that is similar to the Express/Restify approach
29
- */
30
- export interface RouteShorthandMethod<
31
- RawServer extends RawServerBase = RawServerDefault,
32
- RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
33
- RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
34
- > {
35
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
36
- path: string,
37
- handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
38
- ): FastifyInstance<RawServer, RawRequest, RawReply>;
39
- }
40
-
41
- /**
42
- * Fastify Router Shorthand method type that is similar to the Express/Restify approach
43
- */
44
- export interface RouteShorthandMethod<
45
- RawServer extends RawServerBase = RawServerDefault,
46
- RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
47
- RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
48
- > {
49
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
50
- path: string,
51
- opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
52
- ): FastifyInstance<RawServer, RawRequest, RawReply>;
53
- }
54
-
55
12
  /**
56
13
  * Route shorthand options for the various shorthand methods
57
14
  */
@@ -70,7 +27,7 @@ export interface RouteShorthandOptions<
70
27
  logLevel?: LogLevel;
71
28
  config?: ContextConfig;
72
29
  version?: string;
73
- prefixTrailingSlash?: boolean;
30
+ prefixTrailingSlash?: 'slash'|'no-slash'|'both';
74
31
  errorHandler?: (this: FastifyInstance, error: FastifyError, request: FastifyRequest, reply: FastifyReply) => void;
75
32
  // TODO: Change to actual type.
76
33
  schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error;
@@ -88,19 +45,19 @@ export interface RouteShorthandOptions<
88
45
  }
89
46
 
90
47
  /**
91
- * Fastify route method options.
48
+ * Route handler method declaration.
92
49
  */
93
- export interface RouteOptions<
50
+ export type RouteHandlerMethod<
94
51
  RawServer extends RawServerBase = RawServerDefault,
95
52
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
96
53
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
97
54
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
98
55
  ContextConfig = ContextConfigDefault
99
- > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> {
100
- method: HTTPMethods | HTTPMethods[];
101
- url: string;
102
- handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
103
- }
56
+ > = (
57
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
58
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
59
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
60
+ ) => void | Promise<RouteGeneric['Reply'] | void>
104
61
 
105
62
  /**
106
63
  * Shorthand options including the handler function property
@@ -116,19 +73,42 @@ export interface RouteShorthandOptionsWithHandler<
116
73
  }
117
74
 
118
75
  /**
119
- * Route handler method declaration.
76
+ * Fastify Router Shorthand method type that is similar to the Express/Restify approach
120
77
  */
121
- export type RouteHandlerMethod<
78
+ export interface RouteShorthandMethod<
79
+ RawServer extends RawServerBase = RawServerDefault,
80
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
81
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
82
+ > {
83
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
84
+ path: string,
85
+ opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
86
+ handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
87
+ ): FastifyInstance<RawServer, RawRequest, RawReply>;
88
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
89
+ path: string,
90
+ handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
91
+ ): FastifyInstance<RawServer, RawRequest, RawReply>;
92
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
93
+ path: string,
94
+ opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
95
+ ): FastifyInstance<RawServer, RawRequest, RawReply>;
96
+ }
97
+
98
+ /**
99
+ * Fastify route method options.
100
+ */
101
+ export interface RouteOptions<
122
102
  RawServer extends RawServerBase = RawServerDefault,
123
103
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
124
104
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
125
105
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
126
106
  ContextConfig = ContextConfigDefault
127
- > = (
128
- this: FastifyInstance<RawServer, RawRequest, RawReply>,
129
- request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
130
- reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
131
- ) => void | Promise<RouteGeneric['Reply'] | void>
107
+ > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> {
108
+ method: HTTPMethods | HTTPMethods[];
109
+ url: string;
110
+ handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
111
+ }
132
112
 
133
113
  export type RouteHandler<
134
114
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
package/types/schema.d.ts CHANGED
@@ -20,15 +20,15 @@ export interface FastifyRouteSchemaDef {
20
20
  httpStatus?: string;
21
21
  }
22
22
 
23
- export interface FastifyValidationResult {
24
- errors?: FastifySchemaValidationError[] | null;
25
- }
26
-
27
23
  export interface FastifySchemaValidationError {
28
24
  message?: string;
29
25
  dataPath: string;
30
26
  }
31
27
 
28
+ export interface FastifyValidationResult {
29
+ errors?: FastifySchemaValidationError[] | null;
30
+ }
31
+
32
32
  /**
33
33
  * Compiler for FastifySchema Type
34
34
  */
@@ -3,17 +3,17 @@ import * as http from 'http'
3
3
  import * as https from 'https'
4
4
  import * as http2 from 'http2'
5
5
 
6
- export interface FastifyServerFactory<
7
- RawServer extends RawServerBase = RawServerDefault
8
- > {
9
- (handler: FastifyServerFactoryHandler<RawServer>, opts: object): RawServer;
10
- }
11
-
12
6
  export type FastifyServerFactoryHandler<
13
7
  RawServer extends RawServerBase = RawServerDefault,
14
8
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
15
9
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>
16
10
  > =
17
- RawServer extends http.Server | https.Server ?
18
- (request: http.IncomingMessage & RawRequest, response: http.ServerResponse & RawReply) => void :
19
- (request: http2.Http2ServerRequest & RawRequest, response: http2.Http2ServerResponse & RawReply) => void
11
+ RawServer extends http.Server | https.Server ?
12
+ (request: http.IncomingMessage & RawRequest, response: http.ServerResponse & RawReply) => void :
13
+ (request: http2.Http2ServerRequest & RawRequest, response: http2.Http2ServerResponse & RawReply) => void
14
+
15
+ export interface FastifyServerFactory<
16
+ RawServer extends RawServerBase = RawServerDefault
17
+ > {
18
+ (handler: FastifyServerFactoryHandler<RawServer>, opts: Record<string, unknown>): RawServer;
19
+ }