@gasket/plugin-fastify 6.24.2 → 6.33.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 +22 -2
- package/lib/index.js +39 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -32,9 +32,9 @@ module.exports = {
|
|
|
32
32
|
|
|
33
33
|
All the configurations for the plugin are added under `fastify` in the config:
|
|
34
34
|
|
|
35
|
-
- `compression`: true by default. Can
|
|
35
|
+
- `compression`: true by default. Can be set to false if applying compression
|
|
36
36
|
differently.
|
|
37
|
-
- `excludedRoutesRegex`:
|
|
37
|
+
- `excludedRoutesRegex`: Routes to be excluded based on a regex
|
|
38
38
|
|
|
39
39
|
#### Example configuration
|
|
40
40
|
|
|
@@ -50,6 +50,26 @@ module.exports = {
|
|
|
50
50
|
}
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
### middleware
|
|
54
|
+
|
|
55
|
+
You can configure which paths middleware will run on by adding the middleware configuration array to your local `gasket.config.js`. The array is made up of objects with the name of the middlware (Gasket plugin name) you want to configure and an array of path patterns representing the paths to match for this middleware. Pattern matching entries in the array can come in the form of path strings and/or regular expressions.
|
|
56
|
+
```js
|
|
57
|
+
middleware: [
|
|
58
|
+
{
|
|
59
|
+
plugin:'gasket-plugin-example', // Name of the Gasket plugin
|
|
60
|
+
paths: ['/api']
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
plugin:'@some/gasket-plugin-example',
|
|
64
|
+
paths: [/\/default/]
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
plugin: '@another/gasket-plugin-example'
|
|
68
|
+
paths: ['/proxy', /\/home/]
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
```
|
|
72
|
+
|
|
53
73
|
## Lifecycles
|
|
54
74
|
|
|
55
75
|
### middleware
|
package/lib/index.js
CHANGED
|
@@ -35,6 +35,7 @@ module.exports = {
|
|
|
35
35
|
const compression = require('compression');
|
|
36
36
|
|
|
37
37
|
const { logger, config } = gasket;
|
|
38
|
+
const { middleware: middlewareConfig } = config;
|
|
38
39
|
const excludedRoutesRegex = config.fastify && config.fastify.excludedRoutesRegex;
|
|
39
40
|
const app = fastify({ logger });
|
|
40
41
|
|
|
@@ -58,11 +59,30 @@ module.exports = {
|
|
|
58
59
|
app.use(compression());
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
const middlewares =
|
|
62
|
+
const middlewares = [];
|
|
63
|
+
await gasket.execApply('middleware', async (plugin, handler) => {
|
|
64
|
+
const middleware = handler(app);
|
|
65
|
+
if (middleware) {
|
|
66
|
+
if (middlewareConfig) {
|
|
67
|
+
const pluginName = plugin && plugin.name ? plugin.name : '';
|
|
68
|
+
const mwConfig = middlewareConfig.find(mw => mw.plugin === pluginName);
|
|
69
|
+
if (mwConfig) {
|
|
70
|
+
middleware.paths = mwConfig.paths;
|
|
71
|
+
if (excludedRoutesRegex) {
|
|
72
|
+
middleware.paths.push(excludedRoutesRegex);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
middlewares.push(middleware);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
62
79
|
|
|
63
80
|
debug('applied %s middleware layers to fastify', middlewares.length);
|
|
64
81
|
middlewares.forEach(layer => {
|
|
65
|
-
|
|
82
|
+
const { paths } = layer;
|
|
83
|
+
if (paths) {
|
|
84
|
+
app.use(paths, layer);
|
|
85
|
+
} else if (excludedRoutesRegex) {
|
|
66
86
|
app.use(excludedRoutesRegex, layer);
|
|
67
87
|
} else {
|
|
68
88
|
app.use(layer);
|
|
@@ -106,6 +126,23 @@ module.exports = {
|
|
|
106
126
|
link: 'README.md#errorMiddleware',
|
|
107
127
|
parent: 'createServers',
|
|
108
128
|
after: 'express'
|
|
129
|
+
}],
|
|
130
|
+
configurations: [{
|
|
131
|
+
name: 'fastify',
|
|
132
|
+
link: 'README.md#configuration',
|
|
133
|
+
description: 'Fastify configuration object',
|
|
134
|
+
type: 'object'
|
|
135
|
+
}, {
|
|
136
|
+
name: 'fastify.compression',
|
|
137
|
+
link: 'README.md#configuration',
|
|
138
|
+
description: 'Automatic compression',
|
|
139
|
+
type: 'boolean',
|
|
140
|
+
default: true
|
|
141
|
+
}, {
|
|
142
|
+
name: 'fastify.excludedRoutesRegex',
|
|
143
|
+
link: 'README.md#configuration',
|
|
144
|
+
description: 'Routes to be excluded based on a regex',
|
|
145
|
+
type: 'RegExp'
|
|
109
146
|
}]
|
|
110
147
|
};
|
|
111
148
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gasket/plugin-fastify",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.33.0",
|
|
4
4
|
"description": "Adds fastify support to your application",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"middie": "5.1.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@gasket/engine": "^6.
|
|
48
|
+
"@gasket/engine": "^6.26.1",
|
|
49
49
|
"assume": "^2.3.0",
|
|
50
50
|
"assume-sinon": "^1.1.0",
|
|
51
51
|
"eslint": "^8.7.0",
|
|
@@ -73,5 +73,5 @@
|
|
|
73
73
|
"unicorn/filename-case": "error"
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "e19fab235461a16577f27ad30062b754233e05c9"
|
|
77
77
|
}
|