Registry / web-framework / promised-hooks

promised-hooks

JSON →
library3.1.1jsnpmunverified

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.

npm install promised-hooks
INSTALL
IMPORT
SIG · PROMISED-HOOKS
P
promised-hooks
web-frameworkjavascriptv3.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import hooks from 'promised-hooks'
const hooks = require('promised-hooks')
The package does not export named exports; default import is the correct pattern in ESM.
wrap
import hooks from 'promised-hooks'; hooks.wrap(MyClass)
import { wrap } from 'promised-hooks'
wrap is a method on the default export, not a named export.
pre
import hooks from 'promised-hooks'; MyClass.pre('method', fn)
import { pre } from 'promised-hooks'; pre(MyClass, 'method', fn)
pre is added as a static method on the wrapped target after calling hooks.wrap().

Demonstrates wrapping a class, adding pre hook to override arguments, and post hook to modify the response.

import hooks from 'promised-hooks'; class Service { async fetch(id) { return { id, data: 'original' }; } } hooks.wrap(Service); Service.pre('fetch', async function() { // this is the class instance console.log('pre hook called'); // Override arguments: resolve with __override return { __override: [42] }; }); Service.post('fetch', async function(result) { result.modified = true; return result; }); const service = new Service(); const result = await service.fetch(1); console.log(result); // { id: 42, data: 'original', modified: true }
Debug
Known issues
gotchaThe __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.
fix
Ensure __override provides the exact arguments expected by the original method, either a single value or an array of arguments.
affects: >=1.0.0
gotchaPost hooks can also use __override to replace the response, but if multiple post hooks do this, only the last hook's override takes effect.
fix
Design post hooks to chain properly; consider not using __override in post hooks unless you intend to discard previous responses.
affects: >=1.0.0
breakingVersion 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.
fix
Always call hooks.wrap(MyClass) before adding any pre/post hooks.
affects: <1.0.0
Errors
Common errors & fixes
TypeError: MyClass.pre is not a function
The class/object was not wrapped with hooks.wrap() before calling pre/post.
fix
Add hooks.wrap(MyClass) before MyClass.pre('method', fn).
Cannot read property 'then' of undefined
The hook function did not return a Promise or the target method is not async.
fix
Ensure every hook function returns a Promise (or is async).
UnhandledPromiseRejectionWarning: [object Object]
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.
fix
Use { __override: ... } pattern correctly; if you don't need override, resolve with undefined or a simple value.
Upgrade
Version history
3.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
promised-hooks — npm install promised-hooks · libregistry