1
1
import * as fs from "fs" ;
2
2
import { posix as path } from "path" ;
3
3
4
- import { CodeGenerator } from "../lib" ;
4
+ import { CodeGenerator , GeneratorTemplate } from "../lib" ;
5
5
import * as Templates from "../lib/templates" ;
6
6
7
7
const writeText = ( filename : string , text : string ) : void => {
@@ -13,7 +13,7 @@ const writeText = (filename: string, text: string): void => {
13
13
const generateTypedefCodeOnly = ( inputFilename : string , outputFilename : string , isValidate : boolean ) => {
14
14
const codeGenerator = new CodeGenerator ( inputFilename ) ;
15
15
if ( isValidate ) {
16
- codeGenerator . validate ( {
16
+ codeGenerator . validateOpenApiSchema ( {
17
17
logger : { displayLogLines : 1 } ,
18
18
} ) ;
19
19
}
@@ -29,14 +29,17 @@ const generateTemplateCodeOnly = (
29
29
) : void => {
30
30
const codeGenerator = new CodeGenerator ( inputFilename ) ;
31
31
if ( isValidate ) {
32
- codeGenerator . validate ( {
32
+ codeGenerator . validateOpenApiSchema ( {
33
33
logger : { displayLogLines : 1 } ,
34
34
} ) ;
35
35
}
36
- const code = codeGenerator . generateCode < Templates . ApiClient . Option > ( {
36
+
37
+ const apiClientGeneratorTemplate : GeneratorTemplate < Templates . ApiClient . Option > = {
37
38
generator : Templates . ApiClient . generator ,
38
39
option : option ,
39
- } ) ;
40
+ } ;
41
+
42
+ const code = codeGenerator . generateCode ( [ apiClientGeneratorTemplate ] ) ;
40
43
41
44
writeText ( outputFilename , code ) ;
42
45
} ;
@@ -49,18 +52,53 @@ const generateTypedefWithTemplateCode = (
49
52
) : void => {
50
53
const codeGenerator = new CodeGenerator ( inputFilename ) ;
51
54
if ( isValidate ) {
52
- codeGenerator . validate ( {
55
+ codeGenerator . validateOpenApiSchema ( {
53
56
logger : { displayLogLines : 1 } ,
54
57
} ) ;
55
58
}
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
+ ] ) ;
60
71
61
72
writeText ( outputFilename , code ) ;
62
73
} ;
63
74
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
+
64
102
const main = ( ) => {
65
103
generateTypedefCodeOnly ( "test/api.test.domain/index.yml" , "test/code/typedef-only/api.test.domain.ts" , true ) ;
66
104
generateTypedefCodeOnly ( "test/infer.domain/index.yml" , "test/code/typedef-only/infer.domain.ts" , false ) ;
@@ -69,9 +107,15 @@ const main = () => {
69
107
generateTemplateCodeOnly ( "test/api.test.domain/index.yml" , "test/code/template-only/sync-api.test.domain.ts" , true , { sync : true } ) ;
70
108
generateTemplateCodeOnly ( "test/infer.domain/index.yml" , "test/code/template-only/infer.domain.ts" , false , { sync : true } ) ;
71
109
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
+ } ) ;
74
116
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" ) ;
75
119
} ;
76
120
77
121
main ( ) ;
0 commit comments