swagger-typescript-api 13.0.9 → 13.0.11

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 (69) hide show
  1. package/README.md +3 -3
  2. package/dist/chunk-3OGOYA5P.cjs +104 -0
  3. package/dist/chunk-3OGOYA5P.cjs.map +1 -0
  4. package/dist/chunk-R4CYI34W.js +65 -0
  5. package/dist/chunk-R4CYI34W.js.map +1 -0
  6. package/dist/cli.cjs +43 -0
  7. package/dist/cli.cjs.map +1 -0
  8. package/dist/cli.js +36 -0
  9. package/dist/cli.js.map +1 -0
  10. package/dist/lib.cjs +21 -0
  11. package/dist/lib.cjs.map +1 -0
  12. package/dist/lib.js +4 -0
  13. package/dist/lib.js.map +1 -0
  14. package/index.d.ts +1 -1
  15. package/package.json +25 -14
  16. package/templates/base/http-clients/axios-http-client.ejs +3 -0
  17. package/templates/base/http-clients/fetch-http-client.ejs +3 -3
  18. package/cli/constants.js +0 -6
  19. package/cli/execute.js +0 -180
  20. package/cli/index.js +0 -94
  21. package/cli/operations/display-help.js +0 -177
  22. package/cli/operations/display-version.js +0 -5
  23. package/cli/parse-args.js +0 -24
  24. package/cli/process-option.js +0 -75
  25. package/index.js +0 -344
  26. package/src/code-formatter.js +0 -114
  27. package/src/code-gen-process.js +0 -569
  28. package/src/commands/generate-templates/configuration.js +0 -31
  29. package/src/commands/generate-templates/index.js +0 -10
  30. package/src/commands/generate-templates/templates-gen-process.js +0 -205
  31. package/src/component-type-name-resolver.js +0 -42
  32. package/src/configuration.js +0 -445
  33. package/src/constants.js +0 -77
  34. package/src/index.js +0 -16
  35. package/src/schema-components-map.js +0 -76
  36. package/src/schema-parser/base-schema-parsers/array.js +0 -41
  37. package/src/schema-parser/base-schema-parsers/complex.js +0 -49
  38. package/src/schema-parser/base-schema-parsers/discriminator.js +0 -305
  39. package/src/schema-parser/base-schema-parsers/enum.js +0 -156
  40. package/src/schema-parser/base-schema-parsers/object.js +0 -103
  41. package/src/schema-parser/base-schema-parsers/primitive.js +0 -61
  42. package/src/schema-parser/complex-schema-parsers/all-of.js +0 -26
  43. package/src/schema-parser/complex-schema-parsers/any-of.js +0 -27
  44. package/src/schema-parser/complex-schema-parsers/not.js +0 -9
  45. package/src/schema-parser/complex-schema-parsers/one-of.js +0 -27
  46. package/src/schema-parser/mono-schema-parser.js +0 -46
  47. package/src/schema-parser/schema-formatters.js +0 -164
  48. package/src/schema-parser/schema-parser-fabric.js +0 -130
  49. package/src/schema-parser/schema-parser.js +0 -295
  50. package/src/schema-parser/schema-utils.js +0 -319
  51. package/src/schema-parser/util/enum-key-resolver.js +0 -24
  52. package/src/schema-routes/schema-routes.js +0 -1208
  53. package/src/schema-routes/util/specific-arg-name-resolver.js +0 -24
  54. package/src/schema-walker.js +0 -91
  55. package/src/swagger-schema-resolver.js +0 -195
  56. package/src/templates-worker.js +0 -243
  57. package/src/translators/javascript.js +0 -81
  58. package/src/translators/translator.js +0 -33
  59. package/src/type-name-formatter.js +0 -111
  60. package/src/util/file-system.js +0 -95
  61. package/src/util/id.js +0 -7
  62. package/src/util/internal-case.js +0 -7
  63. package/src/util/logger.js +0 -142
  64. package/src/util/name-resolver.js +0 -103
  65. package/src/util/object-assign.js +0 -17
  66. package/src/util/pascal-case.js +0 -7
  67. package/src/util/random.js +0 -11
  68. package/src/util/request.js +0 -63
  69. package/src/util/sort-by-property.js +0 -15
