@eggjs/router 2.0.1 → 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
 
@@ -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
 
@@ -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');
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.1",
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,12 +54,5 @@
50
54
  "ci": "npm run lint && egg-bin cov",
51
55
  "lint": "eslint ."
52
56
  },
53
- "ci": {
54
- "type": "github",
55
- "version": "8, 10, 11"
56
- },
57
- "engines": {
58
- "node": ">= 8"
59
- },
60
57
  "license": "MIT"
61
58
  }
package/History.md DELETED
@@ -1,187 +0,0 @@
1
-
2
- 2.0.1 / 2021-07-12
3
- ==================
4
-
5
- **fixes**
6
- * [[`2c44ec0`](http://github.com/eggjs/egg-router/commit/2c44ec0bdeff1e8f46d899ed4386d1e28a93a854)] - fix: ensure ctx._matchedRoute and ctx._matchedRouteName to be correct (#7) (Yiyu He <<dead_horse@qq.com>>)
7
-
8
- 2.0.0 / 2019-02-14
9
- ==================
10
-
11
- **features**
12
- * [[`f0b29ec`](https://github.com/eggjs/egg-router/commit/f0b29ec34df32ba9d872f6318f4febd2b4ef9359)] - refactor: use class instead prototype (#4) (fengmk2 <<fengmk2@gmail.com>>)
13
-
14
- 1.2.0 / 2019-02-13
15
- ==================
16
-
17
- **features**
18
- * [[`1a0036a`](http://github.com/eggjs/egg-router/commit/1a0036a64a023b6b7993fecffe8062d5d0150971)] - feat: add routerPath to respond to routerName (#2) (Khaidi Chu <<i@2333.moe>>)
19
-
20
- **others**
21
- * [[`ff723fd`](http://github.com/eggjs/egg-router/commit/ff723fd03630ec49d5fe861abf129a583d214d22)] - chore: add koa-router license (#3) (fengmk2 <<fengmk2@gmail.com>>)
22
-
23
- 1.1.0 / 2019-01-30
24
- ==================
25
-
26
- **features**
27
- * [[`b318dd5`](http://github.com/eggjs/egg-router/commit/b318dd5ff2eea60013ed62b2a435f803c12bf20f)] - feat: add egg-router (dead-horse <<dead_horse@qq.com>>)
28
-
29
- **fixes**
30
- * [[`b3db7b4`](http://github.com/eggjs/egg-router/commit/b3db7b41988d3bf1b4e885ed76b3a8165c1d3b1d)] - fix: add missing dependencies koa-convert (dead-horse <<dead_horse@qq.com>>)
31
- * [[`c280336`](http://github.com/eggjs/egg-router/commit/c2803368a8256bc9504ae3c821eefeb9d1fcbc4d)] - fix: only support node@8 (dead-horse <<dead_horse@qq.com>>)
32
- * [[`7a887a2`](http://github.com/eggjs/egg-router/commit/7a887a252445e602c2ea7e1c6f4cde52a433644d)] - fix: update license (dead-horse <<dead_horse@qq.com>>)
33
-
34
- **others**
35
- * [[`ae40168`](http://github.com/eggjs/egg-router/commit/ae4016862fb8bdaf30e730a9278fbb1455d8b75d)] - docs: clean doc (dead-horse <<dead_horse@qq.com>>)
36
- * [[`e4f21a8`](http://github.com/eggjs/egg-router/commit/e4f21a8ed6dfd72c4d6f4a13bbda9a4e90b0401c)] - chore: fix history (dead-horse <<dead_horse@qq.com>>)
37
-
38
- 1.0.0 / 2019-01-30
39
- ==================
40
-
41
- **others**
42
- * [[`d6496e0`](http://github.com/eggjs/egg-router/commit/d6496e09be6b0f91dcb96611f31ec5ab6ad8ac78)] - refactor: rename to @eggjs/router (dead-horse <<dead_horse@qq.com>>)
43
-
44
- -------------------------------
45
-
46
- # Release History from koa-router
47
-
48
- ## 7.4.0
49
-
50
- - Fix router.url() for multiple nested routers [#407](https://github.com/alexmingoia/koa-router/pull/407)
51
- - `layer.name` added to `ctx` at `ctx.routerName` during routing [#412](https://github.com/alexmingoia/koa-router/pull/412)
52
- - 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.
53
-
54
- ## 7.3.0
55
-
56
- - Router#url() now accepts query parameters to add to generated urls [#396](https://github.com/alexmingoia/koa-router/pull/396)
57
-
58
- ## 7.2.1
59
-
60
- - Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
61
-
62
- ## 7.2.0
63
-
64
- - Fix a bug in Router#url and append Router object to ctx. [#350](https://github.com/alexmingoia/koa-router/pull/350)
65
- - Adds `_matchedRouteName` to context [#337](https://github.com/alexmingoia/koa-router/pull/337)
66
- - Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
67
-
68
- ## 7.1.1
69
-
70
- - Fix bug where param handlers were run out of order [#282](https://github.com/alexmingoia/koa-router/pull/282)
71
-
72
- ## 7.1.0
73
-
74
- - Backports: merge 5.4 work into the 7.x upstream. See 5.4.0 updates for more details.
75
-
76
- ## 7.0.1
77
-
78
- - Fix: allowedMethods should be ctx.method not this.method [#215](https://github.com/alexmingoia/koa-router/pull/215)
79
-
80
- ## 7.0.0
81
-
82
- - The API has changed to match the new promise-based middleware
83
- signature of koa 2. See the
84
- [koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
85
- information.
86
- - Middleware is now always run in the order declared by `.use()` (or `.get()`,
87
- etc.), which matches Express 4 API.
88
-
89
- ## 5.4.0
90
-
91
- - Expose matched route at `ctx._matchedRoute`.
92
-
93
- ## 5.3.0
94
-
95
- - Register multiple routes with array of paths [#203](https://github.com/alexmingoia/koa-router/issue/143).
96
- - Improved router.url() [#143](https://github.com/alexmingoia/koa-router/pull/143)
97
- - Adds support for named routes and regular expressions
98
- [#152](https://github.com/alexmingoia/koa-router/pull/152)
99
- - Add support for custom throw functions for 405 and 501 responses [#206](https://github.com/alexmingoia/koa-router/pull/206)
100
-
101
- ## 5.2.3
102
-
103
- - Fix for middleware running twice when nesting routes [#184](https://github.com/alexmingoia/koa-router/issues/184)
104
-
105
- ## 5.2.2
106
-
107
- - Register routes without params before those with params [#183](https://github.com/alexmingoia/koa-router/pull/183)
108
- - Fix for allowed methods [#182](https://github.com/alexmingoia/koa-router/issues/182)
109
-
110
- ## 5.2.0
111
-
112
- - Add support for async/await. Resolves [#130](https://github.com/alexmingoia/koa-router/issues/130).
113
- - Add support for array of paths by Router#use(). Resolves [#175](https://github.com/alexmingoia/koa-router/issues/175).
114
- - Inherit param middleware when nesting routers. Fixes [#170](https://github.com/alexmingoia/koa-router/issues/170).
115
- - 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).
116
- - Run nested router middleware after parent's. Fixes [#156](https://github.com/alexmingoia/koa-router/issues/156).
117
- - Remove dependency on koa-compose.
118
-
119
- ## 5.1.1
120
-
121
- - Match routes in order they were defined. Fixes #131.
122
-
123
- ## 5.1.0
124
-
125
- - Support mounting router middleware at a given path.
126
-
127
- ## 5.0.1
128
-
129
- - Fix bug with missing parameters when nesting routers.
130
-
131
- ## 5.0.0
132
-
133
- - Remove confusing API for extending koa app with router methods. Router#use()
134
- does not have the same behavior as app#use().
135
- - Add support for nesting routes.
136
- - Remove support for regular expression routes to achieve nestable routers and
137
- enable future trie-based routing optimizations.
138
-
139
- ## 4.3.2
140
-
141
- - Do not send 405 if route matched but status is 404. Fixes #112, closes #114.
142
-
143
- ## 4.3.1
144
-
145
- - Do not run middleware if not yielded to by previous middleware. Fixes #115.
146
-
147
- ## 4.3.0
148
-
149
- - Add support for router prefixes.
150
- - Add MIT license.
151
-
152
- ## 4.2.0
153
-
154
- - Fixed issue with router middleware being applied even if no route was
155
- matched.
156
- - Router.url - new static method to generate url from url pattern and data
157
-
158
- ## 4.1.0
159
-
160
- Private API changed to separate context parameter decoration from route
161
- matching. `Router#match` and `Route#match` are now pure functions that return
162
- an array of routes that match the URL path.
163
-
164
- For modules using this private API that need to determine if a method and path
165
- match a route, `route.methods` must be checked against the routes returned from
166
- `router.match()`:
167
-
168
- ```javascript
169
- var matchedRoute = router.match(path).filter(function (route) {
170
- return ~route.methods.indexOf(method);
171
- }).shift();
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`).