@novice1/routing 1.1.1 → 1.1.4

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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- /// <reference path='lib/router.d.ts' />
2
-
3
- import routing from './lib/router';
4
-
5
- export = routing;
1
+ /// <reference path='lib/router.d.ts' />
2
+
3
+ import routing from './lib/router';
4
+
5
+ export = routing;
package/lib/layer.js CHANGED
@@ -1,124 +1,124 @@
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
-
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
+
124
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,33 +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 && 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
-
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
+
33
33
  module.exports = createMetadataMiddleware;