devinivy / bedwetter

Auto-generated, RESTful, CRUDdy route handlers for Waterline models in hapi
64 stars 11 forks source link

Post Handler hook #21

Closed girishla closed 8 years ago

girishla commented 8 years ago

I need to emit a socketIO event when certain bedwetter handler routes finish successfully(no Boom errors). Bedwetter registers itself as a handler and I can't seem to find a nice way to do a post-handler action.

What would be nice is if bedwetter provides some sort of a post handler hook or alternatively allow bedwetter to be executed as one of the pre-handler functions.

For the time being I can perhaps use the 'onPostHandler' extension point and look for successful routes but that doesn't seem like the best solution. Is there a better way ?

devinivy commented 8 years ago

I achieve this using a plugin called mrhorse. It allows you to attach "policies" onto a route, which is usually a pre- or post-handler hook. In the most recent version of hapi there are actually per-route lifecycle hooks that are very clean and performant. I'd suggest upgrading to the most recent version of hapi if you haven't already. If you can't upgrade (e.g. if you're stuck on node v 0.10) then mrhorse is the way to go.

Edit I'll add, I've used bedwetter and mrhorse for an identical use-case to you, and it has worked great. At the top of each post-handler policy you may need to check if request.response.isBoom since errors from the handler are passed-along to subsequent lifecycle hooks.

girishla commented 8 years ago

mrhorse it is then. Exactly what I was looking for. Thanks,