@koa/router 13.0.1 → 13.1.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 +8 -30
- package/lib/router.js +13 -26
- package/package.json +3 -2
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
|
|
3
|
+
const { pathToRegexp, compile, parse } = require('path-to-regexp');
|
|
4
4
|
|
|
5
5
|
module.exports = class Layer {
|
|
6
6
|
/**
|
|
@@ -13,7 +13,6 @@ 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
|
|
17
16
|
* @returns {Layer}
|
|
18
17
|
* @private
|
|
19
18
|
*/
|
|
@@ -42,19 +41,7 @@ module.exports = class Layer {
|
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
this.path = path;
|
|
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
|
-
}
|
|
44
|
+
this.regexp = pathToRegexp(path, this.paramNames, this.opts);
|
|
58
45
|
}
|
|
59
46
|
|
|
60
47
|
/**
|
|
@@ -129,14 +116,13 @@ module.exports = class Layer {
|
|
|
129
116
|
|
|
130
117
|
const toPath = compile(url, { encode: encodeURIComponent, ...options });
|
|
131
118
|
let replaced;
|
|
132
|
-
|
|
119
|
+
|
|
120
|
+
const tokens = parse(url);
|
|
133
121
|
let replace = {};
|
|
134
122
|
|
|
135
123
|
if (Array.isArray(args)) {
|
|
136
124
|
for (let len = tokens.length, i = 0, j = 0; i < len; i++) {
|
|
137
|
-
if (tokens[i].name)
|
|
138
|
-
replace[tokens[i].name] = args[j++];
|
|
139
|
-
}
|
|
125
|
+
if (tokens[i].name) replace[tokens[i].name] = args[j++];
|
|
140
126
|
}
|
|
141
127
|
} else if (tokens.some((token) => token.name)) {
|
|
142
128
|
replace = params;
|
|
@@ -144,10 +130,6 @@ module.exports = class Layer {
|
|
|
144
130
|
options = params;
|
|
145
131
|
}
|
|
146
132
|
|
|
147
|
-
for (const [key, value] of Object.entries(replace)) {
|
|
148
|
-
replace[key] = String(value);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
133
|
replaced = toPath(replace);
|
|
152
134
|
|
|
153
135
|
if (options && options.query) {
|
|
@@ -230,13 +212,8 @@ module.exports = class Layer {
|
|
|
230
212
|
this.path !== '/' || this.opts.strict === true
|
|
231
213
|
? `${prefix}${this.path}`
|
|
232
214
|
: prefix;
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
} else if (this.path) {
|
|
236
|
-
const { regexp: regex, keys } = pathToRegexp(this.path, this.opts);
|
|
237
|
-
this.regexp = regex;
|
|
238
|
-
this.paramNames = keys;
|
|
239
|
-
}
|
|
215
|
+
this.paramNames = [];
|
|
216
|
+
this.regexp = pathToRegexp(this.path, this.paramNames, this.opts);
|
|
240
217
|
}
|
|
241
218
|
|
|
242
219
|
return this;
|
|
@@ -254,6 +231,7 @@ module.exports = class Layer {
|
|
|
254
231
|
|
|
255
232
|
function safeDecodeURIComponent(text) {
|
|
256
233
|
try {
|
|
234
|
+
// TODO: take a look on `safeDecodeURIComponent` if we use it only with route params let's remove the `replace` method otherwise make it flexible.
|
|
257
235
|
// @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#decoding_query_parameters_from_a_url
|
|
258
236
|
return decodeURIComponent(text.replace(/\+/g, ' '));
|
|
259
237
|
} catch {
|
package/lib/router.js
CHANGED
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
* @link https://github.com/alexmingoia/koa-router
|
|
6
6
|
*/
|
|
7
7
|
const http = require('node:http');
|
|
8
|
-
const util = require('node:util');
|
|
9
8
|
|
|
10
|
-
const debug =
|
|
9
|
+
const debug = require('debug')('koa-router');
|
|
11
10
|
|
|
12
11
|
const compose = require('koa-compose');
|
|
13
12
|
const HttpError = require('http-errors');
|
|
@@ -165,14 +164,14 @@ class Router {
|
|
|
165
164
|
}
|
|
166
165
|
}
|
|
167
166
|
} else {
|
|
168
|
-
const
|
|
167
|
+
const keys = [];
|
|
168
|
+
pathToRegexp(router.opts.prefix || '', keys);
|
|
169
169
|
const routerPrefixHasParam = Boolean(
|
|
170
170
|
router.opts.prefix && keys.length > 0
|
|
171
171
|
);
|
|
172
172
|
router.register(path || '([^/]*)', [], m, {
|
|
173
173
|
end: false,
|
|
174
|
-
ignoreCaptures: !hasPath && !routerPrefixHasParam
|
|
175
|
-
pathIsRegexp: true
|
|
174
|
+
ignoreCaptures: !hasPath && !routerPrefixHasParam
|
|
176
175
|
});
|
|
177
176
|
}
|
|
178
177
|
}
|
|
@@ -380,7 +379,7 @@ class Router {
|
|
|
380
379
|
* @returns {Router}
|
|
381
380
|
*/
|
|
382
381
|
all(name, path, middleware) {
|
|
383
|
-
if (typeof path === 'string'
|
|
382
|
+
if (typeof path === 'string') {
|
|
384
383
|
middleware = Array.prototype.slice.call(arguments, 2);
|
|
385
384
|
} else {
|
|
386
385
|
middleware = Array.prototype.slice.call(arguments, 1);
|
|
@@ -396,12 +395,7 @@ class Router {
|
|
|
396
395
|
)
|
|
397
396
|
throw new Error('You have to provide a path when adding an all handler');
|
|
398
397
|
|
|
399
|
-
|
|
400
|
-
name,
|
|
401
|
-
pathIsRegexp: path instanceof RegExp
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
this.register(path, methods, middleware, { ...this.opts, ...opts });
|
|
398
|
+
this.register(path, methods, middleware, { name });
|
|
405
399
|
|
|
406
400
|
return this;
|
|
407
401
|
}
|
|
@@ -460,10 +454,10 @@ class Router {
|
|
|
460
454
|
* @returns {Layer}
|
|
461
455
|
* @private
|
|
462
456
|
*/
|
|
463
|
-
register(path, methods, middleware,
|
|
457
|
+
register(path, methods, middleware, opts = {}) {
|
|
464
458
|
const router = this;
|
|
465
459
|
const { stack } = this;
|
|
466
|
-
|
|
460
|
+
|
|
467
461
|
// support array of paths
|
|
468
462
|
if (Array.isArray(path)) {
|
|
469
463
|
for (const curPath of path) {
|
|
@@ -477,14 +471,12 @@ class Router {
|
|
|
477
471
|
const route = new Layer(path, methods, middleware, {
|
|
478
472
|
end: opts.end === false ? opts.end : true,
|
|
479
473
|
name: opts.name,
|
|
480
|
-
sensitive: opts.sensitive || false,
|
|
481
|
-
strict: opts.strict || false,
|
|
482
|
-
prefix: opts.prefix || '',
|
|
483
|
-
ignoreCaptures: opts.ignoreCaptures
|
|
484
|
-
pathIsRegexp: opts.pathIsRegexp
|
|
474
|
+
sensitive: opts.sensitive || this.opts.sensitive || false,
|
|
475
|
+
strict: opts.strict || this.opts.strict || false,
|
|
476
|
+
prefix: opts.prefix || this.opts.prefix || '',
|
|
477
|
+
ignoreCaptures: opts.ignoreCaptures
|
|
485
478
|
});
|
|
486
479
|
|
|
487
|
-
// if parent prefix exists, add prefix to new route
|
|
488
480
|
if (this.opts.prefix) {
|
|
489
481
|
route.setPrefix(this.opts.prefix);
|
|
490
482
|
}
|
|
@@ -819,13 +811,8 @@ for (const method of methods) {
|
|
|
819
811
|
`You have to provide a path when adding a ${method} handler`
|
|
820
812
|
);
|
|
821
813
|
|
|
822
|
-
|
|
823
|
-
name,
|
|
824
|
-
pathIsRegexp: path instanceof RegExp
|
|
825
|
-
};
|
|
814
|
+
this.register(path, [method], middleware, { name });
|
|
826
815
|
|
|
827
|
-
// pass opts to register call on verb methods
|
|
828
|
-
this.register(path, [method], middleware, { ...this.opts, ...opts });
|
|
829
816
|
return this;
|
|
830
817
|
};
|
|
831
818
|
}
|
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.
|
|
4
|
+
"version": "13.1.1",
|
|
5
5
|
"author": "Alex Mingoia <talk@alexmingoia.com>",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/koajs/router/issues",
|
|
@@ -21,9 +21,10 @@
|
|
|
21
21
|
}
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
+
"debug": "^4.4.1",
|
|
24
25
|
"http-errors": "^2.0.0",
|
|
25
26
|
"koa-compose": "^4.1.0",
|
|
26
|
-
"path-to-regexp": "^
|
|
27
|
+
"path-to-regexp": "^6.3.0"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@commitlint/cli": "^17.7.2",
|