{"id":18689,"library":"promised-hooks","title":"Promised Hooks","description":"Lightweight middleware utility that adds pre and post hooks to any function returning a Promise. Version 3.1.1 is the current stable release, last updated several years ago with no active development. It allows wrapping classes, functions, or objects to intercept Promise-returning methods, with support for argument overriding and post-hook response modification. Different from general middleware libraries like Express or Koa, it focuses specifically on Promise-based async flows and integrates hooks directly into existing classes or objects without requiring a framework.","status":"maintenance","version":"3.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/sebelga/promised-hooks","tags":["javascript","hooks","promise","middleware"],"install":[{"cmd":"npm install promised-hooks","lang":"bash","label":"npm"},{"cmd":"yarn add promised-hooks","lang":"bash","label":"yarn"},{"cmd":"pnpm add promised-hooks","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package does not export named exports; default import is the correct pattern in ESM.","wrong":"const hooks = require('promised-hooks')","symbol":"default","correct":"import hooks from 'promised-hooks'"},{"note":"wrap is a method on the default export, not a named export.","wrong":"import { wrap } from 'promised-hooks'","symbol":"wrap","correct":"import hooks from 'promised-hooks'; hooks.wrap(MyClass)"},{"note":"pre is added as a static method on the wrapped target after calling hooks.wrap().","wrong":"import { pre } from 'promised-hooks'; pre(MyClass, 'method', fn)","symbol":"pre","correct":"import hooks from 'promised-hooks'; MyClass.pre('method', fn)"}],"quickstart":{"code":"import hooks from 'promised-hooks';\n\nclass Service {\n  async fetch(id) {\n    return { id, data: 'original' };\n  }\n}\n\nhooks.wrap(Service);\n\nService.pre('fetch', async function() {\n  // this is the class instance\n  console.log('pre hook called');\n  // Override arguments: resolve with __override\n  return { __override: [42] };\n});\n\nService.post('fetch', async function(result) {\n  result.modified = true;\n  return result;\n});\n\nconst service = new Service();\nconst result = await service.fetch(1);\nconsole.log(result); // { id: 42, data: 'original', modified: true }","lang":"javascript","description":"Demonstrates wrapping a class, adding pre hook to override arguments, and post hook to modify the response."},"warnings":[{"fix":"Ensure __override provides the exact arguments expected by the original method, either a single value or an array of arguments.","message":"The __override pattern in pre hooks replaces arguments entirely; if you resolve with an object that is not the expected shape, the original method may fail.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Design post hooks to chain properly; consider not using __override in post hooks unless you intend to discard previous responses.","message":"Post hooks can also use __override to replace the response, but if multiple post hooks do this, only the last hook's override takes effect.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always call hooks.wrap(MyClass) before adding any pre/post hooks.","message":"Version 1.0.0 introduced a breaking API change: hooks.wrap() must now be called on the class/object before using pre/post, whereas earlier versions (if any) may have worked differently.","severity":"breaking","affected_versions":"<1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add hooks.wrap(MyClass) before MyClass.pre('method', fn).","cause":"The class/object was not wrapped with hooks.wrap() before calling pre/post.","error":"TypeError: MyClass.pre is not a function"},{"fix":"Ensure every hook function returns a Promise (or is async).","cause":"The hook function did not return a Promise or the target method is not async.","error":"Cannot read property 'then' of undefined"},{"fix":"Use { __override: ... } pattern correctly; if you don't need override, resolve with undefined or a simple value.","cause":"A pre hook resolved with an object that is not the expected __override shape, causing the original method to receive the object instead of arguments.","error":"UnhandledPromiseRejectionWarning: [object Object]"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}