fastify 3.27.4 → 4.0.0-alpha.3

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 (168) 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/Ecosystem.md +9 -0
  7. package/docs/Guides/Getting-Started.md +7 -7
  8. package/docs/Guides/Plugins-Guide.md +1 -1
  9. package/docs/Guides/Serverless.md +3 -3
  10. package/docs/Guides/Testing.md +2 -2
  11. package/docs/Migration-Guide-V4.md +12 -0
  12. package/docs/Reference/ContentTypeParser.md +4 -0
  13. package/docs/Reference/Decorators.md +2 -2
  14. package/docs/Reference/Encapsulation.md +2 -2
  15. package/docs/Reference/Errors.md +51 -6
  16. package/docs/Reference/HTTP2.md +3 -3
  17. package/docs/Reference/Hooks.md +4 -7
  18. package/docs/Reference/LTS.md +5 -4
  19. package/docs/Reference/Plugins.md +3 -3
  20. package/docs/Reference/Reply.md +73 -22
  21. package/docs/Reference/Request.md +1 -3
  22. package/docs/Reference/Routes.md +22 -15
  23. package/docs/Reference/Server.md +69 -119
  24. package/docs/Reference/TypeScript.md +20 -22
  25. package/docs/Reference/Validation-and-Serialization.md +30 -55
  26. package/docs/Type-Providers.md +257 -0
  27. package/examples/asyncawait.js +1 -1
  28. package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
  29. package/examples/benchmark/hooks-benchmark.js +1 -1
  30. package/examples/benchmark/simple.js +1 -1
  31. package/examples/hooks.js +2 -2
  32. package/examples/http2.js +1 -1
  33. package/examples/https.js +1 -1
  34. package/examples/parser.js +13 -3
  35. package/examples/route-prefix.js +1 -1
  36. package/examples/shared-schema.js +1 -1
  37. package/examples/simple-stream.js +18 -0
  38. package/examples/simple.js +1 -1
  39. package/examples/simple.mjs +1 -1
  40. package/examples/typescript-server.ts +1 -1
  41. package/examples/use-plugin.js +1 -1
  42. package/fastify.d.ts +34 -22
  43. package/fastify.js +40 -36
  44. package/lib/configValidator.js +902 -1023
  45. package/lib/contentTypeParser.js +6 -16
  46. package/lib/context.js +36 -10
  47. package/lib/decorate.js +3 -1
  48. package/lib/error-handler.js +158 -0
  49. package/lib/error-serializer.js +257 -0
  50. package/lib/errors.js +51 -9
  51. package/lib/fourOhFour.js +31 -20
  52. package/lib/handleRequest.js +10 -13
  53. package/lib/hooks.js +14 -9
  54. package/lib/pluginOverride.js +0 -3
  55. package/lib/pluginUtils.js +3 -2
  56. package/lib/reply.js +121 -175
  57. package/lib/request.js +13 -10
  58. package/lib/route.js +131 -138
  59. package/lib/schema-controller.js +2 -2
  60. package/lib/schemas.js +27 -1
  61. package/lib/server.js +242 -116
  62. package/lib/symbols.js +5 -3
  63. package/lib/validation.js +11 -9
  64. package/lib/warnings.js +4 -12
  65. package/lib/wrapThenable.js +4 -11
  66. package/package.json +37 -39
  67. package/test/404s.test.js +258 -125
  68. package/test/500s.test.js +3 -3
  69. package/test/als.test.js +1 -1
  70. package/test/async-await.test.js +20 -76
  71. package/test/bodyLimit.test.js +1 -1
  72. package/test/build-certificate.js +6 -7
  73. package/test/case-insensitive.test.js +4 -4
  74. package/test/close-pipelining.test.js +2 -2
  75. package/test/close.test.js +11 -11
  76. package/test/content-parser.test.js +32 -0
  77. package/test/context-config.test.js +52 -0
  78. package/test/custom-http-server.test.js +14 -7
  79. package/test/custom-parser-async.test.js +1 -66
  80. package/test/custom-parser.test.js +92 -159
  81. package/test/custom-querystring-parser.test.js +3 -3
  82. package/test/decorator.test.js +11 -13
  83. package/test/delete.test.js +6 -6
  84. package/test/encapsulated-error-handler.test.js +50 -0
  85. package/test/esm/index.test.js +0 -14
  86. package/test/fastify-instance.test.js +4 -4
  87. package/test/fluent-schema.test.js +4 -4
  88. package/test/genReqId.test.js +1 -1
  89. package/test/get.test.js +4 -4
  90. package/test/handler-context.test.js +2 -2
  91. package/test/head.test.js +1 -1
  92. package/test/helper.js +19 -4
  93. package/test/hooks-async.test.js +15 -48
  94. package/test/hooks.on-ready.test.js +10 -5
  95. package/test/hooks.test.js +78 -119
  96. package/test/http2/closing.test.js +10 -16
  97. package/test/http2/constraint.test.js +1 -1
  98. package/test/http2/head.test.js +1 -1
  99. package/test/http2/plain.test.js +1 -1
  100. package/test/http2/secure-with-fallback.test.js +1 -1
  101. package/test/http2/secure.test.js +1 -1
  102. package/test/http2/unknown-http-method.test.js +4 -10
  103. package/test/https/custom-https-server.test.js +12 -6
  104. package/test/https/https.test.js +1 -1
  105. package/test/input-validation.js +3 -3
  106. package/test/internals/handleRequest.test.js +6 -43
  107. package/test/internals/initialConfig.test.js +41 -12
  108. package/test/internals/logger.test.js +2 -2
  109. package/test/internals/reply.test.js +317 -48
  110. package/test/internals/request.test.js +13 -7
  111. package/test/internals/server.test.js +88 -0
  112. package/test/listen.deprecated.test.js +202 -0
  113. package/test/listen.test.js +140 -145
  114. package/test/logger.test.js +82 -42
  115. package/test/maxRequestsPerSocket.test.js +8 -6
  116. package/test/middleware.test.js +2 -25
  117. package/test/nullable-validation.test.js +53 -16
  118. package/test/output-validation.test.js +1 -1
  119. package/test/plugin.test.js +47 -21
  120. package/test/pretty-print.test.js +22 -10
  121. package/test/promises.test.js +1 -1
  122. package/test/proto-poisoning.test.js +6 -6
  123. package/test/register.test.js +3 -3
  124. package/test/reply-error.test.js +126 -15
  125. package/test/reply-trailers.test.js +270 -0
  126. package/test/request-error.test.js +3 -6
  127. package/test/route-hooks.test.js +18 -18
  128. package/test/route-prefix.test.js +2 -1
  129. package/test/route.test.js +206 -22
  130. package/test/router-options.test.js +2 -2
  131. package/test/schema-examples.test.js +11 -5
  132. package/test/schema-feature.test.js +25 -20
  133. package/test/schema-serialization.test.js +9 -9
  134. package/test/schema-special-usage.test.js +5 -153
  135. package/test/schema-validation.test.js +9 -9
  136. package/test/skip-reply-send.test.js +2 -2
  137. package/test/stream.test.js +82 -23
  138. package/test/throw.test.js +8 -5
  139. package/test/trust-proxy.test.js +6 -6
  140. package/test/type-provider.test.js +20 -0
  141. package/test/types/fastify.test-d.ts +10 -18
  142. package/test/types/hooks.test-d.ts +61 -5
  143. package/test/types/import.js +2 -0
  144. package/test/types/import.ts +1 -0
  145. package/test/types/instance.test-d.ts +68 -17
  146. package/test/types/logger.test-d.ts +44 -15
  147. package/test/types/reply.test-d.ts +2 -1
  148. package/test/types/request.test-d.ts +71 -1
  149. package/test/types/route.test-d.ts +8 -2
  150. package/test/types/schema.test-d.ts +2 -39
  151. package/test/types/type-provider.test-d.ts +424 -0
  152. package/test/url-rewriting.test.js +3 -3
  153. package/test/validation-error-handling.test.js +8 -8
  154. package/test/versioned-routes.test.js +30 -18
  155. package/test/wrapThenable.test.js +7 -6
  156. package/types/content-type-parser.d.ts +17 -8
  157. package/types/hooks.d.ts +182 -85
  158. package/types/instance.d.ts +286 -118
  159. package/types/logger.d.ts +18 -104
  160. package/types/plugin.d.ts +10 -4
  161. package/types/reply.d.ts +18 -12
  162. package/types/request.d.ts +13 -8
  163. package/types/route.d.ts +62 -34
  164. package/types/schema.d.ts +1 -1
  165. package/types/type-provider.d.ts +99 -0
  166. package/types/utils.d.ts +1 -1
  167. package/lib/schema-compilers.js +0 -12
  168. package/test/emit-warning.test.js +0 -166
