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.
- package/.taprc +3 -0
- package/README.md +7 -7
- package/build/build-error-serializer.js +27 -0
- package/build/build-validation.js +47 -35
- package/docs/Guides/Database.md +320 -0
- package/docs/Guides/Ecosystem.md +9 -0
- package/docs/Guides/Getting-Started.md +7 -7
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Serverless.md +3 -3
- package/docs/Guides/Testing.md +2 -2
- package/docs/Migration-Guide-V4.md +12 -0
- package/docs/Reference/ContentTypeParser.md +4 -0
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Encapsulation.md +2 -2
- package/docs/Reference/Errors.md +51 -6
- package/docs/Reference/HTTP2.md +3 -3
- package/docs/Reference/Hooks.md +4 -7
- package/docs/Reference/LTS.md +5 -4
- package/docs/Reference/Plugins.md +3 -3
- package/docs/Reference/Reply.md +73 -22
- package/docs/Reference/Request.md +1 -3
- package/docs/Reference/Routes.md +22 -15
- package/docs/Reference/Server.md +69 -119
- package/docs/Reference/TypeScript.md +20 -22
- package/docs/Reference/Validation-and-Serialization.md +30 -55
- package/docs/Type-Providers.md +257 -0
- package/examples/asyncawait.js +1 -1
- package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
- package/examples/benchmark/hooks-benchmark.js +1 -1
- package/examples/benchmark/simple.js +1 -1
- package/examples/hooks.js +2 -2
- package/examples/http2.js +1 -1
- package/examples/https.js +1 -1
- package/examples/parser.js +13 -3
- package/examples/route-prefix.js +1 -1
- package/examples/shared-schema.js +1 -1
- package/examples/simple-stream.js +18 -0
- package/examples/simple.js +1 -1
- package/examples/simple.mjs +1 -1
- package/examples/typescript-server.ts +1 -1
- package/examples/use-plugin.js +1 -1
- package/fastify.d.ts +34 -22
- package/fastify.js +40 -36
- package/lib/configValidator.js +902 -1023
- package/lib/contentTypeParser.js +6 -16
- package/lib/context.js +36 -10
- package/lib/decorate.js +3 -1
- package/lib/error-handler.js +158 -0
- package/lib/error-serializer.js +257 -0
- package/lib/errors.js +51 -9
- package/lib/fourOhFour.js +31 -20
- package/lib/handleRequest.js +10 -13
- package/lib/hooks.js +14 -9
- package/lib/pluginOverride.js +0 -3
- package/lib/pluginUtils.js +3 -2
- package/lib/reply.js +121 -175
- package/lib/request.js +13 -10
- package/lib/route.js +131 -138
- package/lib/schema-controller.js +2 -2
- package/lib/schemas.js +27 -1
- package/lib/server.js +242 -116
- package/lib/symbols.js +5 -3
- package/lib/validation.js +11 -9
- package/lib/warnings.js +4 -12
- package/lib/wrapThenable.js +4 -11
- package/package.json +37 -39
- package/test/404s.test.js +258 -125
- package/test/500s.test.js +3 -3
- package/test/als.test.js +1 -1
- package/test/async-await.test.js +20 -76
- package/test/bodyLimit.test.js +1 -1
- package/test/build-certificate.js +6 -7
- package/test/case-insensitive.test.js +4 -4
- package/test/close-pipelining.test.js +2 -2
- package/test/close.test.js +11 -11
- package/test/content-parser.test.js +32 -0
- package/test/context-config.test.js +52 -0
- package/test/custom-http-server.test.js +14 -7
- package/test/custom-parser-async.test.js +1 -66
- package/test/custom-parser.test.js +92 -159
- package/test/custom-querystring-parser.test.js +3 -3
- package/test/decorator.test.js +11 -13
- package/test/delete.test.js +6 -6
- package/test/encapsulated-error-handler.test.js +50 -0
- package/test/esm/index.test.js +0 -14
- package/test/fastify-instance.test.js +4 -4
- package/test/fluent-schema.test.js +4 -4
- package/test/genReqId.test.js +1 -1
- package/test/get.test.js +4 -4
- package/test/handler-context.test.js +2 -2
- package/test/head.test.js +1 -1
- package/test/helper.js +19 -4
- package/test/hooks-async.test.js +15 -48
- package/test/hooks.on-ready.test.js +10 -5
- package/test/hooks.test.js +78 -119
- package/test/http2/closing.test.js +10 -16
- package/test/http2/constraint.test.js +1 -1
- package/test/http2/head.test.js +1 -1
- package/test/http2/plain.test.js +1 -1
- package/test/http2/secure-with-fallback.test.js +1 -1
- package/test/http2/secure.test.js +1 -1
- package/test/http2/unknown-http-method.test.js +4 -10
- package/test/https/custom-https-server.test.js +12 -6
- package/test/https/https.test.js +1 -1
- package/test/input-validation.js +3 -3
- package/test/internals/handleRequest.test.js +6 -43
- package/test/internals/initialConfig.test.js +41 -12
- package/test/internals/logger.test.js +2 -2
- package/test/internals/reply.test.js +317 -48
- package/test/internals/request.test.js +13 -7
- package/test/internals/server.test.js +88 -0
- package/test/listen.deprecated.test.js +202 -0
- package/test/listen.test.js +140 -145
- package/test/logger.test.js +82 -42
- package/test/maxRequestsPerSocket.test.js +8 -6
- package/test/middleware.test.js +2 -25
- package/test/nullable-validation.test.js +53 -16
- package/test/output-validation.test.js +1 -1
- package/test/plugin.test.js +47 -21
- package/test/pretty-print.test.js +22 -10
- package/test/promises.test.js +1 -1
- package/test/proto-poisoning.test.js +6 -6
- package/test/register.test.js +3 -3
- package/test/reply-error.test.js +126 -15
- package/test/reply-trailers.test.js +270 -0
- package/test/request-error.test.js +3 -6
- package/test/route-hooks.test.js +18 -18
- package/test/route-prefix.test.js +2 -1
- package/test/route.test.js +206 -22
- package/test/router-options.test.js +2 -2
- package/test/schema-examples.test.js +11 -5
- package/test/schema-feature.test.js +25 -20
- package/test/schema-serialization.test.js +9 -9
- package/test/schema-special-usage.test.js +5 -153
- package/test/schema-validation.test.js +9 -9
- package/test/skip-reply-send.test.js +2 -2
- package/test/stream.test.js +82 -23
- package/test/throw.test.js +8 -5
- package/test/trust-proxy.test.js +6 -6
- package/test/type-provider.test.js +20 -0
- package/test/types/fastify.test-d.ts +10 -18
- package/test/types/hooks.test-d.ts +61 -5
- package/test/types/import.js +2 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +68 -17
- package/test/types/logger.test-d.ts +44 -15
- package/test/types/reply.test-d.ts +2 -1
- package/test/types/request.test-d.ts +71 -1
- package/test/types/route.test-d.ts +8 -2
- package/test/types/schema.test-d.ts +2 -39
- package/test/types/type-provider.test-d.ts +424 -0
- package/test/url-rewriting.test.js +3 -3
- package/test/validation-error-handling.test.js +8 -8
- package/test/versioned-routes.test.js +30 -18
- package/test/wrapThenable.test.js +7 -6
- package/types/content-type-parser.d.ts +17 -8
- package/types/hooks.d.ts +182 -85
- package/types/instance.d.ts +286 -118
- package/types/logger.d.ts +18 -104
- package/types/plugin.d.ts +10 -4
- package/types/reply.d.ts +18 -12
- package/types/request.d.ts +13 -8
- package/types/route.d.ts +62 -34
- package/types/schema.d.ts +1 -1
- package/types/type-provider.d.ts +99 -0
- package/types/utils.d.ts +1 -1
- package/lib/schema-compilers.js +0 -12
- 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
|
+
```
|
package/examples/asyncawait.js
CHANGED
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
package/examples/https.js
CHANGED
package/examples/parser.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const fastify = require('../fastify')({ logger: true })
|
|
4
4
|
const jsonParser = require('fast-json-body')
|
|
5
|
-
const
|
|
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 =
|
|
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
|
})
|
package/examples/route-prefix.js
CHANGED
|
@@ -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
|
+
})
|
package/examples/simple.js
CHANGED
package/examples/simple.mjs
CHANGED
package/examples/use-plugin.js
CHANGED
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 {
|
|
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
|
|
34
|
-
|
|
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
|
|
40
|
-
|
|
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
|
|
46
|
-
|
|
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
|
|
52
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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?:
|
|
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 }
|