forked from netology-code/ndtnf-homeworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the interceptors [netology-code#10]
- Loading branch information
Showing
5 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
19
010-nestjs-validation/src/interceptors/logging.interceptor.ts
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
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
14
010-nestjs-validation/src/interceptors/response.interceptor.ts
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
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"})); | ||
})); | ||
} | ||
} |
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
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(); |