9
9
GeneratorDependency ,
10
10
GeneratorImport ,
11
11
GeneratorMutator ,
12
- GeneratorOptions ,
13
12
GeneratorVerbOptions ,
14
13
getFileInfo ,
15
14
getOrvalGeneratedTypes ,
@@ -180,35 +179,46 @@ const getValidatorOutputRelativePath = (
180
179
} ;
181
180
182
181
const getZvalidatorImports = (
183
- verbOption : GeneratorVerbOptions ,
182
+ verbOptions : GeneratorVerbOptions [ ] ,
183
+ importPath : string ,
184
184
isHonoValidator : boolean ,
185
185
) => {
186
- const imports = [ ] ;
186
+ const importImplementation = verbOptions
187
+ . flatMap ( ( verbOption ) => {
188
+ const imports = [ ] ;
187
189
188
- if ( verbOption . headers ) {
189
- imports . push ( `${ verbOption . operationName } Header` ) ;
190
- }
190
+ if ( verbOption . headers ) {
191
+ imports . push ( `${ verbOption . operationName } Header` ) ;
192
+ }
191
193
192
- if ( verbOption . params . length ) {
193
- imports . push ( `${ verbOption . operationName } Params` ) ;
194
- }
194
+ if ( verbOption . params . length ) {
195
+ imports . push ( `${ verbOption . operationName } Params` ) ;
196
+ }
195
197
196
- if ( verbOption . queryParams ) {
197
- imports . push ( `${ verbOption . operationName } QueryParams` ) ;
198
- }
198
+ if ( verbOption . queryParams ) {
199
+ imports . push ( `${ verbOption . operationName } QueryParams` ) ;
200
+ }
199
201
200
- if ( verbOption . body . definition ) {
201
- imports . push ( `${ verbOption . operationName } Body` ) ;
202
- }
202
+ if ( verbOption . body . definition ) {
203
+ imports . push ( `${ verbOption . operationName } Body` ) ;
204
+ }
203
205
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' ) ;
210
218
211
- return imports . join ( ',\n' ) ;
219
+ return importImplementation
220
+ ? `import {\n${ importImplementation } \n} from '${ importPath } '`
221
+ : '' ;
212
222
} ;
213
223
214
224
const getVerbOptionGroupByTag = (
@@ -299,10 +309,11 @@ const generateHandlers = async (
299
309
}
300
310
301
311
const zodImports = output . override . hono . validator
302
- ? `import { ${ getZvalidatorImports (
303
- verbOption ,
312
+ ? getZvalidatorImports (
313
+ [ verbOption ] ,
314
+ `${ outputPath } .zod` ,
304
315
output . override . hono . validator === 'hono' ,
305
- ) } } from ' ${ outputPath } .zod';`
316
+ )
306
317
: '' ;
307
318
308
319
const content = `import { createFactory } from 'hono/factory';${ validatorImport }
@@ -394,21 +405,18 @@ ${getHonoHandlers({
394
405
}
395
406
396
407
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
+ )
405
413
: '' ;
406
414
407
415
let content = `import { createFactory } from 'hono/factory';${ validatorImport }
408
416
import { ${ Object . values ( verbs )
409
417
. map ( ( verb ) => `${ pascal ( verb . operationName ) } Context` )
410
418
. join ( ',\n' ) } } from '${ outputRelativePath } .context';
411
- ${ zodImports } ;
419
+ ${ zodImports }
412
420
413
421
const factory = createFactory();` ;
414
422
@@ -496,11 +504,11 @@ const factory = createFactory();`;
496
504
}
497
505
498
506
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
+ )
504
512
: '' ;
505
513
506
514
let content = `import { createFactory } from 'hono/factory';${ validatorImport }
@@ -624,9 +632,11 @@ const generateContext = async (
624
632
content += '\n' ;
625
633
}
626
634
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
+ }
630
640
631
641
content += contexts ;
632
642
@@ -710,7 +720,7 @@ const generateZodFiles = async (
710
720
if ( output . mode === 'tags' || output . mode === 'tags-split' ) {
711
721
const groupByTags = getVerbOptionGroupByTag ( verbOptions ) ;
712
722
713
- return Promise . all (
723
+ const builderContexts = await Promise . all (
714
724
Object . entries ( groupByTags ) . map ( async ( [ tag , verbs ] ) => {
715
725
const zods = await Promise . all (
716
726
verbs . map ( ( verbOption ) =>
@@ -729,6 +739,13 @@ const generateZodFiles = async (
729
739
) ,
730
740
) ;
731
741
742
+ if ( zods . every ( ( z ) => z . implementation === '' ) ) {
743
+ return {
744
+ content : '' ,
745
+ path : '' ,
746
+ } ;
747
+ }
748
+
732
749
const allMutators = zods . reduce (
733
750
( acc , z ) => {
734
751
( z . mutators ?? [ ] ) . forEach ( ( mutator ) => {
@@ -758,6 +775,10 @@ const generateZodFiles = async (
758
775
} ;
759
776
} ) ,
760
777
) ;
778
+
779
+ return Promise . all (
780
+ builderContexts . filter ( ( context ) => context . content !== '' ) ,
781
+ ) ;
761
782
}
762
783
763
784
const zods = await Promise . all (
0 commit comments