@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 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`: (object)
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
@@ -0,0 +1,5 @@
1
+ /// <reference path='lib/router.d.ts' />
2
+
3
+ import routing from './lib/router';
4
+
5
+ export = routing;
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
- function Layer() {
6
- var args = Array.from(arguments);
7
- ExpressLayer.apply(this, args);
8
-
9
- this.get_meta = get_meta;
10
- this.set_auth = set_auth;
11
- this.set_auth_if_none = set_auth_if_none;
12
- this.set_validators = set_validators;
13
- this.set_validators_if_none = set_validators_if_none;
14
- this.meta_path = args[0];
15
- }
16
-
17
- inherits(Layer, ExpressLayer);
18
-
19
- function get_meta(path) {
20
- var v = [];
21
- if(path) {
22
- path += this.meta_path;
23
- path = path.replace(/\/{2,}/g, '/');
24
- } else {
25
- path = this.meta_path;
26
- }
27
- if (this.route) {
28
- var tmpMeta = {
29
- path: `${path}`,
30
- methods: this.route.methods
31
- };
32
- if(this.route.meta) {
33
- Object.keys(this.route.meta).forEach(
34
- k => {
35
- tmpMeta[k] = this.route.meta[k]
36
- }
37
- );
38
- }
39
- v.push(tmpMeta);
40
- }
41
-
42
- if (this.handle && this.handle.stack) {
43
- this.handle.stack.forEach(layer => {
44
- if (layer && typeof layer.get_meta === 'function') {
45
- v.push.apply(v,flatten(layer.get_meta(path)));
46
- }
47
- });
48
- }
49
-
50
- return v;
51
- }
52
-
53
- function set_auth() {
54
- if (this.route) {
55
- if(typeof this.route.setAuth === 'function') {
56
- var args = flatten(Array.prototype.slice.call(arguments));
57
- this.route.setAuth.apply(this.route, args);
58
- }
59
- }
60
-
61
- if (this.handle && typeof this.handle.setAuthHandlers === 'function') {
62
- var argsForRouter = flatten(Array.prototype.slice.call(arguments));
63
- argsForRouter.shift();
64
- this.handle.setAuthHandlers.apply(this.handle, argsForRouter);
65
- }
66
- }
67
-
68
- function set_auth_if_none() {
69
- if (this.route) {
70
- if(typeof this.route.hasAuth === 'function' && !this.route.hasAuth()) {
71
- var args = flatten(Array.prototype.slice.call(arguments));
72
- this.route.setAuth.apply(this.route, args);
73
- }
74
- }
75
-
76
- if (this.handle && typeof this.handle.setAuthHandlersIfNone === 'function') {
77
- var argsForRouter = flatten(Array.prototype.slice.call(arguments));
78
- argsForRouter.shift();
79
- this.handle.setAuthHandlersIfNone.apply(this.handle, argsForRouter);
80
- }
81
- }
82
-
83
- function set_validators() {
84
- if (this.route) {
85
- if(typeof this.route.setValidators === 'function') {
86
- var args = flatten(Array.prototype.slice.call(arguments));
87
- this.route.setAuth.apply(this.route, args);
88
- }
89
- }
90
-
91
- if (this.handle && typeof this.handle.setValidators === 'function') {
92
- var argsForRouter = flatten(Array.prototype.slice.call(arguments));
93
- argsForRouter.shift();
94
- this.handle.setValidators.apply(this.handle, argsForRouter);
95
- }
96
- }
97
-
98
- function set_validators_if_none() {
99
- if (this.route) {
100
- if(typeof this.route.hasValidators === 'function' && !this.route.hasValidators()) {
101
- var args = flatten(Array.prototype.slice.call(arguments));
102
- this.route.setValidators.apply(this.route, args);
103
- }
104
- }
105
-
106
- if (this.handle && typeof this.handle.setValidatorsIfNone === 'function') {
107
- var argsForRouter = flatten(Array.prototype.slice.call(arguments));
108
- argsForRouter.shift();
109
- this.handle.setValidatorsIfNone.apply(this.handle, argsForRouter);
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("//") == 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')
23
- requestMeta[p] = meta[p];
24
- }
25
- );
26
- }
27
-
28
- return function noviceRouteMetadataMiddleware(req, res, next) {
29
- req.meta = extend(true, {}, requestMeta);
30
- next();
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;