@@ -1,319 +0,0 @@
1
- import _ from "lodash";
2
- import { SCHEMA_TYPES } from "../constants.js";
3
- import { internalCase } from "../util/internal-case.js";
4
- import { pascalCase } from "../util/pascal-case.js";
5
-
6
- class SchemaUtils {
7
- /** @type {CodeGenConfig} */
8
- config;
9
- /** @type {SchemaComponentsMap} */
10
- schemaComponentsMap;
11
- /** @type {TypeNameFormatter} */
12
- typeNameFormatter;
13
- /** @type {SchemaWalker} */
14
- schemaWalker;
15
-
16
- constructor({
17
- config,
18
- schemaComponentsMap,
19
- typeNameFormatter,
20
- schemaWalker,
21
- }) {
22
- this.config = config;
23
- this.schemaComponentsMap = schemaComponentsMap;
24
- this.typeNameFormatter = typeNameFormatter;
25
- this.schemaWalker = schemaWalker;
26
- }
27
-
28
- getRequiredProperties = (schema) => {
29
- return _.uniq(
30
- (schema && _.isArray(schema.required) && schema.required) || [],
31
- );
32
- };
33
-
34
- isRefSchema = (schema) => {
35
- return !!schema?.$ref;
36
- };
37
-
38
- getEnumNames = (schema) => {
39
- return (
40
- schema["x-enumNames"] ||
41
- schema.xEnumNames ||
42
- schema["x-enumnames"] ||
43
- schema["x-enum-varnames"]
44
- );
45
- };
46
-
47
- getSchemaRefType = (schema) => {
48
- if (!this.isRefSchema(schema)) return null;
49
- // const resolved = this.schemaWalker.findByRef(schema.$ref);
50
- return this.schemaComponentsMap.get(schema.$ref);
51
- };
52
-
53
- isPropertyRequired = (name, propertySchema, rootSchema) => {
54
- if (propertySchema["x-omitempty"] === false) {
55
- return true;
56
- }
57
-
58
- const isRequired = _.isBoolean(propertySchema.required)
59
- ? !!propertySchema.required
60
- : _.isArray(rootSchema.required)
61
- ? rootSchema.required.includes(name)
62
- : !!rootSchema.required;
63
-
64
- if (this.config.convertedFromSwagger2) {
65
- return typeof propertySchema.nullable === this.config.Ts.Keyword.Undefined
66
- ? isRequired
67
- : !propertySchema.nullable;
68
- }
69
- return isRequired;
70
- };
71
-
72
- isNullMissingInType = (schema, type) => {
73
- const { nullable, type: schemaType } = schema || {};
74
- return (
75
- (nullable ||
76
- !!_.get(schema, "x-nullable") ||
77
- schemaType === this.config.Ts.Keyword.Null) &&
78
- _.isString(type) &&
79
- !type.includes(` ${this.config.Ts.Keyword.Null}`) &&
80
- !type.includes(`${this.config.Ts.Keyword.Null} `)
81
- );
82
- };
83
-
84
- safeAddNullToType = (schema, type) => {
85
- if (this.isNullMissingInType(schema, type)) {
86
- return this.config.Ts.UnionType([type, this.config.Ts.Keyword.Null]);
87
- }
88
- return type;
89
- };
90
-
91
- getSchemaPrimitiveType = (rawSchema) => {
92
- const schema = rawSchema || {};
93
-
94
- if (schema.type) {
95
- return internalCase(schema.type);
96
- }
97
- if (schema.enum) {
98
- const enumFieldType = typeof schema.enum[0];
99
- if (enumFieldType === this.config.Ts.Keyword.Undefined) return;
100
-
101
- return internalCase(enumFieldType);
102
- }
103
- if (_.keys(schema.properties).length) {
104
- return SCHEMA_TYPES.OBJECT;
105
- }
106
- if (schema.items) {
107
- return SCHEMA_TYPES.ARRAY;
108
- }
109
-
110
- return null;
111
- };
112
-
113
- checkAndAddRequiredKeys = (schema, resultType) => {
114
- if ("$$requiredKeys" in schema && schema.$$requiredKeys.length) {
115
- this.config.update({
116
- internalTemplateOptions: {
117
- addUtilRequiredKeysType: true,
118
- },
119
- });
120
- return this.config.Ts.TypeWithGeneric(
121
- this.config.Ts.CodeGenKeyword.UtilRequiredKeys,
122
- [
123
- resultType,
124
- this.config.Ts.UnionType(
125
- schema.$$requiredKeys.map(this.config.Ts.StringValue),
126
- ),
127
- ],
128
- );
129
- }
130
-
131
- return resultType;
132
- };
133
-
134
- makeAddRequiredToChildSchema = (parentSchema, childSchema) => {
135
- if (!childSchema) return childSchema;
136
-
137
- const required = _.uniq([
138
- ...this.getRequiredProperties(parentSchema),
139
- ...this.getRequiredProperties(childSchema),
140
- ]);
141
-
142
- const refData = this.getSchemaRefType(childSchema);
143
-
144
- if (refData) {
145
- const refObjectProperties = _.keys(refData.rawTypeData?.properties || {});
146
- const existedRequiredKeys = refObjectProperties.filter((key) =>
147
- required.includes(key),
148
- );
149
-
150
- if (!existedRequiredKeys.length) return childSchema;
151
-
152
- return {
153
- ...childSchema,
154
- $$requiredKeys: existedRequiredKeys,
155
- };
156
- } else if (childSchema.properties) {
157
- const childSchemaProperties = _.keys(childSchema.properties);
158
- const existedRequiredKeys = childSchemaProperties.filter((key) =>
159
- required.includes(key),
160
- );
161
-
162
- if (!existedRequiredKeys.length) return childSchema;
163
-
164
- return {
165
- required: _.uniq([
166
- ...this.getRequiredProperties(childSchema),
167
- ...existedRequiredKeys,
168
- ]),
169
- ...childSchema,
170
- };
171
- }
172
-
173
- return childSchema;
174
- };
175
-
176
- filterSchemaContents = (contents, filterFn) => {
177
- return _.uniq(_.filter(contents, (type) => filterFn(type)));
178
- };
179
-
180
- resolveTypeName = (
181
- typeName,
182
- { suffixes, resolver, prefixes, shouldReserve = true },
183
- ) => {
184
- if (resolver) {
185
- return this.config.componentTypeNameResolver.resolve(null, (reserved) => {
186
- return resolver(pascalCase(typeName), reserved);
187
- });
188
- } else {
189
- return this.config.componentTypeNameResolver.resolve(
190
- [
191
- ...(prefixes || []).map((prefix) =>
192
- pascalCase(`${prefix} ${typeName}`),
193
- ),
194
- ...(suffixes || []).map((suffix) =>
195
- pascalCase(`${typeName} ${suffix}`),
196
- ),
197
- ],
198
- shouldReserve,
199
- );
200
- }
201
- };
202
-
203
- getComplexType = (schema) => {
204
- if (schema.oneOf) return SCHEMA_TYPES.COMPLEX_ONE_OF;
205
- if (schema.allOf) return SCHEMA_TYPES.COMPLEX_ALL_OF;
206
- if (schema.anyOf) return SCHEMA_TYPES.COMPLEX_ANY_OF;
207
- // TODO :(
208
- if (schema.not) return SCHEMA_TYPES.COMPLEX_NOT;
209
-
210
- return SCHEMA_TYPES.COMPLEX_UNKNOWN;
211
- };
212
-
213
- getInternalSchemaType = (schema) => {
214
- if (!_.isEmpty(schema.enum) || !_.isEmpty(this.getEnumNames(schema))) {
215
- return SCHEMA_TYPES.ENUM;
216
- }
217
- if (schema.discriminator) {
218
- return SCHEMA_TYPES.DISCRIMINATOR;
219
- }
220
- if (schema.allOf || schema.oneOf || schema.anyOf || schema.not) {
221
- return SCHEMA_TYPES.COMPLEX;
222
- }
223
- if (!_.isEmpty(schema.properties)) {
224
- return SCHEMA_TYPES.OBJECT;
225
- }
226
- if (schema.type === SCHEMA_TYPES.ARRAY) {
227
- return SCHEMA_TYPES.ARRAY;
228
- }
229
-
230
- return SCHEMA_TYPES.PRIMITIVE;
231
- };
232
-
233
- getSchemaType = (schema) => {
234
- if (!schema) return this.config.Ts.Keyword.Any;
235
-
236
- const refTypeInfo = this.getSchemaRefType(schema);
237
-
238
- if (refTypeInfo) {
239
- return this.checkAndAddRequiredKeys(
240
- schema,
241
- this.safeAddNullToType(
242
- schema,
243
- this.typeNameFormatter.format(refTypeInfo.typeName),
244
- ),
245
- );
246
- }
247
-
248
- let resultType;
249
-
250
- if (this.isConstantSchema(schema)) {
251
- resultType = this.formatJsValue(schema.const);
252
- } else {
253
- const primitiveType = this.getSchemaPrimitiveType(schema);
254
-
255
- if (primitiveType == null) {
256
- return this.config.Ts.Keyword.Any;
257
- }
258
-
259
- const typeAlias =
260
- _.get(this.config.primitiveTypes, [primitiveType, schema.format]) ||
261
- _.get(this.config.primitiveTypes, [primitiveType, "$default"]) ||
262
- this.config.primitiveTypes[primitiveType];
263
-
264
- if (_.isFunction(typeAlias)) {
265
- resultType = typeAlias(schema, this);
266
- } else {
267
- resultType = typeAlias || primitiveType;
268
- }
269
- }
270
-
271
- if (!resultType) {
272
- return this.config.Ts.Keyword.Any;
273
- }
274
-
275
- return this.checkAndAddRequiredKeys(
276
- schema,
277
- this.safeAddNullToType(schema, resultType),
278
- );
279
- };
280
-
281
- buildTypeNameFromPath = (schemaPath) => {
282
- schemaPath = _.uniq(_.compact(schemaPath));
283
-
284
- if (!schemaPath || !schemaPath[0]) return null;
285
-
286
- return pascalCase(
287
- _.camelCase(
288
- _.uniq([schemaPath[0], schemaPath[schemaPath.length - 1]]).join("_"),
289
- ),
290
- );
291
- };
292
-
293
- isConstantSchema(schema) {
294
- return "const" in schema;
295
- }
296
-
297
- formatJsValue = (value) => {
298
- switch (typeof value) {
299
- case "string": {
300
- return this.config.Ts.StringValue(value);
301
- }
302
- case "boolean": {
303
- return this.config.Ts.BooleanValue(value);
304
- }
305
- case "number": {
306
- return this.config.Ts.NumberValue(value);
307
- }
308
- default: {
309
- if (value === null) {
310
- return this.config.Ts.NullValue(value);
311
- }
312
-
313
- return this.config.Ts.Keyword.Any;
314
- }
315
- }
316
- };
317
- }
318
-
319
- export { SchemaUtils };
@@ -1,24 +0,0 @@
1
- import { NameResolver } from "../../util/name-resolver.js";
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
- export { EnumKeyResolver };