如何在 NestJS 拦截器中获取处理程序路由(对于 Express 和 Fastify)
How to get handler route in NestJS Interceptor (For both Express and 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();
}
}
如果事实证明拦截器无法解决问题,而像 Guard 这样的其他东西可以用来获取此信息,我洗耳恭听。
在 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 责备,路径密钥已经 3 年没有更新这一事实在我看来消除了这些担忧:https://github.com/nestjs/nest/blame/master/packages/common/constants.ts
我在尝试获取我正在编写的拦截器中的 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 这样的其他东西可以用来获取此信息,我洗耳恭听。
在 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 责备,路径密钥已经 3 年没有更新这一事实在我看来消除了这些担忧:https://github.com/nestjs/nest/blame/master/packages/common/constants.ts