Skip to content

Commit

Permalink
feat: added clearMethod to remove advices from a specific class method
Browse files Browse the repository at this point in the history
fix #149
  • Loading branch information
k1r0s committed Jun 17, 2019
1 parent fc151dc commit 6ba0f7c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { KEY_ORIGINAL_METHOD, KEY_BEFORE_METHOD, KEY_AFTER_METHOD, KEY_BEFORE_INSTANCE, KEY_AFTER_INSTANCE } from "./constants"
import "reflect-metadata"

export function clearMethod (target, methodName): Function {
const keyOriginalMethod = generateKey(KEY_ORIGINAL_METHOD, methodName)
return Reflect.getMetadata(keyOriginalMethod, target.prototype)
}

export function clearConstructor (target): Function {
const keyOriginalMethod = generateKey(KEY_ORIGINAL_METHOD, "constructor")
return Reflect.getMetadata(keyOriginalMethod, target)
}

export function generateKey (scope, methodName): string {
return `${scope}-${methodName}`
}
5 changes: 1 addition & 4 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { KEY_ORIGINAL_METHOD, KEY_BEFORE_METHOD, KEY_AFTER_METHOD, KEY_BEFORE_INSTANCE, KEY_AFTER_INSTANCE } from "./constants"
import { AdviceRef, MethodSignature, ClassSignature } from "./interfaces"
import { reflect } from "kaop"
import { generateKey } from "./clear"
import "reflect-metadata"

function generateKey (scope, methodName) {
return `${scope}-${methodName}`
}

function wrapMethod (target, methodName, original, befores, afters, caller?) {
const adviceList = [
...(befores || []),
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +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 { inject, provider } from "kaop"
44 changes: 44 additions & 0 deletions test/clear-advices.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { onException, beforeInstance, clearMethod } from "../src/"

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

@IsJose
class Person {
name;

constructor(name) {
this.name = name;
}

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

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

beforeEach(() => {
originalMethod = clearMethod(Person, "sayHello");
// originalConstructor = clearConstructor(Person);
})

it("original method should throw an error", done => {
try {
originalMethod();
} catch (e) {
done();
}
})

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

})

})

0 comments on commit 6ba0f7c

Please sign in to comment.