fastify 4.24.3 → 4.25.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 (59) hide show
  1. package/README.md +10 -5
  2. package/SECURITY.md +27 -0
  3. package/docs/Guides/Ecosystem.md +18 -6
  4. package/docs/Guides/Getting-Started.md +3 -3
  5. package/docs/Guides/Plugins-Guide.md +2 -2
  6. package/docs/Guides/Style-Guide.md +7 -7
  7. package/docs/Reference/ContentTypeParser.md +2 -0
  8. package/docs/Reference/Errors.md +171 -397
  9. package/docs/Reference/Hooks.md +8 -2
  10. package/docs/Reference/Index.md +2 -0
  11. package/docs/Reference/Reply.md +16 -5
  12. package/docs/Reference/Request.md +1 -1
  13. package/docs/Reference/Routes.md +5 -5
  14. package/docs/Reference/TypeScript.md +1 -1
  15. package/docs/Reference/Warnings.md +77 -0
  16. package/fastify.js +37 -20
  17. package/lib/decorate.js +2 -2
  18. package/lib/errors.js +1 -1
  19. package/lib/hooks.js +1 -1
  20. package/lib/pluginUtils.js +10 -1
  21. package/lib/reply.js +5 -5
  22. package/lib/request.js +14 -7
  23. package/lib/route.js +9 -5
  24. package/lib/server.js +20 -27
  25. package/lib/validation.js +5 -5
  26. package/lib/warnings.js +110 -40
  27. package/package.json +3 -3
  28. package/test/close-pipelining.test.js +4 -4
  29. package/test/close.test.js +3 -3
  30. package/test/constrained-routes.test.js +24 -24
  31. package/test/decorator.test.js +27 -22
  32. package/test/default-route.test.js +7 -7
  33. package/test/fastify-instance.test.js +120 -0
  34. package/test/hooks.on-ready.test.js +16 -0
  35. package/test/hooks.test.js +1 -3
  36. package/test/http2/constraint.test.js +1 -1
  37. package/test/internals/errors.test.js +28 -3
  38. package/test/internals/reply.test.js +33 -9
  39. package/test/logger/instantiation.test.js +2 -1
  40. package/test/plugin.4.test.js +47 -0
  41. package/test/register.test.js +5 -5
  42. package/test/reply-trailers.test.js +1 -1
  43. package/test/route.7.test.js +7 -6
  44. package/test/schema-examples.test.js +2 -2
  45. package/test/schema-feature.test.js +11 -11
  46. package/test/server.test.js +51 -0
  47. package/test/types/hooks.test-d.ts +124 -1
  48. package/test/types/instance.test-d.ts +12 -0
  49. package/test/types/logger.test-d.ts +14 -0
  50. package/test/types/reply.test-d.ts +25 -6
  51. package/test/types/request.test-d.ts +3 -2
  52. package/test/types/route.test-d.ts +31 -0
  53. package/test/versioned-routes.test.js +7 -6
  54. package/types/hooks.d.ts +183 -0
  55. package/types/instance.d.ts +12 -12
  56. package/types/reply.d.ts +7 -10
  57. package/types/request.d.ts +2 -1
  58. package/types/route.d.ts +37 -26
  59. package/types/utils.d.ts +10 -0
@@ -22,12 +22,12 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
22
22
  expectType<number>(reply.statusCode)
23
23
  expectType<boolean>(reply.sent)
24
24
  expectType<((payload?: unknown) => FastifyReply)>(reply.send)
25
- expectType<(key: string, value: any) => FastifyReply>(reply.header)
26
- expectType<(values: {[key: string]: any}) => FastifyReply>(reply.headers)
27
- expectType<(key: string) => number | string | string[] | undefined>(reply.getHeader)
28
- expectType<() => { [key: string]: number | string | string[] | undefined }>(reply.getHeaders)
29
- expectType<(key: string) => FastifyReply>(reply.removeHeader)
30
- expectType<(key: string) => boolean>(reply.hasHeader)
25
+ expectAssignable<(key: string, value: any) => FastifyReply>(reply.header)
26
+ expectAssignable<(values: {[key: string]: any}) => FastifyReply>(reply.headers)
27
+ expectAssignable<(key: string) => number | string | string[] | undefined>(reply.getHeader)
28
+ expectAssignable<() => { [key: string]: number | string | string[] | undefined }>(reply.getHeaders)
29
+ expectAssignable<(key: string) => FastifyReply>(reply.removeHeader)
30
+ expectAssignable<(key: string) => boolean>(reply.hasHeader)
31
31
  expectType<{(statusCode: number, url: string): FastifyReply; (url: string): FastifyReply }>(reply.redirect)
32
32
  expectType<() => FastifyReply>(reply.hijack)
33
33
  expectType<() => void>(reply.callNotFound)
@@ -162,3 +162,22 @@ server.get<InvalidReplyHttpCodes>('get-invalid-http-codes-reply-error', async fu
162
162
  999: false
163
163
  })
164
164
  })
165
+
166
+ const httpHeaderHandler: RouteHandlerMethod = function (_request, reply) {
167
+ // accept is a header provided by @types/node
168
+ reply.getHeader('accept')
169
+ reply.getHeaders().accept // eslint-disable-line no-unused-expressions
170
+ reply.hasHeader('accept')
171
+ reply.header('accept', 'test')
172
+ reply.headers({ accept: 'test' })
173
+ reply.removeHeader('accept')
174
+
175
+ // x-fastify-test is not a header provided by @types/node
176
+ // and should not result in a typing error
177
+ reply.getHeader('x-fastify-test')
178
+ reply.getHeaders()['x-fastify-test'] // eslint-disable-line no-unused-expressions
179
+ reply.hasHeader('x-fastify-test')
180
+ reply.header('x-fastify-test', 'test')
181
+ reply.headers({ 'x-fastify-test': 'test' })
182
+ reply.removeHeader('x-fastify-test')
183
+ }
@@ -82,6 +82,7 @@ const getHandler: RouteHandler = function (request, _reply) {
82
82
  expectType<ContextConfigDefault & FastifyRouteConfig & FastifyContextConfig>(request.routeOptions.config)
83
83
  expectType<FastifySchema>(request.routeSchema)
84
84
  expectType<FastifySchema>(request.routeOptions.schema)
85
+ expectType<RouteHandlerMethod>(request.routeOptions.handler)
85
86
 
86
87
  expectType<RequestHeadersDefault & RawRequestDefaultExpression['headers']>(request.headers)
87
88
  request.headers = {}
@@ -121,7 +122,7 @@ function putHandler (request: CustomRequest, reply: FastifyReply) {
121
122
  expectType<RequestParams>(request.params)
122
123
  expectType<RequestHeaders & RawRequestDefaultExpression['headers']>(request.headers)
123
124
  expectType<RequestQuerystring>(request.query)
124
- if (typeof request.body === 'undefined') {
125
+ if (request.body === undefined) {
125
126
  expectType<undefined>(request.body)
126
127
  } else {
127
128
  expectType<string>(request.body.content)
@@ -149,7 +150,7 @@ const customLogger: CustomLoggerInterface = {
149
150
  trace: () => { },
150
151
  debug: () => { },
151
152
  foo: () => { }, // custom severity logger method
152
- child: () => customLogger as pino.Logger<never>
153
+ child: () => customLogger
153
154
  }
154
155
 
155
156
  const serverWithCustomLogger = fastify({ logger: customLogger })
@@ -422,6 +422,37 @@ expectType<boolean>(fastify().hasRoute({
422
422
  constraints: { version: '1.2.0' }
423
423
  }))
424
424
 
425
+ expectType<boolean>(fastify().hasRoute({
426
+ url: '/',
427
+ method: 'GET',
428
+ constraints: { host: 'auth.fastify.dev' }
429
+ }))
430
+
431
+ expectType<boolean>(fastify().hasRoute({
432
+ url: '/',
433
+ method: 'GET',
434
+ constraints: { host: /.*\.fastify\.dev$/ }
435
+ }))
436
+
437
+ expectType<boolean>(fastify().hasRoute({
438
+ url: '/',
439
+ method: 'GET',
440
+ constraints: { host: /.*\.fastify\.dev$/, version: '1.2.3' }
441
+ }))
442
+
443
+ expectType<boolean>(fastify().hasRoute({
444
+ url: '/',
445
+ method: 'GET',
446
+ constraints: {
447
+ // constraints value should accept any value
448
+ number: 12,
449
+ date: new Date(),
450
+ boolean: true,
451
+ function: () => {},
452
+ object: { foo: 'bar' }
453
+ }
454
+ }))
455
+
425
456
  expectType<FastifyInstance>(fastify().route({
426
457
  url: '/',
427
458
  method: 'get',
@@ -663,14 +663,15 @@ test('Vary header check (for documentation example)', t => {
663
663
  test('Should trigger a warning when a versioned route is registered via version option', t => {
664
664
  t.plan(4)
665
665
 
666
- function onWarning (code) {
667
- t.equal(code, 'FSTDEP008')
668
- }
669
- const warning = {
670
- emit: onWarning
666
+ function onWarning () {
667
+ t.pass('FSTDEP008 has been emitted')
671
668
  }
672
669
 
673
- const route = proxyquire('../lib/route', { './warnings': warning })
670
+ const route = proxyquire('../lib/route', {
671
+ './warnings': {
672
+ FSTDEP008: onWarning
673
+ }
674
+ })
674
675
  const fastify = proxyquire('..', { './lib/route.js': route })({ exposeHeadRoutes: false })
675
676
 
676
677
  fastify.route({
package/types/hooks.d.ts CHANGED
@@ -61,6 +61,24 @@ export interface onRequestAsyncHookHandler<
61
61
  ): Promise<unknown>;
62
62
  }
63
63
 
64
+ // helper type which infers whether onRequestHookHandler or onRequestAsyncHookHandler are
65
+ // applicable based on the specified return type.
66
+ export type onRequestMetaHookHandler<
67
+ RawServer extends RawServerBase = RawServerDefault,
68
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
69
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
70
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
71
+ ContextConfig = ContextConfigDefault,
72
+ SchemaCompiler extends FastifySchema = FastifySchema,
73
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
74
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
75
+ Return extends ReturnType<onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
76
+ | ReturnType<onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
77
+ = ReturnType<onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
78
+ > = Return extends ReturnType<onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
79
+ ? onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
80
+ : onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
81
+
64
82
  /**
65
83
  * `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.
66
84
  * Notice: in the `preParsing` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
@@ -102,6 +120,24 @@ export interface preParsingAsyncHookHandler<
102
120
  ): Promise<RequestPayload | unknown>;
103
121
  }
104
122
 
123
+ // helper type which infers whether preParsingHookHandler or preParsingAsyncHookHandler are
124
+ // applicable based on the specified return type.
125
+ export type preParsingMetaHookHandler<
126
+ RawServer extends RawServerBase = RawServerDefault,
127
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
128
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
129
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
130
+ ContextConfig = ContextConfigDefault,
131
+ SchemaCompiler extends FastifySchema = FastifySchema,
132
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
133
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
134
+ Return extends ReturnType<preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
135
+ | ReturnType<preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
136
+ = ReturnType<preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
137
+ > = Return extends ReturnType<preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
138
+ ? preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
139
+ : preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
140
+
105
141
  /**
106
142
  * `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
107
143
  */
@@ -140,6 +176,24 @@ export interface preValidationAsyncHookHandler<
140
176
  ): Promise<unknown>;
141
177
  }
142
178
 
179
+ // helper type which infers whether preValidationHookHandler or preValidationAsyncHookHandler are
180
+ // applicable based on the specified return type.
181
+ export type preValidationMetaHookHandler<
182
+ RawServer extends RawServerBase = RawServerDefault,
183
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
184
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
185
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
186
+ ContextConfig = ContextConfigDefault,
187
+ SchemaCompiler extends FastifySchema = FastifySchema,
188
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
189
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
190
+ Return extends ReturnType<preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
191
+ | ReturnType<preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
192
+ = ReturnType<preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
193
+ > = Return extends ReturnType<preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
194
+ ? preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
195
+ : preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
196
+
143
197
  /**
144
198
  * `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
145
199
  */
@@ -178,6 +232,24 @@ export interface preHandlerAsyncHookHandler<
178
232
  ): Promise<unknown>;
179
233
  }
180
234
 
235
+ // helper type which infers whether preHandlerHookHandler or preHandlerAsyncHookHandler are
236
+ // applicable based on the specified return type.
237
+ export type preHandlerMetaHookHandler<
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
+ SchemaCompiler extends FastifySchema = FastifySchema,
244
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
245
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
246
+ Return extends ReturnType<preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
247
+ | ReturnType<preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
248
+ = ReturnType<preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
249
+ > = Return extends ReturnType<preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
250
+ ? preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
251
+ : preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
252
+
181
253
  // This is used within the `preSerialization` and `onSend` hook handlers
182
254
  interface DoneFuncWithErrOrRes {
183
255
  (): void;
@@ -228,6 +300,25 @@ export interface preSerializationAsyncHookHandler<
228
300
  ): Promise<unknown>;
229
301
  }
230
302
 
303
+ // helper type which infers whether preSerializationHookHandler or preSerializationAsyncHookHandler are
304
+ // applicable based on the specified return type.
305
+ export type preSerializationMetaHookHandler<
306
+ PreSerializationPayload = unknown,
307
+ RawServer extends RawServerBase = RawServerDefault,
308
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
309
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
310
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
311
+ ContextConfig = ContextConfigDefault,
312
+ SchemaCompiler extends FastifySchema = FastifySchema,
313
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
314
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
315
+ Return extends ReturnType<preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
316
+ | ReturnType<preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
317
+ = ReturnType<preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
318
+ > = Return extends ReturnType<preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
319
+ ? preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
320
+ : preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
321
+
231
322
  /**
232
323
  * 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`.
233
324
  * Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null.
@@ -271,6 +362,25 @@ export interface onSendAsyncHookHandler<
271
362
  ): Promise<unknown>;
272
363
  }
273
364
 
365
+ // helper type which infers whether onSendHookHandler or onSendAsyncHookHandler are
366
+ // applicable based on the specified return type.
367
+ export type onSendMetaHookHandler<
368
+ OnSendPayload = unknown,
369
+ RawServer extends RawServerBase = RawServerDefault,
370
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
371
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
372
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
373
+ ContextConfig = ContextConfigDefault,
374
+ SchemaCompiler extends FastifySchema = FastifySchema,
375
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
376
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
377
+ Return extends ReturnType<onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
378
+ | ReturnType<onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
379
+ = ReturnType<onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
380
+ > = Return extends ReturnType<onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
381
+ ? onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
382
+ : onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
383
+
274
384
  /**
275
385
  * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook.
276
386
  * 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.
@@ -310,6 +420,24 @@ export interface onResponseAsyncHookHandler<
310
420
  ): Promise<unknown>;
311
421
  }
312
422
 
423
+ // helper type which infers whether onResponseHookHandler or onResponseAsyncHookHandler are
424
+ // applicable based on the specified return type.
425
+ export type onResponseMetaHookHandler<
426
+ RawServer extends RawServerBase = RawServerDefault,
427
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
428
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
429
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
430
+ ContextConfig = ContextConfigDefault,
431
+ SchemaCompiler extends FastifySchema = FastifySchema,
432
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
433
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
434
+ Return extends ReturnType<onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
435
+ | ReturnType<onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
436
+ = ReturnType<onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
437
+ > = Return extends ReturnType<onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
438
+ ? onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
439
+ : onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
440
+
313
441
  /**
314
442
  * `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)
315
443
  * 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.
@@ -349,6 +477,24 @@ export interface onTimeoutAsyncHookHandler<
349
477
  ): Promise<unknown>;
350
478
  }
351
479
 
480
+ // helper type which infers whether onTimeoutHookHandler or onTimeoutAsyncHookHandler are
481
+ // applicable based on the specified return type.
482
+ export type onTimeoutMetaHookHandler<
483
+ RawServer extends RawServerBase = RawServerDefault,
484
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
485
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
486
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
487
+ ContextConfig = ContextConfigDefault,
488
+ SchemaCompiler extends FastifySchema = FastifySchema,
489
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
490
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
491
+ Return extends ReturnType<onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
492
+ | ReturnType<onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
493
+ = ReturnType<onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
494
+ > = Return extends ReturnType<onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
495
+ ? onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
496
+ : onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
497
+
352
498
  /**
353
499
  * This hook is useful if you need to do some custom error logging or add some specific header in case of error.
354
500
  * It is not intended for changing the error, and calling reply.send will throw an exception.
@@ -394,6 +540,25 @@ export interface onErrorAsyncHookHandler<
394
540
  ): Promise<unknown>;
395
541
  }
396
542
 
543
+ // helper type which infers whether onErrorHookHandler or onErrorAsyncHookHandler are
544
+ // applicable based on the specified return type.
545
+ export type onErrorMetaHookHandler<
546
+ RawServer extends RawServerBase = RawServerDefault,
547
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
548
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
549
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
550
+ ContextConfig = ContextConfigDefault,
551
+ TError extends Error = FastifyError,
552
+ SchemaCompiler extends FastifySchema = FastifySchema,
553
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
554
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
555
+ Return extends ReturnType<onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
556
+ | ReturnType<onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
557
+ = ReturnType<onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
558
+ > = Return extends ReturnType<onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
559
+ ? onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>
560
+ : onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>
561
+
397
562
  /**
398
563
  * `onRequestAbort` is useful if you need to monitor the if the client aborts the request (if the `request.raw.aborted` property is set to `true`).
399
564
  * The `onRequestAbort` hook is executed when a client closes the connection before the entire request has been received. Therefore, you will not be able to send data to the client.
@@ -432,6 +597,24 @@ export interface onRequestAbortAsyncHookHandler<
432
597
  ): Promise<unknown>;
433
598
  }
434
599
 
600
+ // helper type which infers whether onRequestAbortHookHandler or onRequestAbortHookHandler are
601
+ // applicable based on the specified return type.
602
+ export type onRequestAbortMetaHookHandler<
603
+ RawServer extends RawServerBase = RawServerDefault,
604
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
605
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
606
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
607
+ ContextConfig = ContextConfigDefault,
608
+ SchemaCompiler extends FastifySchema = FastifySchema,
609
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
610
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
611
+ Return extends ReturnType<onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
612
+ | ReturnType<onRequestAbortAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
613
+ = ReturnType<onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
614
+ > = Return extends ReturnType<onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
615
+ ? onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
616
+ : onRequestAbortAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
617
+
435
618
  export type LifecycleHook = 'onRequest'
436
619
  | 'preParsing'
437
620
  | 'preValidation'
@@ -137,7 +137,7 @@ export interface FastifyInstance<
137
137
  getSchemas(): Record<string, unknown>;
138
138
 
139
139
  after(): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> & PromiseLike<undefined>;
140
- after(afterListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
140
+ after(afterListener: (err: Error | null) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
141
141
 
142
142
  close(): Promise<undefined>;
143
143
  close(closeListener: () => void): undefined;
@@ -190,7 +190,7 @@ export interface FastifyInstance<
190
190
  listen(port: number | string, address?: string, backlog?: number): Promise<string>;
191
191
 
192
192
  ready(): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> & PromiseLike<undefined>;
193
- ready(readyListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
193
+ ready(readyListener: (err: Error | null) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
194
194
 
195
195
  register: FastifyRegister<FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> & PromiseLike<undefined>>;
196
196
 
@@ -202,16 +202,16 @@ export interface FastifyInstance<
202
202
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
203
203
  ContextConfig = ContextConfigDefault,
204
204
  const SchemaCompiler extends FastifySchema = FastifySchema,
205
- >(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
206
-
207
- get: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
208
- head: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
209
- post: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
210
- put: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
211
- delete: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
212
- options: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
213
- patch: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
214
- all: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
205
+ >(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
206
+
207
+ get: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
208
+ head: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
209
+ post: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
210
+ put: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
211
+ delete: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
212
+ options: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
213
+ patch: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
214
+ all: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>;
215
215
 
216
216
  hasRoute<
217
217
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
package/types/reply.d.ts CHANGED
@@ -6,7 +6,7 @@ import { FastifyRequest } from './request'
6
6
  import { RouteGenericInterface } from './route'
7
7
  import { FastifySchema } from './schema'
8
8
  import { FastifyReplyType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyReplyType } from './type-provider'
9
- import { CodeToReplyKey, ContextConfigDefault, HttpKeys, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, ReplyDefault, ReplyKeysToCodes } from './utils'
9
+ import { CodeToReplyKey, ContextConfigDefault, HttpKeys, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, ReplyDefault, ReplyKeysToCodes, HttpHeader } from './utils'
10
10
 
11
11
  export interface ReplyGenericInterface {
12
12
  Reply?: ReplyDefault;
@@ -48,15 +48,12 @@ export interface FastifyReply<
48
48
  statusCode: number;
49
49
  sent: boolean;
50
50
  send(payload?: ReplyType): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
51
- header(key: string, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
52
- headers(values: {[key: string]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
53
- getHeader(key: string): number | string | string[] | undefined;
54
- getHeaders(): {
55
- // Node's `getHeaders()` can return numbers and arrays, so they're included here as possible types.
56
- [key: string]: number | string | string[] | undefined;
57
- };
58
- removeHeader(key: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
59
- hasHeader(key: string): boolean;
51
+ header(key: HttpHeader, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
52
+ headers(values: Partial<Record<HttpHeader, number | string | string[] | undefined>>): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
53
+ getHeader(key: HttpHeader): number | string | string[] | undefined;
54
+ getHeaders(): Record<HttpHeader, number | string | string[] | undefined>;
55
+ removeHeader(key: HttpHeader): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
56
+ hasHeader(key: HttpHeader): boolean;
60
57
  // Note: should consider refactoring the argument order for redirect. statusCode is optional so it should be after the required url param
61
58
  redirect(statusCode: number, url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
62
59
  redirect(url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
@@ -2,7 +2,7 @@ import { ErrorObject } from '@fastify/ajv-compiler'
2
2
  import { FastifyRequestContext, FastifyContextConfig } from './context'
3
3
  import { FastifyInstance } from './instance'
4
4
  import { FastifyBaseLogger } from './logger'
5
- import { RouteGenericInterface, FastifyRouteConfig } from './route'
5
+ import { RouteGenericInterface, FastifyRouteConfig, RouteHandlerMethod } from './route'
6
6
  import { FastifySchema } from './schema'
7
7
  import { FastifyRequestType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyRequestType } from './type-provider'
8
8
  import { ContextConfigDefault, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from './utils'
@@ -31,6 +31,7 @@ export interface RequestRouteOptions<ContextConfig = ContextConfigDefault, Schem
31
31
  prefixTrailingSlash: string;
32
32
  config: FastifyContextConfig & FastifyRouteConfig & ContextConfig;
33
33
  schema: SchemaCompiler;
34
+ handler: RouteHandlerMethod;
34
35
  }
35
36
 
36
37
  /**
package/types/route.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { FastifyError } from '@fastify/error'
2
+ import { ConstraintStrategy } from 'find-my-way'
2
3
  import { FastifyRequestContext } from './context'
3
- import { onErrorHookHandler, onRequestAbortHookHandler, onRequestHookHandler, onResponseHookHandler, onSendHookHandler, onTimeoutHookHandler, preHandlerHookHandler, preParsingHookHandler, preSerializationHookHandler, preValidationHookHandler } from './hooks'
4
+ import { onErrorMetaHookHandler, onRequestAbortMetaHookHandler, onRequestMetaHookHandler, onResponseMetaHookHandler, onSendMetaHookHandler, onTimeoutMetaHookHandler, preHandlerMetaHookHandler, preParsingMetaHookHandler, preSerializationMetaHookHandler, preValidationMetaHookHandler } from './hooks'
4
5
  import { FastifyInstance } from './instance'
5
6
  import { FastifyBaseLogger, FastifyChildLoggerFactory, LogLevel } from './logger'
6
7
  import { FastifyReply, ReplyGenericInterface } from './reply'
@@ -20,6 +21,16 @@ export interface FastifyRouteConfig {
20
21
 
21
22
  export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface {}
22
23
 
24
+ export type RouteConstraintType = Omit<ConstraintStrategy<any>, 'deriveConstraint'> & {
25
+ deriveConstraint<Context>(req: RawRequestDefaultExpression<RawServerDefault>, ctx?: Context, done?: (err: Error, ...args: any) => any): any,
26
+ }
27
+
28
+ export interface RouteConstraint {
29
+ version?: string
30
+ host?: RegExp | string
31
+ [name: string]: unknown
32
+ }
33
+
23
34
  /**
24
35
  * Route shorthand options for the various shorthand methods
25
36
  */
@@ -43,7 +54,7 @@ export interface RouteShorthandOptions<
43
54
  logLevel?: LogLevel;
44
55
  config?: Omit<FastifyRequestContext<ContextConfig>['config'], 'url' | 'method'>;
45
56
  version?: string;
46
- constraints?: { [name: string]: any },
57
+ constraints?: RouteConstraint,
47
58
  prefixTrailingSlash?: 'slash'|'no-slash'|'both';
48
59
  errorHandler?: (
49
60
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -55,28 +66,27 @@ export interface RouteShorthandOptions<
55
66
  schemaErrorFormatter?: SchemaErrorFormatter;
56
67
 
57
68
  // hooks
58
- onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
59
- | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
60
- preParsing?: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
61
- | preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
62
- preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
63
- | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
64
- preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
65
- | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
66
- preSerialization?: preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
67
- | preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
68
- onSend?: onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
69
- | onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
70
- onResponse?: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
71
- | onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
72
- onTimeout?: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
73
- | onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
74
- onError?: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>
75
- | onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>[];
76
- onRequestAbort?: onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
77
- | onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
69
+ onRequest?: onRequestMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
70
+ | onRequestMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
71
+ preParsing?: preParsingMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
72
+ | preParsingMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
73
+ preValidation?: preValidationMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
74
+ | preValidationMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
75
+ preHandler?: preHandlerMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
76
+ | preHandlerMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
77
+ preSerialization?: preSerializationMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
78
+ | preSerializationMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
79
+ onSend?: onSendMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
80
+ | onSendMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
81
+ onResponse?: onResponseMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
82
+ | onResponseMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
83
+ onTimeout?: onTimeoutMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
84
+ | onTimeoutMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
85
+ onError?: onErrorMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>
86
+ | onErrorMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>[];
87
+ onRequestAbort?: onRequestAbortMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
88
+ | onRequestAbortMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
78
89
  }
79
-
80
90
  /**
81
91
  * Route handler method declaration.
82
92
  */
@@ -120,17 +130,18 @@ export interface RouteShorthandMethod<
120
130
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
121
131
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
122
132
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
133
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
123
134
  > {
124
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyBaseLogger = FastifyBaseLogger>(
135
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema>(
125
136
  path: string,
126
137
  opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>,
127
138
  handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
128
139
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
129
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyBaseLogger = FastifyBaseLogger>(
140
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema>(
130
141
  path: string,
131
142
  handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
132
143
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
133
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyBaseLogger = FastifyBaseLogger>(
144
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema>(
134
145
  path: string,
135
146
  opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
136
147
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
package/types/utils.d.ts CHANGED
@@ -78,3 +78,13 @@ export type RecordKeysToLowercase<Input> = Input extends Record<string, unknown>
78
78
  ]: Input[Key];
79
79
  }
80
80
  : Input;
81
+
82
+ type OmitIndexSignature<T> = {
83
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
84
+ };
85
+
86
+ /**
87
+ * HTTP header strings
88
+ * Use this type only for input values, not for output values.
89
+ */
90
+ export type HttpHeader = keyof OmitIndexSignature<http.OutgoingHttpHeaders> | (string & Record<never, never>);