Enhance: <TInstance, TField extends InferMethodNames<TInstance>>(
    obj: TInstance,
    field: TField,
    middlewares: OneOrMore<
        Middleware<
            InferParameters<TInstance[TField]>,
            InferReturn<TInstance[TField]>,
        >,
    >,
) => void

Enhances a method on an object by wrapping it with one or more middleware.

Unlike Use, which creates a new standalone wrapped function, Enhance mutates the target object in-place by replacing obj[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.

Enhance works with object literal methods, static class methods, class instance methods, class prototype methods.

Type declaration

// 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 mutation
  • Middleware — the middleware type
  • enhanceFactory | enhanceFactory — factory to create an Enhance function

IMPORT_PATH: @daiso-tech/core/middleware