fastify 4.0.0-rc.3 → 4.0.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 (48) hide show
  1. package/README.md +2 -1
  2. package/build/build-validation.js +14 -2
  3. package/docs/Guides/Ecosystem.md +23 -11
  4. package/docs/Guides/Index.md +1 -1
  5. package/docs/Guides/Migration-Guide-V3.md +1 -1
  6. package/docs/Guides/Plugins-Guide.md +3 -3
  7. package/docs/Guides/Prototype-Poisoning.md +1 -1
  8. package/docs/Guides/Recommendations.md +2 -2
  9. package/docs/Guides/Serverless.md +5 -5
  10. package/docs/Guides/Testing.md +3 -1
  11. package/docs/Guides/Write-Plugin.md +3 -3
  12. package/docs/Migration-Guide-V4.md +1 -1
  13. package/docs/Reference/Logging.md +10 -5
  14. package/docs/Reference/Middleware.md +3 -3
  15. package/docs/Reference/Routes.md +2 -2
  16. package/docs/Reference/Server.md +59 -10
  17. package/docs/Reference/TypeScript.md +2 -2
  18. package/docs/Reference/Validation-and-Serialization.md +11 -1
  19. package/fastify.d.ts +2 -1
  20. package/fastify.js +38 -14
  21. package/lib/configValidator.js +456 -332
  22. package/lib/errors.js +4 -0
  23. package/lib/request.js +2 -2
  24. package/lib/route.js +5 -2
  25. package/lib/schemas.js +3 -0
  26. package/lib/validation.js +8 -2
  27. package/package.json +34 -34
  28. package/test/close-pipelining.test.js +4 -2
  29. package/test/close.test.js +93 -3
  30. package/test/custom-http-server.test.js +22 -0
  31. package/test/decorator.test.js +146 -0
  32. package/test/internals/initialConfig.test.js +5 -3
  33. package/test/output-validation.test.js +6 -2
  34. package/test/plugin.test.js +177 -14
  35. package/test/route-prefix.test.js +250 -0
  36. package/test/router-options.test.js +61 -0
  37. package/test/schema-feature.test.js +24 -0
  38. package/test/schema-serialization.test.js +57 -0
  39. package/test/types/fastify.test-d.ts +1 -0
  40. package/test/types/instance.test-d.ts +1 -0
  41. package/test/types/logger.test-d.ts +13 -1
  42. package/test/types/route.test-d.ts +14 -0
  43. package/test/types/type-provider.test-d.ts +6 -0
  44. package/types/instance.d.ts +1 -0
  45. package/types/logger.d.ts +3 -1
  46. package/types/route.d.ts +1 -1
  47. package/types/schema.d.ts +1 -1
  48. package/types/type-provider.d.ts +3 -4
