Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chaining [Discussion] #6

Closed
djensen47 opened this issue May 15, 2014 · 4 comments
Closed

Chaining [Discussion] #6

djensen47 opened this issue May 15, 2014 · 4 comments

Comments

@djensen47
Copy link
Contributor

Like Express, Restify allows chaining on path definitions. It would be great if I could pass a chain of callback functions to the quick mapping.

The use case I have in mind is checking authorization on requests.

I use a plugin handler to do the initial authentication handling. Since not all APIs need to be protected, I have a callback function that I use to "protect" a particular path. The path ends up looking like this.

  var opts = {
    filter: function userFilter(req, res) {
      return {user: req.user.userId};
    }
  }

  var notes = restifyMongoose(Rule, opts);
  server.get('/notes/:id', authenticate, notes.detail());
  server.get('/rules', authenticate, rules.query());

I would prefer to do something like this:

restifyMongoose(models.Note, opts, authenticate, f1, f2, f3).serve('/notes', server);

Just like Restify allows, the constructor would allow you to pass or "chain" as many functions as you need.

Thoughts?

@saintedlama
Copy link
Owner

I like the idea to make serve more flexible. The API looks a little awkward since these route middleware is only applied when using serve something like

restifyMongoose(models.Note, opts, authenticate, f1, f2, f3).serve('/notes', [authenticate, f1, f2, f3], [after1,after2, after3], server);

would fit better

@djensen47
Copy link
Contributor Author

I think you had a copy and paste error. Did you mean …

restifyMongoose(models.Note, opts).serve('/notes', [authenticate, f1, f2, f3], [after1,after2, after3], server);

I think I would put server after the path since it is required.

var notes = restifyMongoose(models.Note, opts);
notes.serve('/notes', server, [authenticate, f1, f2, f3], [after1,after2, after3]);

If you want to chain handlers before and after this function signature isn't going to work. Maybe something like this:

var notes = restifyMongoose(models.Note, opts);
var handlerChain = {
  before: [Function],
  after: [Funtion],
};
notes.serve('/notes', server, handlerChain);

Thoughts?

@saintedlama
Copy link
Owner

Looks good. When we now call that options we have a nice extension point for server

var notes = restifyMongoose(models.Note, opts);
var options = {
  before: [Function],
  after: [Funtion],
};
notes.serve('/notes', server, options);

djensen47 added a commit to djensen47/restify-mongoose that referenced this issue May 18, 2014
Added a new optional `options` parameter to `serve` function, which
allows the caller to include an array of `before` restify handlers
and/or an array of `after` restify handlers.

This resolves [Issue saintedlama#6](saintedlama#6).
@djensen47
Copy link
Contributor Author

Okay, I've implemented this in my pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants