Install the library:
npm install kshook
Import the library:
const KsHook = require('kshook');
Import your custom hook service
const hook = require('../my.service.hook');
For more information on how to create your custom hook service, see Create a preconfigured hook service in your project.
Trigger and event with data
hook.trigger({
subscriber: "Model",
event: "onLoadFailed",
data: {
flow: req.flow,
src: "controller:User:read",
error: { message: error?.message || error, stack: error?.stack }
}
});
Load your custom notifier, subscriber, and processors:
const locator = require('./notifier.locator');
const { models, driver, manager } = require('./db');
Register subscribers for each event in the data model
--------------------------------------------------------------------
| id | event | notifier | value | processor | expression |
--------------------------------------------------------------------
| 1 | logout | locator | newrelic | null | null |
| 2 | login | locator | alert | native | failure EQUAL 11 |
--------------------------------------------------------------------
Configure and run
// Initialize KsHook instance
const hook = KsHook.get();
// Configure supported components
hook.notifier.set({
name: "locator",
target: locator
});
hook.subscriber.get("Model").configure({
models,
driver,
manager
});
// Trigger the 'login' event
const res = hook.trigger({
subscriber: ["Model"],
event: "login",
data: { failure: 10, flow: "6868465468415", step: "STEP-1" }
});
For further information, check the following topics: