Kas*_*tin 5 swagger typescript openapi typeorm nestjs
我有一个实体(图像),它具有如下计算属性(图像路径):
@Entity()
export class Image extends BaseEntity {
@PrimaryGeneratedColumn()
@IsInt()
id: number
@Column({ type: 'varchar', length: 255, unique: true })
@IsString()
@IsNotEmpty()
uid: string
protected mainPath: string
@AfterLoad()
@AfterInsert()
@AfterUpdate()
generateMainPath(): void {
this.mainPath = Math.floor(this.id / 10000) + '/' + Math.floor(this.id / 100) + '/' + this.uid
}
}
Run Code Online (Sandbox Code Playgroud)
我使用mainPath在后端文件系统中存储图像,并将其发送到前端构建img src路径。mainPath成功生成并发送给客户端。
问题在于前端使用 swagger (nswag openapi2tsclient) 生成的基于 openApi 模式构建的打字稿文件,由 Nest.JS 生成。生成的文件中没有接口mainPath属性。IImage是否可以以某种方式声明它,以便在客户端上定义它并填充 ajax 响应提供的值?
PS1。尝试装饰mainPath-@ApiProperty({ type: String })它没有帮助。
PS2。尝试使用@Column({ type: virtual }),但后端给出警告,MySQL不支持该类型。
PS3。它似乎mainPath应该在前端声明为一些通常的未计算属性,因为它将被来自后端的数据预先填充(它不会在前端计算)。
PS4。可能的方法似乎是对数据库和ExtendedImage类使用裸模型,mainPath如果未提供,则在构造函数中计算属性。这是最佳实践吗?
我能够通过使用一些装饰器来获得我想要的东西class-validator。我不知道为什么它有效:
@Entity()
export class Image extends BaseEntity {
@PrimaryGeneratedColumn()
@IsInt()
id: number
@Column({ type: 'varchar', length: 255, unique: true })
@IsString()
@IsNotEmpty()
uid: string
@IsString()
@IsOptional()
protected mainPath: string
@AfterLoad()
@AfterInsert()
@AfterUpdate()
generateMainPath(): void {
this.mainPath = Math.floor(this.id / 10000) + '/' + Math.floor(this.id / 100) + '/' + this.uid
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10349 次 |
| 最近记录: |