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
@@ -1,42 +1,58 @@
1
- const _ = require("lodash");
2
- const { SCHEMA_TYPES } = require("../constants");
3
- const { internalCase } = require("../util/internal-case");
4
- const { pascalCase } = require("../util/pascal-case");
1
+ const _ = require('lodash');
2
+ const { SCHEMA_TYPES } = require('../constants');
3
+ const { internalCase } = require('../util/internal-case');
4
+ const { pascalCase } = require('../util/pascal-case');
5
+ const { camelCase } = require('lodash');
5
6
 
6
7
  class SchemaUtils {
7
- /**
8
- * @type {CodeGenConfig}
9
- */
8
+ /** @type {CodeGenConfig} */
10
9
  config;
11
- /**
12
- * @type {SchemaComponentsMap}
13
- */
10
+ /** @type {SchemaComponentsMap} */
14
11
  schemaComponentsMap;
12
+ /** @type {TypeNameFormatter} */
13
+ typeNameFormatter;
14
+ /** @type {SchemaWalker} */
15
+ schemaWalker;
15
16
 
16
- constructor(config, schemaComponentsMap) {
17
+ constructor({
18
+ config,
19
+ schemaComponentsMap,
20
+ typeNameFormatter,
21
+ schemaWalker,
22
+ }) {
17
23
  this.config = config;
18
24
  this.schemaComponentsMap = schemaComponentsMap;
25
+ this.typeNameFormatter = typeNameFormatter;
26
+ this.schemaWalker = schemaWalker;
19
27
  }
20
28
 
21
29
  getRequiredProperties = (schema) => {
22
- return _.uniq((schema && _.isArray(schema.required) && schema.required) || []);
30
+ return _.uniq(
31
+ (schema && _.isArray(schema.required) && schema.required) || [],
32
+ );
23
33
  };
24
34
 
25
35
  isRefSchema = (schema) => {
26
- return !!(schema && schema["$ref"]);
36
+ return !!(schema && schema['$ref']);
27
37
  };
28
38
 
29
39
  getEnumNames = (schema) => {
30
- return schema["x-enumNames"] || schema["xEnumNames"] || schema["x-enumnames"] || schema["x-enum-varnames"];
40
+ return (
41
+ schema['x-enumNames'] ||
42
+ schema['xEnumNames'] ||
43
+ schema['x-enumnames'] ||
44
+ schema['x-enum-varnames']
45
+ );
31
46
  };
32
47
 
33
48
  getSchemaRefType = (schema) => {
34
49
  if (!this.isRefSchema(schema)) return null;
50
+ // const resolved = this.schemaWalker.findByRef(schema.$ref);
35
51
  return this.schemaComponentsMap.get(schema.$ref);
36
52
  };
37
53
 
38
54
  isPropertyRequired = (name, propertySchema, rootSchema) => {
39
- if (propertySchema["x-omitempty"] === false) {
55
+ if (propertySchema['x-omitempty'] === false) {
40
56
  return true;
41
57
  }
42
58
 
@@ -57,7 +73,9 @@ class SchemaUtils {
57
73
  isNullMissingInType = (schema, type) => {
58
74
  const { nullable, type: schemaType } = schema || {};
59
75
  return (
60
- (nullable || !!_.get(schema, "x-nullable") || schemaType === this.config.Ts.Keyword.Null) &&
76
+ (nullable ||
77
+ !!_.get(schema, 'x-nullable') ||
78
+ schemaType === this.config.Ts.Keyword.Null) &&
61
79
  _.isString(type) &&
62
80
  !type.includes(` ${this.config.Ts.Keyword.Null}`) &&
63
81
  !type.includes(`${this.config.Ts.Keyword.Null} `)
@@ -86,7 +104,7 @@ class SchemaUtils {
86
104
  if (_.keys(schema.properties).length) {
87
105
  return SCHEMA_TYPES.OBJECT;
88
106
  }
89
- if (!!schema.items) {
107
+ if (schema.items) {
90
108
  return SCHEMA_TYPES.ARRAY;
91
109
  }
92
110
 
@@ -94,16 +112,21 @@ class SchemaUtils {
94
112
  };
95
113
 
96
114
  checkAndAddRequiredKeys = (schema, resultType) => {
97
- if ("$$requiredKeys" in schema && schema.$$requiredKeys.length) {
115
+ if ('$$requiredKeys' in schema && schema.$$requiredKeys.length) {
98
116
  this.config.update({
99
117
  internalTemplateOptions: {
100
118
  addUtilRequiredKeysType: true,
101
119
  },
102
120
  });
103
- return this.config.Ts.TypeWithGeneric(this.config.Ts.CodeGenKeyword.UtilRequiredKeys, [
104
- resultType,
105
- this.config.Ts.UnionType(schema.$$requiredKeys.map(this.config.Ts.StringValue)),
106
- ]);
121
+ return this.config.Ts.TypeWithGeneric(
122
+ this.config.Ts.CodeGenKeyword.UtilRequiredKeys,
123
+ [
124
+ resultType,
125
+ this.config.Ts.UnionType(
126
+ schema.$$requiredKeys.map(this.config.Ts.StringValue),
127
+ ),
128
+ ],
129
+ );
107
130
  }
108
131
 
109
132
  return resultType;
@@ -112,13 +135,20 @@ class SchemaUtils {
112
135
  makeAddRequiredToChildSchema = (parentSchema, childSchema) => {
113
136
  if (!childSchema) return childSchema;
114
137
 
115
- const required = _.uniq([...this.getRequiredProperties(parentSchema), ...this.getRequiredProperties(childSchema)]);
138
+ const required = _.uniq([
139
+ ...this.getRequiredProperties(parentSchema),
140
+ ...this.getRequiredProperties(childSchema),
141
+ ]);
116
142
 
117
143
  const refData = this.getSchemaRefType(childSchema);
118
144
 
119
145
  if (refData) {
120
- const refObjectProperties = _.keys((refData.rawTypeData && refData.rawTypeData.properties) || {});
121
- const existedRequiredKeys = refObjectProperties.filter((key) => required.includes(key));
146
+ const refObjectProperties = _.keys(
147
+ (refData.rawTypeData && refData.rawTypeData.properties) || {},
148
+ );
149
+ const existedRequiredKeys = refObjectProperties.filter((key) =>
150
+ required.includes(key),
151
+ );
122
152
 
123
153
  if (!existedRequiredKeys.length) return childSchema;
124
154
 
@@ -128,12 +158,17 @@ class SchemaUtils {
128
158
  };
129
159
  } else if (childSchema.properties) {
130
160
  const childSchemaProperties = _.keys(childSchema.properties);
131
- const existedRequiredKeys = childSchemaProperties.filter((key) => required.includes(key));
161
+ const existedRequiredKeys = childSchemaProperties.filter((key) =>
162
+ required.includes(key),
163
+ );
132
164
 
133
165
  if (!existedRequiredKeys.length) return childSchema;
134
166
 
135
167
  return {
136
- required: _.uniq([...this.getRequiredProperties(childSchema), ...existedRequiredKeys]),
168
+ required: _.uniq([
169
+ ...this.getRequiredProperties(childSchema),
170
+ ...existedRequiredKeys,
171
+ ]),
137
172
  ...childSchema,
138
173
  };
139
174
  }
@@ -145,20 +180,110 @@ class SchemaUtils {
145
180
  return _.uniq(_.filter(contents, (type) => filterFn(type)));
146
181
  };
147
182
 
148
- resolveTypeName = (typeName, suffixes, resolver, shouldReserve = true) => {
183
+ resolveTypeName = (
184
+ typeName,
185
+ { suffixes, resolver, prefixes, shouldReserve = true },
186
+ ) => {
149
187
  if (resolver) {
150
- return this.config.componentTypeNameResolver.resolve((reserved) => {
151
- const variant = resolver(pascalCase(typeName), reserved);
152
- if (variant == null) return variant;
153
- return pascalCase(variant);
188
+ return this.config.componentTypeNameResolver.resolve(null, (reserved) => {
189
+ return resolver(pascalCase(typeName), reserved);
154
190
  });
155
191
  } else {
156
192
  return this.config.componentTypeNameResolver.resolve(
157
- suffixes.map((suffix) => pascalCase(`${typeName} ${suffix}`)),
193
+ [
194
+ ...(prefixes || []).map((prefix) =>
195
+ pascalCase(`${prefix} ${typeName}`),
196
+ ),
197
+ ...(suffixes || []).map((suffix) =>
198
+ pascalCase(`${typeName} ${suffix}`),
199
+ ),
200
+ ],
158
201
  shouldReserve,
159
202
  );
160
203
  }
161
204
  };
205
+
206
+ getComplexType = (schema) => {
207
+ if (schema.oneOf) return SCHEMA_TYPES.COMPLEX_ONE_OF;
208
+ if (schema.allOf) return SCHEMA_TYPES.COMPLEX_ALL_OF;
209
+ if (schema.anyOf) return SCHEMA_TYPES.COMPLEX_ANY_OF;
210
+ // TODO :(
211
+ if (schema.not) return SCHEMA_TYPES.COMPLEX_NOT;
212
+
213
+ return SCHEMA_TYPES.COMPLEX_UNKNOWN;
214
+ };
215
+
216
+ getInternalSchemaType = (schema) => {
217
+ if (!_.isEmpty(schema.enum) || !_.isEmpty(this.getEnumNames(schema))) {
218
+ return SCHEMA_TYPES.ENUM;
219
+ }
220
+ if (schema.discriminator) {
221
+ return SCHEMA_TYPES.DISCRIMINATOR;
222
+ }
223
+ if (schema.allOf || schema.oneOf || schema.anyOf || schema.not) {
224
+ return SCHEMA_TYPES.COMPLEX;
225
+ }
226
+ if (!_.isEmpty(schema.properties)) {
227
+ return SCHEMA_TYPES.OBJECT;
228
+ }
229
+ if (schema.type === SCHEMA_TYPES.ARRAY) {
230
+ return SCHEMA_TYPES.ARRAY;
231
+ }
232
+
233
+ return SCHEMA_TYPES.PRIMITIVE;
234
+ };
235
+
236
+ getSchemaType = (schema) => {
237
+ if (!schema) return this.config.Ts.Keyword.Any;
238
+
239
+ const refTypeInfo = this.getSchemaRefType(schema);
240
+
241
+ if (refTypeInfo) {
242
+ return this.checkAndAddRequiredKeys(
243
+ schema,
244
+ this.safeAddNullToType(
245
+ schema,
246
+ this.typeNameFormatter.format(refTypeInfo.typeName),
247
+ ),
248
+ );
249
+ }
250
+
251
+ const primitiveType = this.getSchemaPrimitiveType(schema);
252
+
253
+ if (primitiveType == null) return this.config.Ts.Keyword.Any;
254
+
255
+ let resultType;
256
+
257
+ const typeAlias =
258
+ _.get(this.config.primitiveTypes, [primitiveType, schema.format]) ||
259
+ _.get(this.config.primitiveTypes, [primitiveType, '$default']) ||
260
+ this.config.primitiveTypes[primitiveType];
261
+
262
+ if (_.isFunction(typeAlias)) {
263
+ resultType = typeAlias(schema, this);
264
+ } else {
265
+ resultType = typeAlias || primitiveType;
266
+ }
267
+
268
+ if (!resultType) return this.config.Ts.Keyword.Any;
269
+
270
+ return this.checkAndAddRequiredKeys(
271
+ schema,
272
+ this.safeAddNullToType(schema, resultType),
273
+ );
274
+ };
275
+
276
+ buildTypeNameFromPath = (schemaPath) => {
277
+ schemaPath = _.uniq(_.compact(schemaPath));
278
+
279
+ if (!schemaPath || !schemaPath[0]) return null;
280
+
281
+ return pascalCase(
282
+ camelCase(
283
+ _.uniq([schemaPath[0], schemaPath[schemaPath.length - 1]]).join('_'),
284
+ ),
285
+ );
286
+ };
162
287
  }
163
288
 
164
289
  module.exports = {
@@ -0,0 +1,26 @@
1
+ const { NameResolver } = require('../../util/name-resolver');
2
+
3
+ class EnumKeyResolver extends NameResolver {
4
+ counter = 1;
5
+ /**
6
+ * @param {CodeGenConfig} config;
7
+ * @param {Logger} logger;
8
+ * @param {string[]} reservedNames
9
+ */
10
+ constructor(config, logger, reservedNames) {
11
+ super(config, logger, reservedNames, (variants) => {
12
+ const generatedVariant =
13
+ (variants[0] && `${variants[0]}${this.counter++}`) ||
14
+ `${this.config.enumKeyResolverName}${this.counter++}`;
15
+ this.logger.debug(
16
+ 'generated fallback type name for enum key - ',
17
+ generatedVariant,
18
+ );
19
+ return generatedVariant;
20
+ });
21
+ }
22
+ }
23
+
24
+ module.exports = {
25
+ EnumKeyResolver,
26
+ };