从响应中删除下划线

Remove underscores from a response

这是控制器:

@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
}

我该怎么做?

编辑:

我可以写一个自定义管道 trim 它们,但除非我弄错了,否则有一个内置的 trim 或其他东西,我不记得了。

您可以通过将 @UseInterceptors(ClassSerializerInterceptor) 添加到控制器来启用 ClassSerializerInterceptor,然后使用 class-transformer 包中的 @Expose 装饰器在序列化期间更改名称。

这是它的样子:

@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