swagger-typescript-api 13.0.0-experimental-1 → 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 (81) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +22 -12
  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 +113 -8
  11. package/index.js +158 -135
  12. package/package.json +35 -30
  13. package/src/code-formatter.js +28 -13
  14. package/src/code-gen-process.js +357 -259
  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 +167 -95
  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 +69 -60
  35. package/src/schema-parser/schema-parser-fabric.js +131 -0
  36. package/src/schema-parser/schema-parser.js +208 -427
  37. package/src/schema-parser/schema-utils.js +123 -58
  38. package/src/schema-parser/util/enum-key-resolver.js +26 -0
  39. package/src/schema-routes/schema-routes.js +1225 -0
  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-formatter.js +33 -18
  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 +52 -60
  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/README.md +17 -17
  57. package/templates/base/README.md +7 -7
  58. package/templates/base/data-contract-jsdoc.ejs +37 -37
  59. package/templates/base/data-contracts.ejs +40 -27
  60. package/templates/base/enum-data-contract.ejs +12 -12
  61. package/templates/base/http-client.ejs +3 -3
  62. package/templates/base/http-clients/axios-http-client.ejs +139 -138
  63. package/templates/base/http-clients/fetch-http-client.ejs +224 -224
  64. package/templates/base/interface-data-contract.ejs +10 -10
  65. package/templates/base/object-field-jsdoc.ejs +28 -28
  66. package/templates/base/route-docs.ejs +30 -30
  67. package/templates/base/route-name.ejs +42 -42
  68. package/templates/base/route-type.ejs +22 -21
  69. package/templates/base/type-data-contract.ejs +15 -15
  70. package/templates/default/README.md +6 -6
  71. package/templates/default/api.ejs +69 -68
  72. package/templates/default/procedure-call.ejs +100 -100
  73. package/templates/default/route-types.ejs +32 -32
  74. package/templates/modular/README.md +6 -6
  75. package/templates/modular/api.ejs +28 -28
  76. package/templates/modular/procedure-call.ejs +100 -100
  77. package/templates/modular/route-types.ejs +18 -18
  78. package/src/schema-parser/schema-processor.js +0 -79
  79. package/src/schema-parser/schema-routes.js +0 -950
  80. package/src/templates.js +0 -182
  81. 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 {SchemaProcessor}
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
 
@@ -42,12 +39,13 @@ class SchemaFormatters {
42
39
  content: this.config.Ts.EnumFieldsWrapper(parsedSchema.content),
43
40
  };
44
41
  },
45
- [SCHEMA_TYPES.OBJECT]: async (parsedSchema) => {
46
- if (parsedSchema.nullable) return this.inline[SCHEMA_TYPES.OBJECT](parsedSchema);
42
+ [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,
50
- content: await this.formatObjectContent(parsedSchema.content),
48
+ content: this.formatObjectContent(parsedSchema.content),
51
49
  };
52
50
  },
53
51
  [SCHEMA_TYPES.PRIMITIVE]: (parsedSchema) => {
@@ -68,26 +66,31 @@ 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
- [SCHEMA_TYPES.OBJECT]: async (parsedSchema) => {
72
+ [SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
75
73
  if (_.isString(parsedSchema.content)) {
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(await 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
  },
@@ -95,23 +98,24 @@ class SchemaFormatters {
95
98
 
96
99
  /**
97
100
  * @param parsedSchema {Record<string, any>}
98
- * @param cfg {{ formatType?: "base" | "inline", schemaType?: string } }
101
+ * @param formatType {"base" | "inline"}
99
102
  */
100
- formatSchema = async (parsedSchema, { schemaType: outerSchemaType, formatType = "base" } = {}) => {
103
+ formatSchema = (parsedSchema, formatType = 'base') => {
101
104
  const schemaType =
102
- outerSchemaType || _.get(parsedSchema, ["schemaType"]) || _.get(parsedSchema, ["$parsed", "schemaType"]);
105
+ _.get(parsedSchema, ['schemaType']) ||
106
+ _.get(parsedSchema, ['$parsed', 'schemaType']);
103
107
  const formatterFn = _.get(this, [formatType, schemaType]);
104
- return (formatterFn && (await formatterFn(parsedSchema))) || parsedSchema;
108
+ return (formatterFn && formatterFn(parsedSchema)) || parsedSchema;
105
109
  };
106
110
 
107
111
  formatDescription = (description, inline) => {
108
- if (!description) return "";
112
+ if (!description) return '';
109
113
 
110
114
  let prettified = description;
111
115
 
112
- prettified = _.replace(prettified, /\*\//g, "*/");
116
+ prettified = _.replace(prettified, /\*\//g, '*/');
113
117
 
114
- const hasMultipleLines = _.includes(prettified, "\n");
118
+ const hasMultipleLines = _.includes(prettified, '\n');
115
119
 
116
120
  if (!hasMultipleLines) return prettified;
117
121
 
@@ -120,35 +124,40 @@ class SchemaFormatters {
120
124
  .split(/\n/g)
121
125
  .map((part) => _.trim(part))
122
126
  .compact()
123
- .join(" ")
127
+ .join(' ')
124
128
  .valueOf();
125
129
  }
126
130
 
127
- return _.replace(prettified, /\n$/g, "");
131
+ return _.replace(prettified, /\n$/g, '');
128
132
  };
129
133
 
130
- formatObjectContent = async (content) => {
131
- return (
132
- await Promise.all(
133
- _.map(content, async (part) => {
134
- const extraSpace = " ";
135
- const result = `${extraSpace}${part.field},\n`;
136
-
137
- const renderedJsDoc = await this.templates.renderTemplate(this.config.templatesToRender.dataContractJsDoc, {
138
- data: part,
139
- });
140
-
141
- const routeNameFromTemplate = renderedJsDoc
142
- .split("\n")
143
- .map((c) => `${extraSpace}${c}`)
144
- .join("\n");
145
-
146
- if (routeNameFromTemplate) return `${routeNameFromTemplate}${result}`;
134
+ formatObjectContent = (content) => {
135
+ const fields = [];
136
+
137
+ for (const part of content) {
138
+ const extraSpace = ' ';
139
+ const result = `${extraSpace}${part.field},\n`;
140
+
141
+ const renderedJsDoc = this.templatesWorker.renderTemplate(
142
+ this.config.templatesToRender.dataContractJsDoc,
143
+ {
144
+ data: part,
145
+ },
146
+ );
147
+
148
+ const routeNameFromTemplate = renderedJsDoc
149
+ .split('\n')
150
+ .map((c) => `${extraSpace}${c}`)
151
+ .join('\n');
152
+
153
+ if (routeNameFromTemplate) {
154
+ fields.push(`${routeNameFromTemplate}${result}`);
155
+ } else {
156
+ fields.push(`${result}`);
157
+ }
158
+ }
147
159
 
148
- return `${result}`;
149
- }),
150
- )
151
- ).join("");
160
+ return fields.join('');
152
161
  };
153
162
  }
154
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
+ };