routup 0.12.0 → 0.13.1

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.
Files changed (66) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +11 -10
  3. package/dist/handler/index.d.ts +1 -1
  4. package/dist/handler/utils.d.ts +2 -2
  5. package/dist/handler/utils.d.ts.map +1 -1
  6. package/dist/index.cjs +579 -0
  7. package/dist/index.cjs.map +1 -0
  8. package/dist/index.d.ts +8 -8
  9. package/dist/index.mjs +561 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/layer/index.d.ts +3 -3
  12. package/dist/layer/module.d.ts +15 -15
  13. package/dist/layer/module.d.ts.map +1 -1
  14. package/dist/layer/type.d.ts +6 -6
  15. package/dist/layer/type.d.ts.map +1 -1
  16. package/dist/layer/utils.d.ts +2 -2
  17. package/dist/layer/utils.d.ts.map +1 -1
  18. package/dist/path/index.d.ts +2 -2
  19. package/dist/path/matcher.d.ts +12 -12
  20. package/dist/path/matcher.d.ts.map +1 -1
  21. package/dist/path/type.d.ts +6 -6
  22. package/dist/path/type.d.ts.map +1 -1
  23. package/dist/route/index.d.ts +3 -3
  24. package/dist/route/module.d.ts +28 -26
  25. package/dist/route/module.d.ts.map +1 -1
  26. package/dist/route/type.d.ts +6 -6
  27. package/dist/route/type.d.ts.map +1 -1
  28. package/dist/route/utils.d.ts +2 -2
  29. package/dist/route/utils.d.ts.map +1 -1
  30. package/dist/router/index.d.ts +2 -2
  31. package/dist/router/module.d.ts +71 -70
  32. package/dist/router/module.d.ts.map +1 -1
  33. package/dist/router/type.d.ts +24 -24
  34. package/dist/router/type.d.ts.map +1 -1
  35. package/dist/type.d.ts +13 -13
  36. package/dist/utils/index.d.ts +4 -4
  37. package/dist/utils/is-instance.d.ts +1 -1
  38. package/dist/utils/path.d.ts +2 -2
  39. package/dist/utils/path.d.ts.map +1 -1
  40. package/dist/utils/promise.d.ts +1 -1
  41. package/dist/utils/request.d.ts +2 -2
  42. package/dist/utils/request.d.ts.map +1 -1
  43. package/package.json +17 -8
  44. package/dist/handler/index.js +0 -21
  45. package/dist/handler/utils.js +0 -29
  46. package/dist/index.js +0 -28
  47. package/dist/layer/index.js +0 -23
  48. package/dist/layer/module.js +0 -74
  49. package/dist/layer/type.js +0 -9
  50. package/dist/layer/utils.js +0 -17
  51. package/dist/path/index.js +0 -22
  52. package/dist/path/matcher.js +0 -74
  53. package/dist/path/type.js +0 -9
  54. package/dist/route/index.js +0 -23
  55. package/dist/route/module.js +0 -135
  56. package/dist/route/type.js +0 -9
  57. package/dist/route/utils.js +0 -17
  58. package/dist/router/index.js +0 -22
  59. package/dist/router/module.js +0 -294
  60. package/dist/router/type.js +0 -9
  61. package/dist/type.js +0 -9
  62. package/dist/utils/index.js +0 -24
  63. package/dist/utils/is-instance.js +0 -16
  64. package/dist/utils/path.js +0 -16
  65. package/dist/utils/promise.js +0 -19
  66. package/dist/utils/request.js +0 -33
