第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用類裝飾器將裝飾器應(yīng)用于所有類方法

如何使用類裝飾器將裝飾器應(yīng)用于所有類方法

繁星coding 2022-12-29 14:13:27
我正在使用實(shí)驗(yàn)性打字稿裝飾器來管理 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實(shí)現(xiàn) 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 }}問題是,我必須對(duì)大多數(shù)其他控制器應(yīng)用相同的裝飾器,而且我發(fā)現(xiàn)它非常重復(fù)。例如,StockController應(yīng)該只允許訪問MERCHANT角色,我必須執(zhí)行如下操作: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);  }}這種方法不僅乏味和重復(fù)而且不安全,因?yàn)槿绻姨砑恿艘粋€(gè)方法Controller并且不小心忘記了將方法添加到子控制器,它們將允許不需要的訪問。我想用類裝飾器處理這個(gè)問題并使用如下內(nèi)容:@requireRole('MERCHANT')class StockController extends Controller {}但是,根據(jù)我在文檔中看到的內(nèi)容:類裝飾器應(yīng)用于類的構(gòu)造函數(shù),可用于觀察、修改或替換類定義。據(jù)我了解,我無法在類裝飾器中實(shí)現(xiàn)“方法掛鉤”。有什么建議么?供您參考,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({});      }    }  }}
查看完整描述

1 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

這可以通過覆蓋類方法來實(shí)現(xiàn)


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);

            }

        }

    }

}

基本上遍歷所有方法(可能圍繞它添加一些邏輯以將某些方法列入白名單/黑名單)并用包裝了方法裝飾器的新方法覆蓋。


這是方法裝飾器的基本示例。


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游樂場(chǎng)


查看完整回答
反對(duì) 回復(fù) 2022-12-29
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)