Skip to content

Commit

Permalink
feat: add exclusive option (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
medfreeman authored Jul 4, 2022
1 parent 3454a7d commit c6a8fc8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Create a new router.
| --- | --- | --- |
| [opts] | <code>Object</code> | |
| [opts.prefix] | <code>String</code> | prefix router paths |
| [opts.exclusive] | <code>Boolean</code> | only run last matched route's controller when there are multiple matches |

**Example**
Basic usage:
Expand Down
4 changes: 3 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = Router;
*
* @alias module:koa-router
* @param {Object=} opts
* @param {Boolean=false} opts.exclusive only run last matched route's controller when there are multiple matches
* @param {String=} opts.prefix prefix router paths
* @constructor
*/
Expand All @@ -60,6 +61,7 @@ function Router(opts = {}) {
'POST',
'DELETE'
];
this.exclusive = !!this.opts.exclusive;

this.params = {};
this.stack = [];
Expand Down Expand Up @@ -364,7 +366,7 @@ Router.prototype.routes = Router.prototype.middleware = function () {
ctx._matchedRouteName = mostSpecificLayer.name;
}

layerChain = matchedLayers.reduce(function(memo, layer) {
layerChain = (router.exclusive ? [mostSpecificLayer] : matchedLayers).reduce(function(memo, layer) {
memo.push(function(ctx, next) {
ctx.captures = layer.captures(path, ctx.captures);
ctx.params = ctx.request.params = layer.params(path, ctx.captures, ctx.params);
Expand Down
50 changes: 50 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,56 @@ describe('Router', function () {
})
});

it('runs multiple controllers when there are multiple matches', function (done) {
const app = new Koa();
const router = new Router();

router
.get('users_single', '/users/:id(.*)', function (ctx, next) {
ctx.body = { single: true };
next();
})
.get('users_all', '/users/all', function (ctx, next) {
ctx.body = Object.assign({}, ctx.body, { all: true });
next();
});

request(http.createServer(app.use(router.routes()).callback()))
.get('/users/all')
.expect(200)
.end(function (err, res) {
if (err) return done(err);
expect(res.body).to.have.property('single', true);
expect(res.body).to.have.property('all', true);
done();
})
});

it('runs only the last match when the \'exclusive\' option is enabled', function (done) {
const app = new Koa();
const router = new Router({ exclusive: true });

router
.get('users_single', '/users/:id(.*)', function (ctx, next) {
ctx.body = { single: true };
next();
})
.get('users_all', '/users/all', function (ctx, next) {
ctx.body = Object.assign({}, ctx.body, { all: true });
next();
});

request(http.createServer(app.use(router.routes()).callback()))
.get('/users/all')
.expect(200)
.end(function (err, res) {
if (err) return done(err);
expect(res.body).to.not.have.property('single');
expect(res.body).to.have.property('all', true);
done();
})
});

it('does not run subsequent middleware without calling next', function (done) {
const app = new Koa();
const router = new Router();
Expand Down

0 comments on commit c6a8fc8

Please sign in to comment.