swagger-typescript-api 12.0.4 → 13.0.0

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.
Files changed (75) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +1 -1
  3. package/cli/constants.js +3 -3
  4. package/cli/execute.js +52 -31
  5. package/cli/index.d.ts +1 -2
  6. package/cli/index.js +18 -17
  7. package/cli/operations/display-help.js +51 -29
  8. package/cli/parse-args.js +3 -3
  9. package/cli/process-option.js +28 -20
  10. package/index.d.ts +99 -7
  11. package/index.js +158 -136
  12. package/package.json +35 -30
  13. package/src/code-formatter.js +28 -13
  14. package/src/code-gen-process.js +367 -264
  15. package/src/commands/generate-templates/configuration.js +2 -2
  16. package/src/commands/generate-templates/index.js +1 -2
  17. package/src/commands/generate-templates/templates-gen-process.js +62 -35
  18. package/src/component-type-name-resolver.js +44 -0
  19. package/src/configuration.js +166 -98
  20. package/src/constants.js +28 -22
  21. package/src/index.js +3 -4
  22. package/src/schema-components-map.js +39 -23
  23. package/src/schema-parser/base-schema-parsers/array.js +43 -0
  24. package/src/schema-parser/base-schema-parsers/complex.js +51 -0
  25. package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
  26. package/src/schema-parser/base-schema-parsers/enum.js +158 -0
  27. package/src/schema-parser/base-schema-parsers/object.js +105 -0
  28. package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
  29. package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
  30. package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
  31. package/src/schema-parser/complex-schema-parsers/not.js +9 -0
  32. package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
  33. package/src/schema-parser/mono-schema-parser.js +48 -0
  34. package/src/schema-parser/schema-formatters.js +58 -44
  35. package/src/schema-parser/schema-parser-fabric.js +131 -0
  36. package/src/schema-parser/schema-parser.js +212 -361
  37. package/src/schema-parser/schema-utils.js +158 -33
  38. package/src/schema-parser/util/enum-key-resolver.js +26 -0
  39. package/src/{schema-parser → schema-routes}/schema-routes.js +472 -203
  40. package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
  41. package/src/schema-walker.js +93 -0
  42. package/src/swagger-schema-resolver.js +61 -28
  43. package/src/templates-worker.js +240 -0
  44. package/src/translators/javascript.js +83 -0
  45. package/src/translators/translator.js +35 -0
  46. package/src/{type-name.js → type-name-formatter.js} +35 -20
  47. package/src/util/file-system.js +30 -14
  48. package/src/util/id.js +2 -2
  49. package/src/util/internal-case.js +1 -1
  50. package/src/util/logger.js +46 -20
  51. package/src/util/name-resolver.js +50 -58
  52. package/src/util/object-assign.js +7 -3
  53. package/src/util/pascal-case.js +1 -1
  54. package/src/util/request.js +5 -5
  55. package/src/util/sort-by-property.js +17 -0
  56. package/templates/base/data-contract-jsdoc.ejs +37 -37
  57. package/templates/base/data-contracts.ejs +40 -28
  58. package/templates/base/enum-data-contract.ejs +12 -12
  59. package/templates/base/http-client.ejs +2 -2
  60. package/templates/base/http-clients/axios-http-client.ejs +139 -138
  61. package/templates/base/http-clients/fetch-http-client.ejs +224 -224
  62. package/templates/base/interface-data-contract.ejs +10 -10
  63. package/templates/base/object-field-jsdoc.ejs +28 -28
  64. package/templates/base/route-docs.ejs +30 -30
  65. package/templates/base/route-name.ejs +42 -42
  66. package/templates/base/route-type.ejs +22 -21
  67. package/templates/base/type-data-contract.ejs +15 -15
  68. package/templates/default/api.ejs +69 -65
  69. package/templates/default/procedure-call.ejs +100 -100
  70. package/templates/default/route-types.ejs +32 -28
  71. package/templates/modular/api.ejs +28 -28
  72. package/templates/modular/procedure-call.ejs +100 -100
  73. package/templates/modular/route-types.ejs +18 -18
  74. package/src/templates.js +0 -177
  75. package/src/translators/JavaScript.js +0 -60
@@ -0,0 +1,34 @@
1
+ const { MonoSchemaParser } = require('../mono-schema-parser');
2
+ const _ = require('lodash');
3
+
4
+ // T1 | T2 | (T1 & T2)
5
+ class AnyOfSchemaParser extends MonoSchemaParser {
6
+ parse() {
7
+ const ignoreTypes = [this.config.Ts.Keyword.Any];
8
+ const combined = _.map(this.schema.anyOf, (childSchema) =>
9
+ this.schemaParserFabric.getInlineParseContent(
10
+ this.schemaUtils.makeAddRequiredToChildSchema(this.schema, childSchema),
11
+ null,
12
+ this.schemaPath,
13
+ ),
14
+ );
15
+ const filtered = this.schemaUtils.filterSchemaContents(
16
+ combined,
17
+ (content) => !ignoreTypes.includes(content),
18
+ );
19
+
20
+ const type = this.config.Ts.UnionType(
21
+ _.compact([
22
+ ...filtered,
23
+ filtered.length > 1 &&
24
+ this.config.Ts.ExpressionGroup(
25
+ this.config.Ts.IntersectionType(filtered),
26
+ ),
27
+ ]),
28
+ );
29
+
30
+ return this.schemaUtils.safeAddNullToType(this.schema, type);
31
+ }
32
+ }
33
+
34
+ module.exports = { AnyOfSchemaParser };
@@ -0,0 +1,9 @@
1
+ const { MonoSchemaParser } = require('../mono-schema-parser');
2
+
3
+ class NotSchemaParser extends MonoSchemaParser {
4
+ parse() {
5
+ return this.config.Ts.Keyword.Any;
6
+ }
7
+ }
8
+
9
+ module.exports = { NotSchemaParser };
@@ -0,0 +1,27 @@
1
+ const { MonoSchemaParser } = require('../mono-schema-parser');
2
+ const _ = require('lodash');
3
+
4
+ // T1 | T2
5
+ class OneOfSchemaParser extends MonoSchemaParser {
6
+ parse() {
7
+ const ignoreTypes = [this.config.Ts.Keyword.Any];
8
+ const combined = _.map(this.schema.oneOf, (childSchema) =>
9
+ this.schemaParserFabric.getInlineParseContent(
10
+ this.schemaUtils.makeAddRequiredToChildSchema(this.schema, childSchema),
11
+ null,
12
+ this.schemaPath,
13
+ ),
14
+ );
15
+
16
+ const filtered = this.schemaUtils.filterSchemaContents(
17
+ combined,
18
+ (content) => !ignoreTypes.includes(content),
19
+ );
20
+
21
+ const type = this.config.Ts.UnionType(filtered);
22
+
23
+ return this.schemaUtils.safeAddNullToType(this.schema, type);
24
+ }
25
+ }
26
+
27
+ module.exports = { OneOfSchemaParser };
@@ -0,0 +1,48 @@
1
+ class MonoSchemaParser {
2
+ schema;
3
+ typeName;
4
+ schemaPath;
5
+
6
+ /** @type {Logger} */
7
+ logger;
8
+ /** @type {SchemaParser} */
9
+ schemaParser;
10
+ /** @type {SchemaParserFabric} */
11
+ schemaParserFabric;
12
+ /** @type {TypeNameFormatter} */
13
+ typeNameFormatter;
14
+ /** @type {SchemaComponentsMap} */
15
+ schemaComponentsMap;
16
+ /** @type {SchemaUtils} */
17
+ schemaUtils;
18
+ /** @type {CodeGenConfig} */
19
+ config;
20
+ /** @type {SchemaFormatters} */
21
+ schemaFormatters;
22
+
23
+ constructor(schemaParser, schema, typeName = null, schemaPath = []) {
24
+ this.schemaParser = schemaParser;
25
+ this.schemaParserFabric = schemaParser.schemaParserFabric;
26
+ this.logger = schemaParser.logger;
27
+ this.schema = schema;
28
+ this.typeName = typeName;
29
+ this.typeNameFormatter = schemaParser.typeNameFormatter;
30
+ this.schemaPath = schemaPath;
31
+ this.schemaComponentsMap = this.schemaParser.schemaComponentsMap;
32
+ this.schemaUtils = this.schemaParser.schemaUtils;
33
+ this.config = this.schemaParser.config;
34
+ this.schemaFormatters = this.schemaParser.schemaFormatters;
35
+ }
36
+
37
+ parse() {
38
+ throw new Error('not implemented');
39
+ }
40
+
41
+ buildTypeNameFromPath = () => {
42
+ return this.schemaUtils.buildTypeNameFromPath(this.schemaPath);
43
+ };
44
+ }
45
+
46
+ module.exports = {
47
+ MonoSchemaParser,
48
+ };
@@ -1,29 +1,24 @@
1
- const { SCHEMA_TYPES } = require("../constants");
2
- const _ = require("lodash");
1
+ const { SCHEMA_TYPES } = require('../constants');
2
+ const _ = require('lodash');
3
3
 
4
4
  class SchemaFormatters {
5
- /**
6
- * @type {CodeGenConfig}
7
- */
5
+ /** @type {CodeGenConfig} */
8
6
  config;
9
- /**
10
- * @type {Logger}
11
- */
7
+ /** @type {Logger} */
12
8
  logger;
9
+ /** @type {TemplatesWorker} */
10
+ templatesWorker;
11
+ /** @type {SchemaUtils} */
12
+ schemaUtils;
13
+
13
14
  /**
14
- * @type {SchemaParser}
15
- */
16
- schemaParser;
17
- /**
18
- * @type {Templates}
15
+ * @param schemaParser {SchemaParser | SchemaParserFabric}
19
16
  */
20
- templates;
21
-
22
- constructor(config, logger, schemaParser, templates) {
23
- this.config = config;
24
- this.logger = logger;
25
- this.schemaParser = schemaParser;
26
- this.templates = templates;
17
+ constructor(schemaParser) {
18
+ this.config = schemaParser.config;
19
+ this.logger = schemaParser.logger;
20
+ this.schemaUtils = schemaParser.schemaUtils;
21
+ this.templatesWorker = schemaParser.templatesWorker;
27
22
  }
28
23
 
29
24
  base = {
@@ -32,7 +27,9 @@ class SchemaFormatters {
32
27
  return {
33
28
  ...parsedSchema,
34
29
  $content: parsedSchema.content,
35
- content: this.config.Ts.UnionType(_.map(parsedSchema.content, ({ value }) => value)),
30
+ content: this.config.Ts.UnionType(
31
+ _.map(parsedSchema.content, ({ value }) => value),
32
+ ),
36
33
  };
37
34
  }
38
35
 
@@ -43,7 +40,8 @@ class SchemaFormatters {
43
40
  };
44
41
  },
45
42
  [SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
46
- if (parsedSchema.nullable) return this.inline[SCHEMA_TYPES.OBJECT](parsedSchema);
43
+ if (parsedSchema.nullable)
44
+ return this.inline[SCHEMA_TYPES.OBJECT](parsedSchema);
47
45
  return {
48
46
  ...parsedSchema,
49
47
  $content: parsedSchema.content,
@@ -68,7 +66,7 @@ class SchemaFormatters {
68
66
  ..._.map(parsedSchema.content, ({ value }) => `${value}`),
69
67
  parsedSchema.nullable && this.config.Ts.Keyword.Null,
70
68
  ]),
71
- ),
69
+ ) || this.config.Ts.Keyword.Any,
72
70
  };
73
71
  },
