Created
November 15, 2017 10:36
-
-
Save claustres/faffe053a8eb89bc41412cd197537ac6 to your computer and use it in GitHub Desktop.
Dynamic hooks configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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