У меня проблемы с попыткой удержать маршрут обработчика Nest JS в перехватчике, о котором я пишу. Например, если у контроллера есть такой маршрут:
@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();
}
}
Если окажется, что перехватчик не сработает, и что-то еще, например гвардия, может сработать, чтобы получить эту информацию, я все уши.