1

i have an endpoint which returns a list with pagination. i'm trying to use nestjs/swagger plugin to automatically collect endpoints and DTOs and etc. plugin works fine with all endpoints except the one which the return type is typeorm pagination.

  findAll(
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page : number,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit : number,
    @Query('domain') domain: string,
  ): Promise<Pagination<webContent>> {
    return this.webService.findAll({ page, limit }, domain);
  }

webcontent is :

export default class webContent {
  @PrimaryColumn()
  domain: string;

  @ManyToMany(() => WebCategory)
  @JoinTable()
  categories: WebCategory[];
}

any solutions?

ShayanJZ
  • 61
  • 4

1 Answers1

2

Play with the following (eg, you can add a constructor function to map properties, or some class-validator fn, like @IsInt, @Min, etc.):

import { Type, applyDecorators } from '@nestjs/common';
import { ApiExtraModels, ApiOkResponse, ApiProperty, getSchemaPath } from '@nestjs/swagger';

import { PaginationResponseDto } from '@shared';

export class PaginationModel<T> implements PaginationResponseDto<T> {
    @ApiProperty({ isArray: true })
    public readonly data: T[];

    @ApiProperty({ example: 1 })
    public readonly total: number;

    @ApiProperty({ example: 1 })
    public readonly page: number;

    @ApiProperty({ example: 1 })
    public readonly pages: number;
}

And a decorator for query:

import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';

export class PaginationQuery {
    @ApiPropertyOptional({
        default: 1,
    })
    @Type(() => Number)
    public readonly page?: number;

    @ApiPropertyOptional({
        default: 20,
    })
    @Type(() => Number)
    public readonly take?: number;
}

A controller decorators for response:

export const ApiPaginatedResponse = <TModel extends Type<any>>(model: TModel) => {
    return applyDecorators(
        ApiExtraModels(PaginationModel),
        ApiQuery({ type: () => PaginationQuery }),
        ApiOkResponse({
            description: 'Successfully received model list',
            schema: {
                allOf: [
                    { $ref: getSchemaPath(PaginationModel) },
                    {
                        properties: {
                            data: {
                                type: 'array',
                                items: { $ref: getSchemaPath(model) },
                            },
                        },
                    },
                ],
            },
        })
    );
};

Example usage:

@Get()
@ApiOperation({ summary: 'find paginated users' })
@ApiPaginatedResponse(UserModel)
// public async findUsers(...otherLogic
zemil
  • 3,235
  • 2
  • 24
  • 33