swagger-typescript-api 11.0.0--beta-3 → 11.0.0--beta-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ const _ = require("lodash");
2
+ const { TemplatesGenConfig } = require("./configuration");
3
+ const { FileSystem } = require("../../util/file-system");
4
+ const { Logger } = require("../../util/logger");
5
+ const path = require("path");
6
+
7
+ class TemplatesGenProcess {
8
+ /**
9
+ * @type {TemplatesGenConfig}
10
+ */
11
+ config;
12
+ /**
13
+ * @type {FileSystem}
14
+ */
15
+ fileSystem;
16
+ /**
17
+ * @type {Logger}
18
+ */
19
+ logger;
20
+
21
+ rootDir = path.resolve(__dirname, "../../../");
22
+
23
+ paths = {
24
+ baseTemplates: "templates/base",
25
+ httpClientTemplates: "templates/base/http-clients",
26
+ moduleApiTemplates: "templates/modular",
27
+ defaultApiTemplates: "templates/default",
28
+ };
29
+
30
+ importTemplatePrefixes = ["@base", "@modular", "@default"];
31
+
32
+ constructor(config) {
33
+ this.config = new TemplatesGenConfig(config);
34
+ this.logger = new Logger(this.config);
35
+ this.fileSystem = new FileSystem();
36
+ }
37
+
38
+ /**
39
+ * @return {Promise<GenerateTemplatesOutput>}
40
+ */
41
+ async start() {
42
+ const templates = this.getTemplates();
43
+
44
+ if (this.config.output) {
45
+ const outputPath = path.resolve(process.cwd(), this.config.output);
46
+
47
+ if (this.fileSystem.pathIsExist(outputPath)) {
48
+ if (this.config.cleanOutput) {
49
+ this.fileSystem.cleanDir(outputPath);
50
+ }
51
+ } else {
52
+ this.fileSystem.createDir(outputPath);
53
+ }
54
+
55
+ templates.forEach((template) => {
56
+ this.fileSystem.createFile({
57
+ path: outputPath,
58
+ fileName: template.name,
59
+ content: template.content,
60
+ withPrefix: false,
61
+ });
62
+ });
63
+ }
64
+
65
+ return {
66
+ files: templates,
67
+ configuration: this.config,
68
+ createFile: this.fileSystem.createFile,
69
+ };
70
+ }
71
+
72
+ getTemplates = () => {
73
+ const outputFiles = [];
74
+ const baseTemplates = this.getTemplateNamesFromDir(this.paths.baseTemplates);
75
+ const httpClientTemplates = this.getTemplateNamesFromDir(this.paths.httpClientTemplates);
76
+ const apiTemplatesPath = this.config.modular ? this.paths.moduleApiTemplates : this.paths.defaultApiTemplates;
77
+ const apiTemplates = this.getTemplateNamesFromDir(apiTemplatesPath);
78
+
79
+ for (const fileName of baseTemplates) {
80
+ outputFiles.push({
81
+ name: fileName,
82
+ content: this.fixTemplateContent(this.getTemplateContent(`${this.paths.baseTemplates}/${fileName}`)),
83
+ });
84
+ }
85
+
86
+ const usingHttpClientTemplate = httpClientTemplates.find((template) =>
87
+ template.startsWith(`${this.config.httpClientType}-`),
88
+ );
89
+
90
+ if (usingHttpClientTemplate) {
91
+ outputFiles.push({
92
+ name: usingHttpClientTemplate,
93
+ content: this.fixTemplateContent(
94
+ this.getTemplateContent(`${this.paths.httpClientTemplates}/${usingHttpClientTemplate}`),
95
+ ),
96
+ });
97
+ }
98
+
99
+ for (const fileName of apiTemplates) {
100
+ outputFiles.push({
101
+ name: fileName,
102
+ content: this.fixTemplateContent(this.getTemplateContent(`${apiTemplatesPath}/${fileName}`)),
103
+ });
104
+ }
105
+
106
+ return outputFiles;
107
+ };
108
+
109
+ fixTemplateContent = (content) => {
110
+ const importsRegExp1 = new RegExp(
111
+ `includeFile\\\("(${this.importTemplatePrefixes.map((v) => `(${v})`).join("|")})\/`,
112
+ "g",
113
+ );
114
+ const importsRegExp2 = new RegExp(
115
+ `includeFile\\\(\`(${this.importTemplatePrefixes.map((v) => `(${v})`).join("|")})\/`,
116
+ "g",
117
+ );
118
+ const importsRegExp3 = new RegExp(
119
+ `includeFile\\\(\'(${this.importTemplatePrefixes.map((v) => `(${v})`).join("|")})\/`,
120
+ "g",
121
+ );
122
+
123
+ return content
124
+ .replace(importsRegExp1, 'includeFile("./')
125
+ .replace(importsRegExp2, "includeFile(`./")
126
+ .replace(importsRegExp3, "includeFile('./");
127
+ };
128
+
129
+ getTemplateNamesFromDir = (dir) => {
130
+ return this.fileSystem.readDir(path.resolve(this.rootDir, dir)).filter((file) => file.endsWith(".ejs"));
131
+ };
132
+
133
+ getTemplateContent = (pathToFile) => {
134
+ return this.fileSystem.getFileContent(path.resolve(this.rootDir, pathToFile));
135
+ };
136
+ }
137
+
138
+ module.exports = {
139
+ TemplatesGenProcess,
140
+ };
@@ -1,10 +1,8 @@
1
1
  const { objectAssign } = require("./util/object-assign");
2
2
  const _ = require("lodash");
3
- const constantsBase = require("./constants.js");
4
- const packageJson = require("../package.json");
5
- const { NameResolver, ComponentTypeNameResolver } = require("./util/name-resolver");
6
- const { cosmiconfigSync } = require("cosmiconfig");
7
3
  const CONSTANTS = require("./constants");
4
+ const { ComponentTypeNameResolver } = require("./util/name-resolver");
5
+ const { cosmiconfigSync } = require("cosmiconfig");
8
6
 
9
7
  const TsKeyword = {
10
8
  Number: "number",
@@ -34,8 +32,8 @@ const TsCodeGenKeyword = {
34
32
  /**
35
33
  * @type {GenerateApiConfiguration["config"]}}
36
34
  */
37
- class Configuration {
38
- version = packageJson.version;
35
+ class CodeGenConfig {
36
+ version = CONSTANTS.PROJECT_VERSION;
39
37
  /** CLI flag */
40
38
  templates = "../templates/default";
41
39
  /** CLI flag */
@@ -315,7 +313,7 @@ class Configuration {
315
313
  prettierOptions: prettierOptions === undefined ? getDefaultPrettierOptions() : prettierOptions,
316
314
  hooks: _.merge(this.hooks, hooks || {}),
317
315
  constants: {
318
- ...constantsBase,
316
+ ...CONSTANTS,
319
317
  ...constants,
320
318
  },
321
319
  templateInfos: templateInfos || this.templateInfos,
@@ -348,5 +346,5 @@ const getDefaultPrettierOptions = () => {
348
346
  };
349
347
 
350
348
  module.exports = {
351
- Configuration,
349
+ CodeGenConfig,
352
350
  };
package/src/constants.js CHANGED
@@ -1,3 +1,4 @@
1
+ const packageJson = require("../package.json");
1
2
  const RESERVED_QUERY_ARG_NAMES = ["query", "queryParams", "queryArg"];
2
3
  const RESERVED_BODY_ARG_NAMES = ["data", "body", "reqBody"];
3
4
  const RESERVED_REQ_PARAMS_ARG_NAMES = ["params", "requestParams", "reqParams", "httpParams"];
@@ -23,6 +24,8 @@ const HTTP_CLIENT = {
23
24
  AXIOS: "axios",
24
25
  };
25
26
 
27
+ const PROJECT_VERSION = packageJson.version;
28
+
26
29
  const FILE_PREFIX = `/* eslint-disable */
27
30
  /* tslint:disable */
28
31
  /*
@@ -39,6 +42,7 @@ const FILE_PREFIX = `/* eslint-disable */
39
42
  module.exports = {
40
43
  FILE_PREFIX,
41
44
  DEFAULT_BODY_ARG_NAME: "data",
45
+ PROJECT_VERSION,
42
46
  SCHEMA_TYPES,
43
47
  HTTP_CLIENT,
44
48
  RESERVED_QUERY_ARG_NAMES,
package/src/index.js CHANGED
@@ -9,6 +9,7 @@
9
9
  const _ = require("lodash");
10
10
  const constants = require("./constants");
11
11
  const { CodeGenProcess } = require("./code-gen-process.js");
12
+ const { generateTemplates } = require("./commands/generate-templates");
12
13
 
13
14
  module.exports = {
14
15
  constants: constants,
@@ -20,5 +21,7 @@ module.exports = {
20
21
  });
21
22
  return await codeGenProcess.start();
22
23
  },
23
- generateTemplates: (config) => {},
24
+ generateTemplates: async (config) => {
25
+ return await generateTemplates(config);
26
+ },
24
27
  };
@@ -6,7 +6,7 @@ class SchemaComponentsMap {
6
6
  */
7
7
  data = {};
8
8
  /**
9
- * @type {Configuration}
9
+ * @type {CodeGenConfig}
10
10
  */
11
11
  config;
12
12
 
@@ -3,7 +3,7 @@ const _ = require("lodash");
3
3
 
4
4
  class SchemaFormatters {
5
5
  /**
6
- * @type {Configuration}
6
+ * @type {CodeGenConfig}
7
7
  */
8
8
  config;
9
9
  /**
@@ -5,7 +5,7 @@ const { internalCase } = require("../util/internal-case");
5
5
 
6
6
  class SchemaParser {
7
7
  /**
8
- * @type {Configuration}
8
+ * @type {CodeGenConfig}
9
9
  */
10
10
  config;
11
11
 
@@ -20,7 +20,7 @@ const CONTENT_KIND = {
20
20
 
21
21
  class SchemaRoutes {
22
22
  /**
23
- * @type {Configuration}
23
+ * @type {CodeGenConfig}
24
24
  */
25
25
  config;
26
26
  /**
@@ -1,4 +1,3 @@
1
- const { Configuration } = require("./configuration.js");
2
1
  const _ = require("lodash");
3
2
  const converter = require("swagger2openapi");
4
3
  const https = require("https");
@@ -7,7 +6,7 @@ const yaml = require("js-yaml");
7
6
 
8
7
  class SwaggerSchemaResolver {
9
8
  /**
10
- * @type {Configuration}
9
+ * @type {CodeGenConfig}
11
10
  */
12
11
  config;
13
12
  /**
package/src/templates.js CHANGED
@@ -5,7 +5,7 @@ const path = require("path");
5
5
 
6
6
  class Templates {
7
7
  /**
8
- * @type {Configuration}
8
+ * @type {CodeGenConfig}
9
9
  */
10
10
  config;
11
11
 
package/src/type-name.js CHANGED
@@ -4,7 +4,7 @@ class TypeName {
4
4
  /** @type {Map<string, string>} */
5
5
  formattedModelNamesMap = new Map();
6
6
 
7
- /** @type {Configuration} */
7
+ /** @type {CodeGenConfig} */
8
8
  config;
9
9
 
10
10
  /** @type {Logger} */
@@ -23,6 +23,10 @@ class FileSystem {
23
23
  return fs.readFileSync(path, { encoding: "UTF-8" });
24
24
  };
25
25
 
26
+ readDir = (path) => {
27
+ return fs.readdirSync(path);
28
+ };
29
+
26
30
  pathIsDir = (path) => {
27
31
  if (!path) return false;
28
32
 
@@ -4,7 +4,7 @@ const _ = require("lodash");
4
4
  class Logger {
5
5
  firstLog = true;
6
6
  /**
7
- * @type {Configuration}
7
+ * @type {CodeGenConfig}
8
8
  */
9
9
  config;
10
10
 
@@ -79,7 +79,7 @@ export class HttpClient<SecurityDataType = unknown> {
79
79
  protected createFormData(input: Record<string, unknown>): FormData {
80
80
  return Object.keys(input || {}).reduce((formData, key) => {
81
81
  const property = input[key];
82
- const propertyContent: Iterable<any> = (property instanceof Array) ? property : [property]
82
+ const propertyContent: any[] = (property instanceof Array) ? property : [property]
83
83
 
84
84
  for (const formItem of propertyContent) {
85
85
  const isFileType = formItem instanceof Blob || formItem instanceof File;
File without changes