11import * as fs from "fs" ;
22import { posix as path } from "path" ;
33
4- import { CodeGenerator } from "../lib" ;
4+ import { CodeGenerator , GeneratorTemplate } from "../lib" ;
55import * as Templates from "../lib/templates" ;
66
77const writeText = ( filename : string , text : string ) : void => {
@@ -13,7 +13,7 @@ const writeText = (filename: string, text: string): void => {
1313const generateTypedefCodeOnly = ( inputFilename : string , outputFilename : string , isValidate : boolean ) => {
1414 const codeGenerator = new CodeGenerator ( inputFilename ) ;
1515 if ( isValidate ) {
16- codeGenerator . validate ( {
16+ codeGenerator . validateOpenApiSchema ( {
1717 logger : { displayLogLines : 1 } ,
1818 } ) ;
1919 }
@@ -29,14 +29,17 @@ const generateTemplateCodeOnly = (
2929) : void => {
3030 const codeGenerator = new CodeGenerator ( inputFilename ) ;
3131 if ( isValidate ) {
32- codeGenerator . validate ( {
32+ codeGenerator . validateOpenApiSchema ( {
3333 logger : { displayLogLines : 1 } ,
3434 } ) ;
3535 }
36- const code = codeGenerator . generateCode < Templates . ApiClient . Option > ( {
36+
37+ const apiClientGeneratorTemplate : GeneratorTemplate < Templates . ApiClient . Option > = {
3738 generator : Templates . ApiClient . generator ,
3839 option : option ,
39- } ) ;
40+ } ;
41+
42+ const code = codeGenerator . generateCode ( [ apiClientGeneratorTemplate ] ) ;
4043
4144 writeText ( outputFilename , code ) ;
4245} ;
@@ -49,18 +52,53 @@ const generateTypedefWithTemplateCode = (
4952) : void => {
5053 const codeGenerator = new CodeGenerator ( inputFilename ) ;
5154 if ( isValidate ) {
52- codeGenerator . validate ( {
55+ codeGenerator . validateOpenApiSchema ( {
5356 logger : { displayLogLines : 1 } ,
5457 } ) ;
5558 }
56- const code = codeGenerator . generateTypeDefinition < Templates . ApiClient . Option > ( {
57- generator : Templates . ApiClient . generator ,
58- option : option ,
59- } ) ;
59+
60+ const code = codeGenerator . generateTypeDefinition ( [
61+ {
62+ generator : ( ) => {
63+ return codeGenerator . getAdditionalTypeStatements ( ) ;
64+ } ,
65+ } ,
66+ {
67+ generator : Templates . ApiClient . generator ,
68+ option : option ,
69+ } ,
70+ ] ) ;
6071
6172 writeText ( outputFilename , code ) ;
6273} ;
6374
75+ const generateSplitCode = ( inputFilename : string , outputDir : string ) => {
76+ const codeGenerator = new CodeGenerator ( inputFilename ) ;
77+
78+ const apiClientGeneratorTemplate : GeneratorTemplate < Templates . ApiClient . Option > = {
79+ generator : Templates . ApiClient . generator ,
80+ option : { sync : false } ,
81+ } ;
82+
83+ const typeDefCode = codeGenerator . generateTypeDefinition ( ) ;
84+ const apiClientCode = codeGenerator . generateCode ( [
85+ {
86+ generator : ( ) => {
87+ return [ `import { Schemas } from "./types";` ] ;
88+ } ,
89+ } ,
90+ {
91+ generator : ( ) => {
92+ return codeGenerator . getAdditionalTypeStatements ( ) ;
93+ } ,
94+ } ,
95+ apiClientGeneratorTemplate ,
96+ ] ) ;
97+
98+ writeText ( path . join ( outputDir , "types.ts" ) , typeDefCode ) ;
99+ writeText ( path . join ( outputDir , "apiClient.ts" ) , apiClientCode ) ;
100+ } ;
101+
64102const main = ( ) => {
65103 generateTypedefCodeOnly ( "test/api.test.domain/index.yml" , "test/code/typedef-only/api.test.domain.ts" , true ) ;
66104 generateTypedefCodeOnly ( "test/infer.domain/index.yml" , "test/code/typedef-only/infer.domain.ts" , false ) ;
@@ -69,9 +107,15 @@ const main = () => {
69107 generateTemplateCodeOnly ( "test/api.test.domain/index.yml" , "test/code/template-only/sync-api.test.domain.ts" , true , { sync : true } ) ;
70108 generateTemplateCodeOnly ( "test/infer.domain/index.yml" , "test/code/template-only/infer.domain.ts" , false , { sync : true } ) ;
71109
72- generateTypedefWithTemplateCode ( "test/api.test.domain/index.yml" , "test/code/typedef-with-template/api.test.domain.ts" , true , { sync : false } ) ;
73- generateTypedefWithTemplateCode ( "test/api.test.domain/index.yml" , "test/code/typedef-with-template/sync-api.test.domain.ts" , true , { sync : true } ) ;
110+ generateTypedefWithTemplateCode ( "test/api.test.domain/index.yml" , "test/code/typedef-with-template/api.test.domain.ts" , true , {
111+ sync : false ,
112+ } ) ;
113+ generateTypedefWithTemplateCode ( "test/api.test.domain/index.yml" , "test/code/typedef-with-template/sync-api.test.domain.ts" , true , {
114+ sync : true ,
115+ } ) ;
74116 generateTypedefWithTemplateCode ( "test/infer.domain/index.yml" , "test/code/typedef-with-template/infer.domain.ts" , false , { sync : false } ) ;
117+
118+ generateSplitCode ( "test/api.test.domain/index.yml" , "test/code/split" ) ;
75119} ;
76120
77121main ( ) ;
0 commit comments