Skip to content

Instantly share code, notes, and snippets.

@claustres
Created November 15, 2017 10:36
Show Gist options
  • Save claustres/faffe053a8eb89bc41412cd197537ac6 to your computer and use it in GitHub Desktop.
Save claustres/faffe053a8eb89bc41412cd197537ac6 to your computer and use it in GitHub Desktop.
Dynamic hooks configuration
function activateHooks (service, serviceHooks) {
// Iterate over hook types (before, after)
_.forOwn(serviceHooks, (hooksDefinition, stage) => {
// Iterate over hooks to create the hook pipeline
let pipeline = []
_.forOwn(hooksDefinition, (hookOptions, hookName) => {
// Jump from name/options to the real hook function
pipeline.push(hooks[hookName](hookOptions))
})
// Replace hooks in place so that we can use it directly with Feathers after
serviceHooks[stage] = { create: pipeline } // We only have create operation to manage
})
// Setup hooks on service
service.hooks(serviceHooks)
}
...
// Process hooks in app setup
_.forOwn(job.hooks, (value, key) => {
let service = app.service(key)
activateHooks(service, value)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment