这是 Controller :
@Controller('products')
@UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }))
export class ProductController {
constructor(
private readonly productService: ProductService,
) {}
@Get()
public async all(): Promise<ProductDto[]> {
return this.productService.all();
}
@Post()
@ApiResponse({type: ProductDto})
public async create(@Body() createProductDto: CreateProductDto): Promise<ProductDto> {
return this.productService.create(createProductDto);
}
}
这是服务:
@Injectable()
export class ProductService {
constructor(
@InjectRepository(ProductEntity)
private readonly productRepository: Repository<ProductEntity>,
) {}
public async all(): Promise<ProductDto[]> {
return this.productRepository.find().then(products => products.map(p => new ProductDto().deserialize(p)));
}
public async create(createProductDto: CreateProductDto): Promise<ProductDto> {
const productEntity = new ProductEntity();
productEntity.image = createProductDto.image;
productEntity.title = createProductDto.title;
return productEntity.save().then(pe => new ProductDto().deserialize(pe));
}
}
这是 ProductDto:
export class ProductDto implements Readonly<ProductDto>, IDeserializable<ProductDto, ProductEntity> {
@ApiProperty({name: 'id'})
@IsNumber()
private _id: number;
@ApiProperty({name: 'likes'})
@IsNumber()
private _likes: number;
@ApiProperty({name: 'title'})
@IsString()
private _title: string;
@ApiProperty({name: 'image'})
@IsString()
private _image: string;
public set id(val: number) {
this._id = val;
}
public get id(): number {
return this._id;
}
public set title(val: string) {
this._title = val;
}
public get title(): string {
return this._title;
}
public set image(val: string) {
this._image = val;
}
public get image(): string {
return this._image;
}
public set likes(val: number) {
this._likes = val;
}
public get likes(): number {
return this._likes;
}
public deserialize(data: ProductEntity): ProductDto {
this.image = data.image;
this.title = data.title;
this.likes = data.likes;
this.id = data.id;
return this;
}
}
响应中包含下划线,我忘记如何删除它们。
当我创建新产品时,我得到:
{
"_image": "https://www.images.com/763267382.jpg",
"_title": "porsche",
"_likes": 0,
"_id": 26
}
但我想要这个:
{
"image": "https://www.images.com/763267382.jpg",
"title": "porsche",
"likes": 0,
"id": 26
}
我该怎么做?
编辑:
我可以编写一个自定义管道来修剪它们,但除非我犯了很大的错误,否则我不记得有内置的修剪或其他东西。
最佳答案
您可以通过将 @UseInterceptors(ClassSerializerInterceptor)
添加到 Controller 来启用 ClassSerializerInterceptor
,然后使用 class- 中的
包在序列化期间更改名称。@Expose
装饰器Transformer
它是这样的:
@Controller('products')
@UseInterceptors(ClassSerializerInterceptor)
@UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }))
export class ProductController {
...
}
和ProductDto
:
import { Expose } from 'class-transformer';
export class ProductDto implements Readonly<ProductDto>, IDeserializable<ProductDto, ProductEntity> {
@Expose({ name: 'id' })
@ApiProperty({name: 'id'})
@IsNumber()
private _id: number;
@Expose({ name: 'likes' })
@ApiProperty({name: 'likes'})
@IsNumber()
private _likes: number;
@Expose({ name: 'title' })
@ApiProperty({name: 'title'})
@IsString()
private _title: string;
@Expose({ name: 'image' })
@ApiProperty({name: 'image'})
@IsString()
private _image: string;
...
您可以找到更多信息here .
关于typescript - 从响应中删除下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71073680/