Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Before all and after all hooks #11

Closed
daffl opened this issue Sep 27, 2014 · 10 comments · Fixed by #16
Closed

Before all and after all hooks #11

daffl opened this issue Sep 27, 2014 · 10 comments · Fixed by #16

Comments

@daffl
Copy link
Member

daffl commented Sep 27, 2014

The hooks plugin should provide the ability to also take just a single hook function (instead of an object) which will be applied to all service methods:

app.service('todos').before(function(hook,next) {
  if(!hooks.params.user) {
    return next(new Error('You need to be authenticated'));
  }
  next();
});

Additionally it could be very helpful to add general hooks to the application which will be applied to every service (this could be easiliy implemented as a service mixin):

app.before({
  create: function(hook, next) {
    next();
  }
});
@TomKaltz
Copy link

+1

@ekryski
Copy link
Member

ekryski commented Feb 3, 2015

+1

I wouldn't mind having something like this for having hooks that apply to all service methods.

var service = Service.extend({
    before: {
        all: [first.common.hook, second.common.hook]
    }
});

@marshallswain
Copy link
Member

I like the array syntax. For the implementation, it would be nice if all would simply add the hooks to each method's queue. So doing something like this:

myService.before({
    all: [requireAuth]
});

myService.before({
    create: [adminOnly]
});

myService.before({
    all: [removeHelperData]
});

would result in create having an array of three hooks: [requireAuth, adminOnly, removeHelperData],

while the others would all have only two: [requireAuth, removeHelperData]

@ekryski
Copy link
Member

ekryski commented Feb 3, 2015

@marshallswain: @daffl and I were discussing in the office and thinking that is basically the case. The before all hooks would be called before the individual service method hooks. The after all would fire after the individual service method hooks.

Does that make sense? Thoughts?

@daffl
Copy link
Member Author

daffl commented Feb 3, 2015

If you define it as an object on your service. What @marshallswain is saying for using .before and .after in that order will be correct, too though.

@daffl
Copy link
Member Author

daffl commented Feb 3, 2015

Although, personally I would prefer this syntax:

myService.before([requireAuth]);

myService.before({
    create: [adminOnly]
});

myService.before([removeHelperData]);

But this would make it inconsistent with how it is defined on a service object.

@marshallswain
Copy link
Member

It sounds like we're all thinking along the same lines. I really like the simplicity of passing the array by itself for all, as is done in @daffl 's last post.

Consider this syntax:

myService.before({
    all: [requireAuth]
    create: [adminOnly]
});

Would we also want to support the above and register all first when they come together like this?

@daffl
Copy link
Member Author

daffl commented Feb 4, 2015

You're right. Doing that would make it less inconsistent. So here is the API (which shouldn't break the existing):

Array and Object notation. Works for both, when adding a before and after property to a service object or calling service.before() and service.after():

// As an object
var before = {
  all: isAuthenticated, // also as an array [ isAuthenticated ]
  create: [ isAdmin, hashPassword ]
}

// As an array will simply become { all: <array> }
var before = [ isAuthenticated, isAdmin ]

// As a single method will become { all: [ <single> ] }
var before = isAuthenticated;

before.all will be registered before any other handler in the current object. after.all will be registered after any other handler in the current object. Current object means that something like this:

var myService = {
  find: function(params, callback),
  before: {
    all: isAuthenticated, // also as an array [ isAuthenticated ]
    create: [ isAdmin, hashPassword ]
  }
};

app.use('/myservice', myService);

app.service('myservice').before({
  all: [ foo ],
  create: [ bar ]
});

Will register a chain of [ isAuthenticated, isAdmin, hashPassword, foo, bar ].

@marshallswain
Copy link
Member

That looks good.

@ekryski
Copy link
Member

ekryski commented Feb 4, 2015

👍 way up!

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

Successfully merging a pull request may close this issue.

4 participants