fix: review import templates #33

Merged
blas merged 11 commits from feat/review-imports into main 2026-03-24 15:48:23 +00:00
3 changed files with 15 additions and 10 deletions
Showing only changes of commit e008144813 - Show all commits

View File

@@ -44,7 +44,7 @@ export function generateCleanArchitecture(
if (rawProperties[k].$ref) { if (rawProperties[k].$ref) {
tsType = rawProperties[k].$ref.split('/').pop() || 'unknown'; tsType = rawProperties[k].$ref.split('/').pop() || 'unknown';
} else if (rawProperties[k].type === 'array' && rawProperties[k].items?.$ref) { } else if (rawProperties[k].type === 'array' && rawProperties[k].items?.$ref) {
tsType = `Array<${rawProperties[k].items.$ref.split('/').pop()}>`; tsType = `${rawProperties[k].items.$ref.split('/').pop()}[]`;
} }
return { return {
name: k, name: k,
@@ -163,7 +163,7 @@ export function generateCleanArchitecture(
returnBaseType = returnType; returnBaseType = returnType;
} else if (responseSchema.type === 'array' && responseSchema.items?.$ref) { } else if (responseSchema.type === 'array' && responseSchema.items?.$ref) {
returnBaseType = responseSchema.items.$ref.split('/').pop() || 'unknown'; returnBaseType = responseSchema.items.$ref.split('/').pop() || 'unknown';
returnType = `Array<${returnBaseType}>`; returnType = `${returnBaseType}[]`;
isListContainer = true; isListContainer = true;
} }
} }
@@ -203,10 +203,10 @@ export function generateCleanArchitecture(
const imports: { classname: string; classFilename: string; classVarName: string }[] = []; const imports: { classname: string; classFilename: string; classVarName: string }[] = [];
Object.keys(schemas).forEach((s) => { Object.keys(schemas).forEach((s) => {
const usedAsReturn = tagsMap[tag].some( const usedAsReturn = tagsMap[tag].some(
(op) => op.returnType === s || op.returnType === `Array<${s}>` (op) => op.returnType === s || op.returnType === `${s}[]`
); );
const usedAsParam = tagsMap[tag].some((op) => const usedAsParam = tagsMap[tag].some((op) =>
op.allParams.some((p) => p.dataType === s || p.dataType === `Array<${s}>`) op.allParams.some((p) => p.dataType === s || p.dataType === `${s}[]`)
); );
if (usedAsReturn || usedAsParam) { if (usedAsReturn || usedAsParam) {
imports.push({ imports.push({

View File

@@ -61,7 +61,7 @@ export function organizeFiles(tempDir: string, outputDir: string): void {
logSuccess(`${filesMoved} DTOs movidos correctamente`); logSuccess(`${filesMoved} DTOs movidos correctamente`);
} }
/** Post-procesa los DTOs generados añadiendo los imports de tipos referenciados. */ /** Post-procesa los DTOs generados añadiendo imports y normalizando Array<T> → T[]. */
export function addDtoImports(outputDir: string): void { export function addDtoImports(outputDir: string): void {
logStep('Añadiendo imports a los DTOs generados...'); logStep('Añadiendo imports a los DTOs generados...');
@@ -85,11 +85,15 @@ export function addDtoImports(outputDir: string): void {
files.forEach((file) => { files.forEach((file) => {
const filePath = path.join(dtosDir, file); const filePath = path.join(dtosDir, file);
let content = fs.readFileSync(filePath, 'utf8'); const originalContent = fs.readFileSync(filePath, 'utf8');
let content = originalContent;
const selfMatch = content.match(/export interface (\w+)/); const selfMatch = content.match(/export interface (\w+)/);
const selfName = selfMatch ? selfMatch[1] : ''; const selfName = selfMatch ? selfMatch[1] : '';
// Normalize Array<T> → T[] (openapi-generator-cli always outputs Array<T>)
content = content.replace(/Array<(\w+)>/g, '$1[]');
// Find all Dto type references in the file body (excluding the interface name itself) // Find all Dto type references in the file body (excluding the interface name itself)
const references = new Set<string>(); const references = new Set<string>();
const typeRegex = /\b(\w+Dto)\b/g; const typeRegex = /\b(\w+Dto)\b/g;
@@ -100,8 +104,6 @@ export function addDtoImports(outputDir: string): void {
} }
} }
if (references.size === 0) return;
// Build import lines for each referenced type that exists in the dtoMap // Build import lines for each referenced type that exists in the dtoMap
const imports: string[] = []; const imports: string[] = [];
references.forEach((ref) => { references.forEach((ref) => {
@@ -112,9 +114,12 @@ export function addDtoImports(outputDir: string): void {
if (imports.length > 0) { if (imports.length > 0) {
content = imports.join('\n') + '\n' + content; content = imports.join('\n') + '\n' + content;
}
if (content !== originalContent) {
fs.writeFileSync(filePath, content); fs.writeFileSync(filePath, content);
filesProcessed++; filesProcessed++;
logInfo(` Imports añadidos a ${file}`); logInfo(` Procesado ${file}`);
} }
}); });

View File

@@ -7,7 +7,7 @@ export function mapSwaggerTypeToTs(type?: string): string {
string: 'string', string: 'string',
boolean: 'boolean', boolean: 'boolean',
number: 'number', number: 'number',
array: 'Array<unknown>', array: 'unknown[]',
object: 'unknown' object: 'unknown'
}; };
return typeMap[type] || 'unknown'; return typeMap[type] || 'unknown';