【问题标题】:How to get handler route in NestJS Interceptor (For both Express and Fastify)如何在 NestJS 拦截器中获取处理程序路由(对于 Express 和 Fastify)
【发布时间】:2020-06-19 20:26:52
【问题描述】:

我在尝试在我正在编写的拦截器中获取 NestJS 处理程序的路由时遇到问题。例如,如果控制器有这样的路由:

  @Get('/params/:p1/:p2')
  routeWithParams(@Param() params): string {
    return `params are ${params.p1} and ${params.p2}`;
  }

我希望能够以编程方式获取值/param/:p1/:p2。使用 url 和去参数化不是一种选择,因为实际上没有办法以 %100 密封的方式这样做。进行了一些挖掘,但没有找到记录在案的方法来获取处理程序的路线。想知道其他人有没有运气?这是我从项目中剥离的一些示例代码:

import { Injectable, ExecutionContext, CallHandler, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Request } from 'express';
import { FastifyRequest } from 'fastify';

function isExpressRequest(request: Request | FastifyRequest): request is Request {
  return (request as FastifyRequest).req === undefined;
}

@Injectable()
export class MyInterceptor implements NestInterceptor {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const request: Request | FastifyRequest = context.switchToHttp().getRequest();

    if( !isExpressRequest(request) ) { // if req fufills the FastifyRequest interface, we will rename the transaction
      const req = request as FastifyRequest;
      const route = `` // TODO how can I grab the route either using the FastifyRequest or ExecutionContext??
    } // otherwise, we are in express request
    const route = `` // TODO how can I grab the route either using the Request or ExecutionContext?

    return next.handle();
  }
}

如果事实证明拦截器无法解决问题,而像 Guard 之类的其他东西可以用来获取这些信息,我会全力以赴。

【问题讨论】:

    标签: express nestjs fastify


    【解决方案1】:

    在与 NestJS Discord 上的好人交谈后,我被指向 Reflectors。因此,使用反射器,我实际上可以获取传递给 HTTP 方法装饰器的路径数据。

    import { Injectable, ExecutionContext, CallHandler, NestInterceptor } from '@nestjs/common';
    import { Reflector } from '@nestjs/core';
    import { Observable } from 'rxjs';
    import { Request } from 'express';
    import { FastifyRequest } from 'fastify';
    import { PATH_METADATA } from '@nestjs/common/constants';
    
    function isExpressRequest(request: Request | FastifyRequest): request is Request {
      return (request as FastifyRequest).req === undefined;
    }
    
    @Injectable()
    export class MyInterceptor implements NestInterceptor {
      constructor(private readonly reflector: Reflector) {}
    
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        const request: Request | FastifyRequest = context.switchToHttp().getRequest();
    
        const path = this.reflector.get<string[]>(PATH_METADATA, context.getHandler()); 
        const method = isExpressRequest(request) ? request.method : (request as FastifyRequest).req.method;
    
        // can now do something with the path and method
    
        return next.handle();
      }
    }
    
    

    现在有一个有效的担忧,即PATH_METADATA 键可能会在 NestJS 中移动,从而破坏此代码。完全有可能并且需要注意。但是根据 git blame for the constants 的说法,path 键已经 3 年没有更新了,这缓解了 imo 的担忧:https://github.com/nestjs/nest/blame/master/packages/common/constants.ts

    【讨论】:

    • 我发现如果你想获取完整路径(包括控制器级别路径和全局前缀),这种方法不太适用。为此,我使用了const path = context.switchToHttp().getRequest().route?.path,这似乎工作正常。
    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 2020-05-22
    • 2020-12-24
    • 2019-01-25
    • 2019-04-13
    • 2022-06-14
    • 1970-01-01
    • 2018-06-18
    相关资源
    最近更新 更多