|
1 | | -import { KEY_ORIGINAL_METHOD, KEY_BEFORE_METHOD, KEY_AFTER_METHOD, KEY_BEFORE_INSTANCE, KEY_AFTER_INSTANCE } from "./constants" |
| 1 | +import { KEY_ORIGINAL_METHOD } from "./constants" |
2 | 2 | import "reflect-metadata" |
3 | 3 |
|
4 | | -export function clearMethod (target, methodName): Function { |
| 4 | +function getMethod (target, methodName): Function { |
5 | 5 | const keyOriginalMethod = generateKey(KEY_ORIGINAL_METHOD, methodName) |
6 | | - return Reflect.getMetadata(keyOriginalMethod, target.prototype) |
| 6 | + return Reflect.getMetadata(keyOriginalMethod, target) |
7 | 7 | } |
8 | 8 |
|
9 | | -export function clearConstructor (target): Function { |
| 9 | +function getConstructor (target): Function { |
10 | 10 | const keyOriginalMethod = generateKey(KEY_ORIGINAL_METHOD, "constructor") |
11 | | - return Reflect.getMetadata(keyOriginalMethod, target) |
| 11 | + return Reflect.getMetadata(keyOriginalMethod, target.prototype) |
| 12 | +} |
| 13 | + |
| 14 | +function reassignMethods (target): void { |
| 15 | + const members = Object.getOwnPropertyNames(target) |
| 16 | + members.forEach(member => { |
| 17 | + if (typeof target[member] === "function") { |
| 18 | + const originalMethod = getMethod(target, member) |
| 19 | + originalMethod && Object.defineProperty(target, member, { value: originalMethod }) |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +export function clearAdvices (target): any { |
| 26 | + const originalConstructor = getConstructor(target) |
| 27 | + reassignMethods(originalConstructor) |
| 28 | + reassignMethods(originalConstructor.prototype) |
| 29 | + return originalConstructor |
12 | 30 | } |
13 | 31 |
|
14 | 32 | export function generateKey (scope, methodName): string { |
15 | 33 | return `${scope}-${methodName}` |
16 | 34 | } |
| 35 | + |
| 36 | +export const clearMethod = getMethod |
0 commit comments