NestJS 的自定义路由装饰器

tps*_*idt 5 javascript typescript typescript-decorator nestjs

我已经为我的路线创建了一些自定义参数装饰器,但我没有找到任何有关如何为路线本身创建装饰器的有用文档。有一些描述如何将现有方法装饰器捆绑在一起,这对我没有帮助。

我想要实现的是一些简单的范围验证。范围已在请求上下文中设置。我目前所拥有的仅基于TypeScript 装饰器,但实际上无处可去:

控制器.ts

@RequiredScope(AuthScope.OWNER)
@Get('/some-route')
async get() {
    ...
}
Run Code Online (Sandbox Code Playgroud)

必需范围.ts

export function RequiredScope(...scopes: string[]) {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        console.log(`Validation of scopes '${scopes.join(',')}' for input '${RequestContext.getScopes().join(',')}'`)
        if (!scopes.map(s => RequestContext.hasOneOfTheScopes(s)).find(valid => !valid)) {
            throw new HttpException(`Missing at least one scope of '${scopes.join(',')}'`, HttpStatus.FORBIDDEN)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是我的请求上下文甚至不可用,因为我的中间件设置了我的上下文还没有启动。请求立即失败。

有人能指出我正确的方向吗?

tps*_*idt 5

正如哈立德·穆罕默德(Khaled Mohamed)的善意提议(谢谢!),用警卫解决这个问题非常简单,也是更好的方法。


在scopes.decorator.ts中创建一个具有所需范围作为参数的装饰器:

export const Scopes = (...scopes: string[]) => SetMetadata('scopes', scopes)
Run Code Online (Sandbox Code Playgroud)

设置一个警卫来检查提供的元数据scopes并检查scopes.guard.ts中是否满足要求:

@Injectable()
export class ScopesGuard implements CanActivate {

    constructor(private reflector: Reflector) {}

    canActivate(
        context: ExecutionContext,
    ): boolean | Promise<boolean> | Observable<boolean> {
        const scopes = this.reflector.get<string[]>('scopes', context.getHandler())
        if (!scopes || scopes.length === 0) return true
        if (!scopes.map(s => RequestContext.hasOneOfTheScopes(s)).find(valid => !valid)) {
            throw new HttpException(`Missing at least one scope of '${scopes.join(',')}'`, HttpStatus.FORBIDDEN)
        }
        return true
    }
}
  
Run Code Online (Sandbox Code Playgroud)

在app.module.ts中全局激活守卫:

@Module({
    imports: [...],
    controllers: [...],
    providers: [
        {
            provide: APP_GUARD,
            useClass: ScopesGuard,
        }
    ],
})
Run Code Online (Sandbox Code Playgroud)

在controller.ts中的路由中使用装饰器:

@Scopes(AuthScope.OWNER)
@Get('/some-route')
async get() {
    ...
}
Run Code Online (Sandbox Code Playgroud)