我定义了一种在我的控制器中获取定单结果的路线。
@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-module生成的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;
}
答案 0 :(得分:0)
将 @ApiTags('ports')
添加到控制器。下面是代码
控制器
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import { PaginationQuery } from './pagination-query.dto';
import {
ApiBearerAuth,
ApiConsumes,
ApiExtension,
ApiHeader,
ApiOperation,
ApiQuery,
ApiResponse,
ApiSecurity,
ApiTags
} from '@nestjs/swagger'
@ApiTags('ports')
@Controller('port')
export class PortController {
@Get()
findAll(@Query() paginationQuery: PaginationQuery) {}
}
分页查询
import { ApiProperty } from '@nestjs/swagger';
export enum LettersEnum {
A = 'A',
B = 'B',
C = 'C'
}
export class PaginationQuery {
@ApiProperty({
minimum: 0,
maximum: 10000,
title: 'Page',
exclusiveMaximum: true,
exclusiveMinimum: true,
format: 'int32',
default: 0
})
page: number;
@ApiProperty({
name: '_sortBy'
})
sortBy: string[];
@ApiProperty()
limit: number;
@ApiProperty({
enum: LettersEnum,
enumName: 'LettersEnum'
})
enum: LettersEnum;
@ApiProperty({
enum: LettersEnum,
enumName: 'LettersEnum',
isArray: true
})
enumArr: LettersEnum;
@ApiProperty()
beforeDate: Date;
@ApiProperty({
type: 'object',
additionalProperties: true
})
filter: Record<string, any>;
static _OPENAPI_METADATA_FACTORY() {
return {
sortBy: { type: () => [String] }
};
}
}