fastify 3.27.2 → 4.0.0-alpha.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 (116) hide show
  1. package/README.md +5 -4
  2. package/build/build-error-serializer.js +27 -0
  3. package/build/build-validation.js +47 -35
  4. package/docs/Migration-Guide-V4.md +12 -0
  5. package/docs/Reference/ContentTypeParser.md +4 -0
  6. package/docs/Reference/Errors.md +51 -6
  7. package/docs/Reference/Hooks.md +4 -7
  8. package/docs/Reference/LTS.md +5 -4
  9. package/docs/Reference/Reply.md +23 -22
  10. package/docs/Reference/Request.md +1 -3
  11. package/docs/Reference/Routes.md +17 -10
  12. package/docs/Reference/Server.md +48 -63
  13. package/docs/Reference/TypeScript.md +11 -13
  14. package/docs/Reference/Validation-and-Serialization.md +28 -53
  15. package/docs/Type-Providers.md +257 -0
  16. package/examples/hooks.js +1 -1
  17. package/examples/simple-stream.js +18 -0
  18. package/fastify.d.ts +34 -22
  19. package/fastify.js +37 -35
  20. package/lib/configValidator.js +902 -1023
  21. package/lib/contentTypeParser.js +6 -16
  22. package/lib/context.js +36 -10
  23. package/lib/decorate.js +3 -1
  24. package/lib/error-handler.js +158 -0
  25. package/lib/error-serializer.js +257 -0
  26. package/lib/errors.js +43 -9
  27. package/lib/fourOhFour.js +31 -20
  28. package/lib/handleRequest.js +10 -13
  29. package/lib/hooks.js +14 -9
  30. package/lib/pluginOverride.js +0 -3
  31. package/lib/pluginUtils.js +3 -2
  32. package/lib/reply.js +28 -157
  33. package/lib/request.js +13 -10
  34. package/lib/route.js +131 -138
  35. package/lib/schema-controller.js +2 -2
  36. package/lib/schemas.js +27 -1
  37. package/lib/server.js +219 -116
  38. package/lib/symbols.js +4 -3
  39. package/lib/validation.js +2 -1
  40. package/lib/warnings.js +2 -12
  41. package/lib/wrapThenable.js +4 -11
  42. package/package.json +31 -35
  43. package/test/404s.test.js +243 -110
  44. package/test/500s.test.js +2 -2
  45. package/test/async-await.test.js +13 -69
  46. package/test/content-parser.test.js +32 -0
  47. package/test/context-config.test.js +52 -0
  48. package/test/custom-http-server.test.js +14 -7
  49. package/test/custom-parser-async.test.js +0 -65
  50. package/test/custom-parser.test.js +54 -121
  51. package/test/decorator.test.js +1 -3
  52. package/test/delete.test.js +5 -5
  53. package/test/encapsulated-error-handler.test.js +50 -0
  54. package/test/esm/index.test.js +0 -14
  55. package/test/fastify-instance.test.js +4 -4
  56. package/test/fluent-schema.test.js +4 -4
  57. package/test/get.test.js +3 -3
  58. package/test/helper.js +18 -3
  59. package/test/hooks-async.test.js +14 -47
  60. package/test/hooks.on-ready.test.js +9 -4
  61. package/test/hooks.test.js +58 -99
  62. package/test/http2/closing.test.js +5 -11
  63. package/test/http2/unknown-http-method.test.js +3 -9
  64. package/test/https/custom-https-server.test.js +12 -6
  65. package/test/input-validation.js +2 -2
  66. package/test/internals/handleRequest.test.js +3 -40
  67. package/test/internals/initialConfig.test.js +33 -12
  68. package/test/internals/reply.test.js +245 -3
  69. package/test/internals/request.test.js +13 -7
  70. package/test/internals/server.test.js +88 -0
  71. package/test/listen.test.js +84 -1
  72. package/test/logger.test.js +80 -40
  73. package/test/maxRequestsPerSocket.test.js +6 -4
  74. package/test/middleware.test.js +2 -25
  75. package/test/nullable-validation.test.js +51 -14
  76. package/test/plugin.test.js +31 -5
  77. package/test/pretty-print.test.js +22 -10
  78. package/test/reply-error.test.js +123 -12
  79. package/test/request-error.test.js +2 -5
  80. package/test/route-hooks.test.js +17 -17
  81. package/test/route-prefix.test.js +2 -1
  82. package/test/route.test.js +204 -20
  83. package/test/router-options.test.js +1 -1
  84. package/test/schema-examples.test.js +11 -5
  85. package/test/schema-feature.test.js +24 -19
  86. package/test/schema-serialization.test.js +9 -9
  87. package/test/schema-special-usage.test.js +14 -81
  88. package/test/schema-validation.test.js +9 -9
  89. package/test/skip-reply-send.test.js +1 -1
  90. package/test/stream.test.js +23 -12
  91. package/test/throw.test.js +8 -5
  92. package/test/type-provider.test.js +20 -0
  93. package/test/types/fastify.test-d.ts +10 -18
  94. package/test/types/import.js +2 -0
  95. package/test/types/import.ts +1 -0
  96. package/test/types/instance.test-d.ts +35 -14
  97. package/test/types/logger.test-d.ts +44 -15
  98. package/test/types/route.test-d.ts +8 -2
  99. package/test/types/schema.test-d.ts +2 -39
  100. package/test/types/type-provider.test-d.ts +417 -0
  101. package/test/validation-error-handling.test.js +8 -8
  102. package/test/versioned-routes.test.js +28 -16
  103. package/test/wrapThenable.test.js +7 -6
  104. package/types/content-type-parser.d.ts +17 -8
  105. package/types/hooks.d.ts +102 -59
  106. package/types/instance.d.ts +124 -104
  107. package/types/logger.d.ts +18 -104
  108. package/types/plugin.d.ts +10 -4
  109. package/types/reply.d.ts +16 -11
  110. package/types/request.d.ts +10 -5
  111. package/types/route.d.ts +42 -31
  112. package/types/schema.d.ts +1 -1
  113. package/types/type-provider.d.ts +99 -0
  114. package/types/utils.d.ts +1 -1
  115. package/lib/schema-compilers.js +0 -12
  116. package/test/emit-warning.test.js +0 -166
@@ -1,27 +1,27 @@
1
1
  'use strict'
2
2
 
3
3
  const { test } = require('tap')
4
- const Joi = require('@hapi/joi')
4
+ const Joi = require('joi')
5
5
  const AJV = require('ajv')
6
6
  const S = require('fluent-json-schema')
7
7
  const Fastify = require('..')
8
8
  const ajvMergePatch = require('ajv-merge-patch')
9
9
  const ajvErrors = require('ajv-errors')
10
10
 
11
- const buildValidatorAJV8 = require('@fastify/ajv-compiler-8')
11
+ const buildValidatorAJV6 = require('@fastify/ajv-compiler-6')
12
12
 
13
- test('Ajv8 usage instead of the bundle one', t => {
13
+ test('Ajv6 usage instead of the bundle one', t => {
14
14
  t.plan(2)
15
15
 
16
- t.test('use new ajv8 option', t => {
16
+ t.test('use old ajv6 option', t => {
17
17
  t.plan(2)
18
18
  const fastify = Fastify({
19
19
  ajv: {
20
- customOptions: { strictRequired: true }
20
+ customOptions: { jsonPointers: true }
21
21
  },
22
22
  schemaController: {
23
23
  compilersFactory: {
24
- buildValidator: buildValidatorAJV8()
24
+ buildValidator: buildValidatorAJV6()
25
25
  }
26
26
  }
27
27
  })
@@ -42,17 +42,17 @@ test('Ajv8 usage instead of the bundle one', t => {
42
42
  })
43
43
 
44
44
  fastify.ready(err => {
45
- t.ok(err)
46
- t.match(err.message, 'strictRequired', 'the new ajv8 option trigger a startup error')
45
+ t.error(err)
46
+ t.pass('the removed ajv6 option let the startup pass')
47
47
  })
48
48
  })
49
49
 
50
- t.test('use new ajv8 option within a response schema', t => {
50
+ t.test('use old ajv6 option within a response schema', t => {
51
51
  t.plan(2)
52
52
  const fastify = Fastify({
53
53
  schemaController: {
54
54
  compilersFactory: {
55
- buildValidator: buildValidatorAJV8()
55
+ buildValidator: buildValidatorAJV6()
56
56
  }
57
57
  }
58
58
  })
@@ -89,79 +89,11 @@ test('Ajv8 usage instead of the bundle one', t => {
89
89
  })
90
90
  })
91
91
 
92
- test('Ajv8 usage with plugins', t => {
93
- t.plan(2)
94
-
95
- t.test('use new ajv8 option', t => {
96
- t.plan(3)
97
- const fastify = Fastify({
98
- ajv: {
99
- customOptions: { validateFormats: true },
100
- plugins: [require('ajv-formats')]
101
- },
102
- schemaController: {
103
- compilersFactory: {
104
- buildValidator: buildValidatorAJV8()
105
- }
106
- }
107
- })
108
-
109
- callIt(fastify, (err, res) => {
110
- t.error(err)
111
- t.equal(res.statusCode, 400)
112
- t.equal(res.json().message, 'body must match format "date"')
113
- })
114
- })
115
-
116
- t.test('use new ajv8 option - avoid check', t => {
117
- t.plan(2)
118
- const fastify = Fastify({
119
- ajv: {
120
- customOptions: { validateFormats: false }
121
- },
122
- schemaController: {
123
- compilersFactory: {
124
- buildValidator: buildValidatorAJV8()
125
- }
126
- }
127
- })
128
-
129
- callIt(fastify, (err, res) => {
130
- t.error(err)
131
- t.equal(res.statusCode, 200)
132
- })
133
- })
134
-
135
- function callIt (fastify, cb) {
136
- fastify.post('/', {
137
- schema: {
138
- body: {
139
- type: 'object',
140
- properties: {
141
- foo: {
142
- type: 'string',
143
- format: 'date'
144
- }
145
- }
146
- }
147
- },
148
- handler (req, reply) { reply.send({ ok: 1 }) }
149
- })
150
-
151
- fastify.inject({
152
- method: 'POST',
153
- url: '/',
154
- payload: { foo: '99' }
155
- }, cb)
156
- }
157
- })
158
-
159
92
  test('Ajv plugins array parameter', t => {
160
93
  t.plan(3)
161
94
  const fastify = Fastify({
162
95
  ajv: {
163
96
  customOptions: {
164
- jsonPointers: true,
165
97
  allErrors: true
166
98
  },
167
99
  plugins: [
@@ -509,7 +441,7 @@ test('setSchemaController in a plugin', t => {
509
441
  ajvInstance.addSchema(baseSchema)
510
442
  ajvInstance.addSchema(refSchema)
511
443
 
512
- const fastify = Fastify()
444
+ const fastify = Fastify({ exposeHeadRoutes: false })
513
445
  fastify.register(schemaPlugin)
514
446
  fastify.get('/', {
515
447
  schema: {
@@ -760,7 +692,8 @@ test('multiple refs with the same ids', t => {
760
692
 
761
693
  fastify.addSchema(baseSchema)
762
694
  fastify.addSchema(refSchema)
763
- fastify.get('/', {
695
+
696
+ fastify.head('/', {
764
697
  schema: {
765
698
  query: refSchema,
766
699
  response: {
@@ -772,7 +705,7 @@ test('multiple refs with the same ids', t => {
772
705
  }
773
706
  })
774
707
 
775
- fastify.head('/', {
708
+ fastify.get('/', {
776
709
  schema: {
777
710
  query: refSchema,
778
711
  response: {
@@ -99,7 +99,7 @@ test('Basic validation test', t => {
99
99
  url: '/'
100
100
  }, (err, res) => {
101
101
  t.error(err)
102
- t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: "body should have required property 'work'" })
102
+ t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: "body must have required property 'work'" })
103
103
  t.equal(res.statusCode, 400)
104
104
  })
105
105
  })
@@ -314,7 +314,7 @@ test('Triple $ref with a simple $id', t => {
314
314
  }, (err, res) => {
315
315
  t.error(err)
316
316
  t.equal(res.statusCode, 400)
317
- t.same(res.json().message, "body should have required property 'foo'")
317
+ t.same(res.json().message, "body must have required property 'foo'")
318
318
  })
319
319
  })
320
320
 
@@ -348,6 +348,7 @@ test('Extending schema', t => {
348
348
  allOf: [
349
349
  { $ref: 'address.id#/definitions/address' },
350
350
  {
351
+ type: 'object',
351
352
  properties: { type: { enum: ['residential', 'business'] } },
352
353
  required: ['type']
353
354
  }
@@ -439,7 +440,7 @@ test('Should work with nested ids', t => {
439
440
  }, (err, res) => {
440
441
  t.error(err)
441
442
  t.equal(res.statusCode, 400)
442
- t.equal(res.json().message, 'params.id should be number')
443
+ t.equal(res.json().message, 'params/id must be number')
443
444
  })
444
445
  })
445
446
 
@@ -536,7 +537,7 @@ test('JSON Schema validation keywords', t => {
536
537
  t.same(res.json(), {
537
538
  statusCode: 400,
538
539
  error: 'Bad Request',
539
- message: 'params.ip should match format "ipv4"'
540
+ message: 'params/ip must match format "ipv4"'
540
541
  })
541
542
  })
542
543
  })
@@ -590,7 +591,7 @@ test('Nested id calls', t => {
590
591
  t.equal(res.statusCode, 400)
591
592
  t.same(res.json(), {
592
593
  error: 'Bad Request',
593
- message: 'body.host.ip should match format "ipv4"',
594
+ message: 'body/host/ip must match format "ipv4"',
594
595
  statusCode: 400
595
596
  })
596
597
  })
@@ -692,7 +693,7 @@ test('Use shared schema and $ref with $id ($ref to $id)', t => {
692
693
  t.equal(res.statusCode, 400)
693
694
  t.same(res.json(), {
694
695
  error: 'Bad Request',
695
- message: "body should have required property 'address'",
696
+ message: "body must have required property 'address'",
696
697
  statusCode: 400
697
698
  })
698
699
  })
@@ -712,8 +713,7 @@ test('Use items with $ref', t => {
712
713
 
713
714
  const body = {
714
715
  type: 'array',
715
- items: { $ref: 'http://example.com/ref-to-external-validator.json#' },
716
- default: []
716
+ items: { $ref: 'http://example.com/ref-to-external-validator.json#' }
717
717
  }
718
718
 
719
719
  fastify.post('/', {
@@ -809,7 +809,7 @@ test('Use $ref to /definitions', t => {
809
809
  t.equal(res.statusCode, 400)
810
810
  t.same(res.json(), {
811
811
  error: 'Bad Request',
812
- message: 'body.test.id should be number',
812
+ message: 'body/test/id must be number',
813
813
  statusCode: 400
814
814
  })
815
815
  })
@@ -297,7 +297,7 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
297
297
  } else {
298
298
  app.addHook(hookOrHandler, async (req, reply) => {
299
299
  reply.hijack()
300
- reply.send('hello from reply.send()')
300
+ return reply.send('hello from reply.send()')
301
301
  })
302
302
  app.get('/', (req, reply) => t.fail('Handler should not be called'))
303
303
  }
@@ -14,7 +14,8 @@ const JSONStream = require('JSONStream')
14
14
  const send = require('send')
15
15
  const Readable = require('stream').Readable
16
16
  const split = require('split2')
17
- const { kDisableRequestLogging, kReplySent } = require('../lib/symbols.js')
17
+ const semver = require('semver')
18
+ const { kDisableRequestLogging } = require('../lib/symbols.js')
18
19
 
19
20
  function getUrl (app) {
20
21
  const { address, port } = app.server.address()
@@ -26,7 +27,7 @@ function getUrl (app) {
26
27
  }
27
28
 
28
29
  test('should respond with a stream', t => {
29
- t.plan(8)
30
+ t.plan(6)
30
31
  const fastify = Fastify()
31
32
 
32
33
  fastify.get('/', function (req, reply) {
@@ -34,18 +35,13 @@ test('should respond with a stream', t => {
34
35
  reply.code(200).send(stream)
35
36
  })
36
37
 
37
- fastify.get('/error', function (req, reply) {
38
- const stream = fs.createReadStream('not-existing-file', 'utf8')
39
- reply.code(200).send(stream)
40
- })
41
-
42
38
  fastify.listen(0, err => {
43
39
  t.error(err)
44
40
  fastify.server.unref()
45
41
 
46
42
  sget(`http://localhost:${fastify.server.address().port}`, function (err, response, data) {
47
43
  t.error(err)
48
- t.equal(response.headers['content-type'], 'application/octet-stream')
44
+ t.equal(response.headers['content-type'], undefined)
49
45
  t.equal(response.statusCode, 200)
50
46
 
51
47
  fs.readFile(__filename, (err, expected) => {
@@ -53,6 +49,21 @@ test('should respond with a stream', t => {
53
49
  t.equal(expected.toString(), data.toString())
54
50
  })
55
51
  })
52
+ })
53
+ })
54
+
55
+ test('should respond with a stream (error)', t => {
56
+ t.plan(3)
57
+ const fastify = Fastify()
58
+
59
+ fastify.get('/error', function (req, reply) {
60
+ const stream = fs.createReadStream('not-existing-file', 'utf8')
61
+ reply.code(200).send(stream)
62
+ })
63
+
64
+ fastify.listen(0, err => {
65
+ t.error(err)
66
+ fastify.server.unref()
56
67
 
57
68
  sget(`http://localhost:${fastify.server.address().port}/error`, function (err, response) {
58
69
  t.error(err)
@@ -582,7 +593,7 @@ test('return a 404 if the stream emits a 404 error', t => {
582
593
  })
583
594
  })
584
595
 
585
- test('should support send module 200 and 404', { only: true }, t => {
596
+ test('should support send module 200 and 404', { skip: semver.gte(process.versions.node, '17.0.0') }, t => {
586
597
  t.plan(8)
587
598
  const fastify = Fastify()
588
599
 
@@ -604,7 +615,7 @@ test('should support send module 200 and 404', { only: true }, t => {
604
615
 
605
616
  sget(url, function (err, response, data) {
606
617
  t.error(err)
607
- t.equal(response.headers['content-type'], 'application/octet-stream')
618
+ t.equal(response.headers['content-type'], 'application/javascript; charset=UTF-8')
608
619
  t.equal(response.statusCode, 200)
609
620
 
610
621
  fs.readFile(__filename, (err, expected) => {
@@ -654,7 +665,7 @@ test('should mark reply as sent before pumping the payload stream into response
654
665
  const handleRequest = proxyquire('../lib/handleRequest', {
655
666
  './wrapThenable': (thenable, reply) => {
656
667
  thenable.then(function (payload) {
657
- t.equal(reply[kReplySent], true)
668
+ t.equal(reply.sent, true)
658
669
  })
659
670
  }
660
671
  })
@@ -671,7 +682,7 @@ test('should mark reply as sent before pumping the payload stream into response
671
682
 
672
683
  fastify.get('/', async function (req, reply) {
673
684
  const stream = fs.createReadStream(__filename, 'utf8')
674
- reply.code(200).send(stream)
685
+ return reply.code(200).send(stream)
675
686
  })
676
687
 
677
688
  fastify.inject({
@@ -17,12 +17,15 @@ test('Fastify should throw on wrong options', t => {
17
17
  test('Fastify should throw on multiple assignment to the same route', t => {
18
18
  t.plan(1)
19
19
  const fastify = Fastify()
20
- fastify.get('/', () => {})
20
+
21
21
  fastify.get('/', () => {})
22
22
 
23
- fastify.ready(err => {
24
- t.equal(err.message, "Method 'GET' already declared for route '/' with constraints '{}'")
25
- })
23
+ try {
24
+ fastify.get('/', () => {})
25
+ t.fail('Should throw on duplicated route declaration')
26
+ } catch (error) {
27
+ t.equal(error.message, "Method 'GET' already declared for route '/'")
28
+ }
26
29
  })
27
30
 
28
31
  test('Fastify should throw for an invalid schema, printing the error route - headers', t => {
@@ -78,7 +81,7 @@ test('Fastify should throw for an invalid shorthand option type', t => {
78
81
  t.fail()
79
82
  } catch (e) {
80
83
  t.equal(e.code, 'FST_ERR_INIT_OPTS_INVALID')
81
- t.match(e.message, /should be boolean/)
84
+ t.match(e.message, /must be boolean/)
82
85
  t.pass()
83
86
  }
84
87
  })
@@ -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'
@@ -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) => {