-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added clearAdvices utility to clear all advices on class, membe…
…r or static re #149
- Loading branch information
Showing
3 changed files
with
43 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,36 @@ | ||
import { KEY_ORIGINAL_METHOD, KEY_BEFORE_METHOD, KEY_AFTER_METHOD, KEY_BEFORE_INSTANCE, KEY_AFTER_INSTANCE } from "./constants" | ||
import { KEY_ORIGINAL_METHOD } from "./constants" | ||
import "reflect-metadata" | ||
|
||
export function clearMethod (target, methodName): Function { | ||
function getMethod (target, methodName): Function { | ||
const keyOriginalMethod = generateKey(KEY_ORIGINAL_METHOD, methodName) | ||
return Reflect.getMetadata(keyOriginalMethod, target.prototype) | ||
return Reflect.getMetadata(keyOriginalMethod, target) | ||
} | ||
|
||
export function clearConstructor (target): Function { | ||
function getConstructor (target): Function { | ||
const keyOriginalMethod = generateKey(KEY_ORIGINAL_METHOD, "constructor") | ||
return Reflect.getMetadata(keyOriginalMethod, target) | ||
return Reflect.getMetadata(keyOriginalMethod, target.prototype) | ||
} | ||
|
||
function reassignMethods (target): void { | ||
const members = Object.getOwnPropertyNames(target) | ||
members.forEach(member => { | ||
if (typeof target[member] === "function") { | ||
const originalMethod = getMethod(target, member) | ||
originalMethod && Object.defineProperty(target, member, { value: originalMethod }) | ||
} | ||
}) | ||
|
||
} | ||
|
||
export function clearAdvices (target): any { | ||
const originalConstructor = getConstructor(target) | ||
reassignMethods(originalConstructor) | ||
reassignMethods(originalConstructor.prototype) | ||
return originalConstructor | ||
} | ||
|
||
export function generateKey (scope, methodName): string { | ||
return `${scope}-${methodName}` | ||
} | ||
|
||
export const clearMethod = getMethod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters