Skip to content

Commit

Permalink
Added the interceptors [netology-code#10]
Browse files Browse the repository at this point in the history
  • Loading branch information
Expl4Life committed Jan 22, 2022
1 parent 63ee0a1 commit e67c9e5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions 010-nestjs-validation/src/books/books.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CreateBookDto } from './dto/create-book.dto';
import { UpdateBookDto } from './dto/update-book.dto';
import { BooksService } from './books.service';
import { Book } from './schemas/book.schema';
import { throwError } from 'rxjs';

@Controller('books')
export class BooksController {
Expand All @@ -29,6 +30,7 @@ export class BooksController {

@Get(':id')
getById(@Param('id') id: string): Promise<Book> {
// return Promise.reject();
return this.BooksService.getById(id)
}

Expand Down
2 changes: 2 additions & 0 deletions 010-nestjs-validation/src/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { LoggingInterceptor } from './logging.interceptor';
export { ResponseInterceptor } from './response.interceptor';
19 changes: 19 additions & 0 deletions 010-nestjs-validation/src/interceptors/logging.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, HttpException, HttpStatus } from '@nestjs/common';
import { HttpArgumentsHost } from '@nestjs/common/interfaces';
import { Observable, throwError } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
console.log('Before...');
const httpCtx: HttpArgumentsHost = context.switchToHttp();
const req = httpCtx.getRequest();
console.log('%clogging.interceptor.ts line:13 req', 'color: #007acc;', req.params);
const now: number = Date.now();

return next
.handle()
.pipe(tap(() => console.log(`After... ${Date.now() - now}ms`)));
}
}
14 changes: 14 additions & 0 deletions 010-nestjs-validation/src/interceptors/response.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, BadGatewayException } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';

@Injectable()
export class ResponseInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map(data => ({ status: "success", data })),
catchError((err: any) => {
return throwError(new BadGatewayException({data: err || 'error', status: "fail"}));
}));
}
}
7 changes: 5 additions & 2 deletions 010-nestjs-validation/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { LoggingInterceptor } from './interceptors/logging.interceptor';
import { ResponseInterceptor } from './interceptors/response.interceptor';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(new LoggingInterceptor(), new ResponseInterceptor())
await app.listen(3000);
}
bootstrap();

0 comments on commit e67c9e5

Please sign in to comment.