Socket
Socket
Sign inDemoInstall

koa-router

Package Overview
Dependencies
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-router - npm Package Compare versions

Comparing version 5.2.3 to 6.0.0

4

history.md
# History
## 6.0.0
- Koa 2.x support. See [#202](https://github.com/alexmingoia/koa-router/pull/202)
## 5.2.3

@@ -4,0 +8,0 @@

21

lib/layer.js

@@ -148,9 +148,9 @@ var debug = require('debug')('koa-router');

* router
* .param('user', function *(id, next) {
* this.user = users[id];
* if (!user) return this.status = 404;
* yield next;
* .param('user', function *(id, ctx, next) {
* ctx.user = users[id];
* if (!user) return ctx.status = 404;
* next();
* })
* .get('/users/:user', function *(next) {
* this.body = this.user;
* .get('/users/:user', function (ctx, next) {
* ctx.body = ctx.user;
* });

@@ -168,9 +168,4 @@ * ```

var params = this.paramNames;
var middleware = function *(next) {
next = fn.call(this, this.params[param], next);
if (typeof next.next === 'function') {
yield *next;
} else {
yield Promise.resolve(next);
}
var middleware = function (ctx, next) {
fn.call(this, ctx.params[param], ctx, next);
};

@@ -177,0 +172,0 @@ middleware.param = param;

@@ -9,2 +9,3 @@ /**

var debug = require('debug')('koa-router');
var compose = require('koa-compose');
var HttpError = require('http-errors');

@@ -31,3 +32,3 @@ var methods = require('methods');

*
* router.get('/', function *(next) {...});
* router.get('/', function (ctx, next) {...});
*

@@ -74,12 +75,12 @@ * app

* router
* .get('/', function *(next) {
* this.body = 'Hello World!';
* .get('/', function (ctx, next) {
* ctx.body = 'Hello World!';
* })
* .post('/users', function *(next) {
* .post('/users', function (ctx, next) {
* // ...
* })
* .put('/users/:id', function *(next) {
* .put('/users/:id', function (ctx, next) {
* // ...
* })
* .del('/users/:id', function *(next) {
* .del('/users/:id', function (ctx, next) {
* // ...

@@ -99,3 +100,3 @@ * });

* ```javascript
* router.get('user', '/users/:id', function *(next) {
* router.get('user', '/users/:id', function (ctx, next) {
* // ...

@@ -115,8 +116,10 @@ * });

* '/users/:id',
* function *(next) {
* this.user = yield User.findOne(this.params.id);
* yield next;
* function (ctx, next) {
* return User.findOne(ctx.params.id).then(function(user) {
* ctx.user = user;
* next();
* });
* },
* function *(next) {
* console.log(this.user);
* function (ctx) {
* console.log(ctx.user);
* // => { id: 17, name: "Alex" }

@@ -135,4 +138,4 @@ * }

*
* posts.get('/', function *(next) {...});
* posts.get('/:pid', function *(next) {...});
* posts.get('/', function *(ctx, next) {...});
* posts.get('/:pid', function *(ctx, next) {...});
* forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());

@@ -162,4 +165,4 @@ *

* ```javascript
* router.get('/:category/:title', function *(next) {
* console.log(this.params);
* router.get('/:category/:title', function (ctx, next) {
* console.log(ctx.params);
* // => { category: 'programming', title: 'how-to-node' }

@@ -214,2 +217,5 @@ * });

*
* // or with an array of paths
* // router.use(['/users', '/admin'], userAuth());
*
* app.use(router.routes());

@@ -306,39 +312,27 @@ * ```

var dispatch = function *dispatch(next) {
debug('%s %s', this.method, this.path);
var dispatch = function dispatch(ctx, next) {
debug('%s %s', ctx.method, ctx.path);
var path = router.opts.routerPath || this.routerPath || this.path;
var matched = router.match(path, this.method);
var layer, i, ii;
var path = router.opts.routerPath || ctx.routerPath || ctx.path;
var matched = router.match(path, ctx.method);
var layerChain, layer, i;
if (this.matched) {
this.matched.push.apply(this.matched, matched.path);
if (ctx.matched) {
ctx.matched.push.apply(ctx.matched, matched.path);
} else {
this.matched = matched.path;
ctx.matched = matched.path;
}
if (matched.pathAndMethod.length) {
i = matched.pathAndMethod.length;
while (matched.route && i--) {
layer = matched.pathAndMethod[i];
ii = layer.stack.length;
this.captures = layer.captures(path, this.captures);
this.params = layer.params(path, this.captures, this.params);
debug('dispatch %s %s', layer.path, layer.regexp);
if (!matched.route) return next();
while (ii--) {
if (layer.stack[ii].constructor.name === 'GeneratorFunction') {
next = layer.stack[ii].call(this, next);
} else {
next = Promise.resolve(layer.stack[ii].call(this, next));
}
}
}
}
layerChain = matched.pathAndMethod.reduce(function(memo, layer) {
memo.push(function(ctx, next) {
ctx.captures = layer.captures(path, ctx.captures);
ctx.params = layer.params(path, ctx.captures, ctx.params);
return next();
});
return memo.concat(layer.stack);
}, []);
if (typeof next.next === 'function') {
yield *next;
} else {
yield next;
}
return compose(layerChain)(ctx, next);
};

@@ -375,36 +369,36 @@

return function *allowedMethods(next) {
yield *next;
return function allowedMethods(ctx, next) {
return next().then(function() {
var allowed = {};
var allowed = {};
if (!this.status || this.status === 404) {
this.matched.forEach(function (route) {
route.methods.forEach(function (method) {
allowed[method] = method;
if (!ctx.status || ctx.status === 404) {
ctx.matched.forEach(function (route) {
route.methods.forEach(function (method) {
allowed[method] = method;
});
});
});
var allowedArr = Object.keys(allowed);
var allowedArr = Object.keys(allowed);
if (!~implemented.indexOf(this.method)) {
if (options.throw) {
throw new HttpError.NotImplemented();
} else {
this.status = 501;
this.set('Allow', allowedArr);
}
} else if (allowedArr.length) {
if (this.method === 'OPTIONS') {
this.status = 204;
} else if (!allowed[this.method]) {
if (!~implemented.indexOf(ctx.method)) {
if (options.throw) {
throw new HttpError.MethodNotAllowed();
throw new HttpError.NotImplemented();
} else {
this.status = 405;
ctx.status = 501;
ctx.set('Allow', allowedArr);
}
} else if (allowedArr.length) {
if (ctx.method === 'OPTIONS') {
ctx.status = 204;
} else if (!allowed[ctx.method]) {
if (options.throw) {
throw new HttpError.MethodNotAllowed();
} else {
ctx.status = 405;
}
}
ctx.set('Allow', allowedArr);
}
this.set('Allow', allowedArr);
}
}
});
};

@@ -454,5 +448,5 @@ };

* ```javascript
* router.all('/login', function *() {
* this.redirect('/sign-in');
* this.status = 301;
* router.all('/login', function (ctx) {
* ctx.redirect('/sign-in');
* ctx.status = 301;
* });

@@ -478,5 +472,5 @@ * ```

return this.all(source, function *() {
this.redirect(destination);
this.status = code || 301;
return this.all(source, function (ctx) {
ctx.redirect(destination);
ctx.status = code || 301;
});

@@ -578,3 +572,3 @@ };

* ```javascript
* router.get('user', '/users/:id', function *(next) {
* router.get('user', '/users/:id', function (ctx, next) {
* // ...

@@ -651,12 +645,14 @@ * });

* router
* .param('user', function *(id, next) {
* this.user = users[id];
* if (!this.user) return this.status = 404;
* yield next;
* .param('user', function (id, ctx, next) {
* ctx.user = users[id];
* if (!ctx.user) return ctx.status = 404;
* return next();
* })
* .get('/users/:user', function *(next) {
* this.body = this.user;
* .get('/users/:user', function (ctx) {
* ctx.body = ctx.user;
* })
* .get('/users/:user/friends', function *(next) {
* this.body = yield this.user.getFriends();
* .get('/users/:user/friends', function (ctx) {
* return ctx.user.getFriends().then(function(friends) {
* ctx.body = friends;
* });
* })

@@ -663,0 +659,0 @@ * // /users/3 => {"id": 3, "name": "Alex"}

@@ -13,3 +13,3 @@ {

"author": "Alex Mingoia <talk@alexmingoia.com>",
"version": "5.2.3",
"version": "6.0.0",
"keywords": [

@@ -24,2 +24,3 @@ "koa",

"http-errors": "^1.3.1",
"koa-compose": "^3.0.0",
"methods": "^1.0.1",

@@ -29,7 +30,7 @@ "path-to-regexp": "^1.1.1"

"devDependencies": {
"expect.js": "^0.3.1",
"gulp": "^3.8.11",
"gulp-mocha": "^2.0.0",
"jsdoc-to-markdown": "^1.1.1",
"expect.js": "^0.3.1",
"koa": "^0.20.0",
"koa": "^2.0.0-alpha.3",
"mocha": "^2.0.1",

@@ -40,9 +41,9 @@ "should": "^6.0.3",

"scripts": {
"test": "[ \"${TRAVIS_NODE_VERSION}\" = \"0.12\" ] && NODE_ENV=test node --harmony-generators node_modules/gulp/bin/gulp.js test || NODE_ENV=test node node_modules/gulp/bin/gulp.js test",
"test": "NODE_ENV=test node node_modules/gulp/bin/gulp.js test",
"docs": "NODE_ENV=test node node_modules/gulp/bin/gulp.js docs"
},
"engines": {
"node": "> 0.12"
"node": ">= 4"
},
"license": "MIT"
}

@@ -26,3 +26,3 @@ # koa-router

## API Reference
* [koa-router](#module_koa-router)

@@ -33,5 +33,5 @@ * [Router](#exp_module_koa-router--Router) ⏏

* [.get|put|post|patch|delete](#module_koa-router--Router+get|put|post|patch|delete) ⇒ <code>Router</code>
* [.param(param, middleware)](#module_koa-router--Router+param) ⇒ <code>Router</code>
* [.routes](#module_koa-router--Router+routes) ⇒ <code>function</code>
* [.use([path], middleware, [...])](#module_koa-router--Router+use) ⇒ <code>Router</code>
* [.routes](#module_koa-router--Router+routes) ⇒ <code>function</code>
* [.prefix(prefix)](#module_koa-router--Router+prefix) ⇒ <code>Router</code>
* [.allowedMethods([options])](#module_koa-router--Router+allowedMethods) ⇒ <code>function</code>

@@ -41,2 +41,3 @@ * [.redirect(source, destination, code)](#module_koa-router--Router+redirect) ⇒ <code>Router</code>

* [.url(name, params)](#module_koa-router--Router+url) ⇒ <code>String</code> &#124; <code>Error</code>
* [.param(param, middleware)](#module_koa-router--Router+param) ⇒ <code>Router</code>
* _static_

@@ -47,3 +48,3 @@ * [.url(path, params)](#module_koa-router--Router.url) ⇒ <code>String</code>

### Router ⏏
**Kind**: Exported class
**Kind**: Exported class
<a name="new_module_koa-router--Router_new"></a>

@@ -59,3 +60,3 @@ #### new Router([opts])

**Example**
**Example**
Basic usage:

@@ -67,3 +68,3 @@

router.get('/', function *(next) {...});
router.get('/', function (ctx, next) {...});

@@ -84,12 +85,12 @@ app

router
.get('/', function *(next) {
this.body = 'Hello World!';
.get('/', function (ctx, next) {
ctx.body = 'Hello World!';
})
.post('/users', function *(next) {
.post('/users', function (ctx, next) {
// ...
})
.put('/users/:id', function *(next) {
.put('/users/:id', function (ctx, next) {
// ...
})
.del('/users/:id', function *(next) {
.del('/users/:id', function (ctx, next) {
// ...

@@ -109,3 +110,3 @@ });

```javascript
router.get('user', '/users/:id', function *(next) {
router.get('user', '/users/:id', function (ctx, next) {
// ...

@@ -125,8 +126,10 @@ });

'/users/:id',
function *(next) {
this.user = yield User.findOne(this.params.id);
yield next;
function (ctx, next) {
return User.findOne(ctx.params.id).then(function(user) {
ctx.user = user;
next();
});
},
function *(next) {
console.log(this.user);
function (ctx) {
console.log(ctx.user);
// => { id: 17, name: "Alex" }

@@ -145,4 +148,4 @@ }

posts.get('/', function *(next) {...});
posts.get('/:pid', function *(next) {...});
posts.get('/', function *(ctx, next) {...});
posts.get('/:pid', function *(ctx, next) {...});
forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());

@@ -172,4 +175,4 @@

```javascript
router.get('/:category/:title', function *(next) {
console.log(this.params);
router.get('/:category/:title', function (ctx, next) {
console.log(ctx.params);
// => { category: 'programming', title: 'how-to-node' }

@@ -179,3 +182,3 @@ });

**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code>

@@ -192,3 +195,3 @@ | Param | Type | Description |

**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code>
<a name="module_koa-router--Router+use"></a>

@@ -201,11 +204,11 @@ #### router.use([path], middleware, [...]) ⇒ <code>Router</code>

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
| Param | Type |
| --- | --- |
| [path] | <code>String</code> |
| middleware | <code>function</code> |
| [...] | <code>function</code> |
| [path] | <code>String</code> |
| middleware | <code>function</code> |
| [...] | <code>function</code> |
**Example**
**Example**
```javascript

@@ -217,2 +220,5 @@ router.use(session(), authorize());

// or with an array of paths
// router.use(['/users', '/admin'], userAuth());
app.use(router.routes());

@@ -224,9 +230,9 @@ ```

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
| Param | Type |
| --- | --- |
| prefix | <code>String</code> |
| prefix | <code>String</code> |
**Example**
**Example**
```javascript

@@ -241,3 +247,3 @@ router.prefix('/things/:thing_id')

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>

@@ -249,3 +255,3 @@ | Param | Type | Description |

**Example**
**Example**
```javascript

@@ -271,9 +277,9 @@ var app = koa();

```javascript
router.all('/login', function *() {
this.redirect('/sign-in');
this.status = 301;
router.all('/login', function (ctx) {
ctx.redirect('/sign-in');
ctx.status = 301;
});
```
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>

@@ -290,7 +296,7 @@ | Param | Type | Description |

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
| Param | Type |
| --- | --- |
| name | <code>String</code> |
| name | <code>String</code> |

@@ -303,3 +309,3 @@ <a name="module_koa-router--Router+url"></a>

```javascript
router.get('user', '/users/:id', function *(next) {
router.get('user', '/users/:id', function (ctx, next) {
// ...

@@ -315,3 +321,3 @@ });

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>

@@ -328,22 +334,24 @@ | Param | Type | Description |

**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code>
| Param | Type |
| --- | --- |
| param | <code>String</code> |
| middleware | <code>function</code> |
| param | <code>String</code> |
| middleware | <code>function</code> |
**Example**
**Example**
```javascript
router
.param('user', function *(id, next) {
this.user = users[id];
if (!this.user) return this.status = 404;
yield next;
.param('user', function (id, ctx, next) {
ctx.user = users[id];
if (!ctx.user) return ctx.status = 404;
return next();
})
.get('/users/:user', function *(next) {
this.body = this.user;
.get('/users/:user', function (ctx) {
ctx.body = ctx.user;
})
.get('/users/:user/friends', function *(next) {
this.body = yield this.user.getFriends();
.get('/users/:user/friends', function (ctx) {
return ctx.user.getFriends().then(function(friends) {
ctx.body = friends;
});
})

@@ -357,3 +365,3 @@ // /users/3 => {"id": 3, "name": "Alex"}

**Kind**: static method of <code>[Router](#exp_module_koa-router--Router)</code>
**Kind**: static method of <code>[Router](#exp_module_koa-router--Router)</code>

@@ -365,3 +373,3 @@ | Param | Type | Description |

**Example**
**Example**
```javascript

@@ -368,0 +376,0 @@ var url = Router.url('/users/:id', {id: 1});

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc