I'm currently exploring ways to streamline the process of specifying @ApiProperty() for each DTO.
I've heard about a method involving the creation of a nest-cli.json
file, where if you define Promise<DTO>
in your controller within nest-swagger, it will automatically generate the output DTO from the route.
The configuration typically appears like this:
nest-cli.json
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions":: {
"plugins": [
{
"name": "@nestjs/swagger",
"options": {
"introspectComments": true
}
}
]
}
}
controller.ts
@Get()
async getMonitors (): Promise<OutputMonitorsDto> { // <-- This is my outputDto
return this.monitorsService.getMonitors()
}
In Swagger, the result would resemble something similar to this: https://i.sstatic.net/LEUez.png
However, I am wondering if there is a way to configure NestJs to achieve the same effect with inputDTO without the need to manually add @ApiProperty
to each DTO?
For instance:
ExampleDto.ts
export class GetListUsersDto {
@ApiProperty()
@IsString()
name: string
@ApiProperty()
@IsString()
email: string
@ApiProperty()
@IsString()
publicApiKey: string
@ApiProperty()
@IsBoolean()
isAdmin: boolean
@ApiProperty()
@IsBoolean()
isDesigner: boolean
@ApiProperty()
@IsBoolean()
isEditor: boolean
@ApiProperty()
@IsBoolean()
isEnabled: boolean
@ApiProperty()
@IsString()
boughtProduct: string
}
With this setup, only tagging with @ApiProperty would display the structured input on Swagger, similar to the example provided above.