Suppose you have a class like this with the Router decorator attached to it.
@Router
class AuthRouter {
constructor(private cacheService: CacheService) {}
}
How do I get the constructor parameter types from within the Router decorator? Assume we have stored a singleton of CacheService that we can access if we knew the class name "CacheService".
function Router(target) {
// somehow get the constructor class name
const dependencyNames = 'CacheService' // an array if multiple args in constructor
// getSingleton is a function that will retrieve
// a singleton of the requested class / object
return new target(getSingleton(dependencyNames))
}
So whenever we use AuthRouter, it will have CacheService already injected into it.
import 'reflect-metadata'
function Router(target) {
const types = Reflect.getMetadata('design:paramtypes', target);
// return a modified constructor
}
Note that you are calling getMetadata with no third param. types will be array of the constructor parameters. types[0].name === 'CacheService' in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With