I'm trying to attach a metadata key-value pair to an object key (method in a TS class), but no action is actually being performed on the element.
import 'reflect-metadata';
export const get = (path: string) => (target: any, key: string, desc: PropertyDescriptor) => {
Reflect.defineMetadata('path', path, target, key);
console.log(`path, target, key -> ${ path }, ${ target }, ${ key }`);
};
Snippet of the call section in the code
@controller('/auth')
export class LoginController {
@get('/login')
getLogin(req: Request, res: Response): void {/* method implementation */};
}
Controller iterating through methods metadata
export const controller = (routePrefix: string) => {
return function (target: Function) {
for (let key in target.prototype) {
const routeHandler = target.prototype[key];
const path = Reflect.getMetadata('path', target.prototype[key]);
console.log('here is the path! -> ' + path);
if (path)
router.get(`${ routePrefix }${ path }`, routeHandler);
}
};
};
"debug" info
// Output
// [start:dev] path, target, key -> /login, [object Object], getLogin
// [start:dev] here is the path! -> undefined