@@ -7,86 +7,61 @@ const _ = require("lodash");
7
7
const { SchemaParser } = require ( "./schema-parser/schema-parser.js" ) ;
8
8
const { SchemaRoutes } = require ( "./schema-parser/schema-routes.js" ) ;
9
9
const { CodeGenConfig } = require ( "./configuration.js" ) ;
10
+ const { SchemaWalker } = require ( "./schema-walker" ) ;
10
11
const { FileSystem } = require ( "./util/file-system" ) ;
11
- const { Templates } = require ( "./templates" ) ;
12
+ const { TemplatesWorker } = require ( "./templates-worker " ) ;
12
13
const { translate : translateToJS } = require ( "./translators/JavaScript" ) ;
13
14
const ts = require ( "typescript" ) ;
14
15
const { CodeFormatter } = require ( "./code-formatter" ) ;
15
16
const { pascalCase } = require ( "./util/pascal-case" ) ;
16
17
const { internalCase } = require ( "./util/internal-case" ) ;
17
18
18
19
class CodeGenProcess {
19
- /**
20
- * @type {CodeGenConfig }
21
- */
20
+ /** @type {CodeGenConfig } */
22
21
config ;
23
- /**
24
- * @type {SwaggerSchemaResolver }
25
- */
22
+ /** @type {SwaggerSchemaResolver } */
26
23
swaggerSchemaResolver ;
27
- /**
28
- * @type {SchemaComponentsMap }
29
- */
30
- schemaComponentMap ;
31
- /**
32
- * @type {Logger }
33
- */
24
+ /** @type {SchemaComponentsMap } */
25
+ schemaComponentsMap ;
26
+ /** @type {Logger } */
34
27
logger ;
35
- /**
36
- * @type {TypeNameFormatter }
37
- */
28
+ /** @type {TypeNameFormatter } */
38
29
typeNameFormatter ;
39
- /**
40
- * @type {SchemaParser }
41
- */
30
+ /** @type {SchemaParser } */
42
31
schemaParser ;
43
- /**
44
- * @type {SchemaRoutes }
45
- */
32
+ /** @type {SchemaRoutes } */
46
33
schemaRoutes ;
47
- /**
48
- * @type {FileSystem }
49
- */
34
+ /** @type {FileSystem } */
50
35
fileSystem ;
51
- /**
52
- * @type {CodeFormatter }
53
- */
36
+ /** @type {CodeFormatter } */
54
37
codeFormatter ;
38
+ /** type {TemplatesWorker} */
39
+ templatesWorker ;
40
+ /** @type {SchemaWalker } */
41
+ schemaWalker ;
55
42
56
43
/**
57
44
*
58
45
* @param config {Partial<import("../index.d.ts").GenerateApiConfiguration['config']>}
59
46
*/
60
47
constructor ( config ) {
61
48
this . config = new CodeGenConfig ( config ) ;
62
- this . logger = new Logger ( this . config ) ;
63
- this . fileSystem = new FileSystem ( ) ;
64
- this . swaggerSchemaResolver = new SwaggerSchemaResolver ( this . config , this . logger , this . fileSystem ) ;
65
- this . schemaComponentMap = new SchemaComponentsMap ( this . config ) ;
66
- this . typeNameFormatter = new TypeNameFormatter ( this . config , this . logger ) ;
67
- this . templates = new Templates ( this . config , this . logger , this . fileSystem , this . getRenderTemplateData ) ;
68
- this . codeFormatter = new CodeFormatter ( this . config ) ;
69
- this . schemaParser = new SchemaParser (
70
- this . config ,
71
- this . logger ,
72
- this . templates ,
73
- this . schemaComponentMap ,
74
- this . typeNameFormatter ,
75
- ) ;
76
- this . schemaRoutes = new SchemaRoutes (
77
- this . config ,
78
- this . schemaParser ,
79
- this . schemaComponentMap ,
80
- this . logger ,
81
- this . templates ,
82
- this . typeNameFormatter ,
83
- ) ;
49
+ this . logger = new Logger ( this ) ;
50
+ this . schemaWalker = new SchemaWalker ( this ) ;
51
+ this . fileSystem = new FileSystem ( this ) ;
52
+ this . swaggerSchemaResolver = new SwaggerSchemaResolver ( this ) ;
53
+ this . schemaComponentsMap = new SchemaComponentsMap ( this ) ;
54
+ this . typeNameFormatter = new TypeNameFormatter ( this ) ;
55
+ this . templatesWorker = new TemplatesWorker ( this ) ;
56
+ this . codeFormatter = new CodeFormatter ( this ) ;
57
+ this . schemaParser = new SchemaParser ( this ) ;
58
+ this . schemaRoutes = new SchemaRoutes ( this ) ;
84
59
this . config . componentTypeNameResolver . logger = this . logger ;
85
60
}
86
61
87
62
async start ( ) {
88
- this . config . update ( { templatePaths : this . templates . getTemplatePaths ( this . config ) } ) ;
89
- this . config . update ( { templatesToRender : this . templates . getTemplates ( this . config ) } ) ;
63
+ this . config . update ( { templatePaths : this . templatesWorker . getTemplatePaths ( this . config ) } ) ;
64
+ this . config . update ( { templatesToRender : this . templatesWorker . getTemplates ( this . config ) } ) ;
90
65
91
66
const swagger = await this . swaggerSchemaResolver . create ( ) ;
92
67
@@ -97,13 +72,16 @@ class CodeGenProcess {
97
72
originalSchema : swagger . originalSchema ,
98
73
} ) ;
99
74
75
+ this . schemaWalker . addSchema ( "$usage" , swagger . usageSchema ) ;
76
+ this . schemaWalker . addSchema ( "$original" , swagger . originalSchema ) ;
77
+
100
78
this . logger . event ( "start generating your typescript api" ) ;
101
79
102
80
this . config . update ( this . config . hooks . onInit ( this . config ) || this . config ) ;
103
81
104
- this . schemaComponentMap . processSchema ( swagger . usageSchema ) ;
82
+ this . schemaComponentsMap . processSchema ( swagger . usageSchema ) ;
105
83
106
- const componentSchemaNames = this . schemaComponentMap . filter ( "schemas" ) . map ( ( c ) => c . typeName ) ;
84
+ const componentSchemaNames = this . schemaComponentsMap . filter ( "schemas" ) . map ( ( c ) => c . typeName ) ;
107
85
108
86
this . config . componentTypeNameResolver . reserve ( componentSchemaNames ) ;
109
87
@@ -116,7 +94,7 @@ class CodeGenProcess {
116
94
parsedSchemas,
117
95
} ) ;
118
96
119
- const usageComponentSchemas = this . schemaComponentMap . filter ( "schemas" ) ;
97
+ const usageComponentSchemas = this . schemaComponentsMap . filter ( "schemas" ) ;
120
98
const sortByProperty = ( propertyName ) => ( o1 , o2 ) => {
121
99
if ( o1 [ propertyName ] > o2 [ propertyName ] ) {
122
100
return 1 ;
@@ -217,8 +195,8 @@ class CodeGenProcess {
217
195
return {
218
196
files : generatedFiles ,
219
197
configuration,
220
- getTemplate : this . templates . getTemplate ,
221
- renderTemplate : this . templates . renderTemplate ,
198
+ getTemplate : this . templatesWorker . getTemplate ,
199
+ renderTemplate : this . templatesWorker . renderTemplate ,
222
200
createFile : this . fileSystem . createFile ,
223
201
formatTSContent : this . codeFormatter . formatCode ,
224
202
} ;
@@ -234,7 +212,7 @@ class CodeGenProcess {
234
212
pascalCase : pascalCase ,
235
213
getInlineParseContent : this . schemaParser . getInlineParseContent ,
236
214
getParseContent : this . schemaParser . getParseContent ,
237
- getComponentByRef : this . schemaComponentMap . get ,
215
+ getComponentByRef : this . schemaComponentsMap . get ,
238
216
parseSchema : this . schemaParser . parseSchema ,
239
217
checkAndAddNull : this . schemaParser . schemaUtils . safeAddNullToType ,
240
218
safeAddNullToType : this . schemaParser . schemaUtils . safeAddNullToType ,
@@ -247,7 +225,7 @@ class CodeGenProcess {
247
225
} ,
248
226
NameResolver : NameResolver ,
249
227
_,
250
- require : this . templates . requireFnFromTemplate ,
228
+ require : this . templatesWorker . requireFnFromTemplate ,
251
229
} ,
252
230
config : this . config ,
253
231
} ;
@@ -291,7 +269,7 @@ class CodeGenProcess {
291
269
return this . createOutputFileInfo (
292
270
configuration ,
293
271
extraTemplate . name ,
294
- this . templates . renderTemplate ( this . fileSystem . getFileContent ( extraTemplate . path ) , configuration ) ,
272
+ this . templatesWorker . renderTemplate ( this . fileSystem . getFileContent ( extraTemplate . path ) , configuration ) ,
295
273
) ;
296
274
} ) ,
297
275
) ;
@@ -307,7 +285,7 @@ class CodeGenProcess {
307
285
308
286
if ( routes . $outOfModule ) {
309
287
if ( generateRouteTypes ) {
310
- const outOfModuleRouteContent = this . templates . renderTemplate ( templatesToRender . routeTypes , {
288
+ const outOfModuleRouteContent = this . templatesWorker . renderTemplate ( templatesToRender . routeTypes , {
311
289
...configuration ,
312
290
route : configuration . routes . $outOfModule ,
313
291
} ) ;
@@ -317,7 +295,7 @@ class CodeGenProcess {
317
295
) ;
318
296
}
319
297
if ( generateClient ) {
320
- const outOfModuleApiContent = this . templates . renderTemplate ( templatesToRender . api , {
298
+ const outOfModuleApiContent = this . templatesWorker . renderTemplate ( templatesToRender . api , {
321
299
...configuration ,
322
300
route : configuration . routes . $outOfModule ,
323
301
} ) ;
@@ -334,7 +312,7 @@ class CodeGenProcess {
334
312
routes . combined ,
335
313
( apiFileInfos , route ) => {
336
314
if ( generateRouteTypes ) {
337
- const routeModuleContent = this . templates . renderTemplate ( templatesToRender . routeTypes , {
315
+ const routeModuleContent = this . templatesWorker . renderTemplate ( templatesToRender . routeTypes , {
338
316
...configuration ,
339
317
route,
340
318
} ) ;
@@ -345,7 +323,7 @@ class CodeGenProcess {
345
323
}
346
324
347
325
if ( generateClient ) {
348
- const apiModuleContent = this . templates . renderTemplate ( templatesToRender . api , {
326
+ const apiModuleContent = this . templatesWorker . renderTemplate ( templatesToRender . api , {
349
327
...configuration ,
350
328
route,
351
329
} ) ;
@@ -366,13 +344,13 @@ class CodeGenProcess {
366
344
this . createOutputFileInfo (
367
345
configuration ,
368
346
fileNames . dataContracts ,
369
- this . templates . renderTemplate ( templatesToRender . dataContracts , configuration ) ,
347
+ this . templatesWorker . renderTemplate ( templatesToRender . dataContracts , configuration ) ,
370
348
) ,
371
349
generateClient &&
372
350
this . createOutputFileInfo (
373
351
configuration ,
374
352
fileNames . httpClient ,
375
- this . templates . renderTemplate ( templatesToRender . httpClient , configuration ) ,
353
+ this . templatesWorker . renderTemplate ( templatesToRender . httpClient , configuration ) ,
376
354
) ,
377
355
...modularApiFileInfos ,
378
356
] ;
@@ -386,10 +364,10 @@ class CodeGenProcess {
386
364
configuration ,
387
365
configuration . fileName ,
388
366
_ . compact ( [
389
- this . templates . renderTemplate ( templatesToRender . dataContracts , configuration ) ,
390
- generateRouteTypes && this . templates . renderTemplate ( templatesToRender . routeTypes , configuration ) ,
391
- generateClient && this . templates . renderTemplate ( templatesToRender . httpClient , configuration ) ,
392
- generateClient && this . templates . renderTemplate ( templatesToRender . api , configuration ) ,
367
+ this . templatesWorker . renderTemplate ( templatesToRender . dataContracts , configuration ) ,
368
+ generateRouteTypes && this . templatesWorker . renderTemplate ( templatesToRender . routeTypes , configuration ) ,
369
+ generateClient && this . templatesWorker . renderTemplate ( templatesToRender . httpClient , configuration ) ,
370
+ generateClient && this . templatesWorker . renderTemplate ( templatesToRender . api , configuration ) ,
393
371
] ) . join ( "\n" ) ,
394
372
) ,
395
373
] ;
0 commit comments