Skip to content

Event Emitting and Async Middleware Hooks πŸͺ

License

Notifications You must be signed in to change notification settings

jaredwray/hookified

Repository files navigation

Hookified

Event and Middleware Hooks for Your Libraries

tests GitHub license codecov npm npm

Features

  • Emit Events via Emittery
  • Middleware Hooks with data passing
  • ESM and Nodejs 20+
  • Maintained on a regular basis!

Installation

npm install hookified --save

Usage

This was built because we constantly wanted hooks and events extended on libraires we are building such as Keyv. This is a simple way to add hooks and events (via emittery) to your libraries.

import { Hookified } from 'hookified';

class MyClass extends Hookified {
  constructor() {
    super();
  }

  async myMethodEmittingEvent() {
    await this.emit('message', 'Hello World');
  }

  //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;
  }
}