fastify 4.0.0-rc.5 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/docs/Guides/Ecosystem.md +17 -17
- package/docs/Guides/Migration-Guide-V3.md +1 -1
- package/docs/Guides/Plugins-Guide.md +3 -3
- package/docs/Guides/Prototype-Poisoning.md +1 -1
- package/docs/Guides/Recommendations.md +2 -2
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Write-Plugin.md +3 -3
- package/docs/Migration-Guide-V4.md +1 -1
- package/docs/Reference/Middleware.md +3 -3
- package/docs/Reference/Routes.md +1 -1
- package/docs/Reference/Validation-and-Serialization.md +11 -1
- package/fastify.js +1 -1
- package/lib/schemas.js +3 -0
- package/lib/validation.js +1 -1
- package/package.json +1 -1
- package/test/schema-serialization.test.js +30 -0
package/README.md
CHANGED
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -12,10 +12,18 @@ section.
|
|
|
12
12
|
[accepts](https://www.npmjs.com/package/accepts) in your request object.
|
|
13
13
|
- [`@fastify/accepts-serializer`](https://github.com/fastify/fastify-accepts-serializer)
|
|
14
14
|
to serialize to output according to `Accept` header.
|
|
15
|
+
- [`@fastify/any-schema`](https://github.com/fastify/any-schema-you-like) Save multiple
|
|
16
|
+
schemas and decide which one to use to serialize the payload
|
|
15
17
|
- [`@fastify/auth`](https://github.com/fastify/fastify-auth) Run multiple auth
|
|
16
18
|
functions in Fastify.
|
|
17
19
|
- [`@fastify/autoload`](https://github.com/fastify/fastify-autoload) Require all
|
|
18
20
|
plugins in a directory.
|
|
21
|
+
- [`@fastify/awilix`](https://github.com/fastify/fastify-awilix) Dependency
|
|
22
|
+
injection support for Fastify, based on
|
|
23
|
+
[awilix](https://github.com/jeffijoe/awilix).
|
|
24
|
+
- [`@fastify/aws-lambda`](https://github.com/fastify/aws-lambda-fastify) allows you to
|
|
25
|
+
easily build serverless web applications/services and RESTful APIs using Fastify
|
|
26
|
+
on top of AWS Lambda and Amazon API Gateway.
|
|
19
27
|
- [`@fastify/basic-auth`](https://github.com/fastify/fastify-basic-auth) Basic
|
|
20
28
|
auth plugin for Fastify.
|
|
21
29
|
- [`@fastify/bearer-auth`](https://github.com/fastify/fastify-bearer-auth) Bearer
|
|
@@ -58,6 +66,7 @@ section.
|
|
|
58
66
|
internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
|
|
59
67
|
- [`@fastify/leveldb`](https://github.com/fastify/fastify-leveldb) Plugin to
|
|
60
68
|
share a common LevelDB connection across Fastify.
|
|
69
|
+
- [`@fastify/middie`](https://github.com/fastify/middie) Middleware engine for Fastify.
|
|
61
70
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
62
71
|
MongoDB connection plugin, with which you can share the same MongoDB
|
|
63
72
|
connection pool across every part of your server.
|
|
@@ -87,6 +96,9 @@ section.
|
|
|
87
96
|
to forward the current HTTP request to another server.
|
|
88
97
|
- [`@fastify/routes`](https://github.com/fastify/fastify-routes) Plugin that
|
|
89
98
|
provides a `Map` of routes.
|
|
99
|
+
- [`@fastify/schedule`](https://github.com/fastify/fastify-schedule) Plugin for
|
|
100
|
+
scheduling periodic jobs, based on
|
|
101
|
+
[toad-scheduler](https://github.com/kibertoad/toad-scheduler).
|
|
90
102
|
- [`@fastify/sensible`](https://github.com/fastify/fastify-sensible) Defaults for
|
|
91
103
|
Fastify that everyone can agree on. It adds some useful decorators such as
|
|
92
104
|
HTTP errors and assertions, but also more request and reply methods.
|
|
@@ -97,26 +109,14 @@ section.
|
|
|
97
109
|
- [`@fastify/swagger`](https://github.com/fastify/fastify-swagger) Plugin for
|
|
98
110
|
serving Swagger/OpenAPI documentation for Fastify, supporting dynamic
|
|
99
111
|
generation.
|
|
100
|
-
- [`@fastify/
|
|
101
|
-
|
|
112
|
+
- [`@fastify/under-pressure`](https://github.com/fastify/under-pressure) Measure process
|
|
113
|
+
load with automatic handling of _"Service Unavailable"_ plugin for Fastify.
|
|
102
114
|
- [`@fastify/url-data`](https://github.com/fastify/fastify-url-data) Decorate the
|
|
103
115
|
`Request` object with a method to access raw URL components.
|
|
104
|
-
- [`
|
|
105
|
-
schemas and decide which one to use to serialize the payload
|
|
106
|
-
- [`aws-lambda-fastify`](https://github.com/fastify/aws-lambda-fastify) allows you to
|
|
107
|
-
easily build serverless web applications/services and RESTful APIs using Fastify
|
|
108
|
-
on top of AWS Lambda and Amazon API Gateway.
|
|
109
|
-
- [`fastify-awilix`](https://github.com/fastify/fastify-awilix) Dependency
|
|
110
|
-
injection support for Fastify, based on
|
|
111
|
-
[awilix](https://github.com/jeffijoe/awilix).
|
|
112
|
-
- [`fastify-schedule`](https://github.com/fastify/fastify-schedule) Plugin for
|
|
113
|
-
scheduling periodic jobs, based on
|
|
114
|
-
[toad-scheduler](https://github.com/kibertoad/toad-scheduler).
|
|
115
|
-
- [`middie`](https://github.com/fastify/middie) Middleware engine for Fastify.
|
|
116
|
-
- [`point-of-view`](https://github.com/fastify/point-of-view) Templates
|
|
116
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
|
|
117
117
|
rendering (_ejs, pug, handlebars, marko_) plugin support for Fastify.
|
|
118
|
-
- [`
|
|
119
|
-
|
|
118
|
+
- [`@fastify/websocket`](https://github.com/fastify/fastify-websocket) WebSocket
|
|
119
|
+
support for Fastify. Built upon [ws](https://github.com/websockets/ws).
|
|
120
120
|
|
|
121
121
|
#### [Community](#community)
|
|
122
122
|
|
|
@@ -15,7 +15,7 @@ framework itself.
|
|
|
15
15
|
|
|
16
16
|
If you use Express middleware in your application, please install and register
|
|
17
17
|
the [`@fastify/express`](https://github.com/fastify/fastify-express) or
|
|
18
|
-
[
|
|
18
|
+
[`@fastify/middie`](https://github.com/fastify/middie) plugin before doing so.
|
|
19
19
|
|
|
20
20
|
**v2:**
|
|
21
21
|
|
|
@@ -458,10 +458,10 @@ console.log(new CustomError())
|
|
|
458
458
|
|
|
459
459
|
If you want to deprecate an API, or you want to warn the user about a specific
|
|
460
460
|
use case, you can use the
|
|
461
|
-
[`
|
|
461
|
+
[`process-warning`](https://github.com/fastify/process-warning) module.
|
|
462
462
|
|
|
463
463
|
```js
|
|
464
|
-
const warning = require('
|
|
464
|
+
const warning = require('process-warning')()
|
|
465
465
|
warning.create('FastifyDeprecation', 'FST_ERROR_CODE', 'message')
|
|
466
466
|
warning.emit('FST_ERROR_CODE')
|
|
467
467
|
```
|
|
@@ -475,7 +475,7 @@ will add it to the [*ecosystem*](https://github.com/fastify/fastify#ecosystem)
|
|
|
475
475
|
section of our documentation!
|
|
476
476
|
|
|
477
477
|
If you want to see some real-world examples, check out:
|
|
478
|
-
- [
|
|
478
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
|
|
479
479
|
rendering (*ejs, pug, handlebars, marko*) plugin support for Fastify.
|
|
480
480
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
481
481
|
MongoDB connection plugin, with this you can share the same MongoDB connection
|
|
@@ -359,7 +359,7 @@ address.
|
|
|
359
359
|
We also got lucky by having full access to mitigate it at the source — didn't
|
|
360
360
|
need to send emails to some unknown framework maintainer and hope for a quick
|
|
361
361
|
answer. hapi's total control over all of its dependencies proved its usefulness
|
|
362
|
-
and security again. Not using [hapi](
|
|
362
|
+
and security again. Not using [hapi](https://hapi.dev)? [Maybe you
|
|
363
363
|
should](https://hueniverse.com/why-you-should-consider-hapi-6163689bd7c2).
|
|
364
364
|
|
|
365
365
|
### The after in happy ever-after
|
|
@@ -172,7 +172,7 @@ backend static-backend
|
|
|
172
172
|
# with 2 primary servers distributed via round-robin
|
|
173
173
|
# and one backup which is used when the first 2 are not reachable
|
|
174
174
|
# This also assumes your fastify servers are listening on port 80.
|
|
175
|
-
# more info:
|
|
175
|
+
# more info: https://nginx.org/en/docs/http/ngx_http_upstream_module.html
|
|
176
176
|
upstream fastify_app {
|
|
177
177
|
server 10.10.11.1:80;
|
|
178
178
|
server 10.10.11.2:80;
|
|
@@ -257,7 +257,7 @@ server {
|
|
|
257
257
|
# trustProxy to the address of your NGINX server so the X-Forwarded
|
|
258
258
|
# fields are used by fastify.
|
|
259
259
|
location / {
|
|
260
|
-
# more info:
|
|
260
|
+
# more info: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
|
|
261
261
|
proxy_http_version 1.1;
|
|
262
262
|
proxy_cache_bypass $http_upgrade;
|
|
263
263
|
proxy_set_header Upgrade $http_upgrade;
|
|
@@ -36,7 +36,7 @@ The sample provided allows you to easily build serverless web
|
|
|
36
36
|
applications/services and RESTful APIs using Fastify on top of AWS Lambda and
|
|
37
37
|
Amazon API Gateway.
|
|
38
38
|
|
|
39
|
-
*Note: Using [aws-lambda
|
|
39
|
+
*Note: Using [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify)
|
|
40
40
|
is just one possible way.*
|
|
41
41
|
|
|
42
42
|
### app.js
|
|
@@ -74,7 +74,7 @@ to your port, so you can still run your Fastify function locally.
|
|
|
74
74
|
### lambda.js
|
|
75
75
|
|
|
76
76
|
```js
|
|
77
|
-
const awsLambdaFastify = require('aws-lambda
|
|
77
|
+
const awsLambdaFastify = require('@fastify/aws-lambda')
|
|
78
78
|
const init = require('./app');
|
|
79
79
|
|
|
80
80
|
const proxy = awsLambdaFastify(init())
|
|
@@ -91,14 +91,14 @@ exports.handler = proxy;
|
|
|
91
91
|
```
|
|
92
92
|
|
|
93
93
|
We just require
|
|
94
|
-
[aws-lambda
|
|
95
|
-
you install the dependency `npm i aws-lambda
|
|
94
|
+
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify) (make sure
|
|
95
|
+
you install the dependency `npm i @fastify/aws-lambda`) and our
|
|
96
96
|
[`app.js`](#appjs) file and call
|
|
97
97
|
the exported `awsLambdaFastify` function with the `app` as the only parameter.
|
|
98
98
|
The resulting `proxy` function has the correct signature to be used as a lambda
|
|
99
99
|
`handler` function. This way all the incoming events (API Gateway requests) are
|
|
100
100
|
passed to the `proxy` function of
|
|
101
|
-
[aws-lambda
|
|
101
|
+
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify).
|
|
102
102
|
|
|
103
103
|
### Example
|
|
104
104
|
|
|
@@ -36,8 +36,8 @@ at:
|
|
|
36
36
|
- [`@fastify/caching`](https://github.com/fastify/fastify-caching)
|
|
37
37
|
- [`@fastify/compress`](https://github.com/fastify/fastify-compress)
|
|
38
38
|
- [`@fastify/cookie`](https://github.com/fastify/fastify-cookie)
|
|
39
|
-
- [
|
|
40
|
-
- [`
|
|
39
|
+
- [`@fastify/under-pressure`](https://github.com/fastify/under-pressure)
|
|
40
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view)
|
|
41
41
|
|
|
42
42
|
## License
|
|
43
43
|
You can license your plugin as you prefer, we do not enforce any kind of
|
|
@@ -91,7 +91,7 @@ it to the [ecosystem](https://github.com/fastify/fastify#ecosystem) section of
|
|
|
91
91
|
our documentation!
|
|
92
92
|
|
|
93
93
|
If you want to see some real world examples, check out:
|
|
94
|
-
- [
|
|
94
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
|
|
95
95
|
rendering (*ejs, pug, handlebars, marko*) plugin support for Fastify.
|
|
96
96
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
97
97
|
MongoDB connection plugin, with this you can share the same MongoDB connection
|
|
@@ -9,4 +9,4 @@ All v3 deprecations have been removed and they will no longer work after upgradi
|
|
|
9
9
|
|
|
10
10
|
### Deprecation of `app.use()`
|
|
11
11
|
|
|
12
|
-
Starting this version of Fastify, we have deprecated the use of `app.use()`. We have decided not to support the use of middlewares. Both [
|
|
12
|
+
Starting this version of Fastify, we have deprecated the use of `app.use()`. We have decided not to support the use of middlewares. Both [`@fastify/middie`](https://github.com/fastify/middie) and [`@fastify/express`](https://github.com/fastify/fastify-express) will still be there and maintained. Use Fastify's [hooks](./Reference/Hooks.md) instead.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
Starting with Fastify v3.0.0, middleware is not supported out of the box and
|
|
6
6
|
requires an external plugin such as
|
|
7
7
|
[`@fastify/express`](https://github.com/fastify/fastify-express) or
|
|
8
|
-
[
|
|
8
|
+
[`@fastify/middie`](https://github.com/fastify/middie).
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
An example of registering the
|
|
@@ -22,11 +22,11 @@ fastify.use(require('ienoopen')())
|
|
|
22
22
|
fastify.use(require('x-xss-protection')())
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
You can also use [
|
|
25
|
+
You can also use [`@fastify/middie`](https://github.com/fastify/middie), which provides
|
|
26
26
|
support for simple Express-style middleware but with improved performance:
|
|
27
27
|
|
|
28
28
|
```js
|
|
29
|
-
await fastify.register(require('middie'))
|
|
29
|
+
await fastify.register(require('@fastify/middie'))
|
|
30
30
|
fastify.use(require('cors')())
|
|
31
31
|
```
|
|
32
32
|
|
package/docs/Reference/Routes.md
CHANGED
|
@@ -540,7 +540,7 @@ You can provide a `version` key in the `constraints` option to a route.
|
|
|
540
540
|
Versioned routes allow you to declare multiple handlers for the same HTTP route
|
|
541
541
|
path, which will then be matched according to each request's `Accept-Version`
|
|
542
542
|
header. The `Accept-Version` header value should follow the
|
|
543
|
-
[semver](
|
|
543
|
+
[semver](https://semver.org/) specification, and routes should be declared with
|
|
544
544
|
exact semver versions for matching.
|
|
545
545
|
|
|
546
546
|
Fastify will require a request `Accept-Version` header to be set if the route
|
|
@@ -567,10 +567,20 @@ fastify.post('/the/url', { schema }, handler)
|
|
|
567
567
|
```
|
|
568
568
|
|
|
569
569
|
As you can see, the response schema is based on the status code. If you want to
|
|
570
|
-
use the same schema for multiple status codes, you can use `'2xx'
|
|
570
|
+
use the same schema for multiple status codes, you can use `'2xx'` or `default`,
|
|
571
|
+
for example:
|
|
571
572
|
```js
|
|
572
573
|
const schema = {
|
|
573
574
|
response: {
|
|
575
|
+
default: {
|
|
576
|
+
type: 'object',
|
|
577
|
+
properties: {
|
|
578
|
+
error: {
|
|
579
|
+
type: 'boolean',
|
|
580
|
+
default: true
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
},
|
|
574
584
|
'2xx': {
|
|
575
585
|
type: 'object',
|
|
576
586
|
properties: {
|
package/fastify.js
CHANGED
package/lib/schemas.js
CHANGED
|
@@ -143,6 +143,9 @@ function getSchemaSerializer (context, statusCode) {
|
|
|
143
143
|
if (responseSchemaDef[fallbackStatusCode]) {
|
|
144
144
|
return responseSchemaDef[fallbackStatusCode]
|
|
145
145
|
}
|
|
146
|
+
if (responseSchemaDef.default) {
|
|
147
|
+
return responseSchemaDef.default
|
|
148
|
+
}
|
|
146
149
|
return false
|
|
147
150
|
}
|
|
148
151
|
|
package/lib/validation.js
CHANGED
|
@@ -7,7 +7,7 @@ const {
|
|
|
7
7
|
kSchemaBody: bodySchema,
|
|
8
8
|
kSchemaResponse: responseSchema
|
|
9
9
|
} = require('./symbols')
|
|
10
|
-
const scChecker = /^[1-5]{1}[0-9]{2}$|^[1-5]xx$/
|
|
10
|
+
const scChecker = /^[1-5]{1}[0-9]{2}$|^[1-5]xx$|^default$/
|
|
11
11
|
|
|
12
12
|
function compileSchemasForSerialization (context, compile) {
|
|
13
13
|
if (!context.schema || !context.schema.response) {
|
package/package.json
CHANGED
|
@@ -738,3 +738,33 @@ test('capital X', t => {
|
|
|
738
738
|
t.equal(res.statusCode, 200)
|
|
739
739
|
})
|
|
740
740
|
})
|
|
741
|
+
|
|
742
|
+
test('allow default as status code and used as last fallback', t => {
|
|
743
|
+
t.plan(3)
|
|
744
|
+
const fastify = Fastify()
|
|
745
|
+
|
|
746
|
+
fastify.route({
|
|
747
|
+
url: '/',
|
|
748
|
+
method: 'GET',
|
|
749
|
+
schema: {
|
|
750
|
+
response: {
|
|
751
|
+
default: {
|
|
752
|
+
type: 'object',
|
|
753
|
+
properties: {
|
|
754
|
+
name: { type: 'string' },
|
|
755
|
+
work: { type: 'string' }
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
},
|
|
760
|
+
handler: (req, reply) => {
|
|
761
|
+
reply.code(200).send({ name: 'Foo', work: 'Bar', nick: 'Boo' })
|
|
762
|
+
}
|
|
763
|
+
})
|
|
764
|
+
|
|
765
|
+
fastify.inject('/', (err, res) => {
|
|
766
|
+
t.error(err)
|
|
767
|
+
t.same(res.json(), { name: 'Foo', work: 'Bar' })
|
|
768
|
+
t.equal(res.statusCode, 200)
|
|
769
|
+
})
|
|
770
|
+
})
|