title: Custom Route Decorators for NestJS
body: <p>I've created some <a href="https://docs.nestjs.com/custom-decorators" rel="nofollow noreferrer">custom parameter decorators</a> for my routes, but I do not find any useful documentation on how to create a decorator for the route itself. There is some description how to bundle existing method decorators together which does not help me.</p>
  <p>What I'm trying to achieve is some simple scope validation. The scopes are already set up in the request context. What I currently have, only based on <a href="https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators" rel="nofollow noreferrer">TypeScript decorators</a>, but is actually going nowhere:</p>
  <p><strong>controller.ts</strong></p>
  <pre><code>@RequiredScope(AuthScope.OWNER)
  @Get('/some-route')
  async get() {
      ...
  }
  </code></pre>
  <p><strong>required-scopes.ts</strong></p>
  <pre><code>export function RequiredScope(...scopes: string[]) {
      return (target: any, propertyKey: string, descriptor: PropertyDescriptor) =&gt; {
          console.log(`Validation of scopes '${scopes.join(',')}' for input '${RequestContext.getScopes().join(',')}'`)
          if (!scopes.map(s =&gt; RequestContext.hasOneOfTheScopes(s)).find(valid =&gt; !valid)) {
              throw new HttpException(`Missing at least one scope of '${scopes.join(',')}'`, HttpStatus.FORBIDDEN)
          }
      }
  }
  </code></pre>
  <p>Problem here is that my request context is not even available because my middleware which does set up my context did not kick in yet. The request is failing immediately.</p>
  <p><strong>Can somebody point me into the right direction?</strong></p>