nestjs swagger 定义 response
John Doenestjs swagger 定义 response
在 swagger 中定义 response 需要新建一个 class。
但是从数据库里面查询出来的数据有些不需要返回。所以可以通过class-transform
进行清除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| export class IBookDetailResponse { constructor(data: Partial<IBookDetailResponse>) { Object.assign(this, data); } @Expose() @ApiProperty({ description: 'id' }) id: number; @Expose() @ApiProperty({ description: '标题' }) title: string; @Expose() @ApiProperty({ description: '封面图' }) cover: string; @Expose() @ApiProperty({ description: '简介' }) intro: string; @Expose() @ApiProperty({ description: '作者名' }) @Transform(({ obj }) => { return obj.user.nickname; }) author: string; @Expose() @ApiProperty({ description: '作者 ID' }) @Transform(({ obj }) => { return obj.user.id; }) authorId: number; @Expose() @ApiProperty({ description: '作者头像' }) @Transform(({ obj }) => { return obj.user.avatar; }) authorAvatar: string;}
|