noa*_*ner 5 decorator typescript
我想在运行时将参数传递给 TypeScript 装饰器,以便装饰方法将使用该参数,原始方法不需要此参数才能运行,但装饰器需要。
我尝试通过将参数传递给装饰方法,然后在装饰器中的原始方法的参数中找到它来实现这一点。
例如我尝试过的:
// calling a decorated method (nestJS code)
@Get('')
@UseGuards(JwtAuthGuard)
async getAll(@CurrentUser() user: User): Promise<any[]> {
//current user is a nestJS decorator extracts the user from the http header
const res = await this.whateverService.original(user);
return res;
}
// @MyDecorator usage
@MyDecorator()
async original(@user user: User): Promise<any>{
// return get all whatever
}
// @MyDecorator decorator
export function MyDecorator() {
return function (target: Object, propertyName: string | symbol, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
// get user param index
const userParamIndex = Reflect.getOwnMetadata(
userMetadataKey,
target,
propertyName
);
if (userParamIndex === undefined) {
throw new Error(`user undefined or missing @user decorator in method ${propertyName.toString()}`);
}
// override method descriptor with proxy method
descriptor.value = async function (...args: any[]) {
return new Promise(async (resolve, reject) => {
const user = args[userParamIndex];
let res;
try {
// await stuff using user arg;
res = await originalMethod();
} catch (err) {
reject(err);
} finally {
// await stuff;
resolve(res);
}
});
};
};
}
// @user decorator
export function user(
target: Object,
propertyKey: string | symbol,
parameterIndex: number
): void {
Reflect.defineMetadata(
userMetadataKey,
parameterIndex,
target,
propertyKey
);
}
Run Code Online (Sandbox Code Playgroud)
我在这个实现中遇到的问题是user原始参数被标记为declared but its value is never read.IDE 中的参数。
有没有办法让IDE知道它正在装饰器中使用?或者也许有更好的实现?
对于更多上下文,我添加的代码经过简化,并不包含所有功能。这一切的目的是:
技术堆栈:NestJS 和 TypeORM。我正在尝试模仿TypeORM 的 Transaction 装饰器之类的东西,但具有额外的功能。
| 归档时间: |
|
| 查看次数: |
1086 次 |
| 最近记录: |