How to access class metadata from method decorator How to access class metadata from method decorator typescript typescript

How to access class metadata from method decorator


The problem is the order in which decorators get executed. Method decorators are executed first, class decorators are executed after. This makes sense if you think about it, the class decorators need the complete class to act upon, and creating the class involves creating the methods and calling their decorators first.

A simple workaround would be for the method decorator to register a callback that would then be called by the class decorator after the topic was set:

function ClassDecorator(topic?: string): ClassDecorator {    return (target) => {        Reflect.defineMetadata('topic', topic, target.prototype);        let topicFns: Array<() => void> = Reflect.getMetadata("topicCallbacks", target.prototype);        if (topicFns) {            topicFns.forEach(fn => fn());        }        return target;    };}interface methodDecoratorOptions {    cmd: string}function MethodDecorator(options: methodDecoratorOptions) {    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {        let topicFns: Array<() => void> = Reflect.getMetadata("topicCallbacks", target);        if (!topicFns) {            Reflect.defineMetadata("topicCallbacks", topicFns = [], target);        }        topicFns.push(() => {            console.log('metaData is: ', Reflect.getMetadata('topic', target));        });    }}@ClassDecorator('auth')class LoginClass {    @MethodDecorator({        cmd: 'login'    })    myMethod() {        console.log('METHOD CALLED');    }}