{"name":"hookified","version":"1.5.0","keywords":["hooks","eventemitter","eventemitter3","middleware","events","hooks","event-hooks","hook-system","event-driven","event-emitter","hook-handlers","middleware","emit","event-management","before-after-hooks","event-listeners","custom-hooks","hook-utilities","typescript-hooks"],"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","_id":"hookified@1.5.0","homepage":"https://github.com/jaredwray/hookified#readme","bugs":{"url":"https://github.com/jaredwray/hookified/issues"},"dist":{"shasum":"bb0ca401840ad6e9fdb0ddbe040592bfa8cb04f8","tarball":"http://registry-npm.ywwl.com/hookified/download/hookified-1.5.0.tgz","fileCount":11,"integrity":"sha512-4U0zw2ibOws7kfGdNCIL6oRg+t6ITxkgi9kUaJ71IDp0ZATHjvY6o7l90RBa/R8H2qOKl47SZISA5a3hNnei1g==","signatures":[{"sig":"MEQCIGztzMett1IXucNIx4MeTMIcEjmhuhV2xm0HjlLjltlqAiBNl4xT7KUupcfnxYnSUSwH4EsqH2O2Jy7H+slzoqLGvQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":91011,"size":11450,"noattachment":false,"key":"/hookified/-/hookified-1.5.0.tgz"},"main":"dist/node/index.cjs","type":"module","types":"dist/node/index.d.ts","module":"dist/node/index.js","exports":{".":{"import":"./dist/node/index.js","require":"./dist/node/index.cjs"},"./browser":{"import":"./dist/browser/index.js","default":"./dist/browser/index.global.js"}},"gitHead":"cb522e9f725697af61e6ccde702b6ee8683880da","scripts":{"test":"xo --fix && vitest run --coverage","build":"rimraf ./dist && tsup","clean":"rimraf ./dist ./coverage ./site/dist","prepare":"npm run build","test:ci":"xo && vitest run --coverage","website:build":"docula build","website:serve":"docula serve"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"repository":{"url":"git+https://github.com/jaredwray/hookified.git","type":"git"},"_npmVersion":"10.8.2","description":"Event and Middleware Hooks","directories":{},"_nodeVersion":"20.18.0","_hasShrinkwrap":false,"devDependencies":{"xo":"^0.59.3","tsup":"^8.3.5","docula":"^0.9.4","rimraf":"^6.0.1","vitest":"^2.1.4","typescript":"^5.6.3","@vitest/coverage-v8":"^2.1.4"},"_npmOperationalInternal":{"tmp":"tmp/hookified_1.5.0_1730230469293_0.6361099685351088","host":"s3://npm-registry-packages"},"readme":"![site/logo.svg](site/logo.svg)\n\n# Event Emitting and Middleware Hooks\n\n[![tests](https://github.com/jaredwray/hookified/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/hookified/actions/workflows/tests.yaml)\n[![GitHub license](https://img.shields.io/github/license/jaredwray/hookified)](https://github.com/jaredwray/hookified/blob/master/LICENSE)\n[![codecov](https://codecov.io/gh/jaredwray/hookified/graph/badge.svg?token=nKkVklTFdA)](https://codecov.io/gh/jaredwray/hookified)\n[![npm](https://img.shields.io/npm/dm/hookified)](https://npmjs.com/package/hookified)\n[![jsDelivr hits](https://img.shields.io/jsdelivr/npm/hm/hookified)](https://www.jsdelivr.com/package/npm/hookified)\n[![npm](https://img.shields.io/npm/v/hookified)](https://npmjs.com/package/hookified)\n\n# Features\n- Simple replacement for EventEmitter\n- Async / Sync Middleware Hooks for Your Methods\n- ESM / CJS with Types and Nodejs 20+\n- Browser Support and Delivered via CDN\n- Maintained on a regular basis!\n\n# Installation\n```bash\nnpm install hookified --save\n```\n\n# Usage\nThis was built because we constantly wanted hooks and events extended on libraires we are building such as [Keyv](https://keyv.org) and [Cacheable](https://cacheable.org). This is a simple way to add hooks and events to your classes.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodEmittingEvent() {\n    this.emit('message', 'Hello World'); //using Emittery\n  }\n\n  //with hooks you can pass data in and if they are subscribed via onHook they can modify the data\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n```\n\nYou can even pass in multiple arguments to the hooks:\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    let data2 = { some: 'data2' };\n    // do something\n    await this.hook('before:myMethod2', data, data2);\n\n    return data;\n  }\n}\n```\n\n# Using it in the Browser\n\n```html\n<script type=\"module\">\n  import { Hookified } from 'https://cdn.jsdelivr.net/npm/hookified/dist/browser/index.js';\n\n  class MyClass extends Hookified {\n    constructor() {\n      super();\n    }\n\n    async myMethodEmittingEvent() {\n      this.emit('message', 'Hello World'); //using Emittery\n    }\n\n    //with hooks you can pass data in and if they are subscribed via onHook they can modify the data\n    async myMethodWithHooks() Promise<any> {\n      let data = { some: 'data' };\n      // do something\n      await this.hook('before:myMethod2', data);\n\n      return data;\n    }\n  }\n</script>\n```\n\nif you are not using ESM modules, you can use the following:\n\n```html\n<script src=\"https://cdn.jsdelivr.net/npm/hookified/dist/browser/index.global.js\"></script>\n<script>\n  class MyClass extends Hookified {\n    constructor() {\n      super();\n    }\n\n    async myMethodEmittingEvent() {\n      this.emit('message', 'Hello World'); //using Emittery\n    }\n\n    //with hooks you can pass data in and if they are subscribed via onHook they can modify the data\n    async myMethodWithHooks() Promise<any> {\n      let data = { some: 'data' };\n      // do something\n      await this.hook('before:myMethod2', data);\n\n      return data;\n    }\n  }\n</script>\n```\n\n# API - Hooks\n\n## .onHook(eventName, handler)\n\nSubscribe to a hook event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n\nconst myClass = new MyClass();\nmyClass.onHook('before:myMethod2', async (data) => {\n  data.some = 'new data';\n});\n```\n\n## .onceHook(eventName, handler)\n\nSubscribe to a hook event once.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.onHookOnce('before:myMethod2', async (data) => {\n  data.some = 'new data';\n});\n\nmyClass.myMethodWithHooks();\n\nconsole.log(myClass.hooks.length); // 0\n```\n\n## .removeHook(eventName)\n\nUnsubscribe from a hook event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n\nconst myClass = new MyClass();\nconst handler = async (data) => {\n  data.some = 'new data';\n};\n\nmyClass.onHook('before:myMethod2', handler);\n\nmyClass.removeHook('before:myMethod2', handler);\n```\n\n## .hook(eventName, ...args)\n\nRun a hook event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n```\n\nin this example we are passing multiple arguments to the hook:\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    let data2 = { some: 'data2' };\n    // do something\n    await this.hook('before:myMethod2', data, data2);\n\n    return data;\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.onHook('before:myMethod2', async (data, data2) => {\n  data.some = 'new data';\n  data2.some = 'new data2';\n});\n\nawait myClass.myMethodWithHooks();\n```\n\n## .hooks\n\nGet all hooks.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n\nconst myClass = new MyClass();\nmyClass.onHook('before:myMethod2', async (data) => {\n  data.some = 'new data';\n});\n\nconsole.log(myClass.hooks);\n```\n\n## .getHooks(eventName)\n\nGet all hooks for an event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n\nconst myClass = new MyClass();\nmyClass.onHook('before:myMethod2', async (data) => {\n  data.some = 'new data';\n});\n\nconsole.log(myClass.getHooks('before:myMethod2'));\n```\n\n## .clearHooks(eventName)\n\nClear all hooks for an event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodWithHooks() Promise<any> {\n    let data = { some: 'data' };\n    // do something\n    await this.hook('before:myMethod2', data);\n\n    return data;\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.onHook('before:myMethod2', async (data) => {\n  data.some = 'new data';\n});\n\nmyClass.clearHooks('before:myMethod2');\n```\n\n# API - Events\n\n## .on(eventName, handler)\n\nSubscribe to an event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodEmittingEvent() {\n    this.emit('message', 'Hello World');\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n```\n\n## .off(eventName, handler)\n\nUnsubscribe from an event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodEmittingEvent() {\n    this.emit('message', 'Hello World');\n  }\n}\n\nconst myClass = new MyClass();\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n\nmyClass.off('message', (message) => {\n  console.log(message);\n});\n```\n\n## .emit(eventName, ...args)\n\nEmit an event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodEmittingEvent() {\n    this.emit('message', 'Hello World');\n  }\n}\n```\n\n## .listeners(eventName)\n\nGet all listeners for an event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodEmittingEvent() {\n    this.emit('message', 'Hello World');\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n\nconsole.log(myClass.listeners('message'));\n```\n\n## .removeAllListeners(eventName)\n\nRemove all listeners for an event.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodEmittingEvent() {\n    this.emit('message', 'Hello World');\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n\nmyClass.removeAllListeners('message');\n```\n\n## .setMaxListeners(maxListeners: number)\n\nSet the maximum number of listeners and will truncate if there are already too many.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n\n  async myMethodEmittingEvent() {\n    this.emit('message', 'Hello World');\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.setMaxListeners(1);\n\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n\nmyClass.on('message', (message) => {\n  console.log(message);\n}); // this will not be added and console warning\n\nconsole.log(myClass.listenerCount('message')); // 1\n```\n\n## .once(eventName, handler)\n\nSubscribe to an event once.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.once('message', (message) => {\n  console.log(message);\n});\n\nmyClass.emit('message', 'Hello World');\n\nmyClass.emit('message', 'Hello World'); // this will not be called\n```\n\n## .prependListener(eventName, handler)\n\nPrepend a listener to an event. This will be called before any other listeners.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.prependListener('message', (message) => {\n  console.log(message);\n});\n```\n\n## .prependOnceListener(eventName, handler)\n\nPrepend a listener to an event once. This will be called before any other listeners.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.prependOnceListener('message', (message) => {\n  console.log(message);\n});\n\nmyClass.emit('message', 'Hello World');\n```\n\n## .eventNames()\n\nGet all event names.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n\nconsole.log(myClass.eventNames());\n```\n\n## .listenerCount(eventName?)\n\nGet the count of listeners for an event or all events if evenName not provided.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n\nconsole.log(myClass.listenerCount('message')); // 1\n```\n\n## .rawListeners(eventName?)\n\nGet all listeners for an event or all events if evenName not provided.\n\n```javascript\nimport { Hookified } from 'hookified';\n\nclass MyClass extends Hookified {\n  constructor() {\n    super();\n  }\n}\n\nconst myClass = new MyClass();\n\nmyClass.on('message', (message) => {\n  console.log(message);\n});\n\nconsole.log(myClass.rawListeners('message'));\n```\n\n# Development and Testing\n\nHookified is written in TypeScript and tests are written in `vitest`. To run the tests, use the following command:\n\nTo setup the environment and run the tests:\n\n```bash\nnpm i && npm test\n```\n\nTo contribute follow the [Contributing Guidelines](CONTRIBUTING.md) and [Code of Conduct](CODE_OF_CONDUCT.md).\n\n# License\n\n[MIT & © Jared Wray](LICENSE)\n\n\n\n\n","publish_time":1730230469510,"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_cnpm_publish_time":1730230469510,"dist-tags":{"latest":"3.2.0"}}