Skip to content

Commit

Permalink
add transform interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaalanMarwan committed May 1, 2022
1 parent b63c12d commit 810bff8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { TransformInterceptor } from 'src/transform.interceptor';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule);

app.useGlobalPipes(new ValidationPipe());
await app.listen(3000);
app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(new TransformInterceptor());
await app.listen(3000);
}
bootstrap();
15 changes: 15 additions & 0 deletions src/transform.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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)));
}
}

0 comments on commit 810bff8

Please sign in to comment.