Reputation: 1401
Might it be possible to develop a method decorator, ie: "@rpcMethod" which modifies a class's prototype ?
At the end I want:
@rpcMethod
get(){
....
}
and at run-time:
instance.getRpcMethods(); //returns ['get']
I tried all sort of things inside the decorator (ie:modify target.prototype) but it all failed.
thanks!
Upvotes: 0
Views: 91
Reputation: 14523
Sorry for late reply, try this:
var rpcMethod = (target: Object, propName: string, propertyDescriptor: TypedPropertyDescriptor<() => any>) => {
var desc = Object.getOwnPropertyDescriptor(target, "getRpcMethods");
if (desc.configurable)
{
Object.defineProperty(target, "getRpcMethods", { value: function() { return this["rpcMethods"]; }, configurable: false });
Object.defineProperty(target, "rpcMethods", { value: [] });
}
target["rpcMethods"].push(propName);
};
class MyClass
{
public getRpcMethods():string[] {
throw new Error("Should be implemented by decorator");
}
@rpcMethod
public get() {
return "x";
}
@rpcMethod
public post() {
return "y";
}
}
var myInstance = new MyClass();
myInstance.getRpcMethods();
Upvotes: 1