@@ -4,7 +4,7 @@ const { NameResolver } = require("./util/name-resolver");
4
4
const { Logger } = require ( "./util/logger.js" ) ;
5
5
const { TypeNameFormatter } = require ( "./type-name-formatter.js" ) ;
6
6
const _ = require ( "lodash" ) ;
7
- const { SchemaParser } = require ( "./schema-parser/schema-parser.js " ) ;
7
+ const { SchemaParserFabric } = require ( "./schema-parser/schema-parser-fabric " ) ;
8
8
const { SchemaRoutes } = require ( "./schema-parser/schema-routes.js" ) ;
9
9
const { CodeGenConfig } = require ( "./configuration.js" ) ;
10
10
const { SchemaWalker } = require ( "./schema-walker" ) ;
@@ -27,8 +27,8 @@ class CodeGenProcess {
27
27
logger ;
28
28
/** @type {TypeNameFormatter } */
29
29
typeNameFormatter ;
30
- /** @type {SchemaParser } */
31
- schemaParser ;
30
+ /** @type {SchemaParserFabric } */
31
+ schemaParserFabric ;
32
32
/** @type {SchemaRoutes } */
33
33
schemaRoutes ;
34
34
/** @type {FileSystem } */
@@ -54,7 +54,7 @@ class CodeGenProcess {
54
54
this . typeNameFormatter = new TypeNameFormatter ( this ) ;
55
55
this . templatesWorker = new TemplatesWorker ( this ) ;
56
56
this . codeFormatter = new CodeFormatter ( this ) ;
57
- this . schemaParser = new SchemaParser ( this ) ;
57
+ this . schemaParserFabric = new SchemaParserFabric ( this ) ;
58
58
this . schemaRoutes = new SchemaRoutes ( this ) ;
59
59
this . config . componentTypeNameResolver . logger = this . logger ;
60
60
}
@@ -86,7 +86,7 @@ class CodeGenProcess {
86
86
this . config . componentTypeNameResolver . reserve ( componentSchemaNames ) ;
87
87
88
88
const parsedSchemas = _ . map ( _ . get ( swagger . usageSchema . components , "schemas" ) , ( schema , typeName ) =>
89
- this . schemaParser . parseSchema ( schema , typeName ) ,
89
+ this . schemaParserFabric . parseSchema ( schema , typeName ) ,
90
90
) ;
91
91
92
92
this . schemaRoutes . attachSchema ( {
@@ -95,43 +95,11 @@ class CodeGenProcess {
95
95
} ) ;
96
96
97
97
const usageComponentSchemas = this . schemaComponentsMap . filter ( "schemas" ) ;
98
- const sortByProperty = ( propertyName ) => ( o1 , o2 ) => {
99
- if ( o1 [ propertyName ] > o2 [ propertyName ] ) {
100
- return 1 ;
101
- }
102
- if ( o1 [ propertyName ] < o2 [ propertyName ] ) {
103
- return - 1 ;
104
- }
105
- return 0 ;
106
- } ;
107
-
108
- const sortSchemas = ( schemas ) => {
109
- if ( this . config . sortTypes ) {
110
- return schemas . sort ( sortByProperty ( "typeName" ) ) . map ( ( schema ) => {
111
- if ( schema . rawTypeData ?. properties ) {
112
- return {
113
- ...schema ,
114
- rawTypeData : {
115
- ...schema . rawTypeData ,
116
- $parsed : schema . rawTypeData . $parsed && {
117
- ...schema . rawTypeData . $parsed ,
118
- content : Array . isArray ( schema . rawTypeData . $parsed . content )
119
- ? schema . rawTypeData . $parsed . content . sort ( sortByProperty ( "name" ) )
120
- : schema . rawTypeData . $parsed . content ,
121
- } ,
122
- } ,
123
- } ;
124
- }
125
- return schema ;
126
- } ) ;
127
- }
128
- return schemas ;
129
- } ;
130
98
131
99
const rawConfiguration = {
132
100
apiConfig : this . createApiConfig ( swagger . usageSchema ) ,
133
101
config : this . config ,
134
- modelTypes : _ . map ( sortSchemas ( usageComponentSchemas ) , this . prepareModelType ) . filter ( Boolean ) ,
102
+ modelTypes : await this . collectModelTypes ( usageComponentSchemas ) ,
135
103
rawModelTypes : usageComponentSchemas ,
136
104
hasSecurityRoutes : this . schemaRoutes . hasSecurityRoutes ,
137
105
hasQueryRoutes : this . schemaRoutes . hasQueryRoutes ,
@@ -156,7 +124,7 @@ class CodeGenProcess {
156
124
this . fileSystem . createDir ( this . config . output ) ;
157
125
}
158
126
159
- const files = this . generateOutputFiles ( {
127
+ const files = await this . generateOutputFiles ( {
160
128
configuration : configuration ,
161
129
} ) ;
162
130
@@ -206,19 +174,19 @@ class CodeGenProcess {
206
174
return {
207
175
utils : {
208
176
Ts : this . config . Ts ,
209
- formatDescription : this . schemaParser . schemaFormatters . formatDescription ,
177
+ formatDescription : this . schemaParserFabric . schemaFormatters . formatDescription ,
210
178
internalCase : internalCase ,
211
179
classNameCase : pascalCase ,
212
180
pascalCase : pascalCase ,
213
- getInlineParseContent : this . schemaParser . getInlineParseContent ,
214
- getParseContent : this . schemaParser . getParseContent ,
181
+ getInlineParseContent : this . schemaParserFabric . getInlineParseContent ,
182
+ getParseContent : this . schemaParserFabric . getParseContent ,
215
183
getComponentByRef : this . schemaComponentsMap . get ,
216
- parseSchema : this . schemaParser . parseSchema ,
217
- checkAndAddNull : this . schemaParser . schemaUtils . safeAddNullToType ,
218
- safeAddNullToType : this . schemaParser . schemaUtils . safeAddNullToType ,
219
- isNeedToAddNull : this . schemaParser . schemaUtils . isNullMissingInType ,
220
- inlineExtraFormatters : this . schemaParser . schemaFormatters . inline ,
221
- formatters : this . schemaParser . schemaFormatters . base ,
184
+ parseSchema : this . schemaParserFabric . parseSchema ,
185
+ checkAndAddNull : this . schemaParserFabric . schemaUtils . safeAddNullToType ,
186
+ safeAddNullToType : this . schemaParserFabric . schemaUtils . safeAddNullToType ,
187
+ isNeedToAddNull : this . schemaParserFabric . schemaUtils . isNullMissingInType ,
188
+ inlineExtraFormatters : this . schemaParserFabric . schemaFormatters . inline ,
189
+ formatters : this . schemaParserFabric . schemaFormatters . base ,
222
190
formatModelName : this . typeNameFormatter . format ,
223
191
fmtToJSDocLine : function fmtToJSDocLine ( line , { eol = true } ) {
224
192
return ` * ${ line } ${ eol ? "\n" : "" } ` ;
@@ -231,13 +199,61 @@ class CodeGenProcess {
231
199
} ;
232
200
} ;
233
201
202
+ collectModelTypes = ( usageComponentSchemas ) => {
203
+ const modelTypes = [ ] ;
204
+
205
+ const sortByProperty = ( propertyName ) => ( o1 , o2 ) => {
206
+ if ( o1 [ propertyName ] > o2 [ propertyName ] ) {
207
+ return 1 ;
208
+ }
209
+ if ( o1 [ propertyName ] < o2 [ propertyName ] ) {
210
+ return - 1 ;
211
+ }
212
+ return 0 ;
213
+ } ;
214
+
215
+ const sortSchemas = ( schemas ) => {
216
+ if ( this . config . sortTypes ) {
217
+ return schemas . sort ( sortByProperty ( "typeName" ) ) . map ( ( schema ) => {
218
+ if ( schema . rawTypeData ?. properties ) {
219
+ return {
220
+ ...schema ,
221
+ rawTypeData : {
222
+ ...schema . rawTypeData ,
223
+ $parsed : schema . rawTypeData . $parsed && {
224
+ ...schema . rawTypeData . $parsed ,
225
+ content : Array . isArray ( schema . rawTypeData . $parsed . content )
226
+ ? schema . rawTypeData . $parsed . content . sort ( sortByProperty ( "name" ) )
227
+ : schema . rawTypeData . $parsed . content ,
228
+ } ,
229
+ } ,
230
+ } ;
231
+ }
232
+ return schema ;
233
+ } ) ;
234
+ }
235
+ return schemas ;
236
+ } ;
237
+
238
+ const sortedComponents = sortSchemas ( usageComponentSchemas ) ;
239
+
240
+ for ( const component of sortedComponents ) {
241
+ const modelType = this . prepareModelType ( component ) ;
242
+ if ( modelType ) {
243
+ modelTypes . push ( modelType ) ;
244
+ }
245
+ }
246
+
247
+ return modelTypes ;
248
+ } ;
249
+
234
250
prepareModelType = ( typeInfo ) => {
235
251
if ( ! typeInfo . typeData ) {
236
- typeInfo . typeData = this . schemaParser . parseSchema ( typeInfo . rawTypeData , typeInfo . typeName ) ;
252
+ typeInfo . typeData = this . schemaParserFabric . parseSchema ( typeInfo . rawTypeData , typeInfo . typeName ) ;
237
253
}
238
254
const rawTypeData = typeInfo . typeData ;
239
- const typeData = this . schemaParser . schemaFormatters . base [ rawTypeData . type ]
240
- ? this . schemaParser . schemaFormatters . base [ rawTypeData . type ] ( rawTypeData )
255
+ const typeData = this . schemaParserFabric . schemaFormatters . base [ rawTypeData . type ]
256
+ ? this . schemaParserFabric . schemaFormatters . base [ rawTypeData . type ] ( rawTypeData )
241
257
: rawTypeData ;
242
258
let { typeIdentifier, name : originalName , content, description } = typeData ;
243
259
const name = this . typeNameFormatter . format ( originalName ) ;
@@ -264,15 +280,13 @@ class CodeGenProcess {
264
280
: this . createSingleFileInfo ( templatesToRender , configuration ) ;
265
281
266
282
if ( ! _ . isEmpty ( configuration . extraTemplates ) ) {
267
- output . push (
268
- ..._ . map ( configuration . extraTemplates , ( extraTemplate ) => {
269
- return this . createOutputFileInfo (
270
- configuration ,
271
- extraTemplate . name ,
272
- this . templatesWorker . renderTemplate ( this . fileSystem . getFileContent ( extraTemplate . path ) , configuration ) ,
273
- ) ;
274
- } ) ,
275
- ) ;
283
+ for ( const extraTemplate of configuration . extraTemplates ) {
284
+ const content = this . templatesWorker . renderTemplate (
285
+ this . fileSystem . getFileContent ( extraTemplate . path ) ,
286
+ configuration ,
287
+ ) ;
288
+ output . push ( this . createOutputFileInfo ( configuration , extraTemplate . name , content ) ) ;
289
+ }
276
290
}
277
291
278
292
return output . filter ( ( fileInfo ) => ! ! fileInfo && ! ! fileInfo . content ) ;
@@ -307,37 +321,29 @@ class CodeGenProcess {
307
321
}
308
322
309
323
if ( routes . combined ) {
310
- modularApiFileInfos . push (
311
- ..._ . reduce (
312
- routes . combined ,
313
- ( apiFileInfos , route ) => {
314
- if ( generateRouteTypes ) {
315
- const routeModuleContent = this . templatesWorker . renderTemplate ( templatesToRender . routeTypes , {
316
- ...configuration ,
317
- route,
318
- } ) ;
319
-
320
- apiFileInfos . push (
321
- this . createOutputFileInfo ( configuration , pascalCase ( `${ route . moduleName } _Route` ) , routeModuleContent ) ,
322
- ) ;
323
- }
324
-
325
- if ( generateClient ) {
326
- const apiModuleContent = this . templatesWorker . renderTemplate ( templatesToRender . api , {
327
- ...configuration ,
328
- route,
329
- } ) ;
330
-
331
- apiFileInfos . push (
332
- this . createOutputFileInfo ( configuration , pascalCase ( route . moduleName ) , apiModuleContent ) ,
333
- ) ;
334
- }
335
-
336
- return apiFileInfos ;
337
- } ,
338
- [ ] ,
339
- ) ,
340
- ) ;
324
+ for ( const route of routes . combined ) {
325
+ if ( generateRouteTypes ) {
326
+ const routeModuleContent = this . templatesWorker . renderTemplate ( templatesToRender . routeTypes , {
327
+ ...configuration ,
328
+ route,
329
+ } ) ;
330
+
331
+ modularApiFileInfos . push (
332
+ this . createOutputFileInfo ( configuration , pascalCase ( `${ route . moduleName } _Route` ) , routeModuleContent ) ,
333
+ ) ;
334
+ }
335
+
336
+ if ( generateClient ) {
337
+ const apiModuleContent = this . templatesWorker . renderTemplate ( templatesToRender . api , {
338
+ ...configuration ,
339
+ route,
340
+ } ) ;
341
+
342
+ modularApiFileInfos . push (
343
+ this . createOutputFileInfo ( configuration , pascalCase ( route . moduleName ) , apiModuleContent ) ,
344
+ ) ;
345
+ }
346
+ }
341
347
}
342
348
343
349
return [
0 commit comments