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

如何在NestJS拦截器中获取处理程序路由(对于Express和Fastify),express,nestjs,fastify,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

在我正在编写的拦截器中,我在尝试获取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();
  }
}

从'@nestjs/common'导入{Injectable,ExecutionContext,CallHandler,NestInterceptor};
从“rxjs”导入{Observable};
从“express”导入{Request};
从“fastify”导入{FastifyRequest};
函数isExpressRequest(请求:请求| FastifyRequest):请求就是请求{
返回(请求为FastifyRequest).req==未定义;
}
@可注射()
导出类MyInterceptor实现了NestInterceptor{
//eslint禁用下一行@typescript eslint/无显式任何
拦截(上下文:ExecutionContext,下一步:CallHandler):可观察{
const-request:request | fastfyrequest=context.switchToHttp().getRequest();
如果(!isExpressRequest(request)){//if req fu填充FastifyRequest接口,我们将重命名事务
const req=作为FastfyRequest的请求;
const route=``//TODO如何使用FastifyRequest或ExecutionContext获取路由??
}//否则,我们将提出明确要求
const route=``//TODO如何使用请求或ExecutionContext获取路由?
返回next.handle();
}
}

如果结果发现拦截器不起作用,而警卫之类的东西可以用来抓取这些信息,我洗耳恭听。

在与NestJS Discord上的好人交谈后,我被指向反射镜。因此,使用反射器,我实际上可以获取传递到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();
  }
}

从'@nestjs/common'导入{Injectable,ExecutionContext,CallHandler,NestInterceptor};
从'@nestjs/core'导入{Reflector};
从“rxjs”导入{Observable};
从“express”导入{Request};
从“fastify”导入{FastifyRequest};
从'@nestjs/common/constants'导入{PATH_METADATA};
函数isExpressRequest(请求:请求| FastifyRequest):请求就是请求{
返回(请求为FastifyRequest).req==未定义;
}
@可注射()
导出类MyInterceptor实现了NestInterceptor{
构造函数(专用只读反射器:反射器){}
//eslint禁用下一行@typescript eslint/无显式任何

intercept(context:ExecutionContext,next:CallHandler):Observable

我发现如果想要获得完整路径(包括控制器级路径和全局前缀),这种方法就不太管用了。为此,我求助于使用
const path=context.switchToHttp().getRequest().route?.path
,这似乎很管用。