nestjs如何使用@ nestjs / swagger生成有关Passport策略路线的文档

时间:2019-08-12 07:36:28

标签: nestjs

我正在制作@nestjs/swagger来生成api文档。但是,如何为经过身份验证的路由生成文档?

嵌套版本

λ nest i
NodeJS Version : v10.16.0
[Nest Information]
platform-express version : 6.0.0
passport version         : 6.1.0
swagger version          : 3.1.0
common version           : 6.0.0
core version             : 6.0.0
jwt version              : 6.1.1

这是一条正常的路线,我可以使用“ @ApiImplicitBody”制作文档:

  @Delete()
  @ApiImplicitBody({
    name: 'id',
    required: true,
    type: String,
  })
  @ApiOkResponse({
    description: 'successfully deleted',
  })
  delete(@Body('id') typeId) {
    return this.typesService.delete(typeId);
  }

此路线需要authentication,如何记录这种类型的路线?

  @UseGuards(AuthGuard('local'))
  @Post('login')
  @ApiOkResponse({
    description: 'result Token',
  })
  async login(@Request() req) {
    return this.authService.login(req.user);
  }

我看着Swagger documentation并尝试了'@ nestjs / swagger'软件包中的一些api,但这没用。

2 个答案:

答案 0 :(得分:0)

您可以使用装饰器<script type="text/javascript"> $('div.options').hide(); $('div.selected').hide(); $('div.col-sm-2').hide(); $('div.col-sm-2').fadeIn(5000); $('.fa-mobile').click(function() { let self = $(this); $('div.options').fadeOut('fast'); $(this).animate({ backgroundColor: "#4BB7E8" }, 1400); $(this).next('.options').slideDown(1000); $(this).next('.options').click(function(event) { let selectedText = event.target.id; console.log(selectedText); $(this).slideUp("slow", function() { $(this).next('.selected').text(selectedText).fadeIn(3000); $('.agent').effect( "shake", { direction: "up", times: 2, distance: 10}, 1000 ); }); }); }); </script> here in the docs获取经过身份验证的路由,以显示在您的swagger文件中。

答案 1 :(得分:0)

这达到了我想要的结果:

路线:

import { UserLoginDto } from './dto/user-login.dto';

  @UseGuards(AuthGuard('local'))
  @Post('login')
  @ApiImplicitBody({ name: '', type: UserLoginDto, })
  @ApiOkResponse({ description: 'result Token' })
  async login(@Request() req) {
    return this.authService.login(req.user);
  }

UserLoginDto:

import { IsNotEmpty, IsString } from 'class-validator';
import { ApiModelProperty } from '@nestjs/swagger';

export class UserLoginDto {
  @IsString()
  @IsNotEmpty()
  @ApiModelProperty({ example: 'ajanuw', description: '账号' })
  readonly username: string;

  @IsString()
  @IsNotEmpty()
  @ApiModelProperty({
    example: '123456',
    description: '密码',
  })
  readonly password: string;
}