Skip to content

Commit

Permalink
Fix multiple resource middleware (closes #3).
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmingoia committed Feb 3, 2014
1 parent e8122db commit 75a33c9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,21 @@ forums.add(threads);

### Multiple middleware

Run middleware before resource actions by passing middleware functions before
your actions:

```javascript
var users = new Resource('users', authorize, actions);
```

Run middleware for specific actions by passing an array:

```javascript
var users = new Resource('users', {
show: [authorize, function *(next) {
// ...
}]
});
```

## MIT Licensed
48 changes: 31 additions & 17 deletions lib/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = Resource;
* `options`
* - methods override map of action names to http method
*
* @param {String} name
* @param {String} name omit to define top-level resource
* @param {Function} actions
* @param {Object} options
* @return {Resource}
Expand All @@ -28,14 +28,30 @@ module.exports = Resource;

function Resource(name, actions, options) {
if (!(this instanceof Resource)) {
return new Resource(name, actions, options);
var args = Array.prototype.slice.call(arguments);
var resource = Object.create(Resource.prototype);
Resource.apply(resource, args);
return resource;
}

if (typeof name === 'object') {
actions = name;
name = null;
this.name = typeof name == 'string' ? name : null;

this.id = name ? lingo.en.singularize(name) : 'id';
this.base = this.name ? '/' + this.name : '/';

var middleware = Array.prototype.slice.call(arguments, this.name ? 1 : 0);

this.actions = middleware.pop();

// if last object has `methods` property, assume it is `options`
if (this.actions.methods) {
options = this.actions;
this.actions = middleware.pop();
}

this.routes = [];
this.resources = [];

this.options = {
methods: defaults((options || {}).methods || {}, {
'options': 'OPTIONS',
Expand All @@ -52,17 +68,12 @@ function Resource(name, actions, options) {
})
};

this.name = name;
this.id = name ? lingo.en.singularize(name) : 'id';
this.base = this.name ? '/' + this.name : '/';
this.actions = actions;
this.routes = [];
this.resources = [];

// create route definition (used for routing) for each resource action
Object.keys(actions).forEach(function(name) {
Object.keys(this.actions).forEach(function(name) {
var action = this.actions[name];
var url = this.base;
var urlTrailing = this.base;
var params = [];

if (url[url.length-1] != '/') {
urlTrailing = url + '/';
Expand All @@ -78,19 +89,22 @@ function Resource(name, actions, options) {
url = urlTrailing + ':' + this.id;
}

var action = actions[name];
// compose multiple action middleware
if (action instanceof Array) {
action = compose(actions[name]);
this.actions[name] = compose(action);
}

var params = [];
// compose resource middleware
if (middleware.length) {
this.actions[name] = compose(middleware.concat([action]));
}

this.routes.push({
method: this.options.methods[name].toUpperCase(),
url: url,
regexp: pathToRegExp(url, params),
params: params,
action: action
action: this.actions[name]
});
}, this);
};
Expand Down
17 changes: 17 additions & 0 deletions test/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ describe('Resource', function() {
.end(done);
});

it('composes resource middleware for each action', function(done) {
var app = koa();

app.use(Resource('users', function *(next) {
this.status = 200;
}, {
show: function *() {
this.body = 'yo';
}
}).middleware());

request(http.createServer(app.callback()))
.get('/users/1')
.expect(200)
.end(done);
});

it('doesn\'t call multiple controller actions', function(done) {
var app = koa();
var counter = 0;
Expand Down

0 comments on commit 75a33c9

Please sign in to comment.