{"library":"mhook","title":"Node.js Middleware Hooks","description":"`mhook` is a small Node.js library that provides middleware-like hook functionality, particularly useful for structuring relations within entities in applications like ODMs or ORMs. It allows developers to define a set of named \"actions\" and then bind multiple hook functions to these actions. When an action is \"triggered,\" all associated hooks are executed sequentially. Hooks can be asynchronous, supporting both traditional Node.js error-first callbacks and Promises for indicating completion or failure. The current stable version is 1.0.1. Its key differentiators include a simple API for sequential hook execution and built-in support for both callback and promise-based asynchronous operations, making it flexible for various async patterns. It focuses purely on the hooking mechanism without dictating how the hooked objects should behave beyond providing `on` and `trigger` methods.","language":"javascript","status":"maintenance","last_verified":"Thu Apr 23","install":{"commands":["npm install mhook"],"cli":null},"imports":["const { Hook } = require('mhook');","const mhook = require('mhook');\nconst Hook = mhook.Hook;","const mhook = require('mhook');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const { Hook } = require('mhook'); // Correct CJS way to get Hook\n\n// 1. Define actions for the hook instance\nconst hook = new Hook(['beforeUpdate', 'afterUpdate', 'afterRemove']);\n\n// 2. Add a hook with a callback\nhook.on('beforeUpdate', function(done) {\n  console.log('Hook 1: beforeUpdate (callback style) started.');\n  // Simulate async work\n  setTimeout(() => {\n    console.log('Hook 1: beforeUpdate (callback style) finished.');\n    done(); // Signal completion\n  }, 100);\n});\n\n// 3. Add another hook with a promise\nhook.on('beforeUpdate', function(data) {\n  console.log('Hook 2: beforeUpdate (promise style) started with data:', data);\n  return new Promise((resolve) => {\n    // Simulate async work\n    setTimeout(() => {\n      console.log('Hook 2: beforeUpdate (promise style) finished.');\n      resolve(); // Signal completion\n    }, 50);\n  });\n});\n\n// 4. Trigger the 'beforeUpdate' action with arguments\nconsole.log('\\nTriggering beforeUpdate (callback-based)...');\nhook.trigger('beforeUpdate', [{ id: 1, name: 'oldName' }], function(err) {\n  if (err) {\n    console.error('Trigger failed (callback):', err);\n  } else {\n    console.log('All beforeUpdate hooks done (callback-based).');\n  }\n});\n\n// 5. Trigger the 'beforeUpdate' action again, using the promise return\nconsole.log('\\nTriggering beforeUpdate (promise-based)...');\nhook.trigger('beforeUpdate', [{ id: 2, name: 'anotherName' }])\n  .then(() => {\n    console.log('All beforeUpdate hooks successfully done (promise-based).');\n  })\n  .catch((err) => {\n    console.error('One of the beforeUpdate hooks failed (promise-based):', err);\n  });","lang":"javascript","description":"This example demonstrates how to define actions, bind both callback-based and promise-based hook functions to an action, and then trigger that action, showing how arguments are passed and results are handled.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}