Skip to content

Commit 344f385

Browse files
committed
feat: added clearAdvices utility to clear all advices on class, member or static
re #149
1 parent 9b2b820 commit 344f385

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

src/clear.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
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"
22
import "reflect-metadata"
33

4-
export function clearMethod (target, methodName): Function {
4+
function getMethod (target, methodName): Function {
55
const keyOriginalMethod = generateKey(KEY_ORIGINAL_METHOD, methodName)
6-
return Reflect.getMetadata(keyOriginalMethod, target.prototype)
6+
return Reflect.getMetadata(keyOriginalMethod, target)
77
}
88

9-
export function clearConstructor (target): Function {
9+
function getConstructor (target): Function {
1010
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
1230
}
1331

1432
export function generateKey (scope, methodName): string {
1533
return `${scope}-${methodName}`
1634
}
35+
36+
export const clearMethod = getMethod

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export { afterMethod, beforeMethod, afterInstance, beforeInstance } from "./deco
22
export { AdviceRef, ClassSignature, Metadata, MethodSignature } from "./interfaces"
33
export { onException } from "./on-exception"
44
export { applyAspect } from "./apply-aspect"
5-
export { clearMethod } from "./clear"
5+
export { clearAdvices, clearMethod } from "./clear"
66
export { inject, provider } from "kaop"

test/clear-advices.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { onException, beforeInstance, clearMethod } from "../src/"
1+
import { onException, afterMethod, beforeInstance, clearAdvices } from "../src/"
2+
import "reflect-metadata"
23

34
const catchVoid = onException(meta => meta.handle())
45
const IsJose = beforeInstance(meta => meta.args = ["José"])
6+
const return3 = afterMethod(meta => meta.result = 3)
57

68
@IsJose
79
class Person {
@@ -11,34 +13,32 @@ class Person {
1113
this.name = name;
1214
}
1315

16+
@return3
17+
static getType() {
18+
return 2;
19+
}
20+
1421
@catchVoid
1522
sayHello () {
1623
throw new Error("sayHello err")
1724
}
1825
}
1926

2027
describe("clearing advices from classes", () => {
21-
let originalMethod;
22-
// let originalConstructor;
28+
const clearClass = clearAdvices(Person)
2329

24-
beforeEach(() => {
25-
originalMethod = clearMethod(Person, "sayHello");
26-
// originalConstructor = clearConstructor(Person);
30+
it("original method should throw an error", () => {
31+
const p = new clearClass
32+
expect(p.sayHello).toThrow(Error)
2733
})
2834

29-
it("original method should throw an error", done => {
30-
try {
31-
originalMethod();
32-
} catch (e) {
33-
done();
34-
}
35+
it("should be able to invoque constructor as normal", () => {
36+
const p = new clearClass("Samuel")
37+
expect(p.name).toBe("Samuel")
3538
})
3639

37-
it.skip("should be able to invoque constructor as normal", done => {
38-
// const pinst = new originalConstructor("Samuel");
39-
//
40-
// console.log(pinst.name);
41-
40+
it("should be able to clear static methods", () => {
41+
expect(clearClass.getType()).toBe(2)
4242
})
4343

4444
})

0 commit comments

Comments
 (0)