-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add TransformInterceptor and HttpExceptionFilter
- Loading branch information
Showing
29 changed files
with
459 additions
and
377 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { Permissions, PermissionsMetadataKey } from './permissions.decorator'; | ||
export { CurrentUser } from './current-user.decorator'; | ||
export { Language } from './language.decorator'; | ||
export { GqlCtx } from './gql-ctx.decorator'; | ||
export * from './permissions.decorator'; | ||
export * from './current-user.decorator'; | ||
export * from './language.decorator'; | ||
export * from './gql-ctx.decorator'; |
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,32 @@ | ||
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common'; | ||
|
||
export interface IHttpException { | ||
statusCode: number; | ||
message: string; | ||
lang: string; | ||
} | ||
|
||
@Catch() | ||
export class HttpExceptionFilter implements ExceptionFilter { | ||
catch(exception: unknown, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const res = ctx.getResponse(); | ||
const req = ctx.getRequest(); | ||
|
||
const statusCode = exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR; | ||
const lang = req.language || ''; | ||
|
||
// @ts-ignore | ||
let message = exception?.sqlMessage || exception?.message || 'KERNEL PANIC!!!'; | ||
|
||
if (exception instanceof HttpException) { | ||
message = exception.message; | ||
} | ||
|
||
res.status(statusCode).json({ | ||
statusCode, | ||
message, | ||
lang, | ||
}); | ||
} | ||
} |
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 @@ | ||
export * from './http-exception.filter'; |
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
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 +1,2 @@ | ||
export * from '@leaa/api/src/interceptors/http-cache.interceptor'; | ||
export * from './http-cache.interceptor'; | ||
export * from './transform.interceptor'; |
50 changes: 26 additions & 24 deletions
50
packages/leaa-api/src/interceptors/transform.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 |
---|---|---|
@@ -1,31 +1,33 @@ | ||
import { Injectable, HttpException, NestInterceptor, CallHandler, ExecutionContext, HttpStatus } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { Observable, throwError } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, HttpStatus } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { IResponse, IRequest } from '@leaa/api/src/interfaces'; | ||
|
||
@Injectable() | ||
export class ErrorsInterceptor implements NestInterceptor { | ||
// constructor(private readonly reflector: Reflector) {} | ||
export interface IHttpData<T> { | ||
statusCode: number; | ||
message: string; | ||
data: T; | ||
lang: string; | ||
} | ||
|
||
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> { | ||
const call$ = next.handle(); | ||
@Injectable() | ||
export class TransformInterceptor<T> implements NestInterceptor<T, IHttpData<T>> { | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<IHttpData<T>> { | ||
const ctx = context.switchToHttp(); | ||
const res: IResponse = ctx.getResponse(); | ||
const req: IRequest = ctx.getRequest(); | ||
|
||
return call$.pipe( | ||
catchError((error: any): any => { | ||
if (error instanceof HttpException) { | ||
return Promise.resolve({ | ||
code: error.getStatus(), | ||
message: error.getResponse(), | ||
}); | ||
} | ||
const statusCode = res.statusCode || HttpStatus.OK; | ||
const message = 'Success'; | ||
const lang = req.language || ''; | ||
|
||
if (error.code && error.details) { | ||
return Promise.resolve({ | ||
code: error.code, | ||
message: error.details, | ||
}); | ||
} | ||
}), | ||
return next.handle().pipe( | ||
map((data) => ({ | ||
statusCode, | ||
message, | ||
lang, | ||
data, | ||
})), | ||
); | ||
} | ||
} |
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
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
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
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
43 changes: 43 additions & 0 deletions
43
packages/leaa-api/src/modules/category/category.controller.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,43 @@ | ||
import { Controller, UseGuards, Get, Req, Query } from '@nestjs/common'; | ||
import { Crud, CrudController, ParsedRequest, CrudRequest } from '@nestjsx/crud'; | ||
|
||
import { Permissions } from '@leaa/api/src/decorators'; | ||
// import { curdConfig } from '@leaa/api/src/configs'; | ||
import { CreateCategoryInput, UpdateCategoryInput } from '@leaa/common/src/dtos/category'; | ||
import { JwtGuard, PermissionsGuard } from '@leaa/api/src/guards'; | ||
import { Category } from '@leaa/common/src/entrys'; | ||
import { CategoryService } from './category.service'; | ||
import { QueryBuilder } from 'typeorm'; | ||
|
||
@Crud({ | ||
model: { | ||
type: Category, | ||
}, | ||
query: { | ||
maxLimit: 1000, | ||
alwaysPaginate: true, | ||
}, | ||
routes: { | ||
getManyBase: { decorators: [UseGuards(JwtGuard, PermissionsGuard)] }, | ||
getOneBase: { decorators: [UseGuards(JwtGuard, PermissionsGuard)] }, | ||
createOneBase: { decorators: [UseGuards(JwtGuard, PermissionsGuard), Permissions('category.item-create')] }, | ||
deleteOneBase: { | ||
decorators: [UseGuards(JwtGuard, PermissionsGuard), Permissions('category.item-delete')], | ||
returnDeleted: true, | ||
}, | ||
}, | ||
dto: { | ||
create: CreateCategoryInput, | ||
update: UpdateCategoryInput, | ||
}, | ||
}) | ||
@Controller('/categories') | ||
export class CategoryController implements CrudController<Category> { | ||
constructor(public service: CategoryService) {} | ||
|
||
@Get('tree') | ||
// async tree(@ParsedRequest() req: CrudRequest) { | ||
async tree(@Req() req: CrudRequest, @Query() query: any) { | ||
return this.service.tree(); | ||
} | ||
} |
Oops, something went wrong.