( obj: O, field: T, wrapped: WrappedFunction, enumerable: boolean = true, )
| 90 | * the rare case where we might patch a non-enumerable method. |
| 91 | */ |
| 92 | export function wrapMethod<O extends {}, T extends string & keyof O>( |
| 93 | obj: O, |
| 94 | field: T, |
| 95 | wrapped: WrappedFunction, |
| 96 | enumerable: boolean = true, |
| 97 | ): void { |
| 98 | const original = obj[field]; |
| 99 | if (typeof original !== 'function') { |
| 100 | throw new Error(`Cannot wrap method: ${field} is not a function`); |
| 101 | } |
| 102 | if (getOriginalFunction(original)) { |
| 103 | throw new Error(`Attempting to wrap method ${field} multiple times`); |
| 104 | } |
| 105 | markFunctionWrapped(wrapped, original); |
| 106 | Object.defineProperty(obj, field, { |
| 107 | writable: true, |
| 108 | configurable: true, |
| 109 | enumerable, |
| 110 | value: wrapped, |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * This extracts the original function if available. See |
no test coverage detected