Skip to content

Instantly share code, notes, and snippets.

@abigmiu
Created December 23, 2022 11:49
Show Gist options
  • Save abigmiu/89f0b7890ce2d98509d158b822a20619 to your computer and use it in GitHub Desktop.
Save abigmiu/89f0b7890ce2d98509d158b822a20619 to your computer and use it in GitHub Desktop.
// 在 swagger 中定义 response 需要新建一个 class。 但是从数据库里面查询出来的数据有些不需要返回。所以可以通过`class-transform` 进行清除
// response class
import { ConsoleLogger } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';
import { Exclude, Expose, Transform } from 'class-transformer';
@Exclude()
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment