@koa/router 8.0.3 → 8.0.8
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 +7 -6
- package/lib/layer.js +7 -6
- package/lib/router.js +68 -47
- package/package.json +35 -30
package/README.md
CHANGED
|
@@ -17,12 +17,6 @@ Router middleware for [koa](https://github.com/koajs/koa)
|
|
|
17
17
|
* Multiple and nestable routers
|
|
18
18
|
* `async/await` support
|
|
19
19
|
|
|
20
|
-
## Call for Maintainers
|
|
21
|
-
|
|
22
|
-
This module is forked from the original [koa-router](https://github.com/ZijianHe/koa-router) due to its lack of activity. `koa-router` is the most widely used router module in the Koa community and we need maintainers. If you're interested in fixing bugs or implementing new features feel free to open a pull request. We'll be adding active contributors as collaborators.
|
|
23
|
-
|
|
24
|
-
Thanks to the original authors @alexmingoia and the original team for their great work.
|
|
25
|
-
|
|
26
20
|
## Migrating to 7 / Koa 2
|
|
27
21
|
|
|
28
22
|
- The API has changed to match the new promise-based middleware
|
|
@@ -53,3 +47,10 @@ Run tests using `npm test`.
|
|
|
53
47
|
## Support
|
|
54
48
|
|
|
55
49
|
If you have any problem or suggestion please open an issue [here](https://github.com/koajs/router/issues).
|
|
50
|
+
|
|
51
|
+
## Call for Maintainers
|
|
52
|
+
|
|
53
|
+
This module is forked from the original [koa-router](https://github.com/ZijianHe/koa-router) due to its lack of activity. `koa-router` is the most widely used router module in the Koa community and we need maintainers. If you're interested in fixing bugs or implementing new features feel free to open a pull request. We'll be adding active contributors as collaborators.
|
|
54
|
+
|
|
55
|
+
Thanks to the original authors @alexmingoia and the original team for their great work.
|
|
56
|
+
|
package/lib/layer.js
CHANGED
|
@@ -25,15 +25,16 @@ function Layer(path, methods, middleware, opts) {
|
|
|
25
25
|
this.paramNames = [];
|
|
26
26
|
this.stack = Array.isArray(middleware) ? middleware : [middleware];
|
|
27
27
|
|
|
28
|
-
methods.
|
|
29
|
-
var l = this.methods.push(
|
|
28
|
+
for(var i = 0; i < methods.length; i++) {
|
|
29
|
+
var l = this.methods.push(methods[i].toUpperCase());
|
|
30
30
|
if (this.methods[l-1] === 'GET') {
|
|
31
|
-
|
|
31
|
+
this.methods.unshift('HEAD');
|
|
32
32
|
}
|
|
33
|
-
}
|
|
33
|
+
}
|
|
34
34
|
|
|
35
35
|
// ensure middleware is a function
|
|
36
|
-
this.stack.
|
|
36
|
+
for (var i = 0; i < this.stack.length; i++) {
|
|
37
|
+
var fn = this.stack[i];
|
|
37
38
|
var type = (typeof fn);
|
|
38
39
|
if (type !== 'function') {
|
|
39
40
|
throw new Error(
|
|
@@ -41,7 +42,7 @@ function Layer(path, methods, middleware, opts) {
|
|
|
41
42
|
+ "must be a function, not `" + type + "`"
|
|
42
43
|
);
|
|
43
44
|
}
|
|
44
|
-
}
|
|
45
|
+
}
|
|
45
46
|
|
|
46
47
|
this.path = path;
|
|
47
48
|
this.regexp = pathToRegExp(path, this.paramNames, this.opts);
|
package/lib/router.js
CHANGED
|
@@ -187,25 +187,28 @@ function Router(opts) {
|
|
|
187
187
|
* @returns {Router}
|
|
188
188
|
*/
|
|
189
189
|
|
|
190
|
-
methods.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
190
|
+
for (var i = 0; i < methods.length; i++) {
|
|
191
|
+
function setMethodVerb(method) {
|
|
192
|
+
Router.prototype[method] = function(name, path, middleware) {
|
|
193
|
+
var middleware;
|
|
194
|
+
|
|
195
|
+
if (typeof path === "string" || path instanceof RegExp) {
|
|
196
|
+
middleware = Array.prototype.slice.call(arguments, 2);
|
|
197
|
+
} else {
|
|
198
|
+
middleware = Array.prototype.slice.call(arguments, 1);
|
|
199
|
+
path = name;
|
|
200
|
+
name = null;
|
|
201
|
+
}
|
|
201
202
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
203
|
+
this.register(path, [method], middleware, {
|
|
204
|
+
name: name
|
|
205
|
+
});
|
|
205
206
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
207
|
+
return this;
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
setMethodVerb(methods[i]);
|
|
211
|
+
}
|
|
209
212
|
|
|
210
213
|
// Alias for `router.delete()` because delete is a reserved word
|
|
211
214
|
Router.prototype.del = Router.prototype['delete'];
|
|
@@ -247,10 +250,11 @@ Router.prototype.use = function () {
|
|
|
247
250
|
|
|
248
251
|
// support array of paths
|
|
249
252
|
if (Array.isArray(middleware[0]) && typeof middleware[0][0] === 'string') {
|
|
250
|
-
middleware[0]
|
|
253
|
+
var arrPaths = middleware[0];
|
|
254
|
+
for (var i = 0; i < arrPaths.length; i++) {
|
|
255
|
+
var p = arrPaths[i];
|
|
251
256
|
router.use.apply(router, [p].concat(middleware.slice(1)));
|
|
252
|
-
}
|
|
253
|
-
|
|
257
|
+
}
|
|
254
258
|
return this;
|
|
255
259
|
}
|
|
256
260
|
|
|
@@ -259,30 +263,40 @@ Router.prototype.use = function () {
|
|
|
259
263
|
path = middleware.shift();
|
|
260
264
|
}
|
|
261
265
|
|
|
262
|
-
middleware.
|
|
266
|
+
for (var i = 0; i < middleware.length; i++) {
|
|
267
|
+
var m = middleware[i];
|
|
263
268
|
if (m.router) {
|
|
264
|
-
|
|
269
|
+
var cloneRouter = Object.assign(Object.create(Router.prototype), m.router, {
|
|
265
270
|
stack: m.router.stack.slice(0)
|
|
266
271
|
});
|
|
267
272
|
|
|
268
|
-
cloneRouter.stack.
|
|
269
|
-
|
|
273
|
+
for (var j = 0; j < cloneRouter.stack.length; j++) {
|
|
274
|
+
var nestedLayer = cloneRouter.stack[j];
|
|
275
|
+
var cloneLayer = Object.assign(
|
|
276
|
+
Object.create(Layer.prototype),
|
|
277
|
+
nestedLayer
|
|
278
|
+
);
|
|
270
279
|
|
|
271
280
|
if (path) cloneLayer.setPrefix(path);
|
|
272
281
|
if (router.opts.prefix) cloneLayer.setPrefix(router.opts.prefix);
|
|
273
282
|
router.stack.push(cloneLayer);
|
|
274
|
-
|
|
275
|
-
}
|
|
283
|
+
cloneRouter.stack[j] = cloneLayer;
|
|
284
|
+
}
|
|
276
285
|
|
|
277
286
|
if (router.params) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
287
|
+
function setRouterParams(paramArr) {
|
|
288
|
+
var routerParams = paramArr;
|
|
289
|
+
for (var j = 0; j < routerParams.length; j++) {
|
|
290
|
+
var key = routerParams[j];
|
|
291
|
+
cloneRouter.param(key, router.params[key]);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
setRouterParams(Object.keys(router.params));
|
|
281
295
|
}
|
|
282
296
|
} else {
|
|
283
297
|
router.register(path || '(.*)', [], m, { end: false, ignoreCaptures: !hasPath });
|
|
284
298
|
}
|
|
285
|
-
}
|
|
299
|
+
}
|
|
286
300
|
|
|
287
301
|
return this;
|
|
288
302
|
};
|
|
@@ -305,10 +319,11 @@ Router.prototype.prefix = function (prefix) {
|
|
|
305
319
|
|
|
306
320
|
this.opts.prefix = prefix;
|
|
307
321
|
|
|
308
|
-
this.stack.
|
|
322
|
+
for (var i = 0; i < this.stack.length; i++) {
|
|
323
|
+
var route = this.stack[i];
|
|
309
324
|
route.setPrefix(prefix);
|
|
310
|
-
}
|
|
311
|
-
|
|
325
|
+
}
|
|
326
|
+
|
|
312
327
|
return this;
|
|
313
328
|
};
|
|
314
329
|
|
|
@@ -415,11 +430,13 @@ Router.prototype.allowedMethods = function (options) {
|
|
|
415
430
|
var allowed = {};
|
|
416
431
|
|
|
417
432
|
if (!ctx.status || ctx.status === 404) {
|
|
418
|
-
ctx.matched.
|
|
419
|
-
route
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
433
|
+
for (var i = 0; i < ctx.matched.length; i++) {
|
|
434
|
+
var route = ctx.matched[i];
|
|
435
|
+
for (var j = 0; j < route.methods.length; j++) {
|
|
436
|
+
var method = route.methods[j];
|
|
437
|
+
allowed[method] = method
|
|
438
|
+
}
|
|
439
|
+
}
|
|
423
440
|
|
|
424
441
|
var allowedArr = Object.keys(allowed);
|
|
425
442
|
|
|
@@ -549,9 +566,10 @@ Router.prototype.register = function (path, methods, middleware, opts) {
|
|
|
549
566
|
|
|
550
567
|
// support array of paths
|
|
551
568
|
if (Array.isArray(path)) {
|
|
552
|
-
path.
|
|
553
|
-
|
|
554
|
-
|
|
569
|
+
for (var i = 0; i < path.length; i++) {
|
|
570
|
+
var curPath = path[i];
|
|
571
|
+
router.register.call(router, curPath, methods, middleware, opts);
|
|
572
|
+
}
|
|
555
573
|
|
|
556
574
|
return this;
|
|
557
575
|
}
|
|
@@ -571,9 +589,10 @@ Router.prototype.register = function (path, methods, middleware, opts) {
|
|
|
571
589
|
}
|
|
572
590
|
|
|
573
591
|
// add parameter middleware
|
|
574
|
-
Object.keys(this.params).
|
|
592
|
+
for (var i = 0; i < Object.keys(this.params).length; i++) {
|
|
593
|
+
var param = Object.keys(this.params)[i];
|
|
575
594
|
route.param(param, this.params[param]);
|
|
576
|
-
}
|
|
595
|
+
}
|
|
577
596
|
|
|
578
597
|
stack.push(route);
|
|
579
598
|
|
|
@@ -712,11 +731,13 @@ Router.prototype.match = function (path, method) {
|
|
|
712
731
|
* @returns {Router}
|
|
713
732
|
*/
|
|
714
733
|
|
|
715
|
-
Router.prototype.param = function
|
|
734
|
+
Router.prototype.param = function(param, middleware) {
|
|
716
735
|
this.params[param] = middleware;
|
|
717
|
-
this.stack.
|
|
736
|
+
for (var i = 0; i < this.stack.length; i++) {
|
|
737
|
+
var route = this.stack[i];
|
|
718
738
|
route.param(param, middleware);
|
|
719
|
-
}
|
|
739
|
+
}
|
|
740
|
+
|
|
720
741
|
return this;
|
|
721
742
|
};
|
|
722
743
|
|
|
@@ -734,7 +755,7 @@ Router.prototype.param = function (param, middleware) {
|
|
|
734
755
|
* @param {Object} params url parameters
|
|
735
756
|
* @returns {String}
|
|
736
757
|
*/
|
|
737
|
-
Router.url = function (path
|
|
758
|
+
Router.url = function (path) {
|
|
738
759
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
739
760
|
return Layer.prototype.url.apply({ path: path }, args);
|
|
740
761
|
};
|
package/package.json
CHANGED
|
@@ -1,44 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koa/router",
|
|
3
3
|
"description": "Router middleware for koa. Provides RESTful resource routing.",
|
|
4
|
-
"
|
|
5
|
-
"type": "git",
|
|
6
|
-
"url": "https://github.com/koajs/koa-router.git"
|
|
7
|
-
},
|
|
8
|
-
"main": "lib/router.js",
|
|
9
|
-
"files": [
|
|
10
|
-
"lib"
|
|
11
|
-
],
|
|
4
|
+
"version": "8.0.8",
|
|
12
5
|
"author": "Alex Mingoia <talk@alexmingoia.com>",
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"router",
|
|
18
|
-
"route"
|
|
19
|
-
],
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/koajs/router/issues",
|
|
8
|
+
"email": "niftylettuce@gmail.com"
|
|
9
|
+
},
|
|
20
10
|
"dependencies": {
|
|
21
|
-
"debug": "^
|
|
22
|
-
"http-errors": "^1.3
|
|
23
|
-
"koa-compose": "^
|
|
24
|
-
"methods": "^1.
|
|
25
|
-
"path-to-regexp": "
|
|
26
|
-
"urijs": "^1.19.
|
|
11
|
+
"debug": "^4.1.1",
|
|
12
|
+
"http-errors": "^1.7.3",
|
|
13
|
+
"koa-compose": "^4.1.0",
|
|
14
|
+
"methods": "^1.1.2",
|
|
15
|
+
"path-to-regexp": "1.x",
|
|
16
|
+
"urijs": "^1.19.2"
|
|
27
17
|
},
|
|
28
18
|
"devDependencies": {
|
|
29
19
|
"expect.js": "^0.3.1",
|
|
30
|
-
"jsdoc-to-markdown": "^5.0.
|
|
31
|
-
"koa": "2.
|
|
32
|
-
"mocha": "^6.2.
|
|
33
|
-
"should": "^
|
|
20
|
+
"jsdoc-to-markdown": "^5.0.3",
|
|
21
|
+
"koa": "^2.11.0",
|
|
22
|
+
"mocha": "^6.2.2",
|
|
23
|
+
"should": "^13.2.3",
|
|
34
24
|
"supertest": "^4.0.2"
|
|
35
25
|
},
|
|
36
|
-
"scripts": {
|
|
37
|
-
"test": "NODE_ENV=test mocha test/**/*.js",
|
|
38
|
-
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md"
|
|
39
|
-
},
|
|
40
26
|
"engines": {
|
|
41
27
|
"node": ">= 8.0.0"
|
|
42
28
|
},
|
|
43
|
-
"
|
|
29
|
+
"files": [
|
|
30
|
+
"lib"
|
|
31
|
+
],
|
|
32
|
+
"homepage": "https://github.com/koajs/router",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"koa",
|
|
35
|
+
"middleware",
|
|
36
|
+
"route",
|
|
37
|
+
"router"
|
|
38
|
+
],
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"main": "lib/router.js",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/koajs/koa-router.git"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
|
|
47
|
+
"test": "NODE_ENV=test mocha test/**/*.js"
|
|
48
|
+
}
|
|
44
49
|
}
|