@koa/router 13.0.1 → 13.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/HISTORY.md +187 -0
- package/lib/layer.js +7 -30
- package/lib/router.js +12 -24
- package/package.json +3 -3
package/HISTORY.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# History
|
|
2
|
+
|
|
3
|
+
**[History has moved to the Releases tab of GitHub](https://github.com/koajs/router/releases).**
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Log
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## 9.0.0 / 2020-04-09
|
|
10
|
+
|
|
11
|
+
* Update `path-to-regexp`. Migration path: change usage of `'*'` in routes to `(.*)` or `:splat*`.
|
|
12
|
+
* Example: `router.get('*', ....)` becomes `router.get('(.*)') ....)`
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## 8.0.0 / 2019-06-16
|
|
16
|
+
|
|
17
|
+
**others**
|
|
18
|
+
|
|
19
|
+
* [b5dd5e8](http://github.com/koajs/koa-router/commit/b5dd5e8f00e841b7061a62ab6228cbe96a999470)] - chore: rename to @koa/router (dead-horse)
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
Changelogs inherit from koa-router.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## 7.4.0
|
|
27
|
+
|
|
28
|
+
* Fix router.url() for multiple nested routers [#407](https://github.com/alexmingoia/koa-router/pull/407)
|
|
29
|
+
* `layer.name` added to `ctx` at `ctx.routerName` during routing [#412](https://github.com/alexmingoia/koa-router/pull/412)
|
|
30
|
+
* Router.use() was erroneously settings `(.*)` as a prefix to all routers nested with .use that did not pass an explicit prefix string as the first argument. This resulted in routes being matched that should not have been, included the running of multiple route handlers in error. [#369](https://github.com/alexmingoia/koa-router/issues/369) and [#410](https://github.com/alexmingoia/koa-router/issues/410) include information on this issue.
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
## 7.3.0
|
|
34
|
+
|
|
35
|
+
* Router#url() now accepts query parameters to add to generated urls [#396](https://github.com/alexmingoia/koa-router/pull/396)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## 7.2.1
|
|
39
|
+
|
|
40
|
+
* Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## 7.2.0
|
|
44
|
+
|
|
45
|
+
* Fix a bug in Router#url and append Router object to ctx. [#350](https://github.com/alexmingoia/koa-router/pull/350)
|
|
46
|
+
* Adds `_matchedRouteName` to context [#337](https://github.com/alexmingoia/koa-router/pull/337)
|
|
47
|
+
* Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## 7.1.1
|
|
51
|
+
|
|
52
|
+
* Fix bug where param handlers were run out of order [#282](https://github.com/alexmingoia/koa-router/pull/282)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
## 7.1.0
|
|
56
|
+
|
|
57
|
+
* Backports: merge 5.4 work into the 7.x upstream. See 5.4.0 updates for more details.
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
## 7.0.1
|
|
61
|
+
|
|
62
|
+
* Fix: allowedMethods should be ctx.method not this.method [#215](https://github.com/alexmingoia/koa-router/pull/215)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
## 7.0.0
|
|
66
|
+
|
|
67
|
+
* The API has changed to match the new promise-based middleware
|
|
68
|
+
signature of koa 2. See the
|
|
69
|
+
[koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
|
|
70
|
+
information.
|
|
71
|
+
* Middleware is now always run in the order declared by `.use()` (or `.get()`,
|
|
72
|
+
etc.), which matches Express 4 API.
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## 5.4.0
|
|
76
|
+
|
|
77
|
+
* Expose matched route at `ctx._matchedRoute`.
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## 5.3.0
|
|
81
|
+
|
|
82
|
+
* Register multiple routes with array of paths [#203](https://github.com/alexmingoia/koa-router/issue/143).
|
|
83
|
+
* Improved router.url() [#143](https://github.com/alexmingoia/koa-router/pull/143)
|
|
84
|
+
* Adds support for named routes and regular expressions
|
|
85
|
+
[#152](https://github.com/alexmingoia/koa-router/pull/152)
|
|
86
|
+
* Add support for custom throw functions for 405 and 501 responses [#206](https://github.com/alexmingoia/koa-router/pull/206)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## 5.2.3
|
|
90
|
+
|
|
91
|
+
* Fix for middleware running twice when nesting routes [#184](https://github.com/alexmingoia/koa-router/issues/184)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
## 5.2.2
|
|
95
|
+
|
|
96
|
+
* Register routes without params before those with params [#183](https://github.com/alexmingoia/koa-router/pull/183)
|
|
97
|
+
* Fix for allowed methods [#182](https://github.com/alexmingoia/koa-router/issues/182)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
## 5.2.0
|
|
101
|
+
|
|
102
|
+
* Add support for async/await. Resolves [#130](https://github.com/alexmingoia/koa-router/issues/130).
|
|
103
|
+
* Add support for array of paths by Router#use(). Resolves [#175](https://github.com/alexmingoia/koa-router/issues/175).
|
|
104
|
+
* Inherit param middleware when nesting routers. Fixes [#170](https://github.com/alexmingoia/koa-router/issues/170).
|
|
105
|
+
* Default router middleware without path to root. Fixes [#161](https://github.com/alexmingoia/koa-router/issues/161), [#155](https://github.com/alexmingoia/koa-router/issues/155), [#156](https://github.com/alexmingoia/koa-router/issues/156).
|
|
106
|
+
* Run nested router middleware after parent's. Fixes [#156](https://github.com/alexmingoia/koa-router/issues/156).
|
|
107
|
+
* Remove dependency on koa-compose.
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
## 5.1.1
|
|
111
|
+
|
|
112
|
+
* Match routes in order they were defined. Fixes #131.
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## 5.1.0
|
|
116
|
+
|
|
117
|
+
* Support mounting router middleware at a given path.
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
## 5.0.1
|
|
121
|
+
|
|
122
|
+
* Fix bug with missing parameters when nesting routers.
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## 5.0.0
|
|
126
|
+
|
|
127
|
+
* Remove confusing API for extending koa app with router methods. Router#use()
|
|
128
|
+
does not have the same behavior as app#use().
|
|
129
|
+
* Add support for nesting routes.
|
|
130
|
+
* Remove support for regular expression routes to achieve nestable routers and
|
|
131
|
+
enable future trie-based routing optimizations.
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
## 4.3.2
|
|
135
|
+
|
|
136
|
+
* Do not send 405 if route matched but status is 404. Fixes #112, closes #114.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
## 4.3.1
|
|
140
|
+
|
|
141
|
+
* Do not run middleware if not yielded to by previous middleware. Fixes #115.
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
## 4.3.0
|
|
145
|
+
|
|
146
|
+
* Add support for router prefixes.
|
|
147
|
+
* Add MIT license.
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
## 4.2.0
|
|
151
|
+
|
|
152
|
+
* Fixed issue with router middleware being applied even if no route was
|
|
153
|
+
matched.
|
|
154
|
+
* Router.url - new static method to generate url from url pattern and data
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
## 4.1.0
|
|
158
|
+
|
|
159
|
+
Private API changed to separate context parameter decoration from route
|
|
160
|
+
matching. `Router#match` and `Route#match` are now pure functions that return
|
|
161
|
+
an array of routes that match the URL path.
|
|
162
|
+
|
|
163
|
+
For modules using this private API that need to determine if a method and path
|
|
164
|
+
match a route, `route.methods` must be checked against the routes returned from
|
|
165
|
+
`router.match()`:
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
var matchedRoute = router.match(path).filter(function (route) {
|
|
169
|
+
return ~route.methods.indexOf(method);
|
|
170
|
+
}).shift();
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
## 4.0.0
|
|
175
|
+
|
|
176
|
+
405, 501, and OPTIONS response handling was moved into separate middleware
|
|
177
|
+
`router.allowedMethods()`. This resolves incorrect 501 or 405 responses when
|
|
178
|
+
using multiple routers.
|
|
179
|
+
|
|
180
|
+
### Breaking changes
|
|
181
|
+
|
|
182
|
+
4.x is mostly backwards compatible with 3.x, except for the following:
|
|
183
|
+
|
|
184
|
+
* Instantiating a router with `new` and `app` returns the router instance,
|
|
185
|
+
whereas 3.x returns the router middleware. When creating a router in 4.x, the
|
|
186
|
+
only time router middleware is returned is when creating using the
|
|
187
|
+
`Router(app)` signature (with `app` and without `new`).
|
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;
|
package/lib/router.js
CHANGED
|
@@ -165,14 +165,14 @@ class Router {
|
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
} else {
|
|
168
|
-
const
|
|
168
|
+
const keys = [];
|
|
169
|
+
pathToRegexp(router.opts.prefix || '', keys);
|
|
169
170
|
const routerPrefixHasParam = Boolean(
|
|
170
171
|
router.opts.prefix && keys.length > 0
|
|
171
172
|
);
|
|
172
173
|
router.register(path || '([^/]*)', [], m, {
|
|
173
174
|
end: false,
|
|
174
|
-
ignoreCaptures: !hasPath && !routerPrefixHasParam
|
|
175
|
-
pathIsRegexp: true
|
|
175
|
+
ignoreCaptures: !hasPath && !routerPrefixHasParam
|
|
176
176
|
});
|
|
177
177
|
}
|
|
178
178
|
}
|
|
@@ -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') {
|
|
384
384
|
middleware = Array.prototype.slice.call(arguments, 2);
|
|
385
385
|
} else {
|
|
386
386
|
middleware = Array.prototype.slice.call(arguments, 1);
|
|
@@ -396,12 +396,7 @@ class Router {
|
|
|
396
396
|
)
|
|
397
397
|
throw new Error('You have to provide a path when adding an all handler');
|
|
398
398
|
|
|
399
|
-
|
|
400
|
-
name,
|
|
401
|
-
pathIsRegexp: path instanceof RegExp
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
this.register(path, methods, middleware, { ...this.opts, ...opts });
|
|
399
|
+
this.register(path, methods, middleware, { name });
|
|
405
400
|
|
|
406
401
|
return this;
|
|
407
402
|
}
|
|
@@ -460,10 +455,10 @@ class Router {
|
|
|
460
455
|
* @returns {Layer}
|
|
461
456
|
* @private
|
|
462
457
|
*/
|
|
463
|
-
register(path, methods, middleware,
|
|
458
|
+
register(path, methods, middleware, opts = {}) {
|
|
464
459
|
const router = this;
|
|
465
460
|
const { stack } = this;
|
|
466
|
-
|
|
461
|
+
|
|
467
462
|
// support array of paths
|
|
468
463
|
if (Array.isArray(path)) {
|
|
469
464
|
for (const curPath of path) {
|
|
@@ -477,14 +472,12 @@ class Router {
|
|
|
477
472
|
const route = new Layer(path, methods, middleware, {
|
|
478
473
|
end: opts.end === false ? opts.end : true,
|
|
479
474
|
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
|
|
475
|
+
sensitive: opts.sensitive || this.opts.sensitive || false,
|
|
476
|
+
strict: opts.strict || this.opts.strict || false,
|
|
477
|
+
prefix: opts.prefix || this.opts.prefix || '',
|
|
478
|
+
ignoreCaptures: opts.ignoreCaptures
|
|
485
479
|
});
|
|
486
480
|
|
|
487
|
-
// if parent prefix exists, add prefix to new route
|
|
488
481
|
if (this.opts.prefix) {
|
|
489
482
|
route.setPrefix(this.opts.prefix);
|
|
490
483
|
}
|
|
@@ -819,13 +812,8 @@ for (const method of methods) {
|
|
|
819
812
|
`You have to provide a path when adding a ${method} handler`
|
|
820
813
|
);
|
|
821
814
|
|
|
822
|
-
|
|
823
|
-
name,
|
|
824
|
-
pathIsRegexp: path instanceof RegExp
|
|
825
|
-
};
|
|
815
|
+
this.register(path, [method], middleware, { name });
|
|
826
816
|
|
|
827
|
-
// pass opts to register call on verb methods
|
|
828
|
-
this.register(path, [method], middleware, { ...this.opts, ...opts });
|
|
829
817
|
return this;
|
|
830
818
|
};
|
|
831
819
|
}
|
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.1.0",
|
|
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": "^6.3.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@commitlint/cli": "^17.7.2",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
|
|
69
69
|
"lint": "xo --fix && remark . -qfo && fixpack",
|
|
70
70
|
"prepare": "husky install",
|
|
71
|
-
"
|
|
71
|
+
"prextest": "npm run lint",
|
|
72
72
|
"test": "mocha test/**/*.js",
|
|
73
73
|
"test:watch": "mocha test/**/*.js --watch"
|
|
74
74
|
}
|