@@ -261,6 +261,7 @@ type InitialConfig = Readonly<{
261
261
  http2?: boolean,
262
262
  https?: boolean | Readonly<{ allowHTTP1: boolean }>,
263
263
  ignoreTrailingSlash?: boolean,
264
+ ignoreDuplicateSlashes?: boolean,
264
265
  disableRequestLogging?: boolean,
265
266
  maxParamLength?: number,
266
267
  onProtoPoisoning?: 'error' | 'remove' | 'ignore',
@@ -1,4 +1,4 @@
1
- import { expectType } from 'tsd'
1
+ import { expectError, expectType } from 'tsd'
2
2
  import fastify, {
3
3
  FastifyLogFn,
4
4
  LogLevel,
@@ -212,3 +212,15 @@ const passPinoOption = fastify({
212
212
  })
213
213
 
214
214
  expectType<FastifyBaseLogger>(passPinoOption.log)
215
+
216
+ const childParent = fastify().log
217
+ // we test different option variant here
218
+ expectType<FastifyLoggerInstance>(childParent.child({}, { level: 'info' }))
219
+ expectType<FastifyLoggerInstance>(childParent.child({}, { redact: ['pass', 'pin'] }))
220
+ expectType<FastifyLoggerInstance>(childParent.child({}, { serializers: { key: () => {} } }))
221
+ expectType<FastifyLoggerInstance>(childParent.child({}, { level: 'info', redact: ['pass', 'pin'], serializers: { key: () => {} } }))
222
+
223
+ // no option pass
224
+ expectError(childParent.child())
225
+ // wrong option
226
+ expectError(childParent.child({}, { nonExist: true }))
@@ -27,6 +27,14 @@ const routeHandler: RouteHandlerMethod = function (request, reply) {
27
27
  expectType<FastifyReply>(reply)
28
28
  }
29
29
 
30
+ const routeHandlerWithReturnValue: RouteHandlerMethod = function (request, reply) {
31
+ expectType<FastifyInstance>(this)
32
+ expectType<FastifyRequest>(request)
33
+ expectType<FastifyReply>(reply)
34
+
35
+ return reply.send()
36
+ }
37
+
30
38
  type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options'
31
39
 
32
40
  ;['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS'].forEach(method => {
@@ -216,3 +224,9 @@ expectType<FastifyInstance>(fastify().route({
216
224
  expectError(fastify().route({
217
225
  prefixTrailingSlash: true // Not a valid value
218
226
  }))
227
+
228
+ expectType<FastifyInstance>(fastify().route({
229
+ url: '/',
230
+ method: 'GET',
231
+ handler: routeHandlerWithReturnValue
232
+ }))
@@ -13,6 +13,12 @@ import { RouteGenericInterface } from '../../types/route'
13
13
 
14
14
  const server = fastify()
15
15
 
16
+ // -------------------------------------------------------------------
17
+ // Default (unknown)
18
+ // -------------------------------------------------------------------
19
+
20
+ expectAssignable(server.get('/', (req) => expectType<unknown>(req.body)))
21
+
16
22
  // -------------------------------------------------------------------
17
23
  // Remapping
18
24
  // -------------------------------------------------------------------
@@ -619,6 +619,7 @@ export interface FastifyInstance<
619
619
  http2?: boolean,
620
620
  https?: boolean | Readonly<{ allowHTTP1: boolean }>,
621
621
  ignoreTrailingSlash?: boolean,
622
+ ignoreDuplicateSlashes?: boolean,
622
623
  disableRequestLogging?: boolean,
623
624
  maxParamLength?: number,
624
625
  onProtoPoisoning?: ProtoAction,
package/types/logger.d.ts CHANGED
@@ -17,11 +17,13 @@ export type LogLevel = pino.Level
17
17
 
18
18
  export type Bindings = pino.Bindings
19
19
 
20
+ export type ChildLoggerOptions = pino.ChildLoggerOptions
21
+
20
22
  export type FastifyLoggerInstance = pino.Logger
21
23
  // TODO make pino export BaseLogger again
22
24
  // export type FastifyBaseLogger = pino.BaseLogger & {
23
25
  export type FastifyBaseLogger = pino.Logger & {
24
- child(bindings: Bindings): FastifyBaseLogger
26
+ child(bindings: Bindings, options?: ChildLoggerOptions): FastifyBaseLogger
25
27
  }
26
28
 
27
29
  export interface FastifyLoggerStreamDestination {
package/types/route.d.ts CHANGED
@@ -154,7 +154,7 @@ export type RouteHandler<
154
154
  this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
155
155
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, RequestType, Logger>,
156
156
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
157
- ) => void | Promise<RouteGeneric['Reply'] | void>
157
+ ) => RouteGeneric['Reply'] | void | Promise<RouteGeneric['Reply'] | void>
158
158
 
159
159
  export type DefaultRoute<Request, Reply> = (
160
160
  req: Request,
package/types/schema.d.ts CHANGED
@@ -4,7 +4,7 @@ import { FastifyInstance, FastifyServerOptions } from '../fastify'
4
4
  * Schemas in Fastify follow the JSON-Schema standard. For this reason
5
5
  * we have opted to not ship strict schema based types. Instead we provide
6
6
  * an example in our documentation on how to solve this problem. Check it
7
- * out here: https://github.com/fastify/fastify/blob/main/docs/TypeScript.md#json-schema
7
+ * out here: https://github.com/fastify/fastify/blob/main/docs/Reference/TypeScript.md#json-schema
8
8
  */
9
9
  export interface FastifySchema {
10
10
  body?: unknown;
@@ -11,9 +11,8 @@ export interface FastifyTypeProvider {
11
11
  readonly output: unknown,
12
12
  }
13
13
 
14
- export interface FastifyTypeProviderDefault extends FastifyTypeProvider {
15
- output: unknown
16
- }
14
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
15
+ export interface FastifyTypeProviderDefault extends FastifyTypeProvider {}
17
16
 
18
17
  export type CallTypeProvider<F extends FastifyTypeProvider, I> = (F & { input: I })['output']
19
18
 
@@ -93,7 +92,7 @@ TypeProvider,
93
92
  SchemaCompiler,
94
93
  RouteGeneric
95
94
  > extends infer Return ?
96
- (void | Promise<Return | void>)
95
+ (Return | void | Promise<Return | void>)
97
96
  // review: support both async and sync return types
98
97
  // (Promise<Return> | Return | Promise<void> | void)
99
98
  : unknown