In decorators, you aren't able to get class properties, or do any sort of injection, so you wouldn't be able to get this.reflector
or anything like that. What you could do is set up your own decorator that mimics @Get()
and uses the Reflect.getOwnMetadata()
methods, then returns the ``@Get()` decorator. Might be a bit messy, but something along the lines of
export function Get(path: string): MethodDecorator {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const version = Reflect.getMetadata('version', target, propertyKey);
Reflect.defineMetadata(PATH_METADATA, version + path, descriptor.value);
Reflect.defineMetadata(METHOD_METADATA, RequestMethod.GET, descriptor.value);
return descriptor;
}
}
Where PATH_METHOD
and METHOD_METADATA
are improted from @nestjs/common/constants
and RequestMethod
is imported from @nestjs/common/enums
. This would create a new @Get()
decorator for you that works in tandem with your @SetMetadata()
method. If I remember correctly decorators are ran bottom up, so make sure @SetVersion()
comes before the @Get()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…