Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nestjs] MapInceptor returns empty response for Promise #247

Closed
VladDobrovolskiy opened this issue Jan 24, 2021 · 4 comments
Closed

[nestjs] MapInceptor returns empty response for Promise #247

VladDobrovolskiy opened this issue Jan 24, 2021 · 4 comments
Labels
bug Something isn't working question Further information is requested

Comments

@VladDobrovolskiy
Copy link

I tried to follow this guide https://automapperts.netlify.app/docs/nestjs

But it feels like MapInterceptor does not work with promises.

Here're my two code samples:

  1. This one works like a charm:
  @Get()
  async get(@User() firebaseUser: FirebaseUser): Promise<UserProfileVM> {
    try {
      const foundUser = await this.userProfilesService.getUserProfileByFireID(
        firebaseUser.uid,
      );

      return this.mapper.map(foundUser, UserProfileVM, UserProfileDto); 
    } catch (exception) {
      this.throwHttpException(exception);
    }
  }
  1. This one does not work:
  @Get()
  @UseInterceptors(MapInterceptor(UserProfileVM, UserProfileDto))
  async get(@User() firebaseUser: FirebaseUser): Promise<UserProfileDto> {
    try {
      return await this.userProfilesService.getUserProfileByFireID(
        firebaseUser.uid,
      );
    } catch (exception) {
      this.throwHttpException(exception);
    }
  }

The endpoint returns empty body in http response.

I really like the new approach with interceptors but I don't know how to use them here.
Help me please.

@nartc nartc added the bug Something isn't working label Jan 25, 2021
@nartc
Copy link
Owner

nartc commented Jan 25, 2021

Would you be able to provide a sample reproduce repo? I can't seem to reproduce this with my setup.

@nartc nartc added the question Further information is requested label Jan 25, 2021
@nartc nartc closed this as completed in 93e3761 Jan 25, 2021
@notmedia
Copy link

notmedia commented Mar 8, 2021

@VladDobrovolskiy I think you should use MapInterceptor as follows (first argument is TO, and the second argument is FROM:

@UseInterceptors(MapInterceptor(UserProfileDto, UserProfileVM))

By the way, @nartc is it possible (or can be a future request) to use MapInterceptor before route handle?
In case when I get DTO object and want convert it to something different. Or it's a bad pattern and I should do it on service layer?

@nartc
Copy link
Owner

nartc commented Mar 8, 2021

@notmedia not possible with an Interceptor but it is possible with a Pipe like: @Body(MapPipe(Destination, Source))

If this is something you’d like, please open a feature request so we can track it

@notmedia
Copy link

Created an issue - #275 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants