From 6ba0f7cf124b2af7d56457829488ff8ed34c4bf3 Mon Sep 17 00:00:00 2001 From: k1r0s Date: Mon, 17 Jun 2019 21:29:34 +0200 Subject: [PATCH] feat: added clearMethod to remove advices from a specific class method fix #149 --- src/clear.ts | 16 ++++++++++++++ src/decorators.ts | 5 +---- src/index.ts | 1 + test/clear-advices.spec.ts | 44 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/clear.ts create mode 100644 test/clear-advices.spec.ts diff --git a/src/clear.ts b/src/clear.ts new file mode 100644 index 00000000..a6bfccfc --- /dev/null +++ b/src/clear.ts @@ -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}` +} diff --git a/src/decorators.ts b/src/decorators.ts index 096b99ce..edffe36d 100644 --- a/src/decorators.ts +++ b/src/decorators.ts @@ -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 || []), diff --git a/src/index.ts b/src/index.ts index 1811f12a..74d870fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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" diff --git a/test/clear-advices.spec.ts b/test/clear-advices.spec.ts new file mode 100644 index 00000000..d29c8c36 --- /dev/null +++ b/test/clear-advices.spec.ts @@ -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); + + }) + +})