Skip to content

Commit c5a204b

Browse files
committed
refactor: project source code
1 parent 7dd1a86 commit c5a204b

24 files changed

+253
-161
lines changed

src/code-formatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class CodeFormatter {
88
*/
99
config;
1010

11-
constructor(config) {
11+
constructor({ config }) {
1212
this.config = config;
1313
}
1414

src/code-gen-process.js

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,61 @@ const _ = require("lodash");
77
const { SchemaParser } = require("./schema-parser/schema-parser.js");
88
const { SchemaRoutes } = require("./schema-parser/schema-routes.js");
99
const { CodeGenConfig } = require("./configuration.js");
10+
const { SchemaWalker } = require("./schema-walker");
1011
const { FileSystem } = require("./util/file-system");
11-
const { Templates } = require("./templates");
12+
const { TemplatesWorker } = require("./templates-worker");
1213
const { translate: translateToJS } = require("./translators/JavaScript");
1314
const ts = require("typescript");
1415
const { CodeFormatter } = require("./code-formatter");
1516
const { pascalCase } = require("./util/pascal-case");
1617
const { internalCase } = require("./util/internal-case");
1718

1819
class CodeGenProcess {
19-
/**
20-
* @type {CodeGenConfig}
21-
*/
20+
/** @type {CodeGenConfig} */
2221
config;
23-
/**
24-
* @type {SwaggerSchemaResolver}
25-
*/
22+
/** @type {SwaggerSchemaResolver} */
2623
swaggerSchemaResolver;
27-
/**
28-
* @type {SchemaComponentsMap}
29-
*/
30-
schemaComponentMap;
31-
/**
32-
* @type {Logger}
33-
*/
24+
/** @type {SchemaComponentsMap} */
25+
schemaComponentsMap;
26+
/** @type {Logger} */
3427
logger;
35-
/**
36-
* @type {TypeNameFormatter}
37-
*/
28+
/** @type {TypeNameFormatter} */
3829
typeNameFormatter;
39-
/**
40-
* @type {SchemaParser}
41-
*/
30+
/** @type {SchemaParser} */
4231
schemaParser;
43-
/**
44-
* @type {SchemaRoutes}
45-
*/
32+
/** @type {SchemaRoutes} */
4633
schemaRoutes;
47-
/**
48-
* @type {FileSystem}
49-
*/
34+
/** @type {FileSystem} */
5035
fileSystem;
51-
/**
52-
* @type {CodeFormatter}
53-
*/
36+
/** @type {CodeFormatter} */
5437
codeFormatter;
38+
/** type {TemplatesWorker} */
39+
templatesWorker;
40+
/** @type {SchemaWalker} */
41+
schemaWalker;
5542

5643
/**
5744
*
5845
* @param config {Partial<import("../index.d.ts").GenerateApiConfiguration['config']>}
5946
*/
6047
constructor(config) {
6148
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);
8459
this.config.componentTypeNameResolver.logger = this.logger;
8560
}
8661

8762
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) });
9065

9166
const swagger = await this.swaggerSchemaResolver.create();
9267

@@ -97,13 +72,16 @@ class CodeGenProcess {
9772
originalSchema: swagger.originalSchema,
9873
});
9974

75+
this.schemaWalker.addSchema("$usage", swagger.usageSchema);
76+
this.schemaWalker.addSchema("$original", swagger.originalSchema);
77+
10078
this.logger.event("start generating your typescript api");
10179

10280
this.config.update(this.config.hooks.onInit(this.config) || this.config);
10381

104-
this.schemaComponentMap.processSchema(swagger.usageSchema);
82+
this.schemaComponentsMap.processSchema(swagger.usageSchema);
10583

106-
const componentSchemaNames = this.schemaComponentMap.filter("schemas").map((c) => c.typeName);
84+
const componentSchemaNames = this.schemaComponentsMap.filter("schemas").map((c) => c.typeName);
10785

10886
this.config.componentTypeNameResolver.reserve(componentSchemaNames);
10987

@@ -116,7 +94,7 @@ class CodeGenProcess {
11694
parsedSchemas,
11795
});
11896

