- Simple replacement for EventEmitter
- Async Middleware Hooks for Your Methods
- ESM / CJS with Types and Nodejs 20+
- Maintained on a regular basis!
npm install hookified --save
This was built because we constantly wanted hooks and events extended on libraires we are building such as Keyv and Cacheable. This is a simple way to add hooks and events to your classes.
import { Hookified } from 'hookified';
class MyClass extends Hookified {
constructor() {
super();
}
async myMethodEmittingEvent() {
this.emit('message', 'Hello World'); //using Emittery
}
//with hooks you can pass data in and if they are subscribed via onHook they can modify the data
async myMethodWithHooks() Promise<any> {
let data = { some: 'data' };
// do something
await this.hook('before:myMethod2', data);
return data;
}
}
You can even pass in multiple arguments to the hooks:
import { Hookified } from 'hookified';
class MyClass extends Hookified {
constructor() {
super();
}
async myMethodWithHooks() Promise<any> {
let data = { some: 'data' };
let data2 = { some: 'data2' };
// do something
await this.hook('before:myMethod2', data, data2);
return data;
}
}
Subscribe to a hook event.
Unsubscribe from a hook event.
Run a hook event.
Get all hooks.
Get all hooks for an event.
Subscribe to an event.
Unsubscribe from an event.
Emit an event.
Get all listeners for an event.
Remove all listeners for an event.
Set the maximum number of listeners and will truncate if there are already too many.
Hookified is written in TypeScript and tests are written in vitest
. To run the tests, use the following command:
To setup the environment and run the tests:
npm i && npm test
To contribute follow the Contributing Guidelines and Code of Conduct.