Created
May 9, 2021 13:16
-
-
Save arielweinberger/f5c02406b48bb0e145e8542c7006649f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | |
} | |
} |
Thanks, @arielweinberger !
classToPlain()
will be renamed to instanceToPlain()
in class-validator
.
I already checked operation of it. in your udemy cource😊.
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)));
}
}
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?
as @royeradames and @ysuzuki19 suggests, instanceToPlain is the way to take off the deprecated. See the doc:
Assuming v0.5.1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, @arielweinberger 👋
Would be great to wrap the
data
parameter with parentheses in themap
method to follow the prettier.