Skip to content

Commit

Permalink
feat: added clearAdvices utility to clear all advices on class, membe…
Browse files Browse the repository at this point in the history
…r or static

re #149
  • Loading branch information
k1r0s committed Jun 23, 2019
1 parent 9b2b820 commit 344f385
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
30 changes: 25 additions & 5 deletions src/clear.ts
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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export { afterMethod, beforeMethod, afterInstance, beforeInstance } from "./deco
export { AdviceRef, ClassSignature, Metadata, MethodSignature } from "./interfaces"
export { onException } from "./on-exception"
export { applyAspect } from "./apply-aspect"
export { clearMethod } from "./clear"
export { clearAdvices, clearMethod } from "./clear"
export { inject, provider } from "kaop"
34 changes: 17 additions & 17 deletions test/clear-advices.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { onException, beforeInstance, clearMethod } from "../src/"
import { onException, afterMethod, beforeInstance, clearAdvices } from "../src/"
import "reflect-metadata"

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

@IsJose
class Person {
Expand All @@ -11,34 +13,32 @@ class Person {
this.name = name;
}

@return3
static getType() {
return 2;
}

@catchVoid
sayHello () {
throw new Error("sayHello err")
}
}

describe("clearing advices from classes", () => {
let originalMethod;
// let originalConstructor;
const clearClass = clearAdvices(Person)

beforeEach(() => {
originalMethod = clearMethod(Person, "sayHello");
// originalConstructor = clearConstructor(Person);
it("original method should throw an error", () => {
const p = new clearClass
expect(p.sayHello).toThrow(Error)
})

it("original method should throw an error", done => {
try {
originalMethod();
} catch (e) {
done();
}
it("should be able to invoque constructor as normal", () => {
const p = new clearClass("Samuel")
expect(p.name).toBe("Samuel")
})

it.skip("should be able to invoque constructor as normal", done => {
// const pinst = new originalConstructor("Samuel");
//
// console.log(pinst.name);

it("should be able to clear static methods", () => {
expect(clearClass.getType()).toBe(2)
})

})

0 comments on commit 344f385

Please sign in to comment.