#Interceptors and access to piped/transformed body/form-data/params/query?

3 messages · Page 1 of 1 (latest)

hollow vine
#

I know I can get access to the Request/IncomingMessage object, and the response, and the handler through the ExecutionContext... I can get the body, query, and params from the request, but apparently not the multipart body.

I'd also prefer to access the actual arguments delivered to the handler function, with the class-transforms and all of that junk already applied. This is for an auditing system that needs to be able to capture the class name, method name, and arguments, measure a runtime, and capture the response code; interceptors feel like the best fit, but from what I'm seeing they just barely miss out on getting access to the tranformed argument array.

I'm looking through the nestjs and platform-express source code trying to maybe follow the marble. It seems this is where the handler is invoked, and the handler gets wrapped and passed the arguments inline as it's passed into the interceptors manager, but the interceptors manager never gets that argument array; only the args from context.getArgs() which are not what I am looking for.

I'm aware I can hit up metadata from design:params to get the classes it is going to be returning, but I didn't see a way to actually resolve those within the interceptor.

Additionally, I'm not sure where the mutlipart/form-data gets inflated into a body; is that coming from multer? The only decorator I use from there is for a single file upload, but within that same request I am able to consume other form body params as a body DTO, but it is not there as the body of the IncomingMessage in an interceptor; presumably because that is happening in an interceptor as well? I tried to gleen insight from the multer interceptor... does mixin somhow fix this?

Sorry for the longwinded question.

hollow vine
#

I think I figured out part of my question, about how to access the multipart-body in the interceptor!
I was looking at the request body in the top scope of intercept():

export class MyInterceptor implements NestInterceptor {
  intercept(context, next) {
    const req = context.switchToHttp().getRequest(); 
    debugger; // <-- here

    return next.handle().pipe(
      tap({
        next: () => {
          /* after-run logic */
        },
      }),
    );
  }
}

—instead, I needed to move it to be in the after-run logic area, that way it's been mutated by the other interceptors, etc.

export class MyInterceptor implements NestInterceptor {
  intercept(context, next) {
    // not here, silly

    return next.handle().pipe(
      tap({
        next: () => {
          const req = context.switchToHttp().getRequest(); 
          debugger; // <-- here
        },
      }),
    );
  }
}
#

This might be enough for my needs; I may be able to get by without the actual dto classes.