fastify 3.27.3 → 4.0.0-alpha.2

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 (164) hide show
  1. package/.taprc +3 -0
  2. package/README.md +7 -7
  3. package/build/build-error-serializer.js +27 -0
  4. package/build/build-validation.js +47 -35
  5. package/docs/Guides/Database.md +320 -0
  6. package/docs/Guides/Getting-Started.md +7 -7
  7. package/docs/Guides/Plugins-Guide.md +1 -1
  8. package/docs/Guides/Serverless.md +3 -3
  9. package/docs/Guides/Testing.md +2 -2
  10. package/docs/Migration-Guide-V4.md +12 -0
  11. package/docs/Reference/ContentTypeParser.md +4 -0
  12. package/docs/Reference/Decorators.md +2 -2
  13. package/docs/Reference/Encapsulation.md +2 -2
  14. package/docs/Reference/Errors.md +51 -6
  15. package/docs/Reference/HTTP2.md +3 -3
  16. package/docs/Reference/Hooks.md +4 -7
  17. package/docs/Reference/LTS.md +5 -4
  18. package/docs/Reference/Plugins.md +3 -3
  19. package/docs/Reference/Reply.md +23 -22
  20. package/docs/Reference/Request.md +1 -3
  21. package/docs/Reference/Routes.md +22 -15
  22. package/docs/Reference/Server.md +69 -119
  23. package/docs/Reference/TypeScript.md +20 -22
  24. package/docs/Reference/Validation-and-Serialization.md +30 -55
  25. package/docs/Type-Providers.md +257 -0
  26. package/examples/asyncawait.js +1 -1
  27. package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
  28. package/examples/benchmark/hooks-benchmark.js +1 -1
  29. package/examples/benchmark/simple.js +1 -1
  30. package/examples/hooks.js +2 -2
  31. package/examples/http2.js +1 -1
  32. package/examples/https.js +1 -1
  33. package/examples/parser.js +1 -1
  34. package/examples/route-prefix.js +1 -1
  35. package/examples/shared-schema.js +1 -1
  36. package/examples/simple-stream.js +18 -0
  37. package/examples/simple.js +1 -1
  38. package/examples/simple.mjs +1 -1
  39. package/examples/typescript-server.ts +1 -1
  40. package/examples/use-plugin.js +1 -1
  41. package/fastify.d.ts +34 -22
  42. package/fastify.js +40 -36
  43. package/lib/configValidator.js +902 -1023
  44. package/lib/contentTypeParser.js +6 -16
  45. package/lib/context.js +36 -10
  46. package/lib/decorate.js +3 -1
  47. package/lib/error-handler.js +158 -0
  48. package/lib/error-serializer.js +257 -0
  49. package/lib/errors.js +43 -9
  50. package/lib/fourOhFour.js +31 -20
  51. package/lib/handleRequest.js +10 -13
  52. package/lib/hooks.js +14 -9
  53. package/lib/pluginOverride.js +0 -3
  54. package/lib/pluginUtils.js +3 -2
  55. package/lib/reply.js +29 -158
  56. package/lib/request.js +13 -10
  57. package/lib/route.js +131 -138
  58. package/lib/schema-controller.js +2 -2
  59. package/lib/schemas.js +27 -1
  60. package/lib/server.js +241 -116
  61. package/lib/symbols.js +4 -3
  62. package/lib/validation.js +2 -1
  63. package/lib/warnings.js +4 -12
  64. package/lib/wrapThenable.js +4 -11
  65. package/package.json +37 -39
  66. package/test/404s.test.js +258 -125
  67. package/test/500s.test.js +3 -3
  68. package/test/als.test.js +1 -1
  69. package/test/async-await.test.js +20 -76
  70. package/test/bodyLimit.test.js +1 -1
  71. package/test/build-certificate.js +6 -7
  72. package/test/case-insensitive.test.js +4 -4
  73. package/test/close-pipelining.test.js +2 -2
  74. package/test/close.test.js +11 -11
  75. package/test/content-parser.test.js +32 -0
  76. package/test/context-config.test.js +52 -0
  77. package/test/custom-http-server.test.js +14 -7
  78. package/test/custom-parser-async.test.js +1 -66
  79. package/test/custom-parser.test.js +92 -159
  80. package/test/custom-querystring-parser.test.js +3 -3
  81. package/test/decorator.test.js +11 -13
  82. package/test/delete.test.js +6 -6
  83. package/test/encapsulated-error-handler.test.js +50 -0
  84. package/test/esm/index.test.js +0 -14
  85. package/test/fastify-instance.test.js +4 -4
  86. package/test/fluent-schema.test.js +4 -4
  87. package/test/genReqId.test.js +1 -1
  88. package/test/get.test.js +4 -4
  89. package/test/handler-context.test.js +2 -2
  90. package/test/head.test.js +1 -1
  91. package/test/helper.js +19 -4
  92. package/test/hooks-async.test.js +15 -48
  93. package/test/hooks.on-ready.test.js +10 -5
  94. package/test/hooks.test.js +78 -119
  95. package/test/http2/closing.test.js +10 -16
  96. package/test/http2/constraint.test.js +1 -1
  97. package/test/http2/head.test.js +1 -1
  98. package/test/http2/plain.test.js +1 -1
  99. package/test/http2/secure-with-fallback.test.js +1 -1
  100. package/test/http2/secure.test.js +1 -1
  101. package/test/http2/unknown-http-method.test.js +4 -10
  102. package/test/https/custom-https-server.test.js +12 -6
  103. package/test/https/https.test.js +1 -1
  104. package/test/input-validation.js +3 -3
  105. package/test/internals/handleRequest.test.js +6 -43
  106. package/test/internals/initialConfig.test.js +41 -12
  107. package/test/internals/logger.test.js +2 -2
  108. package/test/internals/reply.test.js +281 -40
  109. package/test/internals/request.test.js +13 -7
  110. package/test/internals/server.test.js +88 -0
  111. package/test/listen.deprecated.test.js +202 -0
  112. package/test/listen.test.js +118 -150
  113. package/test/logger.test.js +82 -42
  114. package/test/maxRequestsPerSocket.test.js +8 -6
  115. package/test/middleware.test.js +2 -25
  116. package/test/nullable-validation.test.js +53 -16
  117. package/test/output-validation.test.js +1 -1
  118. package/test/plugin.test.js +47 -21
  119. package/test/pretty-print.test.js +22 -10
  120. package/test/promises.test.js +1 -1
  121. package/test/proto-poisoning.test.js +6 -6
  122. package/test/register.test.js +3 -3
  123. package/test/reply-error.test.js +126 -15
  124. package/test/request-error.test.js +3 -6
  125. package/test/route-hooks.test.js +18 -18
  126. package/test/route-prefix.test.js +2 -1
  127. package/test/route.test.js +206 -22
  128. package/test/router-options.test.js +2 -2
  129. package/test/schema-examples.test.js +11 -5
  130. package/test/schema-feature.test.js +25 -20
  131. package/test/schema-serialization.test.js +9 -9
  132. package/test/schema-special-usage.test.js +5 -153
  133. package/test/schema-validation.test.js +9 -9
  134. package/test/skip-reply-send.test.js +2 -2
  135. package/test/stream.test.js +82 -23
  136. package/test/throw.test.js +8 -5
  137. package/test/trust-proxy.test.js +6 -6
  138. package/test/type-provider.test.js +20 -0
  139. package/test/types/fastify.test-d.ts +10 -18
  140. package/test/types/import.js +2 -0
  141. package/test/types/import.ts +1 -0
  142. package/test/types/instance.test-d.ts +68 -17
  143. package/test/types/logger.test-d.ts +44 -15
  144. package/test/types/reply.test-d.ts +2 -1
  145. package/test/types/route.test-d.ts +8 -2
  146. package/test/types/schema.test-d.ts +2 -39
  147. package/test/types/type-provider.test-d.ts +417 -0
  148. package/test/url-rewriting.test.js +3 -3
  149. package/test/validation-error-handling.test.js +8 -8
  150. package/test/versioned-routes.test.js +30 -18
  151. package/test/wrapThenable.test.js +7 -6
  152. package/types/content-type-parser.d.ts +17 -8
  153. package/types/hooks.d.ts +102 -59
  154. package/types/instance.d.ts +244 -118
  155. package/types/logger.d.ts +18 -104
  156. package/types/plugin.d.ts +10 -4
  157. package/types/reply.d.ts +18 -12
  158. package/types/request.d.ts +10 -5
  159. package/types/route.d.ts +42 -31
  160. package/types/schema.d.ts +1 -1
  161. package/types/type-provider.d.ts +99 -0
  162. package/types/utils.d.ts +1 -1
  163. package/lib/schema-compilers.js +0 -12
  164. package/test/emit-warning.test.js +0 -166
@@ -0,0 +1,20 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const Fastify = require('..')
5
+
6
+ test('Should export withTypeProvider function', t => {
7
+ t.plan(1)
8
+ try {
9
+ Fastify().withTypeProvider()
10
+ t.pass()
11
+ } catch (e) {
12
+ t.fail()
13
+ }
14
+ })
15
+
16
+ test('Should return same instance', t => {
17
+ t.plan(1)
18
+ const fastify = Fastify()
19
+ t.equal(fastify, fastify.withTypeProvider())
20
+ })
@@ -7,7 +7,7 @@ import fastify, {
7
7
  LightMyRequestChain,
8
8
  LightMyRequestResponse,
9
9
  LightMyRequestCallback,
10
- InjectOptions
10
+ InjectOptions, FastifyBaseLogger
11
11
  } from '../../fastify'
12
12
  import * as http from 'http'
13
13
  import * as https from 'https'
@@ -50,7 +50,9 @@ expectAssignable<InjectOptions>({ query: '' })
50
50
  fastify({ http2: true, https: {} }).inject().then((resp) => {
51
51
  expectAssignable<LightMyRequestResponse>(resp)
52
52
  })
53
- const lightMyRequestCallback: LightMyRequestCallback = (err: Error, response: LightMyRequestResponse) => {}
53
+ const lightMyRequestCallback: LightMyRequestCallback = (err: Error, response: LightMyRequestResponse) => {
54
+ if (err) throw err
55
+ }
54
56
  fastify({ http2: true, https: {} }).inject({}, lightMyRequestCallback)
55
57
 
56
58
  // server options
@@ -70,9 +72,9 @@ expectAssignable<FastifyInstance>(fastify({ serializerOpts: { rounding: 'ceil' }
70
72
  expectAssignable<FastifyInstance>(fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } }))
71
73
  expectAssignable<FastifyInstance>(fastify({ serializerOpts: { schema: { } } }))
72
74
  expectAssignable<FastifyInstance>(fastify({ serializerOpts: { otherProp: { } } }))
73
- expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>(fastify({ logger: true }))
74
- expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyLoggerInstance>>(fastify({ logger: true }))
75
- expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyLoggerInstance>>(fastify({
75
+ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
76
+ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
77
+ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({
76
78
  logger: {
77
79
  level: 'info',
78
80
  genReqId: () => 'request-id',
@@ -103,6 +105,7 @@ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerR
103
105
  }
104
106
  }))
105
107
  const customLogger = {
108
+ level: 'info',
106
109
  info: () => { },
107
110
  warn: () => { },
108
111
  error: () => { },
@@ -111,7 +114,7 @@ const customLogger = {
111
114
  debug: () => { },
112
115
  child: () => customLogger
113
116
  }
114
- expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyLoggerInstance>>(fastify({ logger: customLogger }))
117
+ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: customLogger }))
115
118
  expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServer() }))
116
119
  expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
117
120
  expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
@@ -174,24 +177,13 @@ expectAssignable<FastifyInstance>(fastify({ return503OnClosing: true }))
174
177
  expectAssignable<FastifyInstance>(fastify({
175
178
  ajv: {
176
179
  customOptions: {
177
- nullable: false
180
+ removeAdditional: 'all'
178
181
  },
179
182
  plugins: [() => { }]
180
183
  }
181
184
  }))
182
185
  expectAssignable<FastifyInstance>(fastify({
183
186
  ajv: {
184
- customOptions: {
185
- nullable: false
186
- },
187
- plugins: [[() => { }, 'keyword']]
188
- }
189
- }))
190
- expectAssignable<FastifyInstance>(fastify({
191
- ajv: {
192
- customOptions: {
193
- nullable: false
194
- },
195
187
  plugins: [[() => { }, ['keyword1', 'keyword2']]]
196
188
  }
197
189
  }))
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ exports.__esModule = true;
@@ -0,0 +1 @@
1
+ import { FastifyLogFn } from '../../fastify'
@@ -1,3 +1,4 @@
1
+ import { expectAssignable, expectDeprecated, expectError, expectNotDeprecated, expectType } from 'tsd'
1
2
  import fastify, {
2
3
  FastifyBodyParser,
3
4
  FastifyError,
@@ -6,10 +7,9 @@ import fastify, {
6
7
  RawRequestDefaultExpression,
7
8
  RawServerDefault
8
9
  } from '../../fastify'
9
- import { expectAssignable, expectError, expectType } from 'tsd'
10
- import { FastifyRequest } from '../../types/request'
11
- import { FastifyReply } from '../../types/reply'
12
10
  import { HookHandlerDoneFunction } from '../../types/hooks'
11
+ import { FastifyReply } from '../../types/reply'
12
+ import { FastifyRequest } from '../../types/request'
13
13
  import { FastifySchemaControllerOptions } from '../../types/schema'
14
14
 
15
15
  const server = fastify()
@@ -31,6 +31,7 @@ expectType<string>(server.printPlugins())
31
31
 
32
32
  expectAssignable<FastifyInstance>(
33
33
  server.setErrorHandler(function (error, request, reply) {
34
+ expectType<FastifyError>(error)
34
35
  expectAssignable<FastifyInstance>(this)
35
36
  })
36
37
  )
@@ -47,10 +48,14 @@ server.setErrorHandler(fastifyErrorHandler)
47
48
  async function asyncFastifyErrorHandler (this: FastifyInstance, error: FastifyError) {}
48
49
  server.setErrorHandler(asyncFastifyErrorHandler)
49
50
 
50
- function nodeJSErrorHandler (error: NodeJS.ErrnoException) {}
51
+ function nodeJSErrorHandler (error: NodeJS.ErrnoException) {
52
+ if (error) { throw error }
53
+ }
51
54
  server.setErrorHandler(nodeJSErrorHandler)
52
55
 
53
- function asyncNodeJSErrorHandler (error: NodeJS.ErrnoException) {}
56
+ function asyncNodeJSErrorHandler (error: NodeJS.ErrnoException) {
57
+ if (error) { throw error }
58
+ }
54
59
  server.setErrorHandler(asyncNodeJSErrorHandler)
55
60
 
56
61
  class CustomError extends Error {
@@ -82,20 +87,20 @@ expectError(server.setErrorHandler<CustomError, ReplyPayload>((error, request, r
82
87
  reply.send({ test: 'foo' })
83
88
  }))
84
89
  // typed sync error handler return error
85
- expectError(server.setErrorHandler<CustomError, ReplyPayload>((error, request, reply) => {
90
+ server.setErrorHandler<CustomError, ReplyPayload>((error, request, reply) => {
86
91
  expectType<CustomError>(error)
87
92
  return { test: 'foo' }
88
- }))
93
+ })
89
94
  // typed async error handler send error
90
95
  expectError(server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
91
96
  expectType<CustomError>(error)
92
97
  reply.send({ test: 'foo' })
93
98
  }))
94
99
  // typed async error handler return error
95
- expectError(server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
100
+ server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
96
101
  expectType<CustomError>(error)
97
102
  return { test: 'foo' }
98
- }))
103
+ })
99
104
 
100
105
  function notFoundHandler (request: FastifyRequest, reply: FastifyReply) {}
101
106
  function notFoundpreHandlerHandler (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) { done() }
