nestjs-paginate icon indicating copy to clipboard operation
nestjs-paginate copied to clipboard

Swagger integration

Open 20Koen02 opened this issue 3 years ago • 3 comments

It would be nice to have swagger documentation support for the parameters. Currently it's not possible to change the limit, page or filter on the docs page.

20Koen02 avatar Feb 25 '22 15:02 20Koen02

Crude way I (partially) solved it: Screenshot from 2022-02-25 16-28-24

20Koen02 avatar Feb 25 '22 15:02 20Koen02

true. some helper would be sweet

ppetzold avatar Feb 25 '22 20:02 ppetzold

@20Koen02

I've created a custom decorator as a temporary solution. Works great

import { applyDecorators } from '@nestjs/common';
import { ApiQuery } from '@nestjs/swagger';

export function PaginateQueryOptions() {
  return applyDecorators(
    ApiQuery({ name: 'page', required: false }),
    ApiQuery({ name: 'limit', required: false }),
    ApiQuery({ name: 'search', required: false }),
    ApiQuery({ name: 'sortBy', required: false }),
    ApiQuery({ name: 'filter', required: false }),
  );
}

frankforpresident avatar Apr 28 '22 11:04 frankforpresident

@frankforpresident Starting from your example I extended it in order to give more information and include the searchBy field (found in the code but almost not documented elsewhere):

import { applyDecorators } from '@nestjs/common';
import { ApiQuery } from '@nestjs/swagger';

export function PaginateQueryOptions() {
    return applyDecorators(
        ApiQuery({ name: 'page', required: false, description: "Page number (starting from 1)", example: 1 }),
        ApiQuery({ name: 'limit', required: false, description: "Number of records per page", example: 10 }),
        ApiQuery({ name: 'search', required: false, description: "Multicolumn search term" }),
        ApiQuery({ name: 'searchBy', required: false, description: "Limit columns to which apply 'search' term", isArray: true, type: 'string' }),
        ApiQuery({ name: 'sortBy', required: false, description: "Format: _field_:_direction_ [direction may be ASC or DESC] e.g. id:DESC" }),
    );
}

Concerning filter query, I'd rather add single @ApiQuery decorators to the involved controller, since this information is very specific to the single configuration and FilterOperators enabled. e.g.

  @ApiQuery({ name: "filter.parentId", required: false, description: "Possible values: '$null' or '$eq:_id_' ", example: "$null" })

bestmazzo avatar Oct 06 '22 16:10 bestmazzo

Here is a decorator that also generate a correct response schema, based on above answers

https://gist.github.com/TheToto/f29a8479393846b47961b2c8397a8217

TheToto avatar Jan 18 '23 23:01 TheToto

Here is a decorator that also generate a correct response schema, based on above answers

https://gist.github.com/TheToto/f29a8479393846b47961b2c8397a8217

Good stuff ! Filter spec could be improve somewhat (see examples in https://github.com/ppetzold/nestjs-paginate#filters).

Also happy to accept a PR with swagger helpers ;)

ppetzold avatar Jan 19 '23 10:01 ppetzold

:tada: This issue has been resolved in version 8.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

ppetzold avatar Aug 24 '23 11:08 ppetzold