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 2.3.2 to 2.3.3

11

lib/resource.js

@@ -76,8 +76,3 @@ /**

case 'read':
var wrapper = function *(next) {
if (this.params[id] !== 'new') {
yield action.call(this, next);
}
};
args = ['GET', baseTrailing + ':' + id, wrapper];
args = ['GET', baseTrailing + ':' + id, action];
break;

@@ -123,6 +118,6 @@ case 'edit':

route.path = base + ':' + this.id + route.path;
route.params = [];
route.regexp = pathToRegexp(route.path, route.params);
route.paramNames = [];
route.regexp = pathToRegexp(route.path, route.paramNames);
}
return this;
};

@@ -57,3 +57,3 @@ /**

return function *dispatch(next) {
var routes = [];
var route;

@@ -64,11 +64,9 @@ this.params = [];

debug('%s %s', this.method, this.path);
if (!(routes = router.match(this.method, this.path, this.params))) {
if (!(route = router.match(this.method, this.path, this.params))) {
return yield next;
}
// Dispatch route middlewares
for (var i=0; i<routes.length; i++) {
debug('dispatch "%s" %s', routes[i].path, routes[i].regexp);
yield routes[i].middleware.call(this, next);
}
// Dispatch route middleware
debug('dispatch "%s" %s', route.path, route.regexp);
yield route.middleware.call(this, next);
};

@@ -200,3 +198,2 @@ };

var routes = this.routes[method.toLowerCase()];
var matchingRoutes = [];

@@ -207,12 +204,7 @@ for (var len = routes.length, i=0; i<len; i++) {

if (routes[i].match(method, path, params)) {
debug('match "%s" %s', routes[i].path, routes[i].regexp);
matchingRoutes.push(routes[i]);
return routes[i];
}
}
if (matchingRoutes.length > 0) {
return matchingRoutes;
} else {
return false;
}
return false;
};

@@ -219,0 +211,0 @@

@@ -9,3 +9,3 @@ {

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

@@ -25,3 +25,3 @@ "koa",

"devDependencies": {
"koa": "0.3.0",
"koa": "0.0.2",
"mocha": "1.12.0",

@@ -32,3 +32,3 @@ "should": "1.2.2",

"scripts": {
"test": "NODE_ENV=test node_modules/mocha/bin/mocha --harmony-generators --recursive"
"test": "node_modules/mocha/bin/mocha --harmony-generators --recursive"
},

@@ -35,0 +35,0 @@ "engines": {

@@ -63,89 +63,11 @@ /**

app.use(router.middleware());
var forums = app.resource('forums', {
index: function *() {}
});
var threads = app.resource('threads', {
index: function *() {},
show: function *() {
should.exist(this.params);
this.params.should.have.property('forum', '54');
this.params.should.have.property('thread', '12');
this.status = 200;
}
});
var forums = app.resource('forums', { index: function *() {} });
var threads = app.resource('threads', { index: function *() {} });
forums.add(threads);
threads.base.should.equal('/forums/:forum/threads');
request(http.createServer(app.callback()))
.get('/forums/54/threads/12')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
done();
});
should.exist(router.routes.get[1]);
router.routes.get[1].should.be.a('object');
router.routes.get[1].should.have.property('path', '/forums/:forum/threads');
done();
});
it('doesn\'t call multiple controller actions', function(done) {
var app = koa();
var router = new Router(app);
app.use(router.middleware());
var counter = 0;
function *increaseCounter() {
counter++;
this.status = 204;
}
app.resource('threads', {
index: increaseCounter,
new: increaseCounter,
create: increaseCounter,
show: increaseCounter,
edit: increaseCounter,
update: increaseCounter,
destroy: increaseCounter,
});
var server = http.createServer(app.callback());
request(server)
.get('/threads')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
request(server)
.get('/threads/new')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
request(server)
.post('/threads')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
request(server)
.get('/threads/1234')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
request(server)
.get('/threads/1234/edit')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
request(server)
.put('/threads/1234')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
request(server)
.get('/threads/1234')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
counter.should.equal(7);
done();
});
});
});
});
});
});
});
});
});

@@ -63,57 +63,10 @@ /**

it('matches multiple routes', function(done) {
it('supports generators for route middleware', function(done) {
var app = koa();
var counter = 0;
app.use(Router(app));
app.get('/:lastname', function *(next) {
this.should.have.property('params');
this.params.should.have.property('lastname', 'smith');
this.status = 204;
counter++;
});
app.get('/:surname', function *(next) {
this.should.have.property('params');
this.params.should.have.property('surname', 'smith');
this.status = 204;
counter++;
});
var server = http.createServer(app.callback());
request(server)
.get('/smith')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
counter.should.equal(2);
app.use(function(next) {
return function *() {
done();
};
});
});
it('no matches after throw', function(done) {
var app = koa();
var counter = 0;
app.use(Router(app));
app.get('/', function *(next) {
counter++;
this.throw(403);
});
app.get('/', function *(next) {
counter++;
});
var server = http.createServer(app.callback());
request(server)
.get('/')
.expect(403)
.end(function(err, res) {
if (err) return done(err);
counter.should.equal(1);
done();
});
});
it('supports generators for route middleware', function(done) {
var app = koa();
app.use(Router(app));
app.use(function *() {
done();
});
var readVersion = function() {

@@ -120,0 +73,0 @@ return function(fn) {

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