Skip to content

Commit 4ad99d3

Browse files
committed
refactor: project source code, prepare it for future async operations
1 parent c5a204b commit 4ad99d3

25 files changed

+427
-277
lines changed

src/code-gen-process.js

Lines changed: 97 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { NameResolver } = require("./util/name-resolver");
44
const { Logger } = require("./util/logger.js");
55
const { TypeNameFormatter } = require("./type-name-formatter.js");
66
const _ = require("lodash");
7-
const { SchemaParser } = require("./schema-parser/schema-parser.js");
7+
const { SchemaParserFabric } = require("./schema-parser/schema-parser-fabric");
88
const { SchemaRoutes } = require("./schema-parser/schema-routes.js");
99
const { CodeGenConfig } = require("./configuration.js");
1010
const { SchemaWalker } = require("./schema-walker");
@@ -27,8 +27,8 @@ class CodeGenProcess {
2727
logger;
2828
/** @type {TypeNameFormatter} */
2929
typeNameFormatter;
30-
/** @type {SchemaParser} */
31-
schemaParser;
30+
/** @type {SchemaParserFabric} */
31+
schemaParserFabric;
3232
/** @type {SchemaRoutes} */
3333
schemaRoutes;
3434
/** @type {FileSystem} */
@@ -54,7 +54,7 @@ class CodeGenProcess {
5454
this.typeNameFormatter = new TypeNameFormatter(this);
5555
this.templatesWorker = new TemplatesWorker(this);
5656
this.codeFormatter = new CodeFormatter(this);
57-
this.schemaParser = new SchemaParser(this);
57+
this.schemaParserFabric = new SchemaParserFabric(this);
5858
this.schemaRoutes = new SchemaRoutes(this);
5959
this.config.componentTypeNameResolver.logger = this.logger;
6060
}
@@ -86,7 +86,7 @@ class CodeGenProcess {
8686
this.config.componentTypeNameResolver.reserve(componentSchemaNames);
8787

8888
const parsedSchemas = _.map(_.get(swagger.usageSchema.components, "schemas"), (schema, typeName) =>
89-
this.schemaParser.parseSchema(schema, typeName),
89+
this.schemaParserFabric.parseSchema(schema, typeName),
9090
);
9191

9292
this.schemaRoutes.attachSchema({
@@ -95,43 +95,11 @@ class CodeGenProcess {
9595
});
9696

9797
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-
};
13098

13199
const rawConfiguration = {
132100
apiConfig: this.createApiConfig(swagger.usageSchema),
133101
config: this.config,
134-
modelTypes: _.map(sortSchemas(usageComponentSchemas), this.prepareModelType).filter(Boolean),
102+
modelTypes: await this.collectModelTypes(usageComponentSchemas),
135103
rawModelTypes: usageComponentSchemas,
136104
hasSecurityRoutes: this.schemaRoutes.hasSecurityRoutes,
137105
hasQueryRoutes: this.schemaRoutes.hasQueryRoutes,
@@ -156,7 +124,7 @@ class CodeGenProcess {
156124
this.fileSystem.createDir(this.config.output);
157125
}
158126

159-
const files = this.generateOutputFiles({
127+
const files = await this.generateOutputFiles({
160128
configuration: configuration,
161129
});
162130

@@ -206,19 +174,19 @@ class CodeGenProcess {
206174
return {
207175
utils: {
208176
Ts: this.config.Ts,
209-
formatDescription: this.schemaParser.schemaFormatters.formatDescription,
177+
formatDescription: this.schemaParserFabric.schemaFormatters.formatDescription,
210178
internalCase: internalCase,
211179
classNameCase: pascalCase,
212180
pascalCase: pascalCase,
213-
getInlineParseContent: this.schemaParser.getInlineParseContent,
214-
getParseContent: this.schemaParser.getParseContent,
181+
getInlineParseContent: this.schemaParserFabric.getInlineParseContent,
182+
getParseContent: this.schemaParserFabric.getParseContent,
215183
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,
222190
formatModelName: this.typeNameFormatter.format,
223191
fmtToJSDocLine: function fmtToJSDocLine(line, { eol = true }) {
224192
return ` * ${line}${eol ? "\n" : ""}`;
@@ -231,13 +199,61 @@ class CodeGenProcess {
231199
};
232200
};
233201

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+
234250
prepareModelType = (typeInfo) => {
235251
if (!typeInfo.typeData) {
236-
typeInfo.typeData = this.schemaParser.parseSchema(typeInfo.rawTypeData, typeInfo.typeName);
252+
typeInfo.typeData = this.schemaParserFabric.parseSchema(typeInfo.rawTypeData, typeInfo.typeName);
237253
}
238254
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)
241257
: rawTypeData;
242258
let { typeIdentifier, name: originalName, content, description } = typeData;
243259
const name = this.typeNameFormatter.format(originalName);
@@ -264,15 +280,13 @@ class CodeGenProcess {
264280
: this.createSingleFileInfo(templatesToRender, configuration);
265281

266282
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+
}
276290
}
277291

278292
return output.filter((fileInfo) => !!fileInfo && !!fileInfo.content);
@@ -307,37 +321,29 @@ class CodeGenProcess {
307321
}
308322

309323
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+
}
341347
}
342348

343349
return [

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ class ArraySchemaParser extends MonoSchemaParser {
88
const { type, description, items } = this.schema || {};
99

1010
if (_.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
11-
const tupleContent = items.map((item) => this.schemaParser.getInlineParseContent(item, null, this.schemaPath));
11+
const tupleContent = [];
12+
for (const item of items) {
13+
tupleContent.push(
14+
this.schemaParserFabric
15+
.createSchemaParser({ schema: item, schemaPath: this.schemaPath })
16+
.getInlineParseContent(),
17+
);
18+
}
1219
contentType = this.config.Ts.Tuple(tupleContent);
1320
} else {
14-
const content = this.schemaParser.getInlineParseContent(items, null, this.schemaPath);
21+
const content = this.schemaParserFabric
22+
.createSchemaParser({ schema: items, schemaPath: this.schemaPath })
23+
.getInlineParseContent();
1524
contentType = this.config.Ts.ArrayType(content);
1625
}
1726

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class ComplexSchemaParser extends MonoSchemaParser {
2424
this.config.Ts.ExpressionGroup(complexSchemaContent),
2525
this.schemaUtils.getInternalSchemaType(simpleSchema) === SCHEMA_TYPES.OBJECT &&
2626
this.config.Ts.ExpressionGroup(
27-
this.schemaParser.getInlineParseContent(simpleSchema, null, this.schemaPath),
27+
this.schemaParserFabric
28+
.createSchemaParser({ schema: simpleSchema, schemaPath: this.schemaPath })
29+
.getInlineParseContent(),
2830
),
2931
]),
3032
) || this.config.Ts.Keyword.Any,

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
1010
// TODO: disable for now
1111
if (!!this)
1212
// if (this.typeName == null || !discriminator.mapping)
13-
return this.schemaParser.parseSchema(noDiscriminatorSchema, this.typeName, this.schemaPath);
13+
return this.schemaParserFabric
14+
.createSchemaParser({
15+
schema: noDiscriminatorSchema,
16+
typeName: this.typeName,
17+
schemaPath: this.schemaPath,
18+
})
19+
.parseSchema();
1420

1521
const abstractSchemaStruct = this.createAbstractSchemaStruct();
1622
const complexSchemaStruct = this.createComplexSchemaStruct();
@@ -58,7 +64,9 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
5864
const component = this.schemaComponentsMap.createComponent("schemas", mappingTypeName, {
5965
internal: true,
6066
});
61-
const schema = this.schemaParser.parseSchema(component, null, this.schemaPath);
67+
const schema = this.schemaParserFabric
68+
.createSchemaParser({ schema: component, schemaPath: this.schemaPath })
69+
.parseSchema();
6270
schema.genericArgs = [{ name: "Key" }, { name: "Type" }];
6371
schema.internal = true;
6472
schema.content = this.config.Ts.IntersectionType([
@@ -69,7 +77,12 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
6977
}
7078

7179
const createMappingContent = (mappingSchema, mappingKey) => {
72-
const content = this.schemaParser.getInlineParseContent(mappingSchema, null, this.schemaPath);
80+
const content = this.schemaParserFabric
81+
.createSchemaParser({
82+
schema: mappingSchema,
83+
schemaPath: this.schemaPath,
84+
})
85+
.getInlineParseContent();
7386

7487
if (ableToCreateMappingType) {
7588
return this.config.Ts.TypeWithGeneric(mappingTypeName, [this.config.Ts.StringValue(mappingKey), content]);
@@ -136,7 +149,9 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
136149
...schema,
137150
internal: true,
138151
});
139-
const content = this.schemaParser.getInlineParseContent(component, null, this.schemaPath);
152+
const content = this.schemaParserFabric
153+
.createSchemaParser({ schema: component, schemaPath: this.schemaPath })
154+
.getInlineParseContent();
140155

141156
return {
142157
typeName,

0 commit comments

Comments
 (0)