我为我的路由创建了一些自定义参数装饰器,但是我没有找到有关如何为路由本身创建装饰器的有用文档。有一些描述如何捆绑现有方法装饰器的说明,但这对我没有帮助。
我想实现的是一些简单的作用域验证。作用域已经设置在请求上下文中。根据TypeScript装饰器,我目前拥有的东西实际上没有任何进展:
controller.ts
@RequiredScope(AuthScope.OWNER)
@Get('/some-route')
async get() {
...
}
required-scopes.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)
}
}
}
问题在于我的请求上下文甚至还没有可用,因为我的中间件尚未启动设置我的上下文。请求立即失败。
有人能指点我正确的方向吗?