Skip to content

Commit

Permalink
feat: unified return body and error interception
Browse files Browse the repository at this point in the history
  • Loading branch information
hildxd committed Jan 25, 2024
1 parent dc0e280 commit b7fa402
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 64 deletions.
22 changes: 0 additions & 22 deletions apps/api/src/app.controller.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions apps/api/src/app.controller.ts

This file was deleted.

14 changes: 0 additions & 14 deletions apps/api/src/app.module.ts

This file was deleted.

11 changes: 0 additions & 11 deletions apps/api/src/app.service.ts

This file was deleted.

10 changes: 10 additions & 0 deletions apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { GlobalModule } from '../global/global.mudule';
import { UserModule } from '../user/user.module';
import { AuthModule } from '../auth/auth.module';
import { CourseModule } from '../course/course.module';

@Module({
imports: [GlobalModule, UserModule, AuthModule, CourseModule],
})
export class AppModule {}
24 changes: 24 additions & 0 deletions apps/api/src/app/exception.fliter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
} from '@nestjs/common';

import { Request, Response } from 'express';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = exception.getStatus();
const message = exception.getResponse() as any;
response.status(status).json({
code: status,
data: null,
message,
});
}
}
23 changes: 23 additions & 0 deletions apps/api/src/app/transform.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Injectable,
NestInterceptor,
CallHandler,
ExecutionContext,
} from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map((data) => {
return {
data,
code: 200,
message: '',
};
}),
);
}
}
1 change: 1 addition & 0 deletions apps/api/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Get,
UseGuards,
Request,
HttpException,
} from '@nestjs/common';
import { SignDto } from './model/auth.dto';
import { AuthService } from './auth.service';
Expand Down
6 changes: 5 additions & 1 deletion apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { AppModule } from './app/app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { TransformInterceptor } from './app/transform.interceptor';
import { HttpExceptionFilter } from './app/exception.fliter';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand All @@ -13,6 +15,8 @@ async function bootstrap() {
});

app.useGlobalPipes(new ValidationPipe());
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new TransformInterceptor());
const config = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
Expand Down
31 changes: 27 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b7fa402

Please sign in to comment.