@@ -0,0 +1,257 @@
1
+ <h1 align="center">Fastify</h1>
2
+
3
+ ## Type Providers
4
+
5
+ Type Providers are a TypeScript only feature that enables Fastify to statically infer type information directly from inline JSON Schema. They are an alternative to specifying generic arguments on routes; and can greatly reduce the need to keep associated types for each schema defined in your project.
6
+
7
+ ### Providers
8
+
9
+ Type Providers are offered as additional packages you will need to install into your project. Each provider uses a different inference library under the hood; allowing you to select the library most appropriate for your needs. Type Provider packages follow a `fastify-type-provider-{provider-name}` naming convention.
10
+
11
+ The following inference packages are supported:
12
+
13
+ - `json-schema-to-ts` - [github](https://github.com/ThomasAribart/json-schema-to-ts)
14
+ - `typebox` - [github](https://github.com/sinclairzx81/typebox)
15
+
16
+ ### Json Schema to Ts
17
+
18
+ The following sets up a `json-schema-to-ts` Type Provider
19
+
20
+ ```bash
21
+ $ npm install fastify-type-provider-json-schema-to-ts --save
22
+ ```
23
+
24
+ ```typescript
25
+ import { JsonSchemaToTsTypeProvider } from 'fastify-type-provider-json-schema-to-ts'
26
+
27
+ import fastify from 'fastify'
28
+
29
+ const server = fastify().withTypeProvider<JsonSchemaToTsTypeProvider>()
30
+
31
+ server.get('/route', {
32
+ schema: {
33
+ querystring: {
34
+ type: 'object',
35
+ properties: {
36
+ foo: { type: 'number' },
37
+ bar: { type: 'string' },
38
+ },
39
+ required: ['foo', 'bar']
40
+ }
41
+ } as const // don't forget to use const !
42
+
43
+ }, (request, reply) => {
44
+
45
+ // type Query = { foo: number, bar: string }
46
+
47
+ const { foo, bar } = request.query // type safe!
48
+ })
49
+ ```
50
+
51
+ ### TypeBox
52
+
53
+ The following sets up a TypeBox Type Provider
54
+
55
+ ```bash
56
+ $ npm install fastify-type-provider-typebox --save
57
+ ```
58
+
59
+ ```typescript
60
+ import { TypeBoxTypeProvider, Type } from 'fastify-type-provider-typebox'
61
+
62
+ import fastify from 'fastify'
63
+
64
+ const server = fastify({
65
+ ajv: {
66
+ customOptions: {
67
+ strict: 'log',
68
+ keywords: ['kind', 'modifier'],
69
+ },
70
+ },
71
+ }).withTypeProvider<TypeBoxTypeProvider>()
72
+
73
+ server.get('/route', {
74
+ schema: {
75
+ querystring: Type.Object({
76
+ foo: Type.Number(),
77
+ bar: Type.String()
78
+ })
79
+ }
80
+ }, (request, reply) => {
81
+
82
+ // type Query = { foo: number, bar: string }
83
+
84
+ const { foo, bar } = request.query // type safe!
85
+ })
86
+ ```
87
+
88
+ TypeBox uses the properties `kind` and `modifier` internally. These properties are not strictly valid JSON schema which will cause `AJV@7` and newer versions to throw an invalid schema error. To remove the error it's either necessary to omit the properties by using [`Type.Strict()`](https://github.com/sinclairzx81/typebox#strict) or use the AJV options for adding custom keywords.
89
+
90
+ See also the [TypeBox documentation](https://github.com/sinclairzx81/typebox#validation) on how to set up AJV to work with TypeBox.
91
+
92
+ ### Scoped Type-Provider
93
+
94
+ The provider types don't propagate globally. In encapsulated usage, one can remap the context to use one or more providers (for example, `typebox` and `json-schema-to-ts` can be used in the same application).
95
+
96
+ Example:
97
+
98
+ ```ts
99
+ import Fastify from 'fastify'
100
+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
101
+ import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts'
102
+ import { Type } from '@sinclair/typebox'
103
+
104
+ const fastify = Fastify()
105
+
106
+ function pluginWithTypebox(fastify: FastifyInstance, _opts, done): void {
107
+ fastify.withTypeProvider<TypeBoxTypeProvider>()
108
+ .get('/', {
109
+ schema: {
110
+ body: Type.Object({
111
+ x: Type.String(),
112
+ y: Type.Number(),
113
+ z: Type.Boolean()
114
+ })
115
+ }
116
+ }, (req) => {
117
+ const { x, y, z } = req.body // type safe
118
+ });
119
+ done()
120
+ }
121
+
122
+ function pluginWithJsonSchema(fastify: FastifyInstance, _opts, done): void {
123
+ fastify.withTypeProvider<JsonSchemaToTsProvider>()
124
+ .get('/', {
125
+ schema: {
126
+ body: {
127
+ type: 'object',
128
+ properties: {
129
+ x: { type: 'string' },
130
+ y: { type: 'number' },
131
+ z: { type: 'boolean' }
132
+ },
133
+ } as const
134
+ }
135
+ }, (req) => {
136
+ const { x, y, z } = req.body // type safe
137
+ });
138
+ done()
139
+ }
140
+
141
+ fastify.register(pluginWithJsonSchema)
142
+ fastify.register(pluginWithTypebox)
143
+ ```
144
+
145
+ It's also important to mention that once the types don't propagate globally, _currently_ is not possible to avoid multiple registrations on routes when dealing with several scopes, see bellow:
146
+
147
+ ```ts
148
+ import Fastify from 'fastify'
149
+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
150
+ import { Type } from '@sinclair/typebox'
151
+
152
+ const server = Fastify({
153
+ ajv: {
154
+ customOptions: {
155
+ strict: 'log',
156
+ keywords: ['kind', 'modifier'],
157
+ },
158
+ },
159
+ }).withTypeProvider<TypeBoxTypeProvider>()
160
+
161
+ server.register(plugin1) // wrong
162
+ server.register(plugin2) // correct
163
+
164
+ function plugin1(fastify: FastifyInstance, _opts, done): void {
165
+ fastify.get('/', {
166
+ schema: {
167
+ body: Type.Object({
168
+ x: Type.String(),
169
+ y: Type.Number(),
170
+ z: Type.Boolean()
171
+ })
172
+ }
173
+ }, (req) => {
174
+ // it doesn't works! in a new scope needs to call `withTypeProvider` again
175
+ const { x, y, z } = req.body
176
+ });
177
+ done()
178
+ }
179
+
180
+ function plugin2(fastify: FastifyInstance, _opts, done): void {
181
+ const server = fastify.withTypeProvider<TypeBoxTypeProvider>()
182
+
183
+ server.get('/', {
184
+ schema: {
185
+ body: Type.Object({
186
+ x: Type.String(),
187
+ y: Type.Number(),
188
+ z: Type.Boolean()
189
+ })
190
+ }
191
+ }, (req) => {
192
+ // works
193
+ const { x, y, z } = req.body
194
+ });
195
+ done()
196
+ }
197
+ ```
198
+
199
+ ### Type Definition of FastifyInstance + TypeProvider
200
+
201
+ When working with modules one has to make use of `FastifyInstance` with Type Provider generics. See the example below:
202
+
203
+ ```ts
204
+ // index.ts
205
+ import Fastify from 'fastify'
206
+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
207
+ import { registerRoutes } from './routes'
208
+
209
+ const server = Fastify({
210
+ ajv: {
211
+ customOptions: {
212
+ strict: 'log',
213
+ keywords: ['kind', 'modifier'],
214
+ },
215
+ },
216
+ }).withTypeProvider<TypeBoxTypeProvider>()
217
+
218
+ registerRoutes(server)
219
+
220
+ server.listen({ port: 3000 })
221
+ ```
222
+
223
+ ```ts
224
+ // routes.ts
225
+ import { Type } from '@sinclair/typebox'
226
+ import {
227
+ FastifyInstance,
228
+ FastifyLoggerInstance,
229
+ RawReplyDefaultExpression,
230
+ RawRequestDefaultExpression,
231
+ RawServerDefault
232
+ } from 'fastify'
233
+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
234
+
235
+ type FastifyTypebox = FastifyInstance<
236
+ RawServerDefault,
237
+ RawRequestDefaultExpression<RawServerDefault>,
238
+ RawReplyDefaultExpression<RawServerDefault>,
239
+ FastifyLoggerInstance,
240
+ TypeBoxTypeProvider
241
+ >;
242
+
243
+ export function registerRoutes(fastify: FastifyTypebox): void {
244
+ fastify.get('/', {
245
+ schema: {
246
+ body: Type.Object({
247
+ x: Type.String(),
248
+ y: Type.Number(),
249
+ z: Type.Boolean()
250
+ })
251
+ }
252
+ }, (req) => {
253
+ // works
254
+ const { x, y, z } = req.body
255
+ });
256
+ }
257
+ ```
@@ -31,6 +31,6 @@ fastify
31
31
  return { hello: 'world' }
32
32
  })
33
33
 
34
- fastify.listen(3000, err => {
34
+ fastify.listen({ port: 3000 }, err => {
35
35
  if (err) throw err
36
36
  })
@@ -37,7 +37,7 @@ fastify.get('/', opts, function (request, reply) {
37
37
  reply.send({ hello: 'world' })
38
38
  })
39
39
 
40
- fastify.listen(3000, function (err) {
40
+ fastify.listen({ port: 3000 }, function (err) {
41
41
  if (err) {
42
42
  throw err
43
43
  }
@@ -45,7 +45,7 @@ fastify.get('/', opts, function (request, reply) {
45
45
  reply.send({ hello: 'world' })
46
46
  })
47
47
 
48
- fastify.listen(3000, function (err) {
48
+ fastify.listen({ port: 3000 }, function (err) {
49
49
  if (err) {
50
50
  throw err
51
51
  }
@@ -25,6 +25,6 @@ fastify
25
25
  .send({ hello: 'world' })
26
26
  })
27
27
 
28
- fastify.listen(3000, (err, address) => {
28
+ fastify.listen({ port: 3000 }, (err, address) => {
29
29
  if (err) throw err
30
30
  })
package/examples/hooks.js CHANGED
@@ -37,7 +37,7 @@ fastify
37
37
  console.log('onRequest')
38
38
  done()
39
39
  })
40
- .addHook('preParsing', function (request, reply, done) {
40
+ .addHook('preParsing', function (request, reply, payload, done) {
41
41
  console.log('preParsing')
42
42
  done()
43
43
  })
@@ -81,7 +81,7 @@ fastify.post('/', optsPost, function (req, reply) {
81
81
  reply.send({ hello: 'world' })
82
82
  })
83
83
 
84
- fastify.listen(3000, function (err) {
84
+ fastify.listen({ port: 3000 }, function (err) {
85
85
  if (err) {
86
86
  throw err
87
87
  }
package/examples/http2.js CHANGED
@@ -32,6 +32,6 @@ fastify
32
32
  reply.send({ hello: 'world' })
33
33
  })
34
34
 
35
- fastify.listen(3000, err => {
35
+ fastify.listen({ port: 3000 }, err => {
36
36
  if (err) throw err
37
37
  })
package/examples/https.js CHANGED
@@ -31,6 +31,6 @@ fastify
31
31
  reply.send({ hello: 'world' })
32
32
  })
33
33
 
34
- fastify.listen(3000, err => {
34
+ fastify.listen({ port: 3000 }, err => {
35
35
  if (err) throw err
36
36
  })
@@ -2,7 +2,7 @@
2
2
 
3
3
  const fastify = require('../fastify')({ logger: true })
4
4
  const jsonParser = require('fast-json-body')
5
- const qs = require('qs')
5
+ const querystring = require('querystring')
6
6
 
7
7
  // Handled by fastify
8
8
  // curl -X POST -d '{"hello":"world"}' -H'Content-type: application/json' http://localhost:3000/
@@ -22,7 +22,7 @@ fastify.addContentTypeParser('application/x-www-form-urlencoded', function (requ
22
22
  })
23
23
  payload.on('end', function () {
24
24
  try {
25
- const parsed = qs.parse(body)
25
+ const parsed = querystring.parse(body)
26
26
  done(null, parsed)
27
27
  } catch (e) {
28
28
  done(e)
@@ -31,11 +31,21 @@ fastify.addContentTypeParser('application/x-www-form-urlencoded', function (requ
31
31
  payload.on('error', done)
32
32
  })
33
33
 
34
+ // curl -X POST -d '{"hello":"world"}' -H'Content-type: application/vnd.custom+json' http://localhost:3000/
35
+ fastify.addContentTypeParser(/^application\/.+\+json$/, { parseAs: 'string' }, fastify.getDefaultJsonParser('error', 'ignore'))
36
+
37
+ // remove default json parser
38
+ // curl -X POST -d '{"hello":"world"}' -H'Content-type: application/json' http://localhost:3000/ is now no longer handled by fastify
39
+ fastify.removeContentTypeParser('application/json')
40
+
41
+ // This call would remove any content type parser
42
+ // fastify.removeAllContentTypeParsers()
43
+
34
44
  fastify
35
45
  .post('/', function (req, reply) {
36
46
  reply.send(req.body)
37
47
  })
38
48
 
39
- fastify.listen(3000, err => {
49
+ fastify.listen({ port: 3000 }, err => {
40
50
  if (err) throw err
41
51
  })
@@ -31,7 +31,7 @@ fastify.register(function (instance, options, done) {
31
31
  done()
32
32
  }, { prefix: '/italian' })
33
33
 
34
- fastify.listen(8000, function (err) {
34
+ fastify.listen({ port: 8000 }, function (err) {
35
35
  if (err) {
36
36
  throw err
37
37
  }
@@ -31,6 +31,6 @@ fastify
31
31
  reply.send({ hello: 'world' })
32
32
  })
33
33
 
34
- fastify.listen(3000, err => {
34
+ fastify.listen({ port: 3000 }, err => {
35
35
  if (err) throw err
36
36
  })
@@ -0,0 +1,18 @@
1
+ 'use strict'
2
+
3
+ const fastify = require('../fastify')({
4
+ logger: false
5
+ })
6
+
7
+ const Readable = require('stream').Readable
8
+
9
+ fastify
10
+ .get('/', function (req, reply) {
11
+ const stream = Readable.from(['hello world'])
12
+ reply.send(stream)
13
+ })
14
+
15
+ fastify.listen({ port: 3000 }, (err, address) => {
16
+ if (err) throw err
17
+ fastify.log.info(`server listening on ${address}`)
18
+ })
@@ -25,6 +25,6 @@ fastify
25
25
  .send({ hello: 'world' })
26
26
  })
27
27
 
28
- fastify.listen(3000, (err, address) => {
28
+ fastify.listen({ port: 3000 }, (err, address) => {
29
29
  if (err) throw err
30
30
  })
@@ -24,4 +24,4 @@ app.get('/', schema, async function (req, reply) {
24
24
  return { hello: 'world' }
25
25
  })
26
26
 
27
- app.listen(3000).catch(console.error)
27
+ app.listen({ port: 3000 }).catch(console.error)
@@ -70,7 +70,7 @@ server.get<{
70
70
  });
71
71
 
72
72
  // Start your server
73
- server.listen(8080, (err, address) => {
73
+ server.listen({ port: 8080 }, (err, address) => {
74
74
  if (err) {
75
75
  console.error(err);
76
76
  process.exit(1);
@@ -18,7 +18,7 @@ fastify.register(require('./plugin'), opts, function (err) {
18
18
  if (err) throw err
19
19
  })
20
20
 
21
- fastify.listen(3000, function (err) {
21
+ fastify.listen({ port: 3000 }, function (err) {
22
22
  if (err) {
23
23
  throw err
24
24
  }
package/fastify.d.ts CHANGED
@@ -5,18 +5,21 @@ import { ConstraintStrategy, HTTPVersion } from 'find-my-way'
5
5
 
6
6
  import { FastifyRequest, RequestGenericInterface } from './types/request'
7
7
  import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression } from './types/utils'
8
- import { FastifyLoggerInstance, FastifyLoggerOptions } from './types/logger'
8
+ import { FastifyBaseLogger, FastifyLoggerInstance, FastifyLoggerOptions, PinoLoggerOptions } from './types/logger'
9
9
  import { FastifyInstance } from './types/instance'
10
10
  import { FastifyServerFactory } from './types/serverFactory'
11
11
  import { Options as AjvOptions } from '@fastify/ajv-compiler'
12
+ import { Options as FJSOptions } from '@fastify/fast-json-stringify-compiler'
12
13
  import { FastifyError } from 'fastify-error'
13
14
  import { FastifyReply } from './types/reply'
14
15
  import { FastifySchemaValidationError } from './types/schema'
15
16
  import { ConstructorAction, ProtoAction } from "./types/content-type-parser";
16
17
  import { Socket } from 'net'
17
- import { Options as FJSOptions } from 'fast-json-stringify'
18
18
  import { ValidatorCompiler } from '@fastify/ajv-compiler'
19
- import { FastifySerializerCompiler } from './types/schema';
19
+ import { SerializerCompiler } from '@fastify/fast-json-stringify-compiler'
20
+ import { FastifySchema } from './types/schema'
21
+ import { FastifyContextConfig } from './types/context'
22
+ import { FastifyTypeProvider, FastifyTypeProviderDefault } from './types/type-provider'
20
23
 
21
24
  /**
22
25
  * Fastify factory function for the standard fastify http, https, or http2 server instance.
@@ -30,31 +33,39 @@ declare function fastify<
30
33
  Server extends http2.Http2SecureServer,
31
34
  Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
32
35
  Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
33
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
34
- >(opts: FastifyHttp2SecureOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
36
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance,
37
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
38
+ >(opts: FastifyHttp2SecureOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
39
+
35
40
  declare function fastify<
36
41
  Server extends http2.Http2Server,
37
42
  Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
38
43
  Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
39
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
40
- >(opts: FastifyHttp2Options<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
44
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance,
45
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
46
+ >(opts: FastifyHttp2Options<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
47
+
41
48
  declare function fastify<
42
49
  Server extends https.Server,
43
50
  Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
44
51
  Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
45
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
46
- >(opts: FastifyHttpsOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
52
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance,
53
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
54
+ >(opts: FastifyHttpsOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
55
+
47
56
  declare function fastify<
48
57
  Server extends http.Server,
49
58
  Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
50
59
  Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
51
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
52
- >(opts?: FastifyServerOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger>>
60
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance,
61
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
62
+ >(opts?: FastifyServerOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
63
+
53
64
  export default fastify
54
65
 
55
66
  export type FastifyHttp2SecureOptions<
56
67
  Server extends http2.Http2SecureServer,
57
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
68
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance
58
69
  > = FastifyServerOptions<Server, Logger> & {
59
70
  http2: true,
60
71
  https: http2.SecureServerOptions,
@@ -63,7 +74,7 @@ export type FastifyHttp2SecureOptions<
63
74
 
64
75
  export type FastifyHttp2Options<
65
76
  Server extends http2.Http2Server,
66
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
77
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance
67
78
  > = FastifyServerOptions<Server, Logger> & {
68
79
  http2: true,
69
80
  http2SessionTimeout?: number
@@ -71,7 +82,7 @@ export type FastifyHttp2Options<
71
82
 
72
83
  export type FastifyHttpsOptions<
73
84
  Server extends https.Server,
74
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
85
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance
75
86
  > = FastifyServerOptions<Server, Logger> & {
76
87
  https: https.ServerOptions
77
88
  }
@@ -92,7 +103,7 @@ export interface ConnectionError extends Error {
92
103
  */
93
104
  export type FastifyServerOptions<
94
105
  RawServer extends RawServerBase = RawServerDefault,
95
- Logger extends FastifyLoggerInstance = FastifyLoggerInstance
106
+ Logger extends FastifyBaseLogger = FastifyLoggerInstance
96
107
  > = {
97
108
  ignoreTrailingSlash?: boolean,
98
109
  connectionTimeout?: number,
@@ -107,14 +118,14 @@ export type FastifyServerOptions<
107
118
  exposeHeadRoutes?: boolean,
108
119
  onProtoPoisoning?: ProtoAction,
109
120
  onConstructorPoisoning?: ConstructorAction,
110
- logger?: boolean | FastifyLoggerOptions<RawServer> | Logger,
121
+ logger?: boolean | FastifyLoggerOptions<RawServer> & PinoLoggerOptions | Logger,
111
122
  serializerOpts?: FJSOptions | Record<string, unknown>,
112
123
  serverFactory?: FastifyServerFactory<RawServer>,
113
124
  caseSensitive?: boolean,
114
125
  requestIdHeader?: string,
115
126
  requestIdLogLabel?: string;
116
127
  jsonShorthand?: boolean;
117
- genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface>(req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>>) => string,
128
+ genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault>(req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>) => string,
118
129
  trustProxy?: boolean | string | string[] | number | TrustProxyFunction,
119
130
  querystringParser?: (str: string) => { [key: string]: unknown },
120
131
  /**
@@ -140,7 +151,7 @@ export type FastifyServerOptions<
140
151
  };
141
152
  compilersFactory?: {
142
153
  buildValidator?: ValidatorCompiler;
143
- buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions["serializerOpts"]) => FastifySerializerCompiler<unknown>;
154
+ buildSerializer?: SerializerCompiler;
144
155
  };
145
156
  };
146
157
  return503OnClosing?: boolean,
@@ -148,10 +159,10 @@ export type FastifyServerOptions<
148
159
  customOptions?: AjvOptions,
149
160
  plugins?: (Function | [Function, unknown])[]
150
161
  },
151
- frameworkErrors?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface>(
162
+ frameworkErrors?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema>(
152
163
  error: FastifyError,
153
- req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>>,
154
- res: FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>>
164
+ req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>,
165
+ res: FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RequestGeneric, FastifyContextConfig, SchemaCompiler, TypeProvider>
155
166
  ) => void,
156
167
  rewriteUrl?: (req: RawRequestDefaultExpression<RawServer>) => string,
157
168
  schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error,
@@ -183,7 +194,7 @@ export { FastifyRequest, RequestGenericInterface } from './types/request'
183
194
  export { FastifyReply } from './types/reply'
184
195
  export { FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, FastifyPlugin } from './types/plugin'
185
196
  export { FastifyInstance, PrintRoutesOptions } from './types/instance'
186
- export { FastifyLoggerOptions, FastifyLoggerInstance, FastifyLogFn, LogLevel } from './types/logger'
197
+ export { FastifyLoggerOptions, FastifyBaseLogger, FastifyLoggerInstance, FastifyLogFn, LogLevel } from './types/logger'
187
198
  export { FastifyContext, FastifyContextConfig } from './types/context'
188
199
  export { RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler } from './types/route'
189
200
  export * from './types/register'
@@ -193,4 +204,5 @@ export { FastifySchema, FastifySchemaCompiler } from './types/schema'
193
204
  export { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './types/utils'
194
205
  export * from './types/hooks'
195
206
  export { FastifyServerFactory, FastifyServerFactoryHandler } from './types/serverFactory'
207
+ export { FastifyTypeProvider, FastifyTypeProviderDefault } from './types/type-provider'
196
208
  export { fastify }