如何使用 class 装饰器将装饰器应用于所有 class 方法

How to apply a decorator to all class methods using class decorator

我正在使用实验性打字稿装饰器来管理 express 中的访问控制。

class AccountController extends Controller {
  
  login(req: Request, res: Response) {
    const { email, password } = req.body;
    const token = await this.model.login(email, password);
    return res.json({
      token
    });
  }

  @hasRole('ADMIN')
  list(req: Request, res: Response) {
    res.json({
      data: await this.model.findAll()
    });
  }
}

hasRole 方法装饰器工作正常,我很满意。

Controller 实现 REST 方法:

class Controller {
  list(req: Request, res: Response) { // impl }
  get(req: Request, res: Response) { // impl } 
  create(req: Request, res: Response) { // impl }
  update(req: Request, res: Response) { // impl }
  delete(req: Request, res: Response) { // impl }
}

问题是,我必须对大多数其他控制器应用相同的装饰器,而且我发现它非常重复。例如,StockController 应该只允许访问 MERCHANT 角色,我必须执行如下操作:

class StockController extends Controller {
  @hasRole('MERCHANT')
  list(req: Request, res: Response) { 
    return super.list(req, res);
  }
  @hasRole('MERCHANT')
  get(req: Request, res: Response) { 
    return super.get(req, res);
  } 
  @hasRole('MERCHANT')
  create(req: Request, res: Response) { 
    return super.create(req, res);
  }
  @hasRole('MERCHANT')
  update(req: Request, res: Response) { 
    return super.update(req, res);
  }
  @hasRole('MERCHANT')
  delete(req: Request, res: Response) { 
    return super.delete(req, res);
  }
}

这种方法不仅乏味和重复而且不安全,因为如果我添加一个方法到Controller并且不小心忘记将方法添加到子控制器,它们将允许不需要的访问。

我想用 class 装饰器来处理这个问题,并使用类似下面的东西:

@requireRole('MERCHANT')
class StockController extends Controller {}

但是,从我在 doc 中看到的情况来看:

The class decorator is applied to the constructor of the class and can be used to observe, modify, or replace a class definition.

据我了解,我无法在 class 装饰器中实现“方法挂钩”。有什么建议吗?

供您参考,hasRole 装饰器如下所示:

export function hasRole(role: string) {
  return function(target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
    const originalMethod = descriptor.value;
    descriptor.value = function(req: Request, res: Response) {
      const user = res.locals.user;
      if (user && user.hasRole(role)) {
        originalMethod.apply(this, [req, res]);
      } else {
        res.status(403).json({});
      }
    }
  }
}

这可以通过覆盖 class 方法来实现

function AttachToAllClassDecorator<T>(someParam: string) {
    return function(target: new (...params: any[]) => T) {
        for (const key of Object.getOwnPropertyNames(target.prototype)) {
            // maybe blacklist methods here
            let descriptor = Object.getOwnPropertyDescriptor(target.prototype, key);
            if (descriptor) {
                descriptor = someDecorator(someParam)(key, descriptor);
                Object.defineProperty(target.prototype, key, descriptor);
            }
        }
    }
}

基本上遍历所有方法(可能会为 whitelisting/blacklisting 一些方法添加一些逻辑)并用包装了方法装饰器的新方法覆盖。

这是方法装饰器的基本示例。

function someDecorator(someParam: string): (methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor {
    return (methodName: string, descriptor: PropertyDescriptor): PropertyDescriptor => {
        let method = descriptor.value;

        descriptor.value = function(...args: any[]) {
            console.warn(`Here for descriptor ${methodName} with param ${someParam}`);

            return method.apply(this, args);
        }

        return descriptor;
    }
}

TS Playground