我想要创建一个装饰器,它将获取一个类的所有方法,并使用特定的功能包装它们,在这个例子中,只需要这样记录:
export function CustDec<T extends new (...args: any[]) => any>(Target: T) {
return class extends Target {
constructor(...args: any[]) {
super(...args);
console.log('@--------------------@');
(Target as any).prototype.alphaMethod = async (args: any[]) => {
console.log('@-before-@');
await (Target as any).alphaMethod();
console.log('@-after-@');
};
}
};
}问题是,当我将这个装饰器应用于我的类时:
import { Controller, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Firewall } from 'src/auth/decorators/firewall.decorator';
import { CustDec } from './feedback.decorator';
@CustDec
@ApiTags('feedback')
@Controller('feedback')
export class FeedbackController {
constructor() {
setTimeout(async () => {
await this.alphaMethod(); // <--- here
}, 3000);
}
@Firewall()
@Post('')
async alphaMethod() { // <-- and here
return 'some promised result';
}
}当我试图在控制器上调用/feedback端点时,我会得到一个错误,好像Firewall和Post装饰器在Firewall更改了alphaMethod之后停止了工作。即使我从我的alphaMethod构造函数方法中调用FeedbackController,我也会得到另一个错误:
@-before-@
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
TypeError: Target.alphaMethod is not a function
at FeedbackController.Target.alphaMethod (/home/zagrava/workspace/test-nest/backend/src/feedback/feedback.decorator.ts:8:31)
at Timeout._onTimeout (/home/zagrava/workspace/test-nest/backend/src/feedback/feedback.controller.ts:12:18)
at listOnTimeout (node:internal/timers:564:17)
at processTimers (node:internal/timers:507:7)如何使我的CustDec用日志正确包装类的所有方法?
发布于 2022-07-17 07:37:15
类装饰器可以实现如下所示:
function intercept<T extends { new(...args: any[]): {} }>(target: T) {
const methods = getMethods(target.prototype);
for (const method of methods) {
const currentMethod = target.prototype[method]
target.prototype[method] = async (...args: any[]) => {
console.log("intercepted", new Date());
const result = currentMethod(args)
if (result instanceof Promise) {
await result
}
console.log("executed", new Date());
return result;
}
}
}其思想是获取类的所有函数(构造函数本身除外),并将它们包装在自定义函数中。在这种情况下,获得类的函数的助手方法是有用的:
const getMethods = (obj: any) => Object.getOwnPropertyNames(obj).filter(item => typeof obj[item] === 'function' && item !== "constructor")https://stackoverflow.com/questions/73009667
复制相似问题