I define a route to get a paginated result of orders in my controller.
@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 };
}
However I would like to have some documentation about the query params (of type OrderQueryDto & PaginatedQueryDto
).
I did not find out any decorator, that creates documentation (and test-fields) for the api description that is generated by the nest swagger-module.
I think I am searching for something like @ApiImplicityQueryString({ type: OrderQueryDto & PaginatedQueryDto })
.
I know there is a way to document it like so:
@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 };
}
BTW, the DTOs look like this
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;
}