The type of the object containing the method
The method name (constrained to invokable keys of TInstance)
The target object whose method will be enhanced
The name of the method to wrap with middleware
One or more middleware to apply, executed in priority order
void — the object is mutated directly
// 2. Define a class with a method to enhance
class UserService {
async getUser(id: string): Promise<{ name: string }> {
console.log(`Fetching user ${id}...`);
return { name: "Alice" };
}
}
function main(enhance: Enhance): void {
// 4. Enhance the method — mutates the instance in-place
const service = new UserService();
enhance(service, "getUser", [loggingMiddleware, cacheMiddleware]);
// 5. Call as usual — middleware runs automatically
await service.getUser("123");
// Logs:
// getUser called with: ["123"]
// Fetching user 123...
// getUser returned: { name: "Alice" }
}
Use — creates a new wrapped function without mutationMiddleware — the middleware typeenhanceFactory — factory to create an Enhance functionIMPORT_PATH: @daiso-tech/core/middleware
Enhances a method on an object by wrapping it with one or more middleware.
Unlike
Use, which creates a new standalone wrapped function,Enhancemutates the target object in-place by replacingobj[field]with a version that runs through the middleware chain before delegating to the original method.This is useful for augmenting existing class instances or objects with cross-cutting concerns (logging, caching, retry, metrics, etc.) without changing their public API — callers continue to invoke the method the same way.
Enhanceworks with object literal methods, static class methods, class instance methods, class prototype methods.