Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {INestApplication, ValidationPipe} from "@nestjs/common";
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { CommonResponseInterceptor } from '@/common/interceptor';

export function nestConfig(app: INestApplication) {
app.useGlobalPipes(
new ValidationPipe({
transform: true,
whitelist: true,
}),
);
}
app.useGlobalPipes(
new ValidationPipe({
transform: true,
whitelist: true,
}),
);

app.useGlobalInterceptors(new CommonResponseInterceptor());
}
1 change: 1 addition & 0 deletions src/common/interceptor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './response.interceptor';
34 changes: 34 additions & 0 deletions src/common/interceptor/response.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
HttpStatus,
} from '@nestjs/common';
import { catchError, map, Observable, of } from 'rxjs';

@Injectable()
export class CommonResponseInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map((data) => {
const statusCode: HttpStatus = context
.switchToHttp()
.getResponse().statusCode;
return {
success: true,
statusCode: statusCode,
data: data,
};
}),
catchError((err) => {
return of({
success: false,
statusCode: err.statusCode,
errorCode: err.errorCode,
errorMessage: err.message,
});
}),
);
}
}