Skip to content

createMethodEnhancer

createMethodEnhancer<K>(methodName: K): <T>(fn: Funcware<MethodOf<T, K>>) => MethodEnhancer<K, T>

Defined in: ext/methodware/method-enhancer.ts:59

Creates a MethodEnhancer for a method on a target object.

Usage:

  1. Call with the method name to match (string key).
  2. Call with a Funcware to wrap/enhance the method.
  3. Call the resulting enhancer with your target object.

Returns an unenhance function to restore the original method.

See createMethodEnhancer for the enhancer function type returned. See Funcware for the enhancer function type. Funcware is a higher-order function: (…args) => (fn) => F. It lets you wrap or modify the original method logic.

Type Parameter
K extends string
ParameterType
methodNameK

<T>(fn: Funcware<MethodOf<T, K>>): MethodEnhancer<K, T>

Type Parameter
T extends HasMethod<K>
ParameterType
fnFuncware<MethodOf<T, K>>

MethodEnhancer<K, T>

const enhancer = createMethodEnhancer('foo')(fn => (...args) => {
console.log('before', args);
const r = fn(...args);
console.log('after', r);
return r;
});
const unenhance = enhancer(target);
target.foo('will', 'be', 'enhanced');
unenhance();
target.foo('will', 'NOT be', 'enhanced');

This function is useful for dynamically enhancing methods on objects, such as adding logging, instrumentation, or custom behavior. It is commonly used in middleware, plugin, or extension systems where you want to intercept method calls and restore the original implementation when no longer needed.

export const createMethodEnhancer =
<K extends string>(methodName: K) =>
<T extends HasMethod<K>>(
fn: Funcware<MethodOf<T, K>>
): MethodEnhancer<K, T> =>
(target: T) => {
return enhanceMethod(target, methodName, fn);
};