fastify 3.5.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 (84) hide show
  1. package/docs/ContentTypeParser.md +3 -0
  2. package/docs/Ecosystem.md +7 -2
  3. package/docs/Hooks.md +0 -5
  4. package/docs/Lifecycle.md +2 -2
  5. package/docs/Logging.md +1 -1
  6. package/docs/Recommendations.md +17 -0
  7. package/docs/Reply.md +2 -2
  8. package/docs/Request.md +4 -2
  9. package/docs/Routes.md +21 -3
  10. package/docs/Server.md +51 -3
  11. package/docs/Style-Guide.md +180 -0
  12. package/docs/TypeScript.md +359 -359
  13. package/docs/Validation-and-Serialization.md +11 -5
  14. package/examples/parser.js +1 -1
  15. package/fastify.d.ts +2 -2
  16. package/fastify.js +39 -7
  17. package/lib/contentTypeParser.js +16 -11
  18. package/lib/context.js +5 -19
  19. package/lib/decorate.js +1 -1
  20. package/lib/errors.js +2 -2
  21. package/lib/fourOhFour.js +8 -7
  22. package/lib/handleRequest.js +5 -5
  23. package/lib/hooks.js +4 -4
  24. package/lib/logger.js +7 -7
  25. package/lib/pluginUtils.js +4 -2
  26. package/lib/reply.js +27 -24
  27. package/lib/reqIdGenFactory.js +2 -2
  28. package/lib/request.js +20 -8
  29. package/lib/route.js +12 -10
  30. package/lib/schemas.js +1 -1
  31. package/lib/server.js +5 -5
  32. package/lib/symbols.js +2 -1
  33. package/lib/validation.js +9 -8
  34. package/lib/warnings.js +3 -0
  35. package/lib/wrapThenable.js +2 -2
  36. package/package.json +10 -10
  37. package/test/404s.test.js +15 -15
  38. package/test/async-await.test.js +7 -7
  39. package/test/custom-parser-async.test.js +2 -2
  40. package/test/custom-parser.test.js +38 -8
  41. package/test/emit-warning.test.js +31 -0
  42. package/test/fastify-instance.test.js +33 -1
  43. package/test/helper.js +1 -1
  44. package/test/hooks.test.js +6 -6
  45. package/test/http2/head.test.js +1 -1
  46. package/test/http2/plain.test.js +1 -1
  47. package/test/http2/secure-with-fallback.test.js +1 -1
  48. package/test/http2/secure.test.js +1 -1
  49. package/test/http2/unknown-http-method.test.js +1 -1
  50. package/test/https/https.test.js +2 -1
  51. package/test/inject.test.js +2 -2
  52. package/test/internals/all.test.js +1 -1
  53. package/test/internals/hookRunner.test.js +1 -1
  54. package/test/internals/logger.test.js +1 -1
  55. package/test/internals/reply.test.js +62 -7
  56. package/test/internals/request.test.js +31 -5
  57. package/test/internals/version.test.js +43 -0
  58. package/test/listen.test.js +12 -0
  59. package/test/logger.test.js +11 -11
  60. package/test/nullable-validation.test.js +108 -0
  61. package/test/pretty-print.test.js +9 -14
  62. package/test/reply-error.test.js +2 -2
  63. package/test/request-error.test.js +81 -0
  64. package/test/route-hooks.test.js +10 -10
  65. package/test/stream.test.js +11 -11
  66. package/test/types/fastify.test-d.ts +1 -2
  67. package/test/types/instance.test-d.ts +17 -3
  68. package/test/types/logger.test-d.ts +35 -2
  69. package/test/types/register.test-d.ts +16 -0
  70. package/test/types/request.test-d.ts +1 -1
  71. package/test/types/route.test-d.ts +5 -0
  72. package/test/validation-error-handling.test.js +66 -0
  73. package/test/versioned-routes.test.js +55 -0
  74. package/types/.eslintrc.json +4 -1
  75. package/types/content-type-parser.d.ts +0 -15
  76. package/types/hooks.d.ts +138 -16
  77. package/types/instance.d.ts +86 -14
  78. package/types/logger.d.ts +13 -11
  79. package/types/plugin.d.ts +5 -7
  80. package/types/register.d.ts +8 -7
  81. package/types/request.d.ts +3 -1
  82. package/types/route.d.ts +38 -58
  83. package/types/schema.d.ts +4 -4
  84. package/types/serverFactory.d.ts +9 -9
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
+ }