Nest: Would like to be able to use Interceptors to rewrite Request values

Created on 5 Jan 2018  路  4Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[x] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I am attempting to use an Interceptor to modify the incoming request (particularly request.params or request.query). However, it appears that RouteParamsFactory.exchangeKeyForValue() is called prior to the interceptors so these rewritten values are never seen.

I considered a pipe, but pipes do not have sufficient context about the request for me to make my modifications.

I considered using a middleware, but those are executed prior to the router so route params are not available yet.

Expected behavior

I would like the ability to use interceptors to rewrite the incoming request before values are extracted from the request.

It is sort of possible, as you can modify the request object, and code that uses that reference later will have the modifications. But I think it would be good to have consistent values from the request object regardless of the mechanism of access.

I think it would also be helpful to have a page in the docs that details the order of execution amongst middleware, request parsing pipes, guards, interceptors, filters. There are hints on the various pages and but I think interceptors documentation is missing. For example, how should I populate request.user for a guard? Middleware, the guard itself, or an interceptor? I think the interceptor runs too late from testing, so I implemented in middleware.

Minimal reproduction of the problem with instructions

In interceptor:

@Interceptor()
export class Interceptor implements NestInterceptor {
  async intercept(request, context: ExecutionContext, stream$: Observable<any>) {
    console.log('before interceptor', request.params);

    request.params.id = 4;

    console.log('after interceptor', request.params);
    return stream$;
  }
}

In controller for Users:

  @Get(':id')
  public async getOne(@Param('id') id: number) {
    console.log('getOne', id);
  }

Issuing a request for GET /users/1, I would expect:

before interceptor { id: '1' }
after interceptor { id: 4 }
getOne 4

but instead I get:

before interceptor { id: '1' }
after interceptor { id: 4 }
getOne '1'

What is the motivation / use case for changing the behavior?

I would like the ability to use interceptors to rewrite the incoming request before values are extracted from the request.

type

Most helpful comment

In version 5.0.x, the request param was removed from the intercept method.
However, it is still available via the context object:

@Interceptor()
export class Interceptor implements NestInterceptor {
  async intercept(context: ExecutionContext, stream$: Observable<any>) {
    request = context.switchToHttp().getRequest();
    console.log('before interceptor', request.params);

    request.params.id = 4;

    console.log('after interceptor', request.params);
    return stream$;
  }
}

All 4 comments

Hi @tma-isbx,
It should be fixed in 4.5.7. Could you let me know?

Looks like it is working for me now, thanks!

In version 5.0.x, the request param was removed from the intercept method.
However, it is still available via the context object:

@Interceptor()
export class Interceptor implements NestInterceptor {
  async intercept(context: ExecutionContext, stream$: Observable<any>) {
    request = context.switchToHttp().getRequest();
    console.log('before interceptor', request.params);

    request.params.id = 4;

    console.log('after interceptor', request.params);
    return stream$;
  }
}

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xmlking picture xmlking  路  28Comments

roeehershko picture roeehershko  路  27Comments

szkumorowski picture szkumorowski  路  62Comments

BrunnerLivio picture BrunnerLivio  路  44Comments

fmeynard picture fmeynard  路  45Comments