@koa/router 9.3.0 → 10.1.0
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/lib/layer.js +5 -8
- package/lib/router.js +4 -2
- package/package.json +1 -1
package/lib/layer.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const debug = require('debug')('koa-router');
|
|
2
1
|
const { pathToRegexp, compile, parse } = require('path-to-regexp');
|
|
3
2
|
const { parse: parseUrl, format: formatUrl } = require('url');
|
|
4
3
|
|
|
@@ -25,9 +24,9 @@ function Layer(path, methods, middleware, opts) {
|
|
|
25
24
|
this.paramNames = [];
|
|
26
25
|
this.stack = Array.isArray(middleware) ? middleware : [middleware];
|
|
27
26
|
|
|
28
|
-
for(let i = 0; i < methods.length; i++) {
|
|
27
|
+
for (let i = 0; i < methods.length; i++) {
|
|
29
28
|
const l = this.methods.push(methods[i].toUpperCase());
|
|
30
|
-
if (this.methods[l-1] === 'GET') this.methods.unshift('HEAD');
|
|
29
|
+
if (this.methods[l - 1] === 'GET') this.methods.unshift('HEAD');
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
// ensure middleware is a function
|
|
@@ -42,8 +41,6 @@ function Layer(path, methods, middleware, opts) {
|
|
|
42
41
|
|
|
43
42
|
this.path = path;
|
|
44
43
|
this.regexp = pathToRegexp(path, this.paramNames, this.opts);
|
|
45
|
-
|
|
46
|
-
debug('defined route %s %s', this.methods, `${this.opts.prefix}${this.path}`);
|
|
47
44
|
};
|
|
48
45
|
|
|
49
46
|
/**
|
|
@@ -71,10 +68,10 @@ Layer.prototype.match = function (path) {
|
|
|
71
68
|
Layer.prototype.params = function (path, captures, existingParams) {
|
|
72
69
|
const params = existingParams || {};
|
|
73
70
|
|
|
74
|
-
for (let len = captures.length, i=0; i<len; i++) {
|
|
71
|
+
for (let len = captures.length, i = 0; i < len; i++) {
|
|
75
72
|
if (this.paramNames[i]) {
|
|
76
73
|
const c = captures[i];
|
|
77
|
-
params[this.paramNames[i].name] = c ? safeDecodeURIComponent(c) : c;
|
|
74
|
+
if (c && c.length !== 0) params[this.paramNames[i].name] = c ? safeDecodeURIComponent(c) : c;
|
|
78
75
|
}
|
|
79
76
|
}
|
|
80
77
|
|
|
@@ -128,7 +125,7 @@ Layer.prototype.url = function (params, options) {
|
|
|
128
125
|
let replace = {};
|
|
129
126
|
|
|
130
127
|
if (args instanceof Array) {
|
|
131
|
-
for (let len = tokens.length, i=0, j=0; i<len; i++) {
|
|
128
|
+
for (let len = tokens.length, i = 0, j = 0; i < len; i++) {
|
|
132
129
|
if (tokens[i].name) replace[tokens[i].name] = args[j++];
|
|
133
130
|
}
|
|
134
131
|
} else if (tokens.some(token => token.name)) {
|
package/lib/router.js
CHANGED
|
@@ -362,7 +362,7 @@ Router.prototype.routes = Router.prototype.middleware = function () {
|
|
|
362
362
|
layerChain = matchedLayers.reduce(function(memo, layer) {
|
|
363
363
|
memo.push(function(ctx, next) {
|
|
364
364
|
ctx.captures = layer.captures(path, ctx.captures);
|
|
365
|
-
ctx.params = layer.params(path, ctx.captures, ctx.params);
|
|
365
|
+
ctx.params = ctx.request.params = layer.params(path, ctx.captures, ctx.params);
|
|
366
366
|
ctx.routerPath = layer.path;
|
|
367
367
|
ctx.routerName = layer.name;
|
|
368
368
|
ctx._matchedRoute = layer.path;
|
|
@@ -532,7 +532,7 @@ Router.prototype.redirect = function (source, destination, code) {
|
|
|
532
532
|
if (source[0] !== '/') source = this.url(source);
|
|
533
533
|
|
|
534
534
|
// lookup destination route by name
|
|
535
|
-
if (destination[0] !== '/') destination = this.url(destination);
|
|
535
|
+
if (destination[0] !== '/' && !destination.includes('://')) destination = this.url(destination);
|
|
536
536
|
|
|
537
537
|
return this.all(source, ctx => {
|
|
538
538
|
ctx.redirect(destination);
|
|
@@ -588,6 +588,8 @@ Router.prototype.register = function (path, methods, middleware, opts) {
|
|
|
588
588
|
|
|
589
589
|
stack.push(route);
|
|
590
590
|
|
|
591
|
+
debug('defined route %s %s', route.methods, route.path);
|
|
592
|
+
|
|
591
593
|
return route;
|
|
592
594
|
};
|
|
593
595
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koa/router",
|
|
3
3
|
"description": "Router middleware for koa. Provides RESTful resource routing.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "10.1.0",
|
|
5
5
|
"author": "Alex Mingoia <talk@alexmingoia.com>",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/koajs/router/issues",
|