typescript - 从响应中删除下划线

标签 typescript nestjs

这是 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/

相关文章:

sequelize.js - 在 nestjs/sequelize 上的 sequelize-typescript 中定义关系时出错

typescript - TS2339 : Property 'user' does not exist on type 'Request'

node.js - NestJS和TypeORM无法连接我的本地Postgres数据库。声称我的数据库不存在,即使存在

angular - 有什么方法可以防止以 Angular 破坏组件?

javascript - redux-actions Typescript 声明一个 Action

javascript - 使用 Map 与 Record 之间的差异以及何时使用

angular - 巢JS : return a string

javascript - 类型页是 2 个模块错误声明的一部分

javascript - 使用数字枚举强制值验证

javascript - 如何使用 nestfastify 编写来自 nestjs 异常过滤器的响应