@@ -110,7 +115,9 @@ server.setNotFoundHandler({ preValidation: notFoundpreValidationHandler }, notFo
110
115
  server.setNotFoundHandler({ preValidation: notFoundpreValidationAsyncHandler }, notFoundHandler)
111
116
  server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler }, notFoundHandler)
112
117
 
113
- function invalidErrorHandler (error: number) {}
118
+ function invalidErrorHandler (error: number) {
119
+ if (error) throw error
120
+ }
114
121
  expectError(server.setErrorHandler(invalidErrorHandler))
115
122
 
116
123
  server.setSchemaController({
@@ -148,16 +155,30 @@ expectError(server.setReplySerializer(invalidReplySerializer))
148
155
  function serializerWithInvalidReturn (payload: unknown, statusCode: number) {}
149
156
  expectError(server.setReplySerializer(serializerWithInvalidReturn))
150
157
 
151
- function invalidSchemaErrorFormatter () {}
158
+ function invalidSchemaErrorFormatter (err: Error) {
159
+ if (err) { throw err }
160
+ }
152
161
  expectError(server.setSchemaErrorFormatter(invalidSchemaErrorFormatter))
153
162
 
154
163
  // test listen method callback
155
- expectAssignable<void>(server.listen(3000, '', 0, (err, address) => {}))
156
- expectAssignable<void>(server.listen('3000', '', 0, (err, address) => {}))
157
- expectAssignable<void>(server.listen(3000, '', (err, address) => {}))
158
- expectAssignable<void>(server.listen('3000', '', (err, address) => {}))
159
- expectAssignable<void>(server.listen(3000, (err, address) => {}))
160
- expectAssignable<void>(server.listen('3000', (err, address) => {}))
164
+ expectAssignable<void>(server.listen(3000, '', 0, (err, address) => {
165
+ expectType<Error | null>(err)
166
+ }))
167
+ expectAssignable<void>(server.listen('3000', '', 0, (err, address) => {
168
+ expectType<Error | null>(err)
169
+ }))
170
+ expectAssignable<void>(server.listen(3000, '', (err, address) => {
171
+ expectType<Error | null>(err)
172
+ }))
173
+ expectAssignable<void>(server.listen('3000', '', (err, address) => {
174
+ expectType<Error | null>(err)
175
+ }))
176
+ expectAssignable<void>(server.listen(3000, (err, address) => {
177
+ expectType<Error | null>(err)
178
+ }))
179
+ expectAssignable<void>(server.listen('3000', (err, address) => {
180
+ expectType<Error | null>(err)
181
+ }))
161
182
 
162
183
  // test listen method callback types
163
184
  expectAssignable<void>(server.listen('3000', (err, address) => {
@@ -173,13 +194,43 @@ expectAssignable<PromiseLike<string>>(server.listen('3000', '', 0))
173
194
  expectAssignable<PromiseLike<string>>(server.listen(3000, ''))
174
195
  expectAssignable<PromiseLike<string>>(server.listen('3000', ''))
175
196
 
197
+ // Test variadic listen signatures Typescript deprecation
198
+ expectDeprecated(server.listen(3000))
199
+ expectDeprecated(server.listen('3000'))
200
+ expectDeprecated(server.listen(3000, '', 0))
201
+ expectDeprecated(server.listen('3000', '', 0))
202
+ expectDeprecated(server.listen(3000, ''))
203
+ expectDeprecated(server.listen('3000', ''))
204
+
176
205
  // test listen opts objects
206
+ expectAssignable<PromiseLike<string>>(server.listen())
177
207
  expectAssignable<PromiseLike<string>>(server.listen({ port: 3000 }))
178
208
  expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0' }))
179
209
  expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }))
210
+ expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }))
211
+ expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '::/0', ipv6Only: true }))
212
+
213
+ expectAssignable<void>(server.listen(() => {}))
180
214
  expectAssignable<void>(server.listen({ port: 3000 }, () => {}))
181
215
  expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0' }, () => {}))
182
216
  expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }, () => {}))
217
+ expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }, () => {}))
218
+ expectAssignable<void>(server.listen({ port: 3000, host: '::/0', ipv6Only: true }, () => {}))
219
+
220
+ // test listen opts objects Typescript deprectation exclusion
221
+ expectNotDeprecated(server.listen())
222
+ expectNotDeprecated(server.listen({ port: 3000 }))
223
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0' }))
224
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }))
225
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }))
226
+ expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true }))
227
+
228
+ expectNotDeprecated(server.listen(() => {}))
229
+ expectNotDeprecated(server.listen({ port: 3000 }, () => {}))
230
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0' }, () => {}))
231
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }, () => {}))
232
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }, () => {}))
233
+ expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true }, () => {}))
183
234
 
184
235
  expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as RawReplyDefaultExpression))
185
236
 
@@ -1,8 +1,15 @@
1
1
  import { expectType } from 'tsd'
2
- import fastify, { FastifyLogFn, LogLevel, FastifyLoggerInstance, FastifyError, FastifyRequest, FastifyReply } from '../../fastify'
2
+ import fastify, {
3
+ FastifyLogFn,
4
+ LogLevel,
5
+ FastifyLoggerInstance,
6
+ FastifyRequest,
7
+ FastifyReply,
8
+ FastifyBaseLogger
9
+ } from '../../fastify'
3
10
  import { Server, IncomingMessage, ServerResponse } from 'http'
4
- import pino from 'pino'
5
11
  import * as fs from 'fs'
12
+ import P from 'pino'
6
13
 
7
14
  expectType<FastifyLoggerInstance>(fastify().log)
8
15
 
@@ -15,14 +22,17 @@ class Foo {}
15
22
  expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({ foo: 'bar' }))
16
23
  expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Error()))
17
24
  expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Foo()))
18
- expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](0))
19
25
  })
20
26
 
