@koa/router 13.0.0 → 13.0.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.
- package/lib/layer.js +30 -7
- package/lib/router.js +25 -13
- package/package.json +3 -3
package/lib/layer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { parse: parseUrl, format: formatUrl } = require('node:url');
|
|
2
2
|
|
|
3
|
-
const { pathToRegexp, compile, parse } = require('path-to-regexp');
|
|
3
|
+
const { pathToRegexp, compile, parse, stringify } = require('path-to-regexp');
|
|
4
4
|
|
|
5
5
|
module.exports = class Layer {
|
|
6
6
|
/**
|
|
@@ -13,6 +13,7 @@ module.exports = class Layer {
|
|
|
13
13
|
* @param {String=} opts.name route name
|
|
14
14
|
* @param {String=} opts.sensitive case sensitive (default: false)
|
|
15
15
|
* @param {String=} opts.strict require the trailing slash (default: false)
|
|
16
|
+
* @param {Boolean=} opts.pathIsRegexp if true, treat `path` as a regular expression
|
|
16
17
|
* @returns {Layer}
|
|
17
18
|
* @private
|
|
18
19
|
*/
|
|
@@ -41,7 +42,19 @@ module.exports = class Layer {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
this.path = path;
|
|
44
|
-
|
|
45
|
+
|
|
46
|
+
if (this.opts.pathIsRegexp === true) {
|
|
47
|
+
this.regexp = new RegExp(path);
|
|
48
|
+
} else if (this.path) {
|
|
49
|
+
if (this.opts.strict === true) {
|
|
50
|
+
// path-to-regexp renamed strict to trailing in v8.1.0
|
|
51
|
+
this.opts.trailing = false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
|
|
55
|
+
this.regexp = regex;
|
|
56
|
+
this.paramNames = keys;
|
|
57
|
+
}
|
|
45
58
|
}
|
|
46
59
|
|
|
47
60
|
/**
|
|
@@ -116,13 +129,14 @@ module.exports = class Layer {
|
|
|
116
129
|
|
|
117
130
|
const toPath = compile(url, { encode: encodeURIComponent, ...options });
|
|
118
131
|
let replaced;
|
|
119
|
-
|
|
120
|
-
const tokens = parse(url);
|
|
132
|
+
const { tokens } = parse(url);
|
|
121
133
|
let replace = {};
|
|
122
134
|
|
|
123
135
|
if (Array.isArray(args)) {
|
|
124
136
|
for (let len = tokens.length, i = 0, j = 0; i < len; i++) {
|
|
125
|
-
if (tokens[i].name)
|
|
137
|
+
if (tokens[i].name) {
|
|
138
|
+
replace[tokens[i].name] = args[j++];
|
|
139
|
+
}
|
|
126
140
|
}
|
|
127
141
|
} else if (tokens.some((token) => token.name)) {
|
|
128
142
|
replace = params;
|
|
@@ -130,6 +144,10 @@ module.exports = class Layer {
|
|
|
130
144
|
options = params;
|
|
131
145
|
}
|
|
132
146
|
|
|
147
|
+
for (const [key, value] of Object.entries(replace)) {
|
|
148
|
+
replace[key] = String(value);
|
|
149
|
+
}
|
|
150
|
+
|
|
133
151
|
replaced = toPath(replace);
|
|
134
152
|
|
|
135
153
|
if (options && options.query) {
|
|
@@ -212,8 +230,13 @@ module.exports = class Layer {
|
|
|
212
230
|
this.path !== '/' || this.opts.strict === true
|
|
213
231
|
? `${prefix}${this.path}`
|
|
214
232
|
: prefix;
|
|
215
|
-
this.
|
|
216
|
-
|
|
233
|
+
if (this.opts.pathIsRegexp === true || prefix instanceof RegExp) {
|
|
234
|
+
this.regexp = new RegExp(this.path);
|
|
235
|
+
} else if (this.path) {
|
|
236
|
+
const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
|
|
237
|
+
this.regexp = regex;
|
|
238
|
+
this.paramNames = keys;
|
|
239
|
+
}
|
|
217
240
|
}
|
|
218
241
|
|
|
219
242
|
return this;
|
package/lib/router.js
CHANGED
|
@@ -165,14 +165,14 @@ class Router {
|
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
} else {
|
|
168
|
-
const keys =
|
|
169
|
-
pathToRegexp(router.opts.prefix || '', keys);
|
|
168
|
+
const { keys } = pathToRegexp(router.opts.prefix || '', router.opts);
|
|
170
169
|
const routerPrefixHasParam = Boolean(
|
|
171
170
|
router.opts.prefix && keys.length > 0
|
|
172
171
|
);
|
|
173
172
|
router.register(path || '([^/]*)', [], m, {
|
|
174
173
|
end: false,
|
|
175
|
-
ignoreCaptures: !hasPath && !routerPrefixHasParam
|
|
174
|
+
ignoreCaptures: !hasPath && !routerPrefixHasParam,
|
|
175
|
+
pathIsRegexp: true
|
|
176
176
|
});
|
|
177
177
|
}
|
|
178
178
|
}
|
|
@@ -228,7 +228,7 @@ class Router {
|
|
|
228
228
|
ctx.routerPath;
|
|
229
229
|
const matched = router.match(path, ctx.method);
|
|
230
230
|
if (ctx.matched) {
|
|
231
|
-
ctx.matched.push(matched.path);
|
|
231
|
+
ctx.matched.push.apply(ctx.matched, matched.path);
|
|
232
232
|
} else {
|
|
233
233
|
ctx.matched = matched.path;
|
|
234
234
|
}
|
|
@@ -380,7 +380,7 @@ class Router {
|
|
|
380
380
|
* @returns {Router}
|
|
381
381
|
*/
|
|
382
382
|
all(name, path, middleware) {
|
|
383
|
-
if (typeof path === 'string') {
|
|
383
|
+
if (typeof path === 'string' || path instanceof RegExp) {
|
|
384
384
|
middleware = Array.prototype.slice.call(arguments, 2);
|
|
385
385
|
} else {
|
|
386
386
|
middleware = Array.prototype.slice.call(arguments, 1);
|
|
@@ -396,7 +396,12 @@ class Router {
|
|
|
396
396
|
)
|
|
397
397
|
throw new Error('You have to provide a path when adding an all handler');
|
|
398
398
|
|
|
399
|
-
|
|
399
|
+
const opts = {
|
|
400
|
+
name,
|
|
401
|
+
pathIsRegexp: path instanceof RegExp
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
this.register(path, methods, middleware, { ...this.opts, ...opts });
|
|
400
405
|
|
|
401
406
|
return this;
|
|
402
407
|
}
|
|
@@ -455,10 +460,10 @@ class Router {
|
|
|
455
460
|
* @returns {Layer}
|
|
456
461
|
* @private
|
|
457
462
|
*/
|
|
458
|
-
register(path, methods, middleware,
|
|
463
|
+
register(path, methods, middleware, newOpts = {}) {
|
|
459
464
|
const router = this;
|
|
460
465
|
const { stack } = this;
|
|
461
|
-
|
|
466
|
+
const opts = { ...this.opts, ...newOpts };
|
|
462
467
|
// support array of paths
|
|
463
468
|
if (Array.isArray(path)) {
|
|
464
469
|
for (const curPath of path) {
|
|
@@ -472,12 +477,14 @@ class Router {
|
|
|
472
477
|
const route = new Layer(path, methods, middleware, {
|
|
473
478
|
end: opts.end === false ? opts.end : true,
|
|
474
479
|
name: opts.name,
|
|
475
|
-
sensitive: opts.sensitive ||
|
|
476
|
-
strict: opts.strict ||
|
|
477
|
-
prefix: opts.prefix ||
|
|
478
|
-
ignoreCaptures: opts.ignoreCaptures
|
|
480
|
+
sensitive: opts.sensitive || false,
|
|
481
|
+
strict: opts.strict || false,
|
|
482
|
+
prefix: opts.prefix || '',
|
|
483
|
+
ignoreCaptures: opts.ignoreCaptures,
|
|
484
|
+
pathIsRegexp: opts.pathIsRegexp
|
|
479
485
|
});
|
|
480
486
|
|
|
487
|
+
// if parent prefix exists, add prefix to new route
|
|
481
488
|
if (this.opts.prefix) {
|
|
482
489
|
route.setPrefix(this.opts.prefix);
|
|
483
490
|
}
|
|
@@ -812,8 +819,13 @@ for (const method of methods) {
|
|
|
812
819
|
`You have to provide a path when adding a ${method} handler`
|
|
813
820
|
);
|
|
814
821
|
|
|
815
|
-
|
|
822
|
+
const opts = {
|
|
823
|
+
name,
|
|
824
|
+
pathIsRegexp: path instanceof RegExp
|
|
825
|
+
};
|
|
816
826
|
|
|
827
|
+
// pass opts to register call on verb methods
|
|
828
|
+
this.register(path, [method], middleware, { ...this.opts, ...opts });
|
|
817
829
|
return this;
|
|
818
830
|
};
|
|
819
831
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koa/router",
|
|
3
3
|
"description": "Router middleware for koa. Maintained by Forward Email and Lad.",
|
|
4
|
-
"version": "13.0.
|
|
4
|
+
"version": "13.0.1",
|
|
5
5
|
"author": "Alex Mingoia <talk@alexmingoia.com>",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/koajs/router/issues",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"http-errors": "^2.0.0",
|
|
25
25
|
"koa-compose": "^4.1.0",
|
|
26
|
-
"path-to-regexp": "^
|
|
26
|
+
"path-to-regexp": "^8.1.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@commitlint/cli": "^17.7.2",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"main": "lib/router.js",
|
|
61
61
|
"repository": {
|
|
62
62
|
"type": "git",
|
|
63
|
-
"url": "https://github.com/koajs/router.git"
|
|
63
|
+
"url": "git+https://github.com/koajs/router.git"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"bench": "make -C bench",
|