@eggjs/router 2.0.0 → 2.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/README.md CHANGED
@@ -3,26 +3,31 @@
3
3
  Router core component for [Egg.js](https://github.com/eggjs).
4
4
 
5
5
  > **This repository is a fork of [koa-router](https://github.com/alexmingoia/koa-router).** with some additional features.
6
-
7
- > And thanks for the greate work of @alexmingoia and the original team.
6
+ > And thanks for the great work of @alexmingoia and the original team.
8
7
 
9
8
  ## API Reference
10
9
 
11
- * [egg-router](#module_egg-router)
12
- * [Router](#exp_module_egg-router--Router)
13
- * [new Router([opts])](#new_module_egg-router--Router_new)
14
- * _instance_
15
- * [.get|put|post|patch|delete|del](#module_egg-router--Router+get|put|post|patch|delete|del) ⇒ <code>Router</code>
16
- * [.routes](#module_egg-router--Router+routes) ⇒ <code>function</code>
17
- * [.use([path], middleware)](#module_egg-router--Router+use) ⇒ <code>Router</code>
18
- * [.prefix(prefix)](#module_egg-router--Router+prefix) ⇒ <code>Router</code>
19
- * [.allowedMethods([options])](#module_egg-router--Router+allowedMethods) ⇒ <code>function</code>
20
- * [.redirect(source, destination, [code])](#module_egg-router--Router+redirect) ⇒ <code>Router</code>
21
- * [.route(name)](#module_egg-router--Router+route) ⇒ <code>Layer</code> &#124; <code>false</code>
22
- * [.url(name, params, [options])](#module_egg-router--Router+url) ⇒ <code>String</code> &#124; <code>Error</code>
23
- * [.param(param, middleware)](#module_egg-router--Router+param) ⇒ <code>Router</code>
24
- * _static_
25
- * [.url(path, params)](#module_egg-router--Router.url) ⇒ <code>String</code>
10
+ - [@eggjs/router](#eggjsrouter)
11
+ - [API Reference](#api-reference)
12
+ - [Router ](#router-)
13
+ - [new Router(\[opts\])](#new-routeropts)
14
+ - [router.get|put|post|patch|delete|del ⇒ Router](#routergetputpostpatchdeletedel--router)
15
+ - [Named routes](#named-routes)
16
+ - [Multiple middleware](#multiple-middleware)
17
+ - [Nested routers](#nested-routers)
18
+ - [Router prefixes](#router-prefixes)
19
+ - [URL parameters](#url-parameters)
20
+ - [router.routes ⇒ function](#routerroutes--function)
21
+ - [router.use(\[path\], middleware) ⇒ Router](#routerusepath-middleware--router)
22
+ - [router.prefix(prefix) ⇒ Router](#routerprefixprefix--router)
23
+ - [router.allowedMethods(\[options\]) ⇒ function](#routerallowedmethodsoptions--function)
24
+ - [router.redirect(source, destination, \[code\]) ⇒ Router](#routerredirectsource-destination-code--router)
25
+ - [router.route(name) ⇒ Layer | false](#routerroutename--layer--false)
26
+ - [router.url(name, params, \[options\]) ⇒ String | Error](#routerurlname-params-options--string--error)
27
+ - [router.param(param, middleware) ⇒ Router](#routerparamparam-middleware--router)
28
+ - [Router.url(path, params \[, options\]) ⇒ String](#routerurlpath-params--options--string)
29
+ - [Tests](#tests)
30
+ - [License](#license)
26
31
 
27
32
  <a name="exp_module_egg-router--Router"></a>
28
33
 
@@ -87,8 +92,8 @@ router
87
92
  });
88
93
  ```
89
94
 
90
- When a route is matched, its path is available at `ctx._matchedRoute` and if named,
91
- the name is available at `ctx._matchedRouteName`
95
+ When a route is matched, its path is available at `ctx.routePath` and if named,
96
+ the name is available at `ctx.routeName`
92
97
 
93
98
  Route paths will be translated to regular expressions using
94
99
  [path-to-regexp](https://github.com/pillarjs/path-to-regexp).
@@ -412,3 +417,7 @@ const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});
412
417
  ## Tests
413
418
 
414
419
  Run tests using `npm test`.
420
+
421
+ ## License
422
+
423
+ [MIT](LICENSE)
package/lib/egg_router.js CHANGED
@@ -1,11 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const is = require('is-type-of');
4
- const Router = require('./router');
5
3
  const utility = require('utility');
6
4
  const inflection = require('inflection');
7
5
  const assert = require('assert');
8
6
  const utils = require('./utils');
7
+ const Router = require('./router');
9
8
 
10
9
  const METHODS = [ 'head', 'options', 'get', 'put', 'patch', 'post', 'delete' ];
11
10
 
@@ -55,7 +54,7 @@ const REST_MAP = {
55
54
  class EggRouter extends Router {
56
55
 
57
56
  /**
58
- * @constructor
57
+ * @class
59
58
  * @param {Object} opts - Router options.
60
59
  * @param {Application} app - Application object.
61
60
  */
@@ -138,6 +137,7 @@ class EggRouter extends Router {
138
137
  * @return {Router} return route object.
139
138
  * @since 1.0.0
140
139
  */
140
+
141
141
  resources(...args) {
142
142
  const splited = spliteAndResolveRouterParams({ args, app: this.app });
143
143
  const middlewares = splited.middlewares;
@@ -198,7 +198,7 @@ class EggRouter extends Router {
198
198
  const args = params;
199
199
  let url = route.path;
200
200
 
201
- assert(!is.regExp(url), `Can't get the url for regExp ${url} for by name '${name}'`);
201
+ assert(!(url instanceof RegExp), `Can't get the url for regExp ${url} for by name '${name}'`);
202
202
 
203
203
  const queries = [];
204
204
  if (typeof args === 'object' && args !== null) {
@@ -263,7 +263,7 @@ class EggRouter extends Router {
263
263
  function spliteAndResolveRouterParams({ args, app }) {
264
264
  let prefix;
265
265
  let middlewares;
266
- if (args.length >= 3 && (is.string(args[1]) || is.regExp(args[1]))) {
266
+ if (args.length >= 3 && (typeof args[1] === 'string' || args[1] instanceof RegExp)) {
267
267
  // app.get(name, url, [...middleware], controller)
268
268
  prefix = args.slice(0, 2);
269
269
  middlewares = args.slice(2);
@@ -285,7 +285,7 @@ function spliteAndResolveRouterParams({ args, app }) {
285
285
  * @return {Function} controller function
286
286
  */
287
287
  function resolveController(controller, app) {
288
- if (is.string(controller)) {
288
+ if (typeof controller === 'string') {
289
289
  const actions = controller.split('.');
290
290
  let obj = app.controller;
291
291
  actions.forEach(key => {
package/lib/layer.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const debug = require('debug')('egg-router:layer');
3
+ const debug = require('util').debuglog('egg-router:layer');
4
4
  const pathToRegExp = require('path-to-regexp');
5
5
  const uri = require('urijs');
6
6
  const utility = require('utility');
package/lib/router.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * RESTful resource routing middleware for eggjs.
5
5
  */
6
6
 
7
- const debug = require('debug')('egg-router');
7
+ const debug = require('util').debuglog('egg-router');
8
8
  const compose = require('koa-compose');
9
9
  const HttpError = require('http-errors');
10
10
  const methods = require('methods');
@@ -40,7 +40,7 @@ class Router {
40
40
  * @alias module:koa-router
41
41
  * @param {Object=} opts optional
42
42
  * @param {String=} opts.prefix prefix router paths
43
- * @constructor
43
+ * @class
44
44
  */
45
45
  constructor(opts) {
46
46
  this.opts = opts || {};
@@ -175,18 +175,14 @@ class Router {
175
175
  if (!matched.route) return next();
176
176
 
177
177
  const matchedLayers = matched.pathAndMethod;
178
- const mostSpecificLayer = matchedLayers[matchedLayers.length - 1];
179
- ctx._matchedRoute = mostSpecificLayer.path;
180
- if (mostSpecificLayer.name) {
181
- ctx._matchedRouteName = mostSpecificLayer.name;
182
- }
183
-
184
178
  const layerChain = matchedLayers.reduce(function(memo, layer) {
185
179
  memo.push(function(ctx, next) {
186
180
  ctx.captures = layer.captures(path, ctx.captures);
187
181
  ctx.params = layer.params(path, ctx.captures, ctx.params);
188
- ctx.routerName = layer.name;
189
- ctx.routerPath = layer.path;
182
+ // ctx._matchedRouteName & ctx._matchedRoute for compatibility
183
+ ctx._matchedRouteName = ctx.routerName = layer.name;
184
+ if (!layer.name) ctx._matchedRouteName = undefined;
185
+ ctx._matchedRoute = ctx.routerPath = layer.path;
190
186
  return next();
191
187
  });
192
188
  return memo.concat(layer.stack);
package/lib/utils.js CHANGED
@@ -1,18 +1,34 @@
1
1
  'use strict';
2
2
 
3
3
  const convert = require('koa-convert');
4
- const is = require('is-type-of');
5
4
  const co = require('co');
6
5
 
6
+ function isGeneratorFunction(obj) {
7
+ return obj
8
+ && obj.constructor
9
+ && obj.constructor.name === 'GeneratorFunction';
10
+ }
11
+
7
12
  module.exports = {
8
13
  async callFn(fn, args, ctx) {
9
14
  args = args || [];
10
- if (!is.function(fn)) return;
11
- if (is.generatorFunction(fn)) fn = co.wrap(fn);
15
+ if (typeof fn !== 'function') return;
16
+ if (isGeneratorFunction(fn)) fn = co.wrap(fn);
12
17
  return ctx ? fn.call(ctx, ...args) : fn(...args);
13
18
  },
14
19
 
15
20
  middleware(fn) {
16
- return is.generatorFunction(fn) ? convert(fn) : fn;
21
+ return isGeneratorFunction(fn) ? convert(fn) : fn;
22
+ },
23
+
24
+ isGeneratorFunction,
25
+ isAsyncFunction(obj) {
26
+ return obj
27
+ && obj.constructor
28
+ && obj.constructor.name === 'AsyncFunction';
29
+ },
30
+ isPromise(obj) {
31
+ return obj
32
+ && typeof obj.then === 'function';
17
33
  },
18
34
  };
package/package.json CHANGED
@@ -1,6 +1,13 @@
1
1
  {
2
2
  "name": "@eggjs/router",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
+ "engines": {
5
+ "node": ">= 8.5.0"
6
+ },
7
+ "publishConfig": {
8
+ "access": "public",
9
+ "tag": "latest-2"
10
+ },
4
11
  "description": "Router middleware for egg/koa. Provides RESTful resource routing.",
5
12
  "repository": {
6
13
  "type": "git",
@@ -22,20 +29,17 @@
22
29
  ],
23
30
  "dependencies": {
24
31
  "co": "^4.6.0",
25
- "debug": "^3.1.0",
26
32
  "http-errors": "^1.3.1",
27
33
  "inflection": "^1.12.0",
28
- "is-type-of": "^1.2.1",
29
34
  "koa-compose": "^3.0.0",
30
35
  "koa-convert": "^1.2.0",
31
36
  "methods": "^1.0.1",
32
- "path-to-regexp": "^1.1.1",
37
+ "path-to-regexp": "^1.9.0",
33
38
  "urijs": "^1.19.0",
34
39
  "utility": "^1.15.0"
35
40
  },
36
41
  "devDependencies": {
37
42
  "egg-bin": "^4.10.0",
38
- "egg-ci": "^1.11.0",
39
43
  "eslint": "^5.13.0",
40
44
  "eslint-config-egg": "^7.1.0",
41
45
  "expect.js": "^0.3.1",
@@ -50,11 +54,5 @@
50
54
  "ci": "npm run lint && egg-bin cov",
51
55
  "lint": "eslint ."
52
56
  },
53
- "ci": {
54
- "version": "8, 10, 11"
55
- },
56
- "engines": {
57
- "node": ">= 8"
58
- },
59
57
  "license": "MIT"
60
58
  }
package/History.md DELETED
@@ -1,181 +0,0 @@
1
-
2
- 2.0.0 / 2019-02-14
3
- ==================
4
-
5
- **features**
6
- * [[`f0b29ec`](https://github.com/eggjs/egg-router/commit/f0b29ec34df32ba9d872f6318f4febd2b4ef9359)] - refactor: use class instead prototype (#4) (fengmk2 <<fengmk2@gmail.com>>)
7
-
8
- 1.2.0 / 2019-02-13
9
- ==================
10
-
11
- **features**
12
- * [[`1a0036a`](http://github.com/eggjs/egg-router/commit/1a0036a64a023b6b7993fecffe8062d5d0150971)] - feat: add routerPath to respond to routerName (#2) (Khaidi Chu <<i@2333.moe>>)
13
-
14
- **others**
15
- * [[`ff723fd`](http://github.com/eggjs/egg-router/commit/ff723fd03630ec49d5fe861abf129a583d214d22)] - chore: add koa-router license (#3) (fengmk2 <<fengmk2@gmail.com>>)
16
-
17
- 1.1.0 / 2019-01-30
18
- ==================
19
-
20
- **features**
21
- * [[`b318dd5`](http://github.com/eggjs/egg-router/commit/b318dd5ff2eea60013ed62b2a435f803c12bf20f)] - feat: add egg-router (dead-horse <<dead_horse@qq.com>>)
22
-
23
- **fixes**
24
- * [[`b3db7b4`](http://github.com/eggjs/egg-router/commit/b3db7b41988d3bf1b4e885ed76b3a8165c1d3b1d)] - fix: add missing dependencies koa-convert (dead-horse <<dead_horse@qq.com>>)
25
- * [[`c280336`](http://github.com/eggjs/egg-router/commit/c2803368a8256bc9504ae3c821eefeb9d1fcbc4d)] - fix: only support node@8 (dead-horse <<dead_horse@qq.com>>)
26
- * [[`7a887a2`](http://github.com/eggjs/egg-router/commit/7a887a252445e602c2ea7e1c6f4cde52a433644d)] - fix: update license (dead-horse <<dead_horse@qq.com>>)
27
-
28
- **others**
29
- * [[`ae40168`](http://github.com/eggjs/egg-router/commit/ae4016862fb8bdaf30e730a9278fbb1455d8b75d)] - docs: clean doc (dead-horse <<dead_horse@qq.com>>)
30
- * [[`e4f21a8`](http://github.com/eggjs/egg-router/commit/e4f21a8ed6dfd72c4d6f4a13bbda9a4e90b0401c)] - chore: fix history (dead-horse <<dead_horse@qq.com>>)
31
-
32
- 1.0.0 / 2019-01-30
33
- ==================
34
-
35
- **others**
36
- * [[`d6496e0`](http://github.com/eggjs/egg-router/commit/d6496e09be6b0f91dcb96611f31ec5ab6ad8ac78)] - refactor: rename to @eggjs/router (dead-horse <<dead_horse@qq.com>>)
37
-
38
- -------------------------------
39
-
40
- # Release History from koa-router
41
-
42
- ## 7.4.0
43
-
44
- - Fix router.url() for multiple nested routers [#407](https://github.com/alexmingoia/koa-router/pull/407)
45
- - `layer.name` added to `ctx` at `ctx.routerName` during routing [#412](https://github.com/alexmingoia/koa-router/pull/412)
46
- - 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.
47
-
48
- ## 7.3.0
49
-
50
- - Router#url() now accepts query parameters to add to generated urls [#396](https://github.com/alexmingoia/koa-router/pull/396)
51
-
52
- ## 7.2.1
53
-
54
- - Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
55
-
56
- ## 7.2.0
57
-
58
- - Fix a bug in Router#url and append Router object to ctx. [#350](https://github.com/alexmingoia/koa-router/pull/350)
59
- - Adds `_matchedRouteName` to context [#337](https://github.com/alexmingoia/koa-router/pull/337)
60
- - Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
61
-
62
- ## 7.1.1
63
-
64
- - Fix bug where param handlers were run out of order [#282](https://github.com/alexmingoia/koa-router/pull/282)
65
-
66
- ## 7.1.0
67
-
68
- - Backports: merge 5.4 work into the 7.x upstream. See 5.4.0 updates for more details.
69
-
70
- ## 7.0.1
71
-
72
- - Fix: allowedMethods should be ctx.method not this.method [#215](https://github.com/alexmingoia/koa-router/pull/215)
73
-
74
- ## 7.0.0
75
-
76
- - The API has changed to match the new promise-based middleware
77
- signature of koa 2. See the
78
- [koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
79
- information.
80
- - Middleware is now always run in the order declared by `.use()` (or `.get()`,
81
- etc.), which matches Express 4 API.
82
-
83
- ## 5.4.0
84
-
85
- - Expose matched route at `ctx._matchedRoute`.
86
-
87
- ## 5.3.0
88
-
89
- - Register multiple routes with array of paths [#203](https://github.com/alexmingoia/koa-router/issue/143).
90
- - Improved router.url() [#143](https://github.com/alexmingoia/koa-router/pull/143)
91
- - Adds support for named routes and regular expressions
92
- [#152](https://github.com/alexmingoia/koa-router/pull/152)
93
- - Add support for custom throw functions for 405 and 501 responses [#206](https://github.com/alexmingoia/koa-router/pull/206)
94
-
95
- ## 5.2.3
96
-
97
- - Fix for middleware running twice when nesting routes [#184](https://github.com/alexmingoia/koa-router/issues/184)
98
-
99
- ## 5.2.2
100
-
101
- - Register routes without params before those with params [#183](https://github.com/alexmingoia/koa-router/pull/183)
102
- - Fix for allowed methods [#182](https://github.com/alexmingoia/koa-router/issues/182)
103
-
104
- ## 5.2.0
105
-
106
- - Add support for async/await. Resolves [#130](https://github.com/alexmingoia/koa-router/issues/130).
107
- - Add support for array of paths by Router#use(). Resolves [#175](https://github.com/alexmingoia/koa-router/issues/175).
108
- - Inherit param middleware when nesting routers. Fixes [#170](https://github.com/alexmingoia/koa-router/issues/170).
109
- - 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).
110
- - Run nested router middleware after parent's. Fixes [#156](https://github.com/alexmingoia/koa-router/issues/156).
111
- - Remove dependency on koa-compose.
112
-
113
- ## 5.1.1
114
-
115
- - Match routes in order they were defined. Fixes #131.
116
-
117
- ## 5.1.0
118
-
119
- - Support mounting router middleware at a given path.
120
-
121
- ## 5.0.1
122
-
123
- - Fix bug with missing parameters when nesting routers.
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
- ## 4.3.2
134
-
135
- - Do not send 405 if route matched but status is 404. Fixes #112, closes #114.
136
-
137
- ## 4.3.1
138
-
139
- - Do not run middleware if not yielded to by previous middleware. Fixes #115.
140
-
141
- ## 4.3.0
142
-
143
- - Add support for router prefixes.
144
- - Add MIT license.
145
-
146
- ## 4.2.0
147
-
148
- - Fixed issue with router middleware being applied even if no route was
149
- matched.
150
- - Router.url - new static method to generate url from url pattern and data
151
-
152
- ## 4.1.0
153
-
154
- Private API changed to separate context parameter decoration from route
155
- matching. `Router#match` and `Route#match` are now pure functions that return
156
- an array of routes that match the URL path.
157
-
158
- For modules using this private API that need to determine if a method and path
159
- match a route, `route.methods` must be checked against the routes returned from
160
- `router.match()`:
161
-
162
- ```javascript
163
- var matchedRoute = router.match(path).filter(function (route) {
164
- return ~route.methods.indexOf(method);
165
- }).shift();
166
- ```
167
-
168
- ## 4.0.0
169
-
170
- 405, 501, and OPTIONS response handling was moved into separate middleware
171
- `router.allowedMethods()`. This resolves incorrect 501 or 405 responses when
172
- using multiple routers.
173
-
174
- ### Breaking changes
175
-
176
- 4.x is mostly backwards compatible with 3.x, except for the following:
177
-
178
- - Instantiating a router with `new` and `app` returns the router instance,
179
- whereas 3.x returns the router middleware. When creating a router in 4.x, the
180
- only time router middleware is returned is when creating using the
181
- `Router(app)` signature (with `app` and without `new`).