119-
const usageComponentSchemas = this.schemaComponentMap.filter("schemas");
97+
const usageComponentSchemas = this.schemaComponentsMap.filter("schemas");
12098
const sortByProperty = (propertyName) => (o1, o2) => {
12199
if (o1[propertyName] > o2[propertyName]) {
122100
return 1;
@@ -217,8 +195,8 @@ class CodeGenProcess {
217195
return {
218196
files: generatedFiles,
219197
configuration,
220-
getTemplate: this.templates.getTemplate,
221-
renderTemplate: this.templates.renderTemplate,
198+
getTemplate: this.templatesWorker.getTemplate,
199+
renderTemplate: this.templatesWorker.renderTemplate,
222200
createFile: this.fileSystem.createFile,
223201
formatTSContent: this.codeFormatter.formatCode,
224202
};
@@ -234,7 +212,7 @@ class CodeGenProcess {
234212
pascalCase: pascalCase,
235213
getInlineParseContent: this.schemaParser.getInlineParseContent,
236214
getParseContent: this.schemaParser.getParseContent,
237-
getComponentByRef: this.schemaComponentMap.get,
215+
getComponentByRef: this.schemaComponentsMap.get,
238216
parseSchema: this.schemaParser.parseSchema,
239217
checkAndAddNull: this.schemaParser.schemaUtils.safeAddNullToType,
240218
safeAddNullToType: this.schemaParser.schemaUtils.safeAddNullToType,
@@ -247,7 +225,7 @@ class CodeGenProcess {
247225
},
248226
NameResolver: NameResolver,
249227
_,
250-
require: this.templates.requireFnFromTemplate,
228+
require: this.templatesWorker.requireFnFromTemplate,
251229
},
252230
config: this.config,
253231
};
@@ -291,7 +269,7 @@ class CodeGenProcess {
291269
return this.createOutputFileInfo(
292270
configuration,
293271
extraTemplate.name,
294-
this.templates.renderTemplate(this.fileSystem.getFileContent(extraTemplate.path), configuration),
272+
this.templatesWorker.renderTemplate(this.fileSystem.getFileContent(extraTemplate.path), configuration),
295273
);
296274
}),
297275
);
@@ -307,7 +285,7 @@ class CodeGenProcess {
307285

308286
if (routes.$outOfModule) {
309287
if (generateRouteTypes) {
310-
const outOfModuleRouteContent = this.templates.renderTemplate(templatesToRender.routeTypes, {
288+
const outOfModuleRouteContent = this.templatesWorker.renderTemplate(templatesToRender.routeTypes, {
311289
...configuration,
312290
route: configuration.routes.$outOfModule,
313291
});
@@ -317,7 +295,7 @@ class CodeGenProcess {
317295
);
318296
}
319297
if (generateClient) {
320-
const outOfModuleApiContent = this.templates.renderTemplate(templatesToRender.api, {
298+
const outOfModuleApiContent = this.templatesWorker.renderTemplate(templatesToRender.api, {
321299
...configuration,
322300
route: configuration.routes.$outOfModule,
323301
});
@@ -334,7 +312,7 @@ class CodeGenProcess {
334312
routes.combined,
335313
(apiFileInfos, route) => {
336314
if (generateRouteTypes) {
337-
const routeModuleContent = this.templates.renderTemplate(templatesToRender.routeTypes, {
315+
const routeModuleContent = this.templatesWorker.renderTemplate(templatesToRender.routeTypes, {
338316
...configuration,
339317
route,
340318
});
@@ -345,7 +323,7 @@ class CodeGenProcess {
345323
}
346324

347325
if (generateClient) {
348-
const apiModuleContent = this.templates.renderTemplate(templatesToRender.api, {
326+
const apiModuleContent = this.templatesWorker.renderTemplate(templatesToRender.api, {
349327
...configuration,
350328
route,
351329
});
@@ -366,13 +344,13 @@ class CodeGenProcess {
366344
this.createOutputFileInfo(
367345
configuration,
368346
fileNames.dataContracts,
369-
this.templates.renderTemplate(templatesToRender.dataContracts, configuration),
347+
this.templatesWorker.renderTemplate(templatesToRender.dataContracts, configuration),
370348
),
371349
generateClient &&
372350
this.createOutputFileInfo(
373351
configuration,
374352
fileNames.httpClient,
375-
this.templates.renderTemplate(templatesToRender.httpClient, configuration),
353+
this.templatesWorker.renderTemplate(templatesToRender.httpClient, configuration),
376354
),
377355
...modularApiFileInfos,
378356
];
@@ -386,10 +364,10 @@ class CodeGenProcess {
386364
configuration,
387365
configuration.fileName,
388366
_.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),
393371
]).join("\n"),
394372
),
395373
];

src/commands/generate-templates/templates-gen-process.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class TemplatesGenProcess {
3131

3232
constructor(config) {
3333
this.config = new TemplatesGenConfig(config);
34-
this.logger = new Logger(this.config);
35-
this.fileSystem = new FileSystem();
34+
this.logger = new Logger(this);
35+
this.fileSystem = new FileSystem(this);
3636
}
3737

3838
/**

src/schema-components-map.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
const _ = require("lodash");
22

33
class SchemaComponentsMap {
4-
/**
5-
* @type {Record<string, SchemaComponent>}
6-
*/
4+
/** @type {Record<string, SchemaComponent>} */
75
data = {};
8-
/**
9-
* @type {CodeGenConfig}
10-
*/
6+
/** @type {CodeGenConfig} */
117
config;
128

13-
constructor(config, schema) {
9+
constructor({ config }) {
1410
this.config = config;
15-
this.processSchema(schema);
1611
}
1712

1813
processSchema(schema) {
1914
this.data = {};
2015
if (!schema) return;
2116
_.each(schema.components, (component, componentName) =>
22-
_.each(component, (rawTypeData, typeName) => this.createComponent(componentName, typeName, rawTypeData)),
17+
_.each(component, (rawTypeData, typeName) => {
18+
return this.createComponent(componentName, typeName, rawTypeData);
19+
}),
2320
);
2421
}
2522

src/schema-parser/base-schema-parsers/discriminator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
77
parse() {
88
const { discriminator, ...noDiscriminatorSchema } = this.schema;
99

10-
if (this.typeName == null || !discriminator.mapping)
10+
// TODO: disable for now
11+
if (!!this)
12+
// if (this.typeName == null || !discriminator.mapping)
1113
return this.schemaParser.parseSchema(noDiscriminatorSchema, this.typeName, this.schemaPath);
1214

1315
const abstractSchemaStruct = this.createAbstractSchemaStruct();

src/schema-parser/complex-schema-parsers/all-of.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const { MonoSchemaParser } = require("../mono-schema-parser");
22
const _ = require("lodash");
33

4+
// T1 & T2
45
class AllOfSchemaParser extends MonoSchemaParser {
56
parse() {
6-
const ignoreTypes = [...this.config.jsPrimitiveTypes, this.config.Ts.Keyword.Any];
7+
const ignoreTypes = [this.config.Ts.Keyword.Any];
78
const combined = _.map(this.schema.allOf, (childSchema) =>
89
this.schemaParser.getInlineParseContent(
910
this.schemaUtils.makeAddRequiredToChildSchema(this.schema, childSchema),

src/schema-parser/complex-schema-parsers/any-of.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const { MonoSchemaParser } = require("../mono-schema-parser");
22
const _ = require("lodash");
33

4+
// T1 | T2 | (T1 & T2)
45
class AnyOfSchemaParser extends MonoSchemaParser {
56
parse() {
6-
const ignoreTypes = [...this.config.jsPrimitiveTypes, this.config.Ts.Keyword.Any];
7+
const ignoreTypes = [this.config.Ts.Keyword.Any];
78
const combined = _.map(this.schema.anyOf, (childSchema) =>
89
this.schemaParser.getInlineParseContent(
910
this.schemaUtils.makeAddRequiredToChildSchema(this.schema, childSchema),

src/schema-parser/complex-schema-parsers/one-of.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { MonoSchemaParser } = require("../mono-schema-parser");
22
const _ = require("lodash");
33

4+
// T1 | T2
45
class OneOfSchemaParser extends MonoSchemaParser {
56
parse() {
67
const ignoreTypes = [this.config.Ts.Keyword.Any];

src/schema-parser/schema-formatters.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ class SchemaFormatters {
1515
*/
1616
schemaParser;
1717
/**
18-
* @type {Templates}
18+
* @type {TemplatesWorker}
1919
*/
20-
templates;
20+
templatesWorker;
2121

22-
constructor(config, logger, schemaParser, templates) {
23-
this.config = config;
24-
this.logger = logger;
22+
/**
23+
* @param schemaParser {SchemaParser}
24+
*/
25+
constructor(schemaParser) {
26+
this.config = schemaParser.config;
27+
this.logger = schemaParser.logger;
2528
this.schemaParser = schemaParser;
26-
this.templates = templates;
29+
this.templatesWorker = schemaParser.templatesWorker;
2730
}
2831

2932
base = {
@@ -131,7 +134,7 @@ class SchemaFormatters {
131134
const extraSpace = " ";
132135
const result = `${extraSpace}${part.field},\n`;
133136

134-
const renderedJsDoc = this.templates.renderTemplate(this.config.templatesToRender.dataContractJsDoc, {
137+
const renderedJsDoc = this.templatesWorker.renderTemplate(this.config.templatesToRender.dataContractJsDoc, {
135138
data: part,
136139
});
137140

0 commit comments

Comments
 (0)