Skip to content

Instantly share code, notes, and snippets.

@arielweinberger
Created May 9, 2021 13:16
Show Gist options
  • Save arielweinberger/f5c02406b48bb0e145e8542c7006649f to your computer and use it in GitHub Desktop.
Save arielweinberger/f5c02406b48bb0e145e8542c7006649f to your computer and use it in GitHub Desktop.
import {
NestInterceptor,
ExecutionContext,
Injectable,
CallHandler,
} from '@nestjs/common';
import { classToPlain } from 'class-transformer';
import { map } from 'rxjs/operators';
@Injectable()
export class TransformInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler<any>) {
return next.handle().pipe(map(data => classToPlain(data)));
}
}
@moefar
Copy link

moefar commented Nov 15, 2021

Thanks, @arielweinberger 👋
Would be great to wrap the data parameter with parentheses in the map method to follow the prettier.

@ysuzuki19
Copy link

Thanks, @arielweinberger !

classToPlain() will be renamed to instanceToPlain() in class-validator.

I already checked operation of it. in your udemy cource😊.

@royeradames
Copy link

import {
  NestInterceptor,
  ExecutionContext,
  Injectable,
  CallHandler,
} from '@nestjs/common';
import { instanceToPlain } from 'class-transformer';
import { map } from 'rxjs/operators';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler<any>) {
    return next.handle().pipe(map((data) => instanceToPlain(data)));
  }
}

@captbilard
Copy link

Thanks @arielweinberger I do enjoy your udemy course, a quick question as regards serialization
I understand that creating a transform.interceptor.ts file would be useful in creating custom interceptors, but is there any particular reason as to why we didn't use the ClassSerializerInterceptor provided by Nestjs?

@jcasanella
Copy link

as @royeradames and @ysuzuki19 suggests, instanceToPlain is the way to take off the deprecated. See the doc:

typestack/class-transformer@v0.5.0...v0.5.1#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R15

Assuming v0.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment