fastify 4.5.3 → 4.7.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 (60) hide show
  1. package/docs/Guides/Ecosystem.md +42 -16
  2. package/docs/Guides/Migration-Guide-V4.md +97 -48
  3. package/docs/Guides/Plugins-Guide.md +44 -0
  4. package/docs/Guides/Write-Plugin.md +1 -1
  5. package/docs/Reference/Logging.md +16 -13
  6. package/docs/Reference/Reply.md +5 -5
  7. package/docs/Reference/Request.md +19 -6
  8. package/docs/Reference/Routes.md +2 -2
  9. package/docs/Reference/Server.md +24 -2
  10. package/docs/Reference/Type-Providers.md +5 -19
  11. package/docs/Reference/TypeScript.md +7 -14
  12. package/examples/typescript-server.ts +1 -1
  13. package/fastify.d.ts +2 -2
  14. package/fastify.js +4 -1
  15. package/lib/contentTypeParser.js +10 -9
  16. package/lib/context.js +27 -3
  17. package/lib/error-handler.js +7 -3
  18. package/lib/error-serializer.js +13 -40
  19. package/lib/handleRequest.js +15 -13
  20. package/lib/reply.js +42 -34
  21. package/lib/request.js +36 -18
  22. package/lib/route.js +25 -10
  23. package/lib/schema-controller.js +7 -1
  24. package/lib/schemas.js +1 -0
  25. package/lib/server.js +6 -1
  26. package/lib/symbols.js +2 -0
  27. package/lib/validation.js +4 -3
  28. package/lib/warnings.js +2 -0
  29. package/package.json +26 -26
  30. package/test/404s.test.js +19 -1
  31. package/test/context-config.test.js +2 -1
  32. package/test/handler-context.test.js +12 -30
  33. package/test/has-route.test.js +77 -0
  34. package/test/internals/contentTypeParser.test.js +3 -3
  35. package/test/internals/handleRequest.test.js +4 -3
  36. package/test/internals/reply-serialize.test.js +9 -9
  37. package/test/internals/reply.test.js +10 -9
  38. package/test/internals/request-validate.test.js +97 -9
  39. package/test/internals/request.test.js +57 -3
  40. package/test/internals/validation.test.js +15 -0
  41. package/test/listen.deprecated.test.js +33 -0
  42. package/test/plugin.test.js +1 -1
  43. package/test/reply-code.test.js +59 -0
  44. package/test/route.test.js +29 -0
  45. package/test/router-options.test.js +33 -66
  46. package/test/schema-feature.test.js +25 -0
  47. package/test/schema-special-usage.test.js +29 -0
  48. package/test/schema-validation.test.js +19 -0
  49. package/test/search.test.js +169 -30
  50. package/test/server.test.js +54 -0
  51. package/test/types/fastify.test-d.ts +9 -0
  52. package/test/types/route.test-d.ts +11 -0
  53. package/test/unsupported-httpversion.test.js +0 -26
  54. package/types/hooks.d.ts +25 -25
  55. package/types/instance.d.ts +27 -21
  56. package/types/logger.d.ts +1 -1
  57. package/types/plugin.d.ts +3 -3
  58. package/types/reply.d.ts +2 -2
  59. package/types/request.d.ts +11 -5
  60. package/types/route.d.ts +9 -9
@@ -0,0 +1,54 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const test = t.test
5
+ const Fastify = require('..')
6
+
7
+ test('listen should accept null port', t => {
8
+ t.plan(1)
9
+
10
+ const fastify = Fastify()
11
+ t.teardown(fastify.close.bind(fastify))
12
+ fastify.listen({ port: null }, (err) => {
13
+ t.error(err)
14
+ })
15
+ })
16
+
17
+ test('listen should accept undefined port', t => {
18
+ t.plan(1)
19
+
20
+ const fastify = Fastify()
21
+ t.teardown(fastify.close.bind(fastify))
22
+ fastify.listen({ port: undefined }, (err) => {
23
+ t.error(err)
24
+ })
25
+ })
26
+
27
+ test('listen should accept stringified number port', t => {
28
+ t.plan(1)
29
+
30
+ const fastify = Fastify()
31
+ t.teardown(fastify.close.bind(fastify))
32
+ fastify.listen({ port: '1234' }, (err) => {
33
+ t.error(err)
34
+ })
35
+ })
36
+
37
+ test('listen should reject string port', async (t) => {
38
+ t.plan(2)
39
+
40
+ const fastify = Fastify()
41
+ t.teardown(fastify.close.bind(fastify))
42
+
43
+ try {
44
+ await fastify.listen({ port: 'hello-world' })
45
+ } catch (error) {
46
+ t.same(error.message, 'options.port should be >= 0 and < 65536. Received hello-world.')
47
+ }
48
+
49
+ try {
50
+ await fastify.listen({ port: '1234hello' })
51
+ } catch (error) {
52
+ t.same(error.message, 'options.port should be >= 0 and < 65536. Received 1234hello.')
53
+ }
54
+ })
@@ -8,6 +8,7 @@ import fastify, {
8
8
  LightMyRequestResponse,
9
9
  LightMyRequestCallback,
10
10
  InjectOptions, FastifyBaseLogger,
11
+ RouteGenericInterface,
11
12
  ValidationResult
12
13
  } from '../../fastify'
13
14
  import { ErrorObject as AjvErrorObject } from 'ajv'
@@ -24,6 +25,7 @@ expectType<FastifyInstance<http.Server, http.IncomingMessage, http.ServerRespons
24
25
  expectType<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> & PromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({}))
25
26
  // https server
26
27
  expectType<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & PromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({ https: {} }))
28
+ expectType<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & PromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(fastify({ https: null }))
27
29
  // http2 server
28
30
  expectType<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse> & PromiseLike<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>>>(fastify({ http2: true, http2SessionTimeout: 1000 }))
29
31
  expectType<FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse> & PromiseLike<FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse>>>(fastify({ http2: true, https: {}, http2SessionTimeout: 1000 }))
@@ -231,3 +233,10 @@ const ajvErrorObject: AjvErrorObject = {
231
233
  message: ''
232
234
  }
233
235
  expectAssignable<ValidationResult>(ajvErrorObject)
236
+
237
+ const routeGeneric: RouteGenericInterface = {}
238
+ expectType<unknown>(routeGeneric.Body)
239
+ expectType<unknown>(routeGeneric.Headers)
240
+ expectType<unknown>(routeGeneric.Params)
241
+ expectType<unknown>(routeGeneric.Querystring)
242
+ expectType<unknown>(routeGeneric.Reply)
@@ -230,3 +230,14 @@ expectType<FastifyInstance>(fastify().route({
230
230
  method: 'GET',
231
231
  handler: routeHandlerWithReturnValue
232
232
  }))
233
+
234
+ expectType<boolean>(fastify().hasRoute({
235
+ url: '/',
236
+ method: 'GET'
237
+ }))
238
+
239
+ expectType<boolean>(fastify().hasRoute({
240
+ url: '/',
241
+ method: 'GET',
242
+ constraints: { version: '1.2.0' }
243
+ }))
@@ -4,32 +4,6 @@ const net = require('net')
4
4
  const t = require('tap')
5
5
  const Fastify = require('../fastify')
6
6
 