21
- interface CustomLogger extends FastifyLoggerInstance {
27
+ /*
28
+ // TODO make pino export BaseLogger again
29
+ interface CustomLogger extends FastifyBaseLogger {
22
30
  customMethod(msg: string, ...args: unknown[]): void;
23
31
  }
24
32
 
33
+ // // ToDo https://github.com/pinojs/pino/issues/1100
25
34
  class CustomLoggerImpl implements CustomLogger {
35
+ level = 'info'
26
36
  customMethod (msg: string, ...args: unknown[]) { console.log(msg, args) }
27
37
 
28
38
  // Implementation signature must be compatible with all overloads of FastifyLogFn
@@ -35,7 +45,9 @@ class CustomLoggerImpl implements CustomLogger {
35
45
  fatal (...args: unknown[]) { console.log(args) }
36
46
  trace (...args: unknown[]) { console.log(args) }
37
47
  debug (...args: unknown[]) { console.log(args) }
38
- child () { return new CustomLoggerImpl() }
48
+ silent (...args: unknown[]) { }
49
+
50
+ child (bindings: P.Bindings, options?: P.ChildLoggerOptions): CustomLoggerImpl { return new CustomLoggerImpl() }
39
51
  }
40
52
 
41
53
  const customLogger = new CustomLoggerImpl()
@@ -48,20 +60,21 @@ CustomLoggerImpl
48
60
  >({ logger: customLogger })
49
61
 
50
62
  expectType<CustomLoggerImpl>(serverWithCustomLogger.log)
63
+ */
51
64
 
52
65
  const serverWithPino = fastify<
53
66
  Server,
54
67
  IncomingMessage,
55
68
  ServerResponse,
56
- pino.Logger
69
+ P.Logger
57
70
  >({
58
- logger: pino({
71
+ logger: P({
59
72
  level: 'info',
60
73
  redact: ['x-userinfo']
61
74
  })
62
75
  })
63
76
 
64
- expectType<pino.Logger>(serverWithPino.log)
77
+ expectType<P.Logger>(serverWithPino.log)
65
78
 
66
79
  const serverWithLogOptions = fastify<
67
80
  Server,
@@ -94,16 +107,16 @@ const serverAutoInferringTypes = fastify({
94
107
  }
95
108
  })
96
109
 
97
- expectType<FastifyLoggerInstance>(serverAutoInferringTypes.log)
110
+ expectType<FastifyBaseLogger>(serverAutoInferringTypes.log)
98
111
 
99
112
  const serverWithAutoInferredPino = fastify({
100
- logger: pino({
113
+ logger: P({
101
114
  level: 'info',
102
115
  redact: ['x-userinfo']
103
116
  })
104
117
  })
105
118
 
106
- expectType<pino.Logger>(serverWithAutoInferredPino.log)
119
+ expectType<P.Logger>(serverWithAutoInferredPino.log)
107
120
 
108
121
  const serverAutoInferredFileOption = fastify({
109
122
  logger: {
@@ -112,7 +125,7 @@ const serverAutoInferredFileOption = fastify({
112
125
  }
113
126
  })
114
127
 
115
- expectType<FastifyLoggerInstance>(serverAutoInferredFileOption.log)
128
+ expectType<FastifyBaseLogger>(serverAutoInferredFileOption.log)
116
129
 
117
130
  const serverAutoInferredPinoPrettyBooleanOption = fastify({
118
131
  logger: {
@@ -120,7 +133,7 @@ const serverAutoInferredPinoPrettyBooleanOption = fastify({
120
133
  }
121
134
  })
122
135
 
123
- expectType<FastifyLoggerInstance>(serverAutoInferredPinoPrettyBooleanOption.log)
136
+ expectType<FastifyBaseLogger>(serverAutoInferredPinoPrettyBooleanOption.log)
124
137
 
125
138
  const serverAutoInferredPinoPrettyObjectOption = fastify({
126
139
  logger: {
@@ -141,7 +154,7 @@ const serverAutoInferredPinoPrettyObjectOption = fastify({
141
154
  }
142
155
  })
143
156
 
144
- expectType<FastifyLoggerInstance>(serverAutoInferredPinoPrettyObjectOption.log)
157
+ expectType<FastifyBaseLogger>(serverAutoInferredPinoPrettyObjectOption.log)
145
158
 
146
159
  const serverAutoInferredSerializerObjectOption = fastify({
147
160
  logger: {
@@ -176,10 +189,26 @@ const serverAutoInferredSerializerObjectOption = fastify({
176
189
  }
177
190
  })
178
191
 
179
- expectType<FastifyLoggerInstance>(serverAutoInferredSerializerObjectOption.log)
192
+ expectType<FastifyBaseLogger>(serverAutoInferredSerializerObjectOption.log)
180
193
 
181
194
  const passStreamAsOption = fastify({
182
195
  logger: {
183
196
  stream: fs.createWriteStream('/tmp/stream.out')
184
197
  }
185
198
  })
199
+
200
+ expectType<FastifyBaseLogger>(passStreamAsOption.log)
201
+
202
+ const passPinoOption = fastify({
203
+ logger: {
204
+ redact: ['custom'],
205
+ messageKey: 'msg',
206
+ nestedKey: 'nested',
207
+ prettyPrint: {
208
+
209
+ },
210
+ enabled: true
211
+ }
212
+ })
213
+
214
+ expectType<FastifyBaseLogger>(passPinoOption.log)
@@ -4,6 +4,7 @@ import { RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } fro
4
4
  import { FastifyLoggerInstance } from '../../types/logger'
5
5
  import { RouteGenericInterface } from '../../types/route'
6
6
  import { FastifyInstance } from '../../types/instance'
7
+ import { Buffer } from 'buffer'
7
8
 
8
9
  const getHandler: RouteHandlerMethod = function (_request, reply) {
9
10
  expectType<RawReplyDefaultExpression>(reply.raw)
@@ -28,7 +29,7 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
28
29
  expectType<() => number>(reply.getResponseTime)
29
30
  expectType<(contentType: string) => FastifyReply>(reply.type)
30
31
  expectType<(fn: (payload: any) => string) => FastifyReply>(reply.serializer)
31
- expectType<(payload: any) => string>(reply.serialize)
32
+ expectType<(payload: any) => string | ArrayBuffer | Buffer>(reply.serialize)
32
33
  expectType<(fulfilled: () => void, rejected: (err: Error) => void) => void>(reply.then)
33
34
  expectType<FastifyInstance>(reply.server)
34
35
  }
@@ -1,5 +1,5 @@
1
1
  import fastify, { FastifyInstance, FastifyRequest, FastifyReply, RouteHandlerMethod } from '../../fastify'
2
- import { expectType, expectError, expectAssignable } from 'tsd'
2
+ import { expectType, expectError, expectAssignable, printType } from 'tsd'
3
3
  import { HTTPMethods } from '../../types/utils'
4
4
  import * as http from 'http'
5
5
  import { RequestPayload } from '../../types/hooks'
@@ -44,7 +44,13 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
44
44
  expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {}, routeHandler))
45
45
  expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', { handler: routeHandler }))
46
46
 
47
- expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', { handler: routeHandler, errorHandler: (error, request, reply) => reply.send('error') }))
47
+ expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {
48
+ handler: routeHandler,
49
+ errorHandler: (error, request, reply) => {
50
+ expectType<FastifyError>(error)
51
+ reply.send('error')
52
+ }
53
+ }))
48
54
 
49
55
  interface BodyInterface { prop: string }
50
56
  interface QuerystringInterface { prop: number }
@@ -3,7 +3,7 @@ import fastify, { FastifyInstance, FastifyRequest, FastifySchema } from '../../f
3
3
  import { RouteGenericInterface } from '../../types/route'
4
4
  import { ContextConfigDefault } from '../../types/utils'
5
5
  import { FastifyReply } from '../../types/reply'
6
- import Ajv = require('ajv')
6
+ import Ajv from 'ajv'
7
7
 
8
8
  const server = fastify()
9
9
 
@@ -35,33 +35,6 @@ expectAssignable<FastifyInstance>(server.get(
35
35
  () => { }
36
36
  ))
37
37
 
38
- expectAssignable<FastifyInstance>(server.get<RouteGenericInterface, ContextConfigDefault, { validate:(data: any) => any }>(
39
- '/no-schema',
40
- {
41
- schema: {},
42
- validatorCompiler: ({ schema }) => {
43
- // Error: Property 'validate' does not exist on type 'FastifySchema'.
44
- return (data: any) => schema.validate(data)
45
- }
46
- },
47
- () => { }
48
- ))
49
-
50
- expectAssignable<FastifyInstance>(
51
- server.route<RouteGenericInterface, ContextConfigDefault, { validate:(data: any) => any }>(
52
- {
53
- schema: {},
54
- validatorCompiler: ({ schema }) => {
55
- // Error: Property 'validate' does not exist on type 'FastifySchema'.
56
- return (data: any) => schema.validate(data)
57
- },
58
- method: 'POST',
59
- url: '/',
60
- handler: async (_request: FastifyRequest, _reply: FastifyReply) => {}
61
- }
62
- )
63
- )
64
-
65
38
  expectAssignable<FastifyInstance>(server.setValidatorCompiler(({ schema }) => {
66
39
  return new Ajv().compile(schema)
67
40
  }))
@@ -83,20 +56,10 @@ expectAssignable<FastifyInstance>(server.post('/test', {
83
56
 
84
57
  expectAssignable<FastifyInstance>(server.setValidatorCompiler<FastifySchema & { validate: Record<string, unknown> }>(
85
58
  function ({ schema }) {
86
- return new Ajv().compile(schema.validate)
59
+ return new Ajv().compile(schema)
87
60
  }
88
61
  ))
89
62
 
90
63
  expectAssignable<FastifyInstance>(server.setSerializerCompiler<FastifySchema & { validate: string }>(
91
64
  () => data => JSON.stringify(data)
92
65
  ))
93
-
94
- expectError(server.get(
95
- '/unknown-schema-prop',
96
- {
97
- schema: {
98
- unknown: { type: 'null' }
99
- }
100
- },
101
- () => { }
102
- ))