我定义了一个路由来在我的控制器中获取订单的分页结果。
@Get()
async find(
@Query(new ValidationPipe({ whitelist: true }))
query: OrderQueryDto & PaginatedQueryDto,
) {
const builder = this.orderRepository
.createQueryBuilder('order')
.select('order.id')
.take(query.take)
.skip(query.skip)
.andWhere('order.status != "deleted"');
if (query.status) {
builder.andWhere('order.status = :status');
}
const [items, count] = await builder.getManyAndCount();
return { items, count };
}
但是我想要一些关于查询参数(类型OrderQueryDto & PaginatedQueryDto
)的文档。我没有找到任何装饰器,它为嵌套 swagger 模块生成的 api 描述创建文档(和测试字段)。
我想我正在寻找类似的东西 @ApiImplicityQueryString({ type: OrderQueryDto & PaginatedQueryDto })
。
我知道有一种方法可以像这样记录它:
@Get()
@ApiImplicitQuery({
name: 'take',
required: false,
type: Number,
})
@ApiImplicitQuery({
name: 'skip',
required: false,
type: Number,
})
// And all the other decorators for the remaining properties on OrderQueryDto [...]
async find(
@Query(new ValidationPipe({ whitelist: true }))
query: OrderQueryDto & PaginatedQueryDto,
) {
const builder = this.orderRepository
.createQueryBuilder('order')
.select('order.id')
.take(query.take)
.skip(query.skip)
.andWhere('order.status != "deleted"');
if (query.status) {
builder.andWhere('order.status = :status');
}
const [items, count] = await builder.getManyAndCount();
return { items, count };
}
顺便说一句,DTO 看起来像这样
import { Transform } from 'class-transformer';
import { IsInt, IsOptional } from 'class-validator';
export class PaginatedQueryDto {
@IsInt()
@IsOptional()
@Transform(value => value && parseInt(value, 10))
take?: number;
@IsInt()
@IsOptional()
@Transform(value => value && parseInt(value, 10))
skip?: number;
}
import { IsInt, IsOptional, IsEnum } from 'class-validator';
import { Transform } from 'class-transformer';
import { OrderStatus } from './order.entity';
export class OrderQueryDto {
@IsInt()
@IsOptional()
@Transform(value => value && parseInt(value, 10))
reseller: number;
@IsInt()
@IsOptional()
@Transform(value => value && parseInt(value, 10))
customer: number;
@IsEnum(OrderStatus)
@IsOptional()
status: OrderStatus;
}