74
72
  [SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
@@ -76,18 +74,23 @@ class SchemaFormatters {
76
74
  return {
77
75
  ...parsedSchema,
78
76
  typeIdentifier: this.config.Ts.Keyword.Type,
79
- content: this.schemaParser.schemaUtils.safeAddNullToType(parsedSchema.content),
77
+ content: this.schemaUtils.safeAddNullToType(parsedSchema.content),
80
78
  };
81
79
  }
82
80
 
83
81
  return {
84
82
  ...parsedSchema,
85
83
  typeIdentifier: this.config.Ts.Keyword.Type,
86
- content: this.schemaParser.schemaUtils.safeAddNullToType(
84
+ content: this.schemaUtils.safeAddNullToType(
87
85
  parsedSchema,
88
86
  parsedSchema.content.length
89
- ? this.config.Ts.ObjectWrapper(this.formatObjectContent(parsedSchema.content))
90
- : this.config.Ts.RecordType(Ts.Keyword.String, this.config.Ts.Keyword.Any),
87
+ ? this.config.Ts.ObjectWrapper(
88
+ this.formatObjectContent(parsedSchema.content),
89
+ )
90
+ : this.config.Ts.RecordType(
91
+ this.config.Ts.Keyword.String,
92
+ this.config.Ts.Keyword.Any,
93
+ ),
91
94
  ),
92
95
  };
93
96
  },
@@ -97,20 +100,22 @@ class SchemaFormatters {
97
100
  * @param parsedSchema {Record<string, any>}
98
101
  * @param formatType {"base" | "inline"}
99
102
  */
100
- formatSchema = (parsedSchema, formatType = "base") => {
101
- const schemaType = _.get(parsedSchema, ["schemaType"]) || _.get(parsedSchema, ["$parsed", "schemaType"]);
103
+ formatSchema = (parsedSchema, formatType = 'base') => {
104
+ const schemaType =
105
+ _.get(parsedSchema, ['schemaType']) ||
106
+ _.get(parsedSchema, ['$parsed', 'schemaType']);
102
107
  const formatterFn = _.get(this, [formatType, schemaType]);
103
108
  return (formatterFn && formatterFn(parsedSchema)) || parsedSchema;
104
109
  };
105
110
 
106
111
  formatDescription = (description, inline) => {
107
- if (!description) return "";
112
+ if (!description) return '';
108
113
 
109
114
  let prettified = description;
110
115
 
111
- prettified = _.replace(prettified, /\*\//g, "*/");
116
+ prettified = _.replace(prettified, /\*\//g, '*/');
112
117
 
113
- const hasMultipleLines = _.includes(prettified, "\n");
118
+ const hasMultipleLines = _.includes(prettified, '\n');
114
119
 
115
120
  if (!hasMultipleLines) return prettified;
116
121
 
@@ -119,31 +124,40 @@ class SchemaFormatters {
119
124
  .split(/\n/g)
120
125
  .map((part) => _.trim(part))
121
126
  .compact()
122
- .join(" ")
127
+ .join(' ')
123
128
  .valueOf();
124
129
  }
125
130
 
126
- return _.replace(prettified, /\n$/g, "");
131
+ return _.replace(prettified, /\n$/g, '');
127
132
  };
128
133
 
129
134
  formatObjectContent = (content) => {
130
- return _.map(content, (part) => {
131
- const extraSpace = " ";
135
+ const fields = [];
136
+
137
+ for (const part of content) {
138
+ const extraSpace = ' ';
132
139
  const result = `${extraSpace}${part.field},\n`;
133
140
 
134
- const renderedJsDoc = this.templates.renderTemplate(this.config.templatesToRender.dataContractJsDoc, {
135
- data: part,
136
- });
141
+ const renderedJsDoc = this.templatesWorker.renderTemplate(
142
+ this.config.templatesToRender.dataContractJsDoc,
143
+ {
144
+ data: part,
145
+ },
146
+ );
137
147
 
138
148
  const routeNameFromTemplate = renderedJsDoc
139
- .split("\n")
149
+ .split('\n')
140
150
  .map((c) => `${extraSpace}${c}`)
141
- .join("\n");
151
+ .join('\n');
142
152
 
143
- if (routeNameFromTemplate) return `${routeNameFromTemplate}${result}`;
153
+ if (routeNameFromTemplate) {
154
+ fields.push(`${routeNameFromTemplate}${result}`);
155
+ } else {
156
+ fields.push(`${result}`);
157
+ }
158
+ }
144
159
 
145
- return `${result}`;
146
- }).join("");
160
+ return fields.join('');
147
161
  };
148
162
  }
149
163
 
@@ -0,0 +1,131 @@
1
+ const _ = require('lodash');
2
+ const { SchemaFormatters } = require('./schema-formatters');
3
+ const { SchemaUtils } = require('./schema-utils');
4
+ const { SchemaParser } = require('./schema-parser');
5
+
6
+ class SchemaParserFabric {
7
+ /** @type {CodeGenConfig} */
8
+ config;
9
+ /** @type {Logger} */
10
+ logger;
11
+ /** @type {SchemaComponentsMap} */
12
+ schemaComponentsMap;
13
+ /** @type {TypeNameFormatter} */
14
+ typeNameFormatter;
15
+ /** @type {SchemaFormatters} */
16
+ schemaFormatters;
17
+ /** @type {TemplatesWorker} */
18
+ templatesWorker;
19
+ /** @type {SchemaUtils} */
20
+ schemaUtils;
21
+ /** @type {SchemaWalker} */
22
+ schemaWalker;
23
+
24
+ constructor({
25
+ config,
26
+ logger,
27
+ templatesWorker,
28
+ schemaComponentsMap,
29
+ typeNameFormatter,
30
+ schemaWalker,
31
+ }) {
32
+ this.config = config;
33
+ this.logger = logger;
34
+ this.schemaComponentsMap = schemaComponentsMap;
35
+ this.typeNameFormatter = typeNameFormatter;
36
+ this.templatesWorker = templatesWorker;
37
+ this.schemaWalker = schemaWalker;
38
+ this.schemaUtils = new SchemaUtils(this);
39
+ this.schemaFormatters = new SchemaFormatters(this);
40
+ }
41
+
42
+ createSchemaParser = ({ schema, typeName, schemaPath }) => {
43
+ return new SchemaParser(this, { schema, typeName, schemaPath });
44
+ };
45
+
46
+ /**
47
+ *
48
+ * @param content schema content
49
+ * @param linkedSchema link content to attached schema
50
+ * @param linkedComponent link content and other schema props to attached component
51
+ * @param schemaPath
52
+ * @param otherSchemaProps
53
+ * @returns {{}}
54
+ */
55
+ createSchema = ({
56
+ content,
57
+ linkedSchema = {},
58
+ linkedComponent,
59
+ schemaPath,
60
+ ...otherSchemaProps
61
+ }) => {
62
+ const parser = this.createSchemaParser({
63
+ schema: linkedComponent || linkedSchema,
64
+ schemaPath,
65
+ });
66
+ const parsed = parser.parseSchema();
67
+ parsed.content = content;
68
+ Object.assign(parsed, otherSchemaProps);
69
+ if (linkedComponent) {
70
+ linkedComponent.typeData = parsed;
71
+ }
72
+ return parser.schema;
73
+ };
74
+
75
+ createParsedComponent = ({ typeName, schema, schemaPath }) => {
76
+ const schemaCopy = _.cloneDeep(schema);
77
+ const customComponent = this.schemaComponentsMap.createComponent(
78
+ this.schemaComponentsMap.createRef(['components', 'schemas', typeName]),
79
+ schemaCopy,
80
+ );
81
+ const parsed = this.parseSchema(schemaCopy, null, schemaPath);
82
+ parsed.name = typeName;
83
+ customComponent.typeData = parsed;
84
+
85
+ return customComponent;
86
+ };
87
+
88
+ /**
89
+ *
90
+ * @param schema {any}
91
+ * @param typeName {null | string}
92
+ * @param [schemaPath] {string[]}
93
+ * @return {Record<string, any>}
94
+ */
95
+ parseSchema = (schema, typeName = null, schemaPath = []) => {
96
+ const schemaParser = this.createSchemaParser({
97
+ schema,
98
+ typeName,
99
+ schemaPath,
100
+ });
101
+ return schemaParser.parseSchema();
102
+ };
103
+
104
+ /**
105
+ *
106
+ * @param schema {any}
107
+ * @param typeName {null | string}
108
+ * @param [schemaPath] {string[]}
109
+ * @return {Record<string, any>}
110
+ */
111
+ getInlineParseContent = (schema, typeName, schemaPath) => {
112
+ const parser = this.createSchemaParser({ schema, typeName, schemaPath });
113
+ return parser.getInlineParseContent();
114
+ };
115
+
116
+ /**
117
+ *
118
+ * @param schema {any}
119
+ * @param typeName {null | string}
120
+ * @param [schemaPath] {string[]}
121
+ * @return {Record<string, any>}
122
+ */
123
+ getParseContent = (schema, typeName, schemaPath) => {
124
+ const parser = this.createSchemaParser({ schema, typeName, schemaPath });
125
+ return parser.getParseContent();
126
+ };
127
+ }
128
+
129
+ module.exports = {
130
+ SchemaParserFabric,
131
+ };