feat:自定义全局拦截器、过滤器、验证管道

This commit is contained in:
2023-12-08 13:31:53 +08:00
parent 781962dce0
commit f4b38483d6
15 changed files with 204 additions and 128 deletions
@@ -9,15 +9,11 @@ import {
Post,
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { CreateCategoryDto, QueryCategoryDto, UpdateCategoryDto } from '@/modules/content/dtos';
import { CategoryService } from '@/modules/content/services';
import { AppIntercepter } from '@/modules/core/providers';
@UseInterceptors(AppIntercepter)
@Controller('categories')
export class CategoryController {
constructor(protected service: CategoryService) {}
@@ -31,15 +27,7 @@ export class CategoryController {
@Get()
@SerializeOptions({ groups: ['category-list'] })
async list(
@Query(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
}),
)
@Query()
options: QueryCategoryDto,
) {
return this.service.paginate(options);
@@ -54,16 +42,7 @@ export class CategoryController {
@Post()
@SerializeOptions({ groups: ['category-detail'] })
async create(
@Body(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['create'],
}),
)
@Body()
data: CreateCategoryDto,
) {
return this.service.create(data);
@@ -72,16 +51,7 @@ export class CategoryController {
@Patch()
@SerializeOptions({ groups: ['category-detail'] })
async update(
@Body(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['update'],
}),
)
@Body()
data: UpdateCategoryDto,
) {
return this.service.update(data);
@@ -8,15 +8,11 @@ import {
Post,
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { CreateCommentDto, QueryCommentDto, QueryCommentTreeDto } from '@/modules/content/dtos';
import { CommentService } from '@/modules/content/services';
import { AppIntercepter } from '@/modules/core/providers';
@UseInterceptors(AppIntercepter)
@Controller('comments')
export class CommentController {
constructor(protected service: CommentService) {}
@@ -24,15 +20,7 @@ export class CommentController {
@Get('tree')
@SerializeOptions({ groups: ['comment-tree'] })
async tree(
@Query(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
}),
)
@Query()
query: QueryCommentTreeDto,
) {
return this.service.findTrees(query);
@@ -41,13 +29,7 @@ export class CommentController {
@Get()
@SerializeOptions({ groups: ['comment-list'] })
async list(
@Query(
new ValidationPipe({
transform: true,
forbidUnknownValues: true,
validationError: { target: false },
}),
)
@Query()
query: QueryCommentDto,
) {
return this.service.paginate(query);
@@ -56,15 +38,7 @@ export class CommentController {
@Post()
@SerializeOptions({ groups: ['comment-detail'] })
async store(
@Body(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
}),
)
@Body()
data: CreateCommentDto,
) {
return this.service.create(data);
@@ -9,19 +9,16 @@ import {
Post,
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { CreatePostDto, QueryPostDto, UpdatePostDto } from '@/modules/content/dtos';
import { PostService } from '@/modules/content/services';
import { AppIntercepter } from '@/modules/core/providers';
/**
* 文章控制器
* 负责处理与文章相关的请求,如获取文章列表、创建新文章等。
*/
@UseInterceptors(AppIntercepter)
@Controller('posts')
export class PostController {
constructor(private postService: PostService) {}
@@ -29,15 +26,7 @@ export class PostController {
@Get()
@SerializeOptions({ groups: ['post-list'] })
async list(
@Query(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
}),
)
@Query()
options: QueryPostDto,
) {
return this.postService.paginate(options);
@@ -52,15 +41,7 @@ export class PostController {
@Post()
@SerializeOptions({ groups: ['post-detail'] })
async store(
@Body(
new ValidationPipe({
transform: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['create'],
}),
)
@Body()
data: CreatePostDto,
) {
return this.postService.create(data);
@@ -69,15 +50,7 @@ export class PostController {
@Patch()
@SerializeOptions({ groups: ['post-detail'] })
async update(
@Body(
new ValidationPipe({
transform: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['update'],
}),
)
@Body()
data: UpdatePostDto,
) {
return this.postService.update(data);
@@ -9,15 +9,11 @@ import {
Post,
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { CreateTagDto, QueryCategoryDto, UpdateTagDto } from '@/modules/content/dtos';
import { TagService } from '@/modules/content/services';
import { AppIntercepter } from '@/modules/core/providers';
@UseInterceptors(AppIntercepter)
@Controller('tags')
export class TagController {
constructor(protected service: TagService) {}
@@ -25,15 +21,7 @@ export class TagController {
@Get()
@SerializeOptions({})
async list(
@Query(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
}),
)
@Query()
options: QueryCategoryDto,
) {
return this.service.paginate(options);
@@ -51,16 +39,7 @@ export class TagController {
@Post()
@SerializeOptions({})
async store(
@Body(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['create'],
}),
)
@Body()
data: CreateTagDto,
) {
return this.service.create(data);
@@ -69,16 +48,7 @@ export class TagController {
@Patch()
@SerializeOptions({})
async update(
@Body(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['update'],
}),
)
@Body()
data: UpdateTagDto,
) {
return this.service.update(data);