Nestjs / Swagger:如何记录JWT有效负载?

时间:2018-11-23 12:52:45

标签: swagger nestjs

我有一个控制器的login()方法,如果用户尝试使用带有200 OK有效负载的JWT令牌返回Session或带有带有AcceptTerms有效负载的JWT令牌的451 Unavailable due to legal reasons返回登录需要同意条款和条件的新版本。

@Nest.Post("login")
@ApiOperation({ title: 'Log in the user given email and password',  operationId: 'userLogin'})
@Nest.UseFilters(UnavailableLegalReasonsExceptionFilter)
@ApiResponse({ status: HttpStatus.OK, description: "User was properly logged in", type: JwtToken})
@ApiResponse({ status: 451, description: "User haven't confirmed the latest version of Terms & Conditions", type: JwtToken})
async login(@Nest.Body() body: DTOs.LoginDto): Promise<JwtToken> {
    ...
}

现在,我想用Swagger记录下来。 JwtToken定义为:

export class JwtToken {
    @ApiModelProperty({enum: TOKEN})
    type: TOKEN;

    @ApiModelProperty({description: "Number of seconds"})
    expiresIn: number;

    @ApiModelProperty({ description: "Token", example: "eyJhb ... wWFQ"})
    accessToken: string;
}

TOKEN

export enum TOKEN {
    AcceptTerms = "AcceptTerms",
    Session = "Session"
}

但是写login()仅返回JwtToken是令人困惑的,例如在200的情况下typeSession而在{{1}情况下}是451。有效载荷也大不相同:

AcceptTerms

export class JwtPayload { @ApiModelProperty({enum: TOKEN, description: "Token type"}) type: TOKEN; @ApiModelProperty() issuedAt: number; } export class LoginJwtPayload extends JwtPayload { @ApiModelProperty() name: string; @ApiModelProperty({description: "T&C version accepted"}) tncAccepted: number; } export class AcceptTermsJwtPayload extends JwtPayload { @ApiModelProperty({format: "email"}) email: string; } 的令牌使用200有效负载,而LoginJwtPayload的令牌使用451

如何使用Nest的SwaggerModule对其进行记录?

0 个答案:

没有答案