5

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

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

我希望能够以/param/:p1/:p2编程方式获取价值。使用 url 和 deparameterizing 不是一种选择,因为实际上没有办法以 %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();
  }
}

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

4

1 回答 1

12

在与 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 的常量,路径键已经 3 年没有更新的事实缓解了 imo 的这些担忧:https://github.com/nestjs/nest/blame/master/packages/common/constants。 ts

于 2020-03-07T19:41:21.960 回答