Files
seclusion/plop/templates/api/dto.hbs
charilezhou 695f34bc3d refactor(plop): QueryDto 始终生成并在 controller 中使用
- dto.hbs: 移除 hasQueryDto 条件,始终生成 QueryDto 类
- controller.hbs: 始终导入使用 QueryDto 而非 PaginationQueryDto
- hasQueryDto 现只控制是否提取查询字段,不影响类型声明

便于后续扩展查询参数,无需修改 controller 导入

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-19 20:39:33 +08:00

167 lines
4.7 KiB
Handlebars

import { ApiProperty, ApiPropertyOptional{{#if needsDetailDto}}, PartialType{{/if}} } from '@nestjs/swagger';
{{#if (hasValidation fields)}}
import { {{validationImports fields}}, IsOptional{{#if hasManyToMany}}, IsArray, IsString{{/if}} } from 'class-validator';
{{else}}
import { IsOptional{{#if hasManyToMany}}, IsArray, IsString{{/if}} } from 'class-validator';
{{/if}}
{{#if (or (hasTransform fields) hasManyToMany)}}
import { Type } from 'class-transformer';
{{/if}}
import type {
Create{{pascalCase name}}Dto as ICreate{{pascalCase name}}Dto,
Update{{pascalCase name}}Dto as IUpdate{{pascalCase name}}Dto,
{{pascalCase name}}Response,
} from '@seclusion/shared';
import { createPaginatedResponseDto } from '@/common/crud/dto/paginated-response.dto';
import { PaginationQueryDto } from '@/common/crud/dto/pagination.dto';
/** 创建{{chineseName}}请求 DTO */
export class Create{{pascalCase name}}Dto implements ICreate{{pascalCase name}}Dto {
{{#each createFields}}
{{#if nullable}}
@ApiPropertyOptional({ example: {{{formattedExample this}}}, description: '{{label}}' })
{{else}}
@ApiProperty({ example: {{{formattedExample this}}}, description: '{{label}}' })
{{/if}}
{{#each (validationDecorators this)}}
{{{this}}}
{{/each}}
{{#if nullable}}
@IsOptional()
{{/if}}
{{name}}{{#if nullable}}?{{/if}}: {{tsType this}};
{{/each}}
}
/** 更新{{chineseName}}请求 DTO */
export class Update{{pascalCase name}}Dto implements IUpdate{{pascalCase name}}Dto {
{{#each updateFields}}
@ApiPropertyOptional({ example: {{{formattedExample this}}}, description: '{{label}}' })
{{#each (validationDecorators this)}}
{{{this}}}
{{/each}}
@IsOptional()
{{name}}?: {{tsType this}};
{{/each}}
}
{{#if hasRelations}}
{{#each relations}}
/** {{model}}简要信息 */
export class {{model}}BriefDto {
@ApiProperty({ example: 'clxxx123', description: '{{model}} ID' })
id: string;
{{#each selectFields}}
{{#unless (eq this 'id')}}
@ApiProperty({ description: '{{this}}' })
{{this}}: string;
{{/unless}}
{{/each}}
}
{{/each}}
{{/if}}
{{#if hasManyToMany}}
{{#each manyToMany}}
/** {{targetModel}}简要信息 */
export class {{targetModel}}BriefDto {
@ApiProperty({ example: 'clxxx123', description: '{{targetModel}} ID' })
id: string;
{{#each selectFields}}
{{#unless (eq this 'id')}}
@ApiProperty({ description: '{{this}}' })
{{this}}?: string;
{{/unless}}
{{/each}}
}
/** 分配{{name}} DTO */
export class Assign{{pascalCase name}}Dto {
@ApiProperty({ description: '{{targetModel}} ID 列表', type: [String] })
@IsArray()
@IsString({ each: true })
@Type(() => String)
{{target}}Ids: string[];
}
{{/each}}
{{/if}}
/** {{chineseName}}响应 DTO */
export class {{pascalCase name}}ResponseDto implements {{pascalCase name}}Response {
@ApiProperty({ example: 'clxxx123', description: '{{chineseName}} ID' })
id: string;
{{#each responseFields}}
{{#if nullable}}
@ApiProperty({ example: {{{formattedExample this}}}, description: '{{label}}', nullable: true })
{{name}}: {{tsResponseType this}} | null;
{{else}}
@ApiProperty({ example: {{{formattedExample this}}}, description: '{{label}}' })
{{name}}: {{tsResponseType this}};
{{/if}}
{{/each}}
{{#each relations}}
{{#if includeInList}}
@ApiPropertyOptional({ type: {{model}}BriefDto, description: '{{name}}信息' })
{{name}}?: {{model}}BriefDto;
{{/if}}
{{/each}}
@ApiProperty({ example: '2026-01-16T10:00:00.000Z', description: '创建时间' })
createdAt: string;
@ApiProperty({ example: '2026-01-16T10:00:00.000Z', description: '更新时间' })
updatedAt: string;
{{#if softDelete}}
@ApiPropertyOptional({ example: '2026-01-16T10:00:00.000Z', description: '删除时间', nullable: true })
deletedAt?: string | null;
{{/if}}
}
{{#if needsDetailDto}}
/** {{chineseName}}详情响应 DTO */
export class {{pascalCase name}}DetailResponseDto extends {{pascalCase name}}ResponseDto {
{{#each relations}}
{{#unless includeInList}}
@ApiPropertyOptional({ type: {{model}}BriefDto, description: '{{name}}信息' })
{{name}}?: {{model}}BriefDto;
{{/unless}}
{{/each}}
{{#each manyToMany}}
@ApiProperty({ type: [{{targetModel}}BriefDto], description: '{{name}}列表' })
{{name}}: {{targetModel}}BriefDto[];
{{/each}}
{{#each countRelations}}
@ApiProperty({ example: 0, description: '{{this}}数量' })
{{this}}Count: number;
{{/each}}
}
{{/if}}
/** 分页{{chineseName}}响应 DTO */
export class Paginated{{pascalCase name}}ResponseDto extends createPaginatedResponseDto(
{{pascalCase name}}ResponseDto,
) {}
/** {{chineseName}}查询 DTO */
export class {{pascalCase name}}QueryDto extends PaginationQueryDto {
{{#each queryFields}}
@ApiPropertyOptional({ description: '按{{label}}筛选' })
@IsOptional()
{{name}}?: string;
{{/each}}
}