Node.js Plug-And-Play package
Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.
Main features
- Extention points definition
Simple to declare new extention points, yet a lot of flexibility to the plugin authors. - Hook definition
Plugin writer can intercept calls to a function by placing their own logicl before, after and even switching the default implementation. - Dependency management
Plugins can require other plugins as required dependencies as well as choose the order of execution of each hook. - Promise support
Hook can be synchronous and asynchronous when returning a promise. - Nested/hierachical
Instanciate plugin instances with a parent reference and parent hooks will also be available inside the children.
Learning
We encourage your to read the detailed tutorial on how to create a plugin architected with Plug and Play published by Adaltas.
Here is the documentation:
Quick example
Library and application authors define hooks, see ./sample/lib.js
:
const plugandplay = require('plug-and-play')
const plugins = plugandplay()
module.exports = {
plugins: plugins,
print: function() {
plugins.call({
name: 'hooks:print',
args: {
data: { message: 'hello' }
},
handler: ({data}) => {
console.log(data.message)
}
})
}
}
Users and pluging authors can now register their own hooks, see ./sample/index.js
:
const mysuperlibrary = require('./lib')
mysuperlibrary.plugins.register({
hooks: {
'hooks:print': ({data}, handler) => {
data.message = 'Hello World'
console.log('>>>>>>>>>>>')
const result = handler.call(null, {data: data})
console.log('<<<<<<<<<<<')
return result
}
}
})
mysuperlibrary.print()
While the original print
function was only printing Hello
to stdout, the introduction of this new plugin prints:
>>>>>>>>>>>
Hello world
<<<<<<<<<<<