如何在NestJS拦截器中获取处理程序路由(对于Express和Fastify)

时间:2020-03-07 07:18:18

标签: express nestjs fastify

我在尝试编写的拦截器中试图保留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();
  }
}

如果事实证明拦截器无法解决问题,那么像警卫队这样的人也可以抢到这些信息。

1 个答案:

答案 0 :(得分:5)

在与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 common中移动,从而破坏了此代码。完全可能,值得您注意。但是事实是,根据常量的git怪,路径密钥3年没有更新了,这缓解了imo这些问题:https://github.com/nestjs/nest/blame/master/packages/common/constants.ts