New method-hooks


#1

Inspired by hitchcott’s meteor-method-hooks, I created a new package
This package extends Meteor with four methods:

Meteor.beforeMethods
Meteor.afterMethods
Meteor.beforeAllMethods
Meteor.afterAllMethods

This package differs from hitchcott:method-hooks in that:

  • You can add hooks for all methods (beforeAllMethods and afterAllMethods)
  • It works on both client and server
  • You can add and remove hooks at runtime.
  • After methods can change the methods result

It’s really useful for logging and security purposes or to make your application more modular.


#2

The doco is a bit lacking in how to alter results or manipulate the context of the method. Typically with my logs, I like to collect a object of data at the top level and then at the end of the call or with a exception, the whole call gets printed as one transaction. If I have an object, how would I reference this in a afterMethod hook to log it?


#3

Yeah, I don’t like writing docs ;).

Let’s say you have a method named test like:

Meteor.methods('test',function(inputA,inputB){return 1});

Then you can add a hook like

 Meteor.afterMethods('test',function(inputA,inputB,result){
  return result+1;
})

Now, calling test will return 2.

However, currently if the original method throws an error, after hooks are not called. I could modify this, but I don’t really know what would be the best behavior in this case.

Maybe something like:

Meteor.afterMethods('test',function(inputA,inputB,result){
      return result+1;
    }, function(inputA,inputB,error){
//Called when an error occurs
});