7
- t.test('Will return 505 HTTP error if HTTP version (default) is not supported', t => {
8
- const fastify = Fastify()
9
-
10
- t.teardown(fastify.close.bind(fastify))
11
-
12
- fastify.get('/', (req, reply) => {
13
- reply.send({ hello: 'world' })
14
- })
15
-
16
- fastify.listen({ port: 0 }, err => {
17
- t.error(err)
18
-
19
- const port = fastify.server.address().port
20
- const client = net.createConnection({ port }, () => {
21
- client.write('GET / HTTP/5.1\r\n\r\n')
22
-
23
- client.once('data', data => {
24
- t.match(data.toString(), /505 HTTP Version Not Supported/i)
25
- client.end(() => {
26
- t.end()
27
- })
28
- })
29
- })
30
- })
31
- })
32
-
33
7
  t.test('Will return 505 HTTP error if HTTP version (2.0 when server is 1.1) is not supported', t => {
34
8
  const fastify = Fastify()
35
9
 
package/types/hooks.d.ts CHANGED
@@ -5,7 +5,7 @@ import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyD
5
5
  import { FastifyRequest } from './request'
6
6
  import { FastifyReply } from './reply'
7
7
  import { FastifyError } from '@fastify/error'
8
- import { FastifyLoggerInstance } from './logger'
8
+ import { FastifyBaseLogger } from './logger'
9
9
  import {
10
10
  FastifyTypeProvider,
11
11
  FastifyTypeProviderDefault
@@ -34,7 +34,7 @@ export interface onRequestHookHandler<
34
34
  ContextConfig = ContextConfigDefault,
35
35
  SchemaCompiler extends FastifySchema = FastifySchema,
36
36
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
37
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
37
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
38
38
  > {
39
39
  (
40
40
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -52,7 +52,7 @@ export interface onRequestAsyncHookHandler<
52
52
  ContextConfig = ContextConfigDefault,
53
53
  SchemaCompiler extends FastifySchema = FastifySchema,
54
54
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
55
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
55
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
56
56
  > {
57
57
  (
58
58
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -73,7 +73,7 @@ export interface preParsingHookHandler<
73
73
  ContextConfig = ContextConfigDefault,
74
74
  SchemaCompiler extends FastifySchema = FastifySchema,
75
75
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
76
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
76
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
77
77
  > {
78
78
  (
79
79
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -92,7 +92,7 @@ export interface preParsingAsyncHookHandler<
92
92
  ContextConfig = ContextConfigDefault,
93
93
  SchemaCompiler extends FastifySchema = FastifySchema,
94
94
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
95
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
95
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
96
96
  > {
97
97
  (
98
98
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -113,7 +113,7 @@ export interface preValidationHookHandler<
113
113
  ContextConfig = ContextConfigDefault,
114
114
  SchemaCompiler extends FastifySchema = FastifySchema,
115
115
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
116
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
116
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
117
117
  > {
118
118
  (
119
119
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -131,7 +131,7 @@ export interface preValidationAsyncHookHandler<
131
131
  ContextConfig = ContextConfigDefault,
132
132
  SchemaCompiler extends FastifySchema = FastifySchema,
133
133
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
134
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
134
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
135
135
  > {
136
136
  (
137
137
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -151,7 +151,7 @@ export interface preHandlerHookHandler<
151
151
  ContextConfig = ContextConfigDefault,
152
152
  SchemaCompiler extends FastifySchema = FastifySchema,
153
153
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
154
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
154
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
155
155
  > {
156
156
  (
157
157
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -169,7 +169,7 @@ export interface preHandlerAsyncHookHandler<
169
169
  ContextConfig = ContextConfigDefault,
170
170
  SchemaCompiler extends FastifySchema = FastifySchema,
171
171
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
172
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
172
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
173
173
  > {
174
174
  (
175
175
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -198,7 +198,7 @@ export interface preSerializationHookHandler<
198
198
  ContextConfig = ContextConfigDefault,
199
199
  SchemaCompiler extends FastifySchema = FastifySchema,
200
200
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
201
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
201
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
202
202
  > {
203
203
  (
204
204
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -218,7 +218,7 @@ export interface preSerializationAsyncHookHandler<
218
218
  ContextConfig = ContextConfigDefault,
219
219
  SchemaCompiler extends FastifySchema = FastifySchema,
220
220
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
221
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
221
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
222
222
  > {
223
223
  (
224
224
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -241,7 +241,7 @@ export interface onSendHookHandler<
241
241
  ContextConfig = ContextConfigDefault,
242
242
  SchemaCompiler extends FastifySchema = FastifySchema,
243
243
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
244
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
244
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
245
245
  > {
246
246
  (
247
247
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -261,7 +261,7 @@ export interface onSendAsyncHookHandler<
261
261
  ContextConfig = ContextConfigDefault,
262
262
  SchemaCompiler extends FastifySchema = FastifySchema,
263
263
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
264
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
264
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
265
265
  > {
266
266
  (
267
267
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -283,7 +283,7 @@ export interface onResponseHookHandler<
283
283
  ContextConfig = ContextConfigDefault,
284
284
  SchemaCompiler extends FastifySchema = FastifySchema,
285
285
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
286
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
286
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
287
287
  > {
288
288
  (
289
289
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -301,7 +301,7 @@ export interface onResponseAsyncHookHandler<
301
301
  ContextConfig = ContextConfigDefault,
302
302
  SchemaCompiler extends FastifySchema = FastifySchema,
303
303
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
304
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
304
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
305
305
  > {
306
306
  (
307
307
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -322,7 +322,7 @@ export interface onTimeoutHookHandler<
322
322
  ContextConfig = ContextConfigDefault,
323
323
  SchemaCompiler extends FastifySchema = FastifySchema,
324
324
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
325
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
325
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
326
326
  > {
327
327
  (
328
328
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -340,7 +340,7 @@ export interface onTimeoutAsyncHookHandler<
340
340
  ContextConfig = ContextConfigDefault,
341
341
  SchemaCompiler extends FastifySchema = FastifySchema,
342
342
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
343
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
343
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
344
344
  > {
345
345
  (
346
346
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -364,7 +364,7 @@ export interface onErrorHookHandler<
364
364
  TError extends Error = FastifyError,
365
365
  SchemaCompiler extends FastifySchema = FastifySchema,
366
366
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
367
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
367
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
368
368
  > {
369
369
  (
370
370
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -384,7 +384,7 @@ export interface onErrorAsyncHookHandler<
384
384
  TError extends Error = FastifyError,
385
385
  SchemaCompiler extends FastifySchema = FastifySchema,
386
386
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
387
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
387
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
388
388
  > {
389
389
  (
390
390
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -407,7 +407,7 @@ export interface onRouteHookHandler<
407
407
  ContextConfig = ContextConfigDefault,
408
408
  SchemaCompiler extends FastifySchema = FastifySchema,
409
409
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
410
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
410
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
411
411
  > {
412
412
  (
413
413
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
@@ -424,7 +424,7 @@ export interface onRegisterHookHandler<
424
424
  RawServer extends RawServerBase = RawServerDefault,
425
425
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
426
426
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
427
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
427
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
428
428
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
429
429
  Options extends FastifyPluginOptions = FastifyPluginOptions
430
430
  > {
@@ -442,7 +442,7 @@ export interface onReadyHookHandler<
442
442
  RawServer extends RawServerBase = RawServerDefault,
443
443
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
444
444
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
445
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
445
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
446
446
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
447
447
  > {
448
448
  (
@@ -455,7 +455,7 @@ export interface onReadyAsyncHookHandler<
455
455
  RawServer extends RawServerBase = RawServerDefault,
456
456
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
457
457
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
458
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
458
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
459
459
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
460
460
  > {
461
461
  (
@@ -469,7 +469,7 @@ export interface onCloseHookHandler<
469
469
  RawServer extends RawServerBase = RawServerDefault,
470
470
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
471
471
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
472
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
472
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
473
473
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
474
474
  > {
475
475
  (
@@ -482,7 +482,7 @@ export interface onCloseAsyncHookHandler<
482
482
  RawServer extends RawServerBase = RawServerDefault,
483
483
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
484
484
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
485
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
485
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
486
486
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
487
487
  > {
488
488
  (
@@ -4,7 +4,7 @@ import * as http from 'http'
4
4
  import { CallbackFunc as LightMyRequestCallback, Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse } from 'light-my-request'
5
5
  import { AddContentTypeParser, ConstructorAction, FastifyBodyParser, getDefaultJsonParser, hasContentTypeParser, ProtoAction, removeAllContentTypeParsers, removeContentTypeParser } from './content-type-parser'
6
6
  import { onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler } from './hooks'
7
- import { FastifyLoggerInstance } from './logger'
7
+ import { FastifyBaseLogger } from './logger'
8
8
  import { FastifyRegister } from './register'
9
9
  import { FastifyReply } from './reply'
10
10
  import { FastifyRequest } from './request'
@@ -84,7 +84,7 @@ export interface FastifyInstance<
84
84
  RawServer extends RawServerBase = RawServerDefault,
85
85
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
86
86
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
87
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
87
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
88
88
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
89
89
  > {
90
90
  server: RawServer;
@@ -186,6 +186,12 @@ export interface FastifyInstance<
186
186
  patch: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
187
187
  all: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
188
188
 
189
+ hasRoute<
190
+ RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
191
+ ContextConfig = ContextConfigDefault,
192
+ SchemaCompiler extends FastifySchema = FastifySchema,
193
+ >(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'method' | 'url' | 'constraints'>): boolean;
194
+
189
195
  // addHook: overloads
190
196
 
191
197
  // Lifecycle addHooks
@@ -198,7 +204,7 @@ export interface FastifyInstance<
198
204
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
199
205
  ContextConfig = ContextConfigDefault,
200
206
  SchemaCompiler extends FastifySchema = FastifySchema,
201
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
207
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
202
208
  >(
203
209
  name: 'onRequest',
204
210
  hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -208,7 +214,7 @@ export interface FastifyInstance<
208
214
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
209
215
  ContextConfig = ContextConfigDefault,
210
216
  SchemaCompiler extends FastifySchema = FastifySchema,
211
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
217
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
212
218
  >(
213
219
  name: 'onRequest',
214
220
  hook: onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -222,7 +228,7 @@ export interface FastifyInstance<
222
228
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
223
229
  ContextConfig = ContextConfigDefault,
224
230
  SchemaCompiler extends FastifySchema = FastifySchema,
225
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
231
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
226
232
  >(
227
233
  name: 'preParsing',
228
234
  hook: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -232,7 +238,7 @@ export interface FastifyInstance<
232
238
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
233
239
  ContextConfig = ContextConfigDefault,
234
240
  SchemaCompiler extends FastifySchema = FastifySchema,
235
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
241
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
236
242
  >(
237
243
  name: 'preParsing',
238
244
  hook: preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -245,7 +251,7 @@ export interface FastifyInstance<
245
251
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
246
252
  ContextConfig = ContextConfigDefault,
247
253
  SchemaCompiler extends FastifySchema = FastifySchema,
248
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
254
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
249
255
  >(
250
256
  name: 'preValidation',
251
257
  hook: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -255,7 +261,7 @@ export interface FastifyInstance<
255
261
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
256
262
  ContextConfig = ContextConfigDefault,
257
263
  SchemaCompiler extends FastifySchema = FastifySchema,
258
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
264
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
259
265
  >(
260
266
  name: 'preValidation',
261
267
  hook: preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -268,7 +274,7 @@ export interface FastifyInstance<
268
274
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
269
275
  ContextConfig = ContextConfigDefault,
270
276
  SchemaCompiler extends FastifySchema = FastifySchema,
271
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
277
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
272
278
  >(
273
279
  name: 'preHandler',
274
280
  hook: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -278,7 +284,7 @@ export interface FastifyInstance<
278
284
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
279
285
  ContextConfig = ContextConfigDefault,
280
286
  SchemaCompiler extends FastifySchema = FastifySchema,
281
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
287
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
282
288
  >(
283
289
  name: 'preHandler',
284
290
  hook: preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -293,7 +299,7 @@ export interface FastifyInstance<
293
299
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
294
300
  ContextConfig = ContextConfigDefault,
295
301
  SchemaCompiler extends FastifySchema = FastifySchema,
296
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
302
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
297
303
  >(
298
304
  name: 'preSerialization',
299
305
  hook: preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -304,7 +310,7 @@ export interface FastifyInstance<
304
310
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
305
311
  ContextConfig = ContextConfigDefault,
306
312
  SchemaCompiler extends FastifySchema = FastifySchema,
307
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
313
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
308
314
  >(
309
315
  name: 'preSerialization',
310
316
  hook: preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -319,7 +325,7 @@ export interface FastifyInstance<
319
325
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
320
326
  ContextConfig = ContextConfigDefault,
321
327
  SchemaCompiler extends FastifySchema = FastifySchema,
322
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
328
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
323
329
  >(
324
330
  name: 'onSend',
325
331
  hook: onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -330,7 +336,7 @@ export interface FastifyInstance<
330
336
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
331
337
  ContextConfig = ContextConfigDefault,
332
338
  SchemaCompiler extends FastifySchema = FastifySchema,
333
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
339
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
334
340
  >(
335
341
  name: 'onSend',
336
342
  hook: onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -344,7 +350,7 @@ export interface FastifyInstance<
344
350
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
345
351
  ContextConfig = ContextConfigDefault,
346
352
  SchemaCompiler extends FastifySchema = FastifySchema,
347
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
353
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
348
354
  >(
349
355
  name: 'onResponse',
350
356
  hook: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -354,7 +360,7 @@ export interface FastifyInstance<
354
360
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
355
361
  ContextConfig = ContextConfigDefault,
356
362
  SchemaCompiler extends FastifySchema = FastifySchema,
357
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
363
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
358
364
  >(
359
365
  name: 'onResponse',
360
366
  hook: onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -368,7 +374,7 @@ export interface FastifyInstance<
368
374
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
369
375
  ContextConfig = ContextConfigDefault,
370
376
  SchemaCompiler extends FastifySchema = FastifySchema,
371
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
377
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
372
378
  >(
373
379
  name: 'onTimeout',
374
380
  hook: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -378,7 +384,7 @@ export interface FastifyInstance<
378
384
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
379
385
  ContextConfig = ContextConfigDefault,
380
386
  SchemaCompiler extends FastifySchema = FastifySchema,
381
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
387
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
382
388
  >(
383
389
  name: 'onTimeout',
384
390
  hook: onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
@@ -394,7 +400,7 @@ export interface FastifyInstance<
394
400
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
395
401
  ContextConfig = ContextConfigDefault,
396
402
  SchemaCompiler extends FastifySchema = FastifySchema,
397
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
403
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
398
404
  >(
399
405
  name: 'onError',
400
406
  hook: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>
@@ -404,7 +410,7 @@ export interface FastifyInstance<
404
410
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
405
411
  ContextConfig = ContextConfigDefault,
406
412
  SchemaCompiler extends FastifySchema = FastifySchema,
407
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
413
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
408
414
  >(
409
415
  name: 'onError',
410
416
  hook: onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>
@@ -419,7 +425,7 @@ export interface FastifyInstance<
419
425
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
420
426
  ContextConfig = ContextConfigDefault,
421
427
  SchemaCompiler extends FastifySchema = FastifySchema,
422
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
428
+ Logger extends FastifyBaseLogger = FastifyBaseLogger
423
429
  >(
424
430
  name: 'onRoute',
425
431
  hook: onRouteHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
package/types/logger.d.ts CHANGED
@@ -23,7 +23,7 @@ export type FastifyBaseLogger = pino.BaseLogger & {
23
23
  child(bindings: Bindings, options?: ChildLoggerOptions): FastifyBaseLogger
24
24
  }
25
25
 
26
- // TODO delete FastifyLoggerInstance in the next major release. It seems that it is enough to have only FastifyBaseLogger.
26
+ // TODO delete FastifyBaseLogger in the next major release. It seems that it is enough to have only FastifyBaseLogger.
27
27
  /**
28
28
  * @deprecated Use FastifyBaseLogger instead
29
29
  */
package/types/plugin.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { FastifyInstance } from './instance'
2
2
  import { RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault } from './utils'
3
3
  import { FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
4
- import { FastifyLoggerInstance } from './logger'
4
+ import { FastifyBaseLogger } from './logger'
5
5
 
6
6
  export type FastifyPluginOptions = Record<string, any>
7
7
 
@@ -11,7 +11,7 @@ export type FastifyPluginOptions = Record<string, any>
11
11
  * 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.
12
12
  */
13
13
  export type FastifyPluginCallback<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault> = (
14
- instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>, FastifyLoggerInstance, TypeProvider>,
14
+ instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>, FastifyBaseLogger, TypeProvider>,
15
15
  opts: Options,
16
16
  done: (err?: Error) => void
17
17
  ) => void
@@ -26,7 +26,7 @@ export type FastifyPluginAsync<
26
26
  Server extends RawServerBase = RawServerDefault,
27
27
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
28
28
  > = (
29
- instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>, FastifyLoggerInstance, TypeProvider>,
29
+ instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>, FastifyBaseLogger, TypeProvider>,
30
30
  opts: Options
31
31
  ) => Promise<void>;
32
32
 
package/types/reply.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { RawReplyDefaultExpression, RawServerBase, RawServerDefault, ContextConfigDefault, RawRequestDefaultExpression, ReplyDefault } from './utils'
2
2
  import { FastifyReplyType, ResolveFastifyReplyType, FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
3
3
  import { FastifyContext } from './context'
4
- import { FastifyLoggerInstance } from './logger'
4
+ import { FastifyBaseLogger } from './logger'
5
5
  import { FastifyRequest } from './request'
6
6
  import { RouteGenericInterface } from './route'
7
7
  import { FastifyInstance } from './instance'
@@ -28,7 +28,7 @@ export interface FastifyReply<
28
28
  > {
29
29
  raw: RawReply;
30
30
  context: FastifyContext<ContextConfig>;
31
- log: FastifyLoggerInstance;
31
+ log: FastifyBaseLogger;
32
32
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>;
33
33
  server: FastifyInstance;
34
34
  code(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
@@ -1,4 +1,5 @@
1
- import { FastifyLoggerInstance } from './logger'
1
+ import { ErrorObject } from '@fastify/ajv-compiler'
2
+ import { FastifyBaseLogger } from './logger'
2
3
  import { ContextConfigDefault, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './utils'
3
4
  import { RouteGenericInterface } from './route'
4
5
  import { FastifyInstance } from './instance'
@@ -14,6 +15,11 @@ export interface RequestGenericInterface {
14
15
  Headers?: RequestHeadersDefault;
15
16
  }
16
17
 
18
+ export interface ValidationFunction {
19
+ (input: any): boolean
20
+ errors?: null | ErrorObject[];
21
+ }
22
+
17
23
  /**
18
24
  * FastifyRequest is an instance of the standard http or http2 request objects.
19
25
  * It defaults to http.IncomingMessage, and it also extends the relative request object.
@@ -24,7 +30,7 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
24
30
  SchemaCompiler extends FastifySchema = FastifySchema,
25
31
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
26
32
  ContextConfig = ContextConfigDefault,
27
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
33
+ Logger extends FastifyBaseLogger = FastifyBaseLogger,
28
34
  RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>
29
35
  // ^ Temporary Note: RequestType has been re-ordered to be the last argument in
30
36
  // generic list. This generic argument is now considered optional as it can be
@@ -61,9 +67,9 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
61
67
  readonly is404: boolean;
62
68
  readonly socket: RawRequest['socket'];
63
69
 
64
- getValidationFunction(httpPart: HTTPRequestPart): (input: any) => boolean
65
- getValidationFunction(schema: {[key: string]: any}): (input: any) => boolean
66
- compileValidationSchema(schema: {[key: string]: any}, httpPart?: HTTPRequestPart): (input: any) => boolean
70
+ getValidationFunction(httpPart: HTTPRequestPart): ValidationFunction
71
+ getValidationFunction(schema: {[key: string]: any}): ValidationFunction
72
+ compileValidationSchema(schema: {[key: string]: any}, httpPart?: HTTPRequestPart): ValidationFunction
67
73
  validateInput(input: any, schema: {[key: string]: any}, httpPart?: HTTPRequestPart): boolean
68
74
  validateInput(input: any, httpPart?: HTTPRequestPart): boolean
69
75