@@ -1,29 +0,0 @@
1
- /*
2
- * Copyright (c) 2022-2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "processHandlerExecutionOutput", {
11
- enumerable: true,
12
- get: ()=>processHandlerExecutionOutput
13
- });
14
- const _core = require("@routup/core");
15
- const _utils = require("../utils");
16
- function processHandlerExecutionOutput(res, next, output) {
17
- if ((0, _utils.isPromise)(output)) {
18
- output.then((r)=>{
19
- if (typeof r !== 'undefined') {
20
- (0, _core.send)(res, r);
21
- }
22
- return r;
23
- }).catch(next);
24
- return;
25
- }
26
- if (typeof output !== 'undefined') {
27
- (0, _core.send)(res, output);
28
- }
29
- }
package/dist/index.js DELETED
@@ -1,28 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- _exportStar(require("./handler"), exports);
11
- _exportStar(require("./layer"), exports);
12
- _exportStar(require("./path"), exports);
13
- _exportStar(require("./route"), exports);
14
- _exportStar(require("./router"), exports);
15
- _exportStar(require("./type"), exports);
16
- _exportStar(require("./utils"), exports);
17
- _exportStar(require("@routup/core"), exports);
18
- function _exportStar(from, to) {
19
- Object.keys(from).forEach(function(k) {
20
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
21
- enumerable: true,
22
- get: function() {
23
- return from[k];
24
- }
25
- });
26
- });
27
- return from;
28
- }
@@ -1,23 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- _exportStar(require("./module"), exports);
11
- _exportStar(require("./type"), exports);
12
- _exportStar(require("./utils"), exports);
13
- function _exportStar(from, to) {
14
- Object.keys(from).forEach(function(k) {
15
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
16
- enumerable: true,
17
- get: function() {
18
- return from[k];
19
- }
20
- });
21
- });
22
- return from;
23
- }
@@ -1,74 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "Layer", {
11
- enumerable: true,
12
- get: ()=>Layer
13
- });
14
- const _http = require("@ebec/http");
15
- const _core = require("@routup/core");
16
- const _handler = require("../handler");
17
- const _path = require("../path");
18
- class Layer {
19
- // --------------------------------------------------
20
- isError() {
21
- return this.fn.length === 4;
22
- }
23
- dispatch(req, res, meta, next, err) {
24
- (0, _core.setRequestParams)(req, meta.params || {});
25
- (0, _core.setRequestMountPath)(req, meta.mountPath || '/');
26
- if (typeof err !== 'undefined') {
27
- if (this.fn.length === 4) {
28
- try {
29
- this.fn(err, req, res, next);
30
- } catch (e) {
31
- /* istanbul ignore next */ /* istanbul ignore next */ if (e instanceof Error) {
32
- next(e);
33
- } else {
34
- next(new _http.BadRequestError({
35
- message: 'The request could not be processed by the error handler.'
36
- }));
37
- }
38
- }
39
- return;
40
- }
41
- /* istanbul ignore next */ next(err);
42
- /* istanbul ignore next */ return;
43
- }
44
- /* istanbul ignore next */ if (this.fn.length > 3) {
45
- next();
46
- return;
47
- }
48
- try {
49
- const output = this.fn(req, res, next);
50
- (0, _handler.processHandlerExecutionOutput)(res, next, output);
51
- } catch (e) {
52
- /* istanbul ignore next */ if (e instanceof Error) {
53
- next(e);
54
- } else {
55
- next(new _http.BadRequestError({
56
- message: 'The request could not be processed by the handler.'
57
- }));
58
- }
59
- }
60
- }
61
- // --------------------------------------------------
62
- matchPath(path) {
63
- return this.pathMatcher.test(path);
64
- }
65
- exec(path) {
66
- return this.pathMatcher.exec(path);
67
- }
68
- // --------------------------------------------------
69
- constructor(options, fn){
70
- this['@instanceof'] = Symbol.for('Layer');
71
- this.pathMatcher = new _path.PathMatcher(options.path, options.pathMatcher);
72
- this.fn = fn;
73
- }
74
- }
@@ -1,9 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
@@ -1,17 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "isLayerInstance", {
11
- enumerable: true,
12
- get: ()=>isLayerInstance
13
- });
14
- const _utils = require("../utils");
15
- function isLayerInstance(input) {
16
- return (0, _utils.isInstance)(input, 'Layer');
17
- }
@@ -1,22 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- _exportStar(require("./matcher"), exports);
11
- _exportStar(require("./type"), exports);
12
- function _exportStar(from, to) {
13
- Object.keys(from).forEach(function(k) {
14
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
15
- enumerable: true,
16
- get: function() {
17
- return from[k];
18
- }
19
- });
20
- });
21
- return from;
22
- }
@@ -1,74 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "PathMatcher", {
11
- enumerable: true,
12
- get: ()=>PathMatcher
13
- });
14
- const _pathToRegexp = require("path-to-regexp");
15
- function decodeParam(val) {
16
- /* istanbul ignore next */ if (typeof val !== 'string' || val.length === 0) {
17
- return val;
18
- }
19
- return decodeURIComponent(val);
20
- }
21
- class PathMatcher {
22
- test(path) {
23
- const fastSlash = this.path === '/' && this.regexpOptions.end === false;
24
- if (fastSlash) {
25
- return true;
26
- }
27
- return this.regexp.test(path);
28
- }
29
- exec(path) {
30
- let match = null;
31
- const fastSlash = this.path === '/' && this.regexpOptions.end === false;
32
- if (fastSlash) {
33
- return {
34
- path: '/',
35
- params: {}
36
- };
37
- }
38
- match = this.regexp.exec(path);
39
- if (!match) {
40
- return undefined;
41
- }
42
- if (this.path instanceof RegExp) {
43
- return {
44
- path,
45
- params: {
46
- 0: decodeParam(match[0])
47
- }
48
- };
49
- }
50
- const output = {};
51
- for(let i = 1; i < match.length; i++){
52
- const key = this.regexpKeys[i - 1];
53
- const prop = key.name;
54
- const val = decodeParam(match[i]);
55
- if (typeof val !== 'undefined') {
56
- output[prop] = val;
57
- }
58
- }
59
- return {
60
- path: match[0],
61
- params: output
62
- };
63
- }
64
- constructor(path, options){
65
- this.regexpKeys = [];
66
- this.path = path;
67
- this.regexpOptions = options || {};
68
- if (path instanceof RegExp) {
69
- this.regexp = path;
70
- } else {
71
- this.regexp = (0, _pathToRegexp.pathToRegexp)(path, this.regexpKeys, options);
72
- }
73
- }
74
- }
package/dist/path/type.js DELETED
@@ -1,9 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
@@ -1,23 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- _exportStar(require("./module"), exports);
11
- _exportStar(require("./type"), exports);
12
- _exportStar(require("./utils"), exports);
13
- function _exportStar(from, to) {
14
- Object.keys(from).forEach(function(k) {
15
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
16
- enumerable: true,
17
- get: function() {
18
- return from[k];
19
- }
20
- });
21
- });
22
- return from;
23
- }
@@ -1,135 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "Route", {
11
- enumerable: true,
12
- get: ()=>Route
13
- });
14
- const _smob = require("smob");
15
- const _core = require("@routup/core");
16
- const _layer = require("../layer");
17
- const _path = require("../path");
18
- function _extends() {
19
- _extends = Object.assign || function(target) {
20
- for(var i = 1; i < arguments.length; i++){
21
- var source = arguments[i];
22
- for(var key in source){
23
- if (Object.prototype.hasOwnProperty.call(source, key)) {
24
- target[key] = source[key];
25
- }
26
- }
27
- }
28
- return target;
29
- };
30
- return _extends.apply(this, arguments);
31
- }
32
- class Route {
33
- // --------------------------------------------------
34
- matchPath(path) {
35
- return this.pathMatcher.test(path);
36
- }
37
- matchMethod(method) {
38
- let name = method.toLowerCase();
39
- if (name === _core.Method.HEAD && !(0, _smob.hasOwnProperty)(this.layers, name)) {
40
- name = _core.Method.GET;
41
- }
42
- return Object.prototype.hasOwnProperty.call(this.layers, name);
43
- }
44
- // --------------------------------------------------
45
- getMethods() {
46
- const keys = Object.keys(this.layers);
47
- if ((0, _smob.hasOwnProperty)(this.layers, _core.Method.GET) && !(0, _smob.hasOwnProperty)(this.layers, _core.Method.HEAD)) {
48
- keys.push(_core.Method.HEAD);
49
- }
50
- return keys;
51
- }
52
- // --------------------------------------------------
53
- dispatch(req, res, meta, done) {
54
- /* istanbul ignore next */ if (!req.method) {
55
- done();
56
- return;
57
- }
58
- let name = req.method.toLowerCase();
59
- if (name === _core.Method.HEAD && !(0, _smob.hasOwnProperty)(this.layers, name)) {
60
- name = _core.Method.GET;
61
- }
62
- const layers = this.layers[name];
63
- /* istanbul ignore next */ if (typeof layers === 'undefined' || layers.length === 0 || typeof meta.path === 'undefined') {
64
- done();
65
- return;
66
- }
67
- const layerMeta = _extends({}, meta);
68
- const output = this.pathMatcher.exec(meta.path);
69
- if (output) {
70
- layerMeta.params = (0, _smob.merge)({}, meta.params || {}, output.params);
71
- }
72
- let index = -1;
73
- const next = (err)=>{
74
- index++;
75
- if (index >= layers.length) {
76
- setImmediate(done, err);
77
- return;
78
- }
79
- const layer = layers[index];
80
- if (err && !layer.isError()) {
81
- next(err);
82
- return;
83
- }
84
- layer.dispatch(req, res, _extends({}, layerMeta), next);
85
- };
86
- next();
87
- }
88
- // --------------------------------------------------
89
- register(method, ...handlers) {
90
- this.layers[method] = [];
91
- for(let i = 0; i < handlers.length; i++){
92
- const layer = new _layer.Layer({
93
- path: this.path,
94
- pathMatcher: this.pathMatcherOptions
95
- }, handlers[i]);
96
- this.layers[method].push(layer);
97
- }
98
- }
99
- get(...handlers) {
100
- return this.register(_core.Method.GET, ...handlers);
101
- }
102
- post(...handlers) {
103
- return this.register(_core.Method.POST, ...handlers);
104
- }
105
- put(...handlers) {
106
- return this.register(_core.Method.PUT, ...handlers);
107
- }
108
- patch(...handlers) {
109
- return this.register(_core.Method.PATCH, ...handlers);
110
- }
111
- delete(...handlers) {
112
- return this.register(_core.Method.DELETE, ...handlers);
113
- }
114
- head(...handlers) {
115
- return this.register(_core.Method.HEAD, ...handlers);
116
- }
117
- options(...handlers) {
118
- return this.register(_core.Method.OPTIONS, ...handlers);
119
- }
120
- // --------------------------------------------------
121
- isStrictPath() {
122
- return typeof this.path !== 'string' || this.path !== '/' && this.path.length !== 0;
123
- }
124
- // --------------------------------------------------
125
- constructor(options){
126
- this['@instanceof'] = Symbol.for('Route');
127
- this.layers = {};
128
- this.path = options.path;
129
- this.pathMatcherOptions = _extends({
130
- end: true,
131
- strict: this.isStrictPath()
132
- }, options.pathMatcher);
133
- this.pathMatcher = new _path.PathMatcher(this.path, this.pathMatcherOptions);
134
- }
135
- }
@@ -1,9 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
@@ -1,17 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "isRouteInstance", {
11
- enumerable: true,
12
- get: ()=>isRouteInstance
13
- });
14
- const _utils = require("../utils");
15
- function isRouteInstance(input) {
16
- return (0, _utils.isInstance)(input, 'Route');
17
- }
@@ -1,22 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- _exportStar(require("./module"), exports);
11
- _exportStar(require("./type"), exports);
12
- function _exportStar(from, to) {
13
- Object.keys(from).forEach(function(k) {
14
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
15
- enumerable: true,
16
- get: function() {
17
- return from[k];
18
- }
19
- });
20
- });
21
- return from;
22
- }