I am having issues trying to get a hold of the NestJS handler's route in an interceptor I am writing. For instance, if a Controller had a route as such:
@Get('/params/:p1/:p2')
routeWithParams(@Param() params): string {
return `params are ${params.p1} and ${params.p2}`;
}
I would like the ability to grab the value /param/:p1/:p2
programatically. Using the url and deparameterizing is NOT an option, as there is not really a way to do so in a %100 airtight manner. Did some digging and have not found a documented way to grab the route for the handler. Wondering if anyone else has had luck? Here is some example code I stripped down from my project:
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();
}
}
If it turns out that an interceptor won't do the trick and something else like a Guard could work to grab this information I'm all ears.