Skip to content

Commit 53b6e8c

Browse files
authored
fix(hono): remove unnecessary zod file and import (#1896)
* fix(hono): remove unnecessary zod file generation and import * chore: refactoring for `getZvalidatorImports`
1 parent 2674c88 commit 53b6e8c

File tree

1 file changed

+64
-43
lines changed

1 file changed

+64
-43
lines changed

packages/hono/src/index.ts

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
GeneratorDependency,
1010
GeneratorImport,
1111
GeneratorMutator,
12-
GeneratorOptions,
1312
GeneratorVerbOptions,
1413
getFileInfo,
1514
getOrvalGeneratedTypes,
@@ -180,35 +179,46 @@ const getValidatorOutputRelativePath = (
180179
};
181180

182181
const getZvalidatorImports = (
183-
verbOption: GeneratorVerbOptions,
182+
verbOptions: GeneratorVerbOptions[],
183+
importPath: string,
184184
isHonoValidator: boolean,
185185
) => {
186-
const imports = [];
186+
const importImplementation = verbOptions
187+
.flatMap((verbOption) => {
188+
const imports = [];
187189

188-
if (verbOption.headers) {
189-
imports.push(`${verbOption.operationName}Header`);
190-
}
190+
if (verbOption.headers) {
191+
imports.push(`${verbOption.operationName}Header`);
192+
}
191193

192-
if (verbOption.params.length) {
193-
imports.push(`${verbOption.operationName}Params`);
194-
}
194+
if (verbOption.params.length) {
195+
imports.push(`${verbOption.operationName}Params`);
196+
}
195197

196-
if (verbOption.queryParams) {
197-
imports.push(`${verbOption.operationName}QueryParams`);
198-
}
198+
if (verbOption.queryParams) {
199+
imports.push(`${verbOption.operationName}QueryParams`);
200+
}
199201

200-
if (verbOption.body.definition) {
201-
imports.push(`${verbOption.operationName}Body`);
202-
}
202+
if (verbOption.body.definition) {
203+
imports.push(`${verbOption.operationName}Body`);
204+
}
203205

204-
if (
205-
!isHonoValidator &&
206-
!!verbOption.response.originalSchema?.['200']?.content?.['application/json']
207-
) {
208-
imports.push(`${verbOption.operationName}Response`);
209-
}
206+
if (
207+
!isHonoValidator &&
208+
!!verbOption.response.originalSchema?.['200']?.content?.[
209+
'application/json'
210+
]
211+
) {
212+
imports.push(`${verbOption.operationName}Response`);
213+
}
214+
215+
return imports.join(',\n');
216+
})
217+
.join(',\n');
210218

211-
return imports.join(',\n');
219+
return importImplementation
220+
? `import {\n${importImplementation}\n} from '${importPath}'`
221+
: '';
212222
};
213223

214224
const getVerbOptionGroupByTag = (
@@ -299,10 +309,11 @@ const generateHandlers = async (
299309
}
300310

301311
const zodImports = output.override.hono.validator
302-
? `import { ${getZvalidatorImports(
303-
verbOption,
312+
? getZvalidatorImports(
313+
[verbOption],
314+
`${outputPath}.zod`,
304315
output.override.hono.validator === 'hono',
305-
)} } from '${outputPath}.zod';`
316+
)
306317
: '';
307318

308319
const content = `import { createFactory } from 'hono/factory';${validatorImport}
@@ -394,21 +405,18 @@ ${getHonoHandlers({
394405
}
395406

396407
const zodImports = output.override.hono.validator
397-
? `import { ${Object.values(verbs)
398-
.map((verb) =>
399-
getZvalidatorImports(
400-
verb,
401-
output.override.hono.validator === 'hono',
402-
),
403-
)
404-
.join(',\n')} } from '${outputRelativePath}.zod'`
408+
? getZvalidatorImports(
409+
Object.values(verbs),
410+
`${outputRelativePath}.zod`,
411+
output.override.hono.validator === 'hono',
412+
)
405413
: '';
406414

407415
let content = `import { createFactory } from 'hono/factory';${validatorImport}
408416
import { ${Object.values(verbs)
409417
.map((verb) => `${pascal(verb.operationName)}Context`)
410418
.join(',\n')} } from '${outputRelativePath}.context';
411-
${zodImports};
419+
${zodImports}
412420
413421
const factory = createFactory();`;
414422

@@ -496,11 +504,11 @@ const factory = createFactory();`;
496504
}
497505

498506
const zodImports = output.override.hono.validator
499-
? `import { ${Object.values(verbOptions)
500-
.map((verb) =>
501-
getZvalidatorImports(verb, output.override.hono.validator === 'hono'),
502-
)
503-
.join(',\n')} } from '${outputRelativePath}.zod';`
507+
? getZvalidatorImports(
508+
Object.values(verbOptions),
509+
`${outputRelativePath}.zod`,
510+
output.override.hono.validator === 'hono',
511+
)
504512
: '';
505513

506514
let content = `import { createFactory } from 'hono/factory';${validatorImport}
@@ -624,9 +632,11 @@ const generateContext = async (
624632
content += '\n';
625633
}
626634

627-
content += `import { ${imps
628-
.map((imp) => imp.name)
629-
.join(',\n')} } from '${relativeSchemasPath}';\n\n`;
635+
if (imps.length > 0) {
636+
const importSchemas = imps.map((imp) => imp.name).join(',\n ');
637+
638+
content += `import {\n ${importSchemas}\n} from '${relativeSchemasPath}';\n\n`;
639+
}
630640

631641
content += contexts;
632642

@@ -710,7 +720,7 @@ const generateZodFiles = async (
710720
if (output.mode === 'tags' || output.mode === 'tags-split') {
711721
const groupByTags = getVerbOptionGroupByTag(verbOptions);
712722

713-
return Promise.all(
723+
const builderContexts = await Promise.all(
714724
Object.entries(groupByTags).map(async ([tag, verbs]) => {
715725
const zods = await Promise.all(
716726
verbs.map((verbOption) =>
@@ -729,6 +739,13 @@ const generateZodFiles = async (
729739
),
730740
);
731741

742+
if (zods.every((z) => z.implementation === '')) {
743+
return {
744+
content: '',
745+
path: '',
746+
};
747+
}
748+
732749
const allMutators = zods.reduce(
733750
(acc, z) => {
734751
(z.mutators ?? []).forEach((mutator) => {
@@ -758,6 +775,10 @@ const generateZodFiles = async (
758775
};
759776
}),
760777
);
778+
779+
return Promise.all(
780+
builderContexts.filter((context) => context.content !== ''),
781+
);
761782
}
762783

763784
const zods = await Promise.all(

0 commit comments

Comments
 (0)