-
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 clearMethod to remove advices from a specific class method
fix #149
- Loading branch information
Showing
4 changed files
with
62 additions
and
4 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 |
---|---|---|
@@ -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}` | ||
} |
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
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 |
---|---|---|
@@ -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); | ||
|
||
}) | ||
|
||
}) |