@novice1/routing 1.0.11 → 1.1.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/README.md +19 -1
- package/index.d.ts +5 -0
- package/lib/layer.js +123 -112
- package/lib/route/createLayers.js +39 -39
- package/lib/route/metadataMiddleware.js +32 -33
- package/lib/route.js +205 -205
- package/lib/router/authMethods.js +36 -32
- package/lib/router/editLayersMethods.js +11 -11
- package/lib/router/metaMethods.js +13 -13
- package/lib/router/validatorsMethods.js +36 -32
- package/lib/router.d.ts +433 -0
- package/lib/router.js +314 -315
- package/lib/utils/toArray.js +28 -27
- package/package.json +32 -29
- package/.github/workflows/nodejs.yml +0 -29
- package/test/config/testconfig.js +0 -17
- package/test/tests/00_setup.js +0 -3
- package/test/tests/01_test_register.js +0 -52
- package/test/tests/02_test_route_method.js +0 -29
- package/test/tests/03_test_auth.js +0 -73
- package/test/tests/04_test_set_validators.js +0 -74
- package/test/tests/05_test_set_pre_validators.js +0 -49
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ A JSON object can be sent as the `path` parameter when using route methods (`get
|
|
|
19
19
|
- `name`: (string)
|
|
20
20
|
- `description`: (string)
|
|
21
21
|
- `parameters`: (object)
|
|
22
|
-
- `responses`: (
|
|
22
|
+
- `responses`: (any)
|
|
23
23
|
- `tags`: (string[])
|
|
24
24
|
- `auth`: (boolean)
|
|
25
25
|
- `preValidators`: (function[])
|
|
@@ -161,6 +161,24 @@ routerParent.setValidatorsIfNone(function (req, res, next) {
|
|
|
161
161
|
})
|
|
162
162
|
```
|
|
163
163
|
|
|
164
|
+
## Notes
|
|
165
|
+
|
|
166
|
+
### Typescript
|
|
167
|
+
|
|
168
|
+
This package extends `Request` interface from [express](https://www.npmjs.com/package/express) so you can always extend it more depending on your needs.
|
|
169
|
+
|
|
170
|
+
Example:
|
|
171
|
+
```ts
|
|
172
|
+
declare global {
|
|
173
|
+
namespace Express {
|
|
174
|
+
interface Request {
|
|
175
|
+
// add a property
|
|
176
|
+
session?: Record<string, any>;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
164
182
|
## References
|
|
165
183
|
|
|
166
184
|
- [Express](https://expressjs.com/)
|
package/index.d.ts
ADDED
package/lib/layer.js
CHANGED
|
@@ -1,113 +1,124 @@
|
|
|
1
|
-
var inherits = require('util').inherits;
|
|
2
|
-
var flatten = require('array-flatten');
|
|
3
|
-
var ExpressLayer = require("express/lib/router/layer");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
1
|
+
var inherits = require('util').inherits;
|
|
2
|
+
var { flatten } = require('array-flatten');
|
|
3
|
+
var ExpressLayer = require("express/lib/router/layer");
|
|
4
|
+
var toArray = require('./utils/toArray');
|
|
5
|
+
|
|
6
|
+
function Layer() {
|
|
7
|
+
var args = Array.from(arguments);
|
|
8
|
+
ExpressLayer.apply(this, args);
|
|
9
|
+
|
|
10
|
+
this.get_meta = get_meta;
|
|
11
|
+
this.set_auth = set_auth;
|
|
12
|
+
this.set_auth_if_none = set_auth_if_none;
|
|
13
|
+
this.set_validators = set_validators;
|
|
14
|
+
this.set_validators_if_none = set_validators_if_none;
|
|
15
|
+
this.meta_path = args[0];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
inherits(Layer, ExpressLayer);
|
|
19
|
+
|
|
20
|
+
function get_meta(path) {
|
|
21
|
+
var v = [];
|
|
22
|
+
// can only generate for string paths
|
|
23
|
+
var metaPaths = toArray(this.meta_path);
|
|
24
|
+
metaPaths.forEach(
|
|
25
|
+
metaPath => {
|
|
26
|
+
var loopPath = path;
|
|
27
|
+
if(loopPath) {
|
|
28
|
+
if (typeof metaPath !== 'string') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
loopPath += metaPath;
|
|
32
|
+
loopPath = loopPath.replace(/\/{2,}/g, '/');
|
|
33
|
+
} else {
|
|
34
|
+
loopPath = metaPath;
|
|
35
|
+
}
|
|
36
|
+
if (this.route) {
|
|
37
|
+
var tmpMeta = {
|
|
38
|
+
path: loopPath,
|
|
39
|
+
methods: this.route.methods
|
|
40
|
+
};
|
|
41
|
+
if(this.route.meta) {
|
|
42
|
+
Object.keys(this.route.meta).forEach(
|
|
43
|
+
k => {
|
|
44
|
+
tmpMeta[k] = this.route.meta[k]
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
v.push(tmpMeta);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (this.handle && this.handle.stack && typeof loopPath === 'string') {
|
|
52
|
+
this.handle.stack.forEach(layer => {
|
|
53
|
+
if (layer && typeof layer.get_meta === 'function') {
|
|
54
|
+
v.push.apply(v,flatten(layer.get_meta(loopPath)));
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return v;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function set_auth() {
|
|
65
|
+
if (this.route) {
|
|
66
|
+
if(typeof this.route.setAuth === 'function') {
|
|
67
|
+
var args = flatten(Array.prototype.slice.call(arguments));
|
|
68
|
+
this.route.setAuth.apply(this.route, args);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (this.handle && typeof this.handle.setAuthHandlers === 'function') {
|
|
73
|
+
var argsForRouter = flatten(Array.prototype.slice.call(arguments));
|
|
74
|
+
argsForRouter.shift();
|
|
75
|
+
this.handle.setAuthHandlers.apply(this.handle, argsForRouter);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function set_auth_if_none() {
|
|
80
|
+
if (this.route) {
|
|
81
|
+
if(typeof this.route.hasAuth === 'function' && !this.route.hasAuth()) {
|
|
82
|
+
var args = flatten(Array.prototype.slice.call(arguments));
|
|
83
|
+
this.route.setAuth.apply(this.route, args);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (this.handle && typeof this.handle.setAuthHandlersIfNone === 'function') {
|
|
88
|
+
var argsForRouter = flatten(Array.prototype.slice.call(arguments));
|
|
89
|
+
argsForRouter.shift();
|
|
90
|
+
this.handle.setAuthHandlersIfNone.apply(this.handle, argsForRouter);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function set_validators() {
|
|
95
|
+
if (this.route) {
|
|
96
|
+
if(typeof this.route.setValidators === 'function') {
|
|
97
|
+
var args = flatten(Array.prototype.slice.call(arguments));
|
|
98
|
+
this.route.setAuth.apply(this.route, args);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (this.handle && typeof this.handle.setValidators === 'function') {
|
|
103
|
+
var argsForRouter = flatten(Array.prototype.slice.call(arguments));
|
|
104
|
+
argsForRouter.shift();
|
|
105
|
+
this.handle.setValidators.apply(this.handle, argsForRouter);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function set_validators_if_none() {
|
|
110
|
+
if (this.route) {
|
|
111
|
+
if(typeof this.route.hasValidators === 'function' && !this.route.hasValidators()) {
|
|
112
|
+
var args = flatten(Array.prototype.slice.call(arguments));
|
|
113
|
+
this.route.setValidators.apply(this.route, args);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (this.handle && typeof this.handle.setValidatorsIfNone === 'function') {
|
|
118
|
+
var argsForRouter = flatten(Array.prototype.slice.call(arguments));
|
|
119
|
+
argsForRouter.shift();
|
|
120
|
+
this.handle.setValidatorsIfNone.apply(this.handle, argsForRouter);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
113
124
|
module.exports = Layer;
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
// var Layer = require("./layer"); // no need for extra methods if layer is inside a Route
|
|
2
|
-
var Layer = require("express/lib/router/layer");
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @description returns Layers in opposite order
|
|
6
|
-
* @param {string} type type of layer
|
|
7
|
-
* @param {string} method
|
|
8
|
-
* @param {object} layerOptions
|
|
9
|
-
* @param {Function[]} handles
|
|
10
|
-
* @param {any} dispatchBind
|
|
11
|
-
* @returns {Layer[]}
|
|
12
|
-
*/
|
|
13
|
-
function createLayers(type, method, layerOptions, handles, dispatchBind) {
|
|
14
|
-
var layers = [];
|
|
15
|
-
|
|
16
|
-
handles.forEach(
|
|
17
|
-
handle => {
|
|
18
|
-
/**
|
|
19
|
-
* @todo review the layer arguments
|
|
20
|
-
*/
|
|
21
|
-
var layer = new Layer(
|
|
22
|
-
"/", // TO REVIEW: '/' OR this.path ?
|
|
23
|
-
layerOptions,
|
|
24
|
-
dispatchBind
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
layer.handle = handle;
|
|
28
|
-
layer.name = handle.name;
|
|
29
|
-
layer.method = method;
|
|
30
|
-
// layer[type] = true;
|
|
31
|
-
layer.type = type;
|
|
32
|
-
|
|
33
|
-
layers.unshift(layer);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
return layers;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = createLayers;
|
|
1
|
+
// var Layer = require("./layer"); // no need for extra methods if layer is inside a Route
|
|
2
|
+
var Layer = require("express/lib/router/layer");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description returns Layers in opposite order
|
|
6
|
+
* @param {string} type type of layer
|
|
7
|
+
* @param {string} method
|
|
8
|
+
* @param {object} layerOptions
|
|
9
|
+
* @param {Function[]} handles
|
|
10
|
+
* @param {any} dispatchBind
|
|
11
|
+
* @returns {Layer[]}
|
|
12
|
+
*/
|
|
13
|
+
function createLayers(type, method, layerOptions, handles, dispatchBind) {
|
|
14
|
+
var layers = [];
|
|
15
|
+
|
|
16
|
+
handles.forEach(
|
|
17
|
+
handle => {
|
|
18
|
+
/**
|
|
19
|
+
* @todo review the layer arguments
|
|
20
|
+
*/
|
|
21
|
+
var layer = new Layer(
|
|
22
|
+
"/", // TO REVIEW: '/' OR this.path ?
|
|
23
|
+
layerOptions,
|
|
24
|
+
dispatchBind
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
layer.handle = handle;
|
|
28
|
+
layer.name = handle.name;
|
|
29
|
+
layer.method = method;
|
|
30
|
+
// layer[type] = true;
|
|
31
|
+
layer.type = type;
|
|
32
|
+
|
|
33
|
+
layers.unshift(layer);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return layers;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = createLayers;
|
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
var extend = require('extend');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {string} method
|
|
6
|
-
* @param {string} path
|
|
7
|
-
* @param {object} meta
|
|
8
|
-
* @returns {Function}
|
|
9
|
-
*/
|
|
10
|
-
function createMetadataMiddleware(method, path, meta) {
|
|
11
|
-
var requestMeta = {
|
|
12
|
-
method: method
|
|
13
|
-
};
|
|
14
|
-
if (path && path.indexOf(
|
|
15
|
-
path = path.replace(
|
|
16
|
-
}
|
|
17
|
-
requestMeta.path = path;
|
|
18
|
-
|
|
19
|
-
if (meta) {
|
|
20
|
-
['auth',
|
|
21
|
-
p => {
|
|
22
|
-
if (typeof meta[p] !== 'undefined')
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
var extend = require('extend');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string} method
|
|
6
|
+
* @param {string} path
|
|
7
|
+
* @param {object} meta
|
|
8
|
+
* @returns {Function}
|
|
9
|
+
*/
|
|
10
|
+
function createMetadataMiddleware(method, path, meta) {
|
|
11
|
+
var requestMeta = {
|
|
12
|
+
method: method
|
|
13
|
+
};
|
|
14
|
+
if (path && typeof path === 'string' && path.indexOf('//') == 0) {
|
|
15
|
+
path = path.replace('//', '/');
|
|
16
|
+
}
|
|
17
|
+
requestMeta.path = path;
|
|
18
|
+
|
|
19
|
+
if (meta) {
|
|
20
|
+
['auth', 'name', 'description', 'tags', 'parameters', 'responses'].forEach(
|
|
21
|
+
(p) => {
|
|
22
|
+
if (typeof meta[p] !== 'undefined') requestMeta[p] = meta[p];
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return function noviceRouteMetadataMiddleware(req, res, next) {
|
|
28
|
+
req.meta = extend(true, {}, requestMeta);
|
|
29
|
+
next();
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
34
33
|
module.exports = createMetadataMiddleware;
|