Crud: Swagger docs for "Adding routes"

Created on 13 Mar 2020  路  5Comments  路  Source: nestjsx/crud

I have this model

class User {
  name: string;
  friends: User[];
}

And I have created a crud controller to manage and get user without friends property.
To get friends of a user, I have added an "adding route" :

  @Get(':id/friends')
  @UseInterceptors(CrudRequestInterceptor)
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

All works fine, but there is not swagger documentation for this route. To generate it, I have to add :

import { SerializeHelper } from '@nestjsx/crud/lib/crud/serialize.helper';
[...]
  @Get(':id/friends')
  @UseInterceptors(CrudRequestInterceptor)
  @ApiOperation({ summary: 'Get friends of a user' })
  @ApiParam({
    name: 'id',
    description: 'The id of the user',
    type: 'string',
  })
  @ApiOkResponse({
    schema: {
      oneOf: [
        { $ref: getSchemaPath(SerializeHelper.createGetManyDto(User, User.name)) },
        {
          type: 'array',
          items: { $ref: getSchemaPath(User) },
        },
      ],
    },
  })
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

Do you think we can simplify this declaration by exporting a helper to generate the GetManyDto reponse ?

Or, by adding a new @GetManyResponse decorator that ony generates the response swagger documentation :

  @Get(':id/friends')
  @UseInterceptors(CrudRequestInterceptor)
  @ApiOperation({ summary: 'Get friends of a user' })
  @ApiParam({
    name: 'id',
    description: 'The id of the user',
    type: 'string',
  })
  @GetManyResponse({type: User})
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

Or, by adding a new @GetMany decorator that generates the swagger documentation and the CrudRequestInterceptor interceptor :

  @Get(':id/friends')
  @GetMany({type: User})
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

Most helpful comment

+1
It would be grateful to create a custom GET route and have already generated swagger documentation with the filters, sorting, selecting, etc. Waiting for the update!

All 5 comments

is there any update about that ?

The lib generates swagger for included routes and for overridden routes.
async getFriends route and this.userService.getFriends are not standard routes.
But the general idea of adding decorators like @GetMany() or any other particular decorator for a specific route is definitely the thing. I was thinking of adding some generic decorators for a route CrudOptions customization.

This would be very very helpful

+1
This would be great to have the swagger doc for custom routes when we used CrudRequest in it !

+1
It would be grateful to create a custom GET route and have already generated swagger documentation with the filters, sorting, selecting, etc. Waiting for the update!

Was this page helpful?
0 / 5 - 0 ratings