【发布时间】:2019-08-28 20:39:44
【问题描述】:
我想使用nest.js 实现这样的目标: (与 Spring 框架非常相似)
@Controller('/test')
class TestController {
@Get()
get(@Principal() principal: Principal) {
}
}
阅读文档数小时后,我发现nest.js 支持创建自定义装饰器。所以我决定实现我自己的@Principal 装饰器。装饰器负责从 http 头中检索访问令牌,并使用令牌从我自己的身份验证服务中获取用户的主体。
import { createParamDecorator } from '@nestjs/common';
export const Principal = createParamDecorator((data: string, req) => {
const bearerToken = req.header.Authorization;
// parse.. and call my authService..
// how to call my authService here?
return null;
});
但问题是我不知道如何在装饰器处理程序中获取我的服务实例。可能吗?如何?提前谢谢你
【问题讨论】:
标签: javascript node.js typescript decorator nestjs