swagger-typescript-api 13.0.8 → 13.0.10

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 (67) hide show
  1. package/README.md +1 -1
  2. package/cli/index.d.ts +7 -6
  3. package/dist/chunk-3S356DIU.cjs +105 -0
  4. package/dist/chunk-3S356DIU.cjs.map +1 -0
  5. package/dist/chunk-MTWJNW6B.js +65 -0
  6. package/dist/chunk-MTWJNW6B.js.map +1 -0
  7. package/dist/cli.cjs +43 -0
  8. package/dist/cli.cjs.map +1 -0
  9. package/dist/cli.js +36 -0
  10. package/dist/cli.js.map +1 -0
  11. package/dist/lib.cjs +21 -0
  12. package/dist/lib.cjs.map +1 -0
  13. package/dist/lib.js +4 -0
  14. package/dist/lib.js.map +1 -0
  15. package/package.json +30 -16
  16. package/cli/constants.js +0 -10
  17. package/cli/execute.js +0 -182
  18. package/cli/index.js +0 -96
  19. package/cli/operations/display-help.js +0 -179
  20. package/cli/operations/display-version.js +0 -5
  21. package/cli/parse-args.js +0 -26
  22. package/cli/process-option.js +0 -77
  23. package/index.js +0 -349
  24. package/src/code-formatter.js +0 -116
  25. package/src/code-gen-process.js +0 -571
  26. package/src/commands/generate-templates/configuration.js +0 -33
  27. package/src/commands/generate-templates/index.js +0 -16
  28. package/src/commands/generate-templates/templates-gen-process.js +0 -204
  29. package/src/component-type-name-resolver.js +0 -44
  30. package/src/configuration.js +0 -448
  31. package/src/constants.js +0 -65
  32. package/src/index.js +0 -26
  33. package/src/schema-components-map.js +0 -78
  34. package/src/schema-parser/base-schema-parsers/array.js +0 -43
  35. package/src/schema-parser/base-schema-parsers/complex.js +0 -51
  36. package/src/schema-parser/base-schema-parsers/discriminator.js +0 -307
  37. package/src/schema-parser/base-schema-parsers/enum.js +0 -158
  38. package/src/schema-parser/base-schema-parsers/object.js +0 -105
  39. package/src/schema-parser/base-schema-parsers/primitive.js +0 -63
  40. package/src/schema-parser/complex-schema-parsers/all-of.js +0 -26
  41. package/src/schema-parser/complex-schema-parsers/any-of.js +0 -27
  42. package/src/schema-parser/complex-schema-parsers/not.js +0 -9
  43. package/src/schema-parser/complex-schema-parsers/one-of.js +0 -27
  44. package/src/schema-parser/mono-schema-parser.js +0 -48
  45. package/src/schema-parser/schema-formatters.js +0 -166
  46. package/src/schema-parser/schema-parser-fabric.js +0 -132
  47. package/src/schema-parser/schema-parser.js +0 -300
  48. package/src/schema-parser/schema-utils.js +0 -322
  49. package/src/schema-parser/util/enum-key-resolver.js +0 -26
  50. package/src/schema-routes/schema-routes.js +0 -1213
  51. package/src/schema-routes/util/specific-arg-name-resolver.js +0 -26
  52. package/src/schema-walker.js +0 -93
  53. package/src/swagger-schema-resolver.js +0 -197
  54. package/src/templates-worker.js +0 -243
  55. package/src/translators/javascript.js +0 -83
  56. package/src/translators/translator.js +0 -35
  57. package/src/type-name-formatter.js +0 -113
  58. package/src/util/file-system.js +0 -95
  59. package/src/util/id.js +0 -9
  60. package/src/util/internal-case.js +0 -5
  61. package/src/util/logger.js +0 -144
  62. package/src/util/name-resolver.js +0 -105
  63. package/src/util/object-assign.js +0 -19
  64. package/src/util/pascal-case.js +0 -5
  65. package/src/util/random.js +0 -14
  66. package/src/util/request.js +0 -65
  67. package/src/util/sort-by-property.js +0 -17
package/src/constants.js DELETED
@@ -1,65 +0,0 @@
1
- const packageJson = require("../package.json");
2
- const RESERVED_QUERY_ARG_NAMES = ["query", "queryParams", "queryArg"];
3
- const RESERVED_BODY_ARG_NAMES = ["data", "body", "reqBody"];
4
- const RESERVED_REQ_PARAMS_ARG_NAMES = [
5
- "params",
6
- "requestParams",
7
- "reqParams",
8
- "httpParams",
9
- ];
10
- const RESERVED_PATH_ARG_NAMES = ["path", "pathParams"];
11
- const RESERVED_HEADER_ARG_NAMES = ["headers", "headersParams"];
12
-
13
- const SCHEMA_TYPES = {
14
- ARRAY: "array",
15
- OBJECT: "object",
16
- ENUM: "enum",
17
- REF: "$ref",
18
- PRIMITIVE: "primitive",
19
- COMPLEX: "complex",
20
- DISCRIMINATOR: "discriminator",
21
- COMPLEX_ONE_OF: "oneOf",
22
- COMPLEX_ANY_OF: "anyOf",
23
- COMPLEX_ALL_OF: "allOf",
24
- COMPLEX_NOT: "not",
25
- COMPLEX_UNKNOWN: "__unknown",
26
- };
27
-
28
- const HTTP_CLIENT = {
29
- FETCH: "fetch",
30
- AXIOS: "axios",
31
- };
32
-
33
- const PROJECT_VERSION = packageJson.version;
34
-
35
- const FILE_PREFIX = `/* eslint-disable */
36
- /* tslint:disable */
37
- /*
38
- * ---------------------------------------------------------------
39
- * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
40
- * ## ##
41
- * ## AUTHOR: acacode ##
42
- * ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
43
- * ---------------------------------------------------------------
44
- */
45
-
46
- `;
47
-
48
- module.exports = {
49
- FILE_PREFIX,
50
- DEFAULT_BODY_ARG_NAME: "data",
51
- PROJECT_VERSION,
52
- SCHEMA_TYPES,
53
- HTTP_CLIENT,
54
- RESERVED_QUERY_ARG_NAMES,
55
- RESERVED_BODY_ARG_NAMES,
56
- RESERVED_REQ_PARAMS_ARG_NAMES,
57
- RESERVED_PATH_ARG_NAMES,
58
- RESERVED_HEADER_ARG_NAMES,
59
- PRETTIER_OPTIONS: {
60
- printWidth: 120,
61
- tabWidth: 2,
62
- trailingComma: "all",
63
- parser: "typescript",
64
- },
65
- };
package/src/index.js DELETED
@@ -1,26 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- // Copyright (c) 2019-present acacode
4
- // Node module: swagger-typescript-api
5
- // This file is licensed under the MIT License.
6
- // License text available at https://opensource.org/licenses/MIT
7
- // Repository https://github.com/acacode/swagger-typescript-api
8
-
9
- const constants = require("./constants");
10
- const { CodeGenProcess } = require("./code-gen-process.js");
11
- const { generateTemplates } = require("./commands/generate-templates");
12
-
13
- module.exports = {
14
- constants: constants,
15
- generateApi: async ({ name, prettier, ...config }) => {
16
- const codeGenProcess = new CodeGenProcess({
17
- ...config,
18
- fileName: name,
19
- prettierOptions: prettier,
20
- });
21
- return await codeGenProcess.start();
22
- },
23
- generateTemplates: async (config) => {
24
- return await generateTemplates(config);
25
- },
26
- };
@@ -1,78 +0,0 @@
1
- const _ = require("lodash");
2
-
3
- class SchemaComponentsMap {
4
- /** @type {SchemaComponent[]} */
5
- _data = [];
6
- /** @type {CodeGenConfig} */
7
- config;
8
-
9
- constructor({ config }) {
10
- this.config = config;
11
- }
12
-
13
- clear() {
14
- this._data = [];
15
- }
16
-
17
- createRef = (paths) => {
18
- return ["#", ...paths].join("/");
19
- };
20
-
21
- parseRef = (ref) => {
22
- return ref.split("/");
23
- };
24
-
25
- createComponent($ref, rawTypeData) {
26
- const parsed = this.parseRef($ref);
27
- const typeName = parsed[parsed.length - 1];
28
- const componentName = parsed[parsed.length - 2];
29
- const componentSchema = {
30
- $ref,
31
- typeName,
32
- rawTypeData,
33
- componentName,
34
- /** result from schema parser */
35
- typeData: null,
36
- };
37
-
38
- const usageComponent =
39
- this.config.hooks.onCreateComponent(componentSchema) || componentSchema;
40
-
41
- const refIndex = this._data.findIndex((c) => c.$ref === $ref);
42
-
43
- if (refIndex === -1) {
44
- this._data.push(usageComponent);
45
- } else {
46
- this._data[refIndex] = usageComponent;
47
- }
48
-
49
- return usageComponent;
50
- }
51
-
52
- /**
53
- * @returns {SchemaComponent[]}
54
- */
55
- getComponents() {
56
- return this._data;
57
- }
58
-
59
- /**
60
- * @params {...string[]} componentNames
61
- * @returns {SchemaComponent[]}
62
- */
63
- filter(...componentNames) {
64
- return _.filter(this._data, (it) =>
65
- componentNames.some((componentName) =>
66
- _.startsWith(it.$ref, `#/components/${componentName}`),
67
- ),
68
- );
69
- }
70
-
71
- get($ref) {
72
- return this._data.find((c) => c.$ref === $ref) || null;
73
- }
74
- }
75
-
76
- module.exports = {
77
- SchemaComponentsMap,
78
- };
@@ -1,43 +0,0 @@
1
- const { MonoSchemaParser } = require("../mono-schema-parser");
2
- const _ = require("lodash");
3
- const { SCHEMA_TYPES } = require("../../constants");
4
-
5
- class ArraySchemaParser extends MonoSchemaParser {
6
- parse() {
7
- let contentType;
8
- const { type, description, items } = this.schema || {};
9
-
10
- if (_.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
11
- const tupleContent = [];
12
- for (const item of items) {
13
- tupleContent.push(
14
- this.schemaParserFabric
15
- .createSchemaParser({ schema: item, schemaPath: this.schemaPath })
16
- .getInlineParseContent(),
17
- );
18
- }
19
- contentType = this.config.Ts.Tuple(tupleContent);
20
- } else {
21
- const content = this.schemaParserFabric
22
- .createSchemaParser({ schema: items, schemaPath: this.schemaPath })
23
- .getInlineParseContent();
24
- contentType = this.config.Ts.ArrayType(content);
25
- }
26
-
27
- return {
28
- ...(_.isObject(this.schema) ? this.schema : {}),
29
- $schemaPath: this.schemaPath.slice(),
30
- $parsedSchema: true,
31
- schemaType: SCHEMA_TYPES.PRIMITIVE,
32
- type: SCHEMA_TYPES.PRIMITIVE,
33
- typeIdentifier: this.config.Ts.Keyword.Type,
34
- name: this.typeName,
35
- description: this.schemaFormatters.formatDescription(description),
36
- content: this.schemaUtils.safeAddNullToType(this.schema, contentType),
37
- };
38
- }
39
- }
40
-
41
- module.exports = {
42
- ArraySchemaParser,
43
- };
@@ -1,51 +0,0 @@
1
- const { MonoSchemaParser } = require("../mono-schema-parser");
2
- const _ = require("lodash");
3
- const { SCHEMA_TYPES } = require("../../constants");
4
-
5
- class ComplexSchemaParser extends MonoSchemaParser {
6
- parse() {
7
- const complexType = this.schemaUtils.getComplexType(this.schema);
8
- const simpleSchema = _.omit(
9
- _.clone(this.schema),
10
- _.keys(this.schemaParser._complexSchemaParsers),
11
- );
12
- const complexSchemaContent = this.schemaParser._complexSchemaParsers[
13
- complexType
14
- ](this.schema);
15
-
16
- return {
17
- ...(_.isObject(this.schema) ? this.schema : {}),
18
- $schemaPath: this.schemaPath.slice(),
19
- $parsedSchema: true,
20
- schemaType: SCHEMA_TYPES.COMPLEX,
21
- type: SCHEMA_TYPES.PRIMITIVE,
22
- typeIdentifier: this.config.Ts.Keyword.Type,
23
- name: this.typeName,
24
- description: this.schemaFormatters.formatDescription(
25
- this.schema.description ||
26
- _.compact(_.map(this.schema[complexType], "description"))[0] ||
27
- "",
28
- ),
29
- content:
30
- this.config.Ts.IntersectionType(
31
- _.compact([
32
- this.config.Ts.ExpressionGroup(complexSchemaContent),
33
- this.schemaUtils.getInternalSchemaType(simpleSchema) ===
34
- SCHEMA_TYPES.OBJECT &&
35
- this.config.Ts.ExpressionGroup(
36
- this.schemaParserFabric
37
- .createSchemaParser({
38
- schema: simpleSchema,
39
- schemaPath: this.schemaPath,
40
- })
41
- .getInlineParseContent(),
42
- ),
43
- ]),
44
- ) || this.config.Ts.Keyword.Any,
45
- };
46
- }
47
- }
48
-
49
- module.exports = {
50
- ComplexSchemaParser,
51
- };
@@ -1,307 +0,0 @@
1
- const _ = require("lodash");
2
- const { SCHEMA_TYPES } = require("../../constants");
3
- const { MonoSchemaParser } = require("../mono-schema-parser");
4
-
5
- class DiscriminatorSchemaParser extends MonoSchemaParser {
6
- parse() {
7
- const ts = this.config.Ts;
8
- const { discriminator, ...noDiscriminatorSchema } = this.schema;
9
-
10
- if (!discriminator.mapping) {
11
- return this.schemaParserFabric
12
- .createSchemaParser({
13
- schema: noDiscriminatorSchema,
14
- typeName: this.typeName,
15
- schemaPath: this.schemaPath,
16
- })
17
- .parseSchema();
18
- }
19
-
20
- // https://github.com/acacode/swagger-typescript-api/issues/456
21
- // const skipMappingType = !!noDiscriminatorSchema.oneOf;
22
- const skipMappingType = false;
23
-
24
- const abstractSchemaStruct = this.createAbstractSchemaStruct();
25
- // const complexSchemaStruct = this.createComplexSchemaStruct();
26
- const discriminatorSchemaStruct = this.createDiscriminatorSchema({
27
- skipMappingType,
28
- abstractSchemaStruct,
29
- });
30
-
31
- const schemaContent = ts.IntersectionType(
32
- [
33
- abstractSchemaStruct?.content,
34
- discriminatorSchemaStruct?.content,
35
- ].filter(Boolean),
36
- );
37
-
38
- return {
39
- ...(_.isObject(this.schema) ? this.schema : {}),
40
- $schemaPath: this.schemaPath.slice(),
41
- $parsedSchema: true,
42
- schemaType: SCHEMA_TYPES.COMPLEX,
43
- type: SCHEMA_TYPES.PRIMITIVE,
44
- typeIdentifier: ts.Keyword.Type,
45
- name: this.typeName,
46
- description: this.schemaFormatters.formatDescription(
47
- this.schema.description,
48
- ),
49
- content: schemaContent,
50
- };
51
- }
52
-
53
- createDiscriminatorSchema = ({ skipMappingType, abstractSchemaStruct }) => {
54
- const ts = this.config.Ts;
55
-
56
- const refPath = this.schemaComponentsMap.createRef([
57
- "components",
58
- "schemas",
59
- this.typeName,
60
- ]);
61
- const { discriminator } = this.schema;
62
- const mappingEntries = _.entries(discriminator.mapping);
63
- const ableToCreateMappingType =
64
- !skipMappingType &&
65
- !!(abstractSchemaStruct?.typeName && mappingEntries.length);
66
- const mappingContents = [];
67
- let mappingTypeName;
68
-
69
- /** { mapping_key: SchemaEnum.MappingKey, ... } */
70
- const mappingPropertySchemaEnumKeysMap =
71
- this.createMappingPropertySchemaEnumKeys({
72
- abstractSchemaStruct,
73
- discPropertyName: discriminator.propertyName,
74
- });
75
-
76
- if (ableToCreateMappingType) {
77
- const rawTypeName = `${abstractSchemaStruct.typeName}_${discriminator.propertyName}`;
78
- const generatedTypeName = this.schemaUtils.resolveTypeName(rawTypeName, {
79
- suffixes: this.config.extractingOptions.discriminatorMappingSuffix,
80
- resolver:
81
- this.config.extractingOptions.discriminatorMappingNameResolver,
82
- });
83
-
84
- const content = ts.IntersectionType([
85
- ts.ObjectWrapper(
86
- ts.TypeField({
87
- key: ts.StringValue(discriminator.propertyName),
88
- value: "Key",
89
- }),
90
- ),
91
- "Type",
92
- ]);
93
-
94
- const component = this.schemaParserFabric.createParsedComponent({
95
- typeName: generatedTypeName,
96
- schema: {
97
- type: "object",
98
- properties: {},
99
- genericArgs: [{ name: "Key" }, { name: "Type" }],
100
- internal: true,
101
- },
102
- });
103
-
104
- component.typeData.content = content;
105
-
106
- mappingTypeName = this.typeNameFormatter.format(component.typeName);
107
- }
108
-
109
- /** returns (GenericType<"mapping_key", MappingType>) or ({ discriminatorProperty: "mapping_key" } & MappingType) */
110
- const createMappingContent = (mappingSchema, mappingKey) => {
111
- const content = this.schemaParserFabric
112
- .createSchemaParser({
113
- schema: mappingSchema,
114
- schemaPath: this.schemaPath,
115
- })
116
- .getInlineParseContent();
117
-
118
- const mappingUsageKey =
119
- mappingPropertySchemaEnumKeysMap[mappingKey] ||
120
- ts.StringValue(mappingKey);
121
-
122
- if (ableToCreateMappingType) {
123
- return ts.TypeWithGeneric(mappingTypeName, [mappingUsageKey, content]);
124
- } else {
125
- return ts.ExpressionGroup(
126
- ts.IntersectionType([
127
- ts.ObjectWrapper(
128
- ts.TypeField({
129
- key: discriminator.propertyName,
130
- value: mappingUsageKey,
131
- }),
132
- ),
133
- content,
134
- ]),
135
- );
136
- }
137
- };
138
-
139
- for (const [mappingKey, schema] of mappingEntries) {
140
- const mappingSchema =
141
- typeof schema === "string" ? { $ref: schema } : schema;
142
-
143
- this.mutateMappingDependentSchema({
144
- discPropertyName: discriminator.propertyName,
145
- abstractSchemaStruct,
146
- mappingSchema,
147
- refPath,
148
- mappingPropertySchemaEnumKeysMap,
149
- });
150
-
151
- mappingContents.push(createMappingContent(mappingSchema, mappingKey));
152
- }
153
-
154
- if (skipMappingType) return null;
155
-
156
- const content = ts.ExpressionGroup(ts.UnionType(mappingContents));
157
-
158
- return {
159
- content,
160
- };
161
- };
162
-
163
- createMappingPropertySchemaEnumKeys = ({
164
- abstractSchemaStruct,
165
- discPropertyName,
166
- }) => {
167
- const ts = this.config.Ts;
168
-
169
- let mappingPropertySchemaEnumKeysMap = {};
170
- let mappingPropertySchema = _.get(
171
- abstractSchemaStruct?.component?.rawTypeData,
172
- ["properties", discPropertyName],
173
- );
174
- if (this.schemaUtils.isRefSchema(mappingPropertySchema)) {
175
- mappingPropertySchema = this.schemaUtils.getSchemaRefType(
176
- mappingPropertySchema,
177
- );
178
- }
179
-
180
- if (
181
- mappingPropertySchema?.rawTypeData?.$parsed?.type === SCHEMA_TYPES.ENUM
182
- ) {
183
- mappingPropertySchemaEnumKeysMap = _.reduce(
184
- mappingPropertySchema.rawTypeData.$parsed.enum,
185
- (acc, key, index) => {
186
- const enumKey =
187
- mappingPropertySchema.rawTypeData.$parsed.content[index].key;
188
- acc[key] = ts.EnumUsageKey(
189
- mappingPropertySchema.rawTypeData.$parsed.typeName,
190
- enumKey,
191
- );
192
- return acc;
193
- },
194
- {},
195
- );
196
- }
197
-
198
- return mappingPropertySchemaEnumKeysMap;
199
- };
200
-
201
- mutateMappingDependentSchema = ({
202
- discPropertyName,
203
- abstractSchemaStruct,
204
- mappingSchema,
205
- refPath,
206
- mappingPropertySchemaEnumKeysMap,
207
- }) => {
208
- const complexSchemaKeys = _.keys(this.schemaParser._complexSchemaParsers);
209
- // override parent dependencies
210
- if (mappingSchema.$ref && abstractSchemaStruct?.component?.$ref) {
211
- const mappingRefSchema =
212
- this.schemaUtils.getSchemaRefType(mappingSchema)?.rawTypeData;
213
- if (mappingRefSchema) {
214
- complexSchemaKeys.forEach((schemaKey) => {
215
- if (_.isArray(mappingRefSchema[schemaKey])) {
216
- mappingRefSchema[schemaKey] = mappingRefSchema[schemaKey].map(
217
- (schema) => {
218
- if (schema.$ref === refPath) {
219
- return {
220
- ...schema,
221
- $ref: abstractSchemaStruct.component.$ref,
222
- };
223
- }
224
- if (
225
- this.schemaUtils.getInternalSchemaType(schema) ===
226
- SCHEMA_TYPES.OBJECT
227
- ) {
228
- for (const schemaPropertyName in schema.properties) {
229
- const schemaProperty =
230
- schema.properties[schemaPropertyName];
231
- if (
232
- schemaPropertyName === discPropertyName &&
233
- this.schemaUtils.getInternalSchemaType(schemaProperty) ===
234
- SCHEMA_TYPES.ENUM &&
235
- schemaProperty.enum.length === 1 &&
236
- mappingPropertySchemaEnumKeysMap[schemaProperty.enum[0]]
237
- ) {
238
- schema.properties[schemaPropertyName] =
239
- this.schemaParserFabric.createSchema({
240
- content:
241
- mappingPropertySchemaEnumKeysMap[
242
- schemaProperty.enum[0]
243
- ],
244
- });
245
- }
246
- }
247
- }
248
- return schema;
249
- },
250
- );
251
- }
252
- });
253
- }
254
- }
255
- };
256
-
257
- createAbstractSchemaStruct = () => {
258
- // eslint-disable-next-line no-unused-vars
259
- const { discriminator, ...noDiscriminatorSchema } = this.schema;
260
- const complexSchemaKeys = _.keys(this.schemaParser._complexSchemaParsers);
261
- const schema = _.omit(_.clone(noDiscriminatorSchema), complexSchemaKeys);
262
- const schemaIsAny =
263
- this.schemaParserFabric.getInlineParseContent(_.cloneDeep(schema)) ===
264
- this.config.Ts.Keyword.Any;
265
- const schemaIsEmpty = !_.keys(schema).length;
266
-
267
- if (schemaIsEmpty || schemaIsAny) return null;
268
-
269
- const typeName = this.schemaUtils.resolveTypeName(this.typeName, {
270
- prefixes: this.config.extractingOptions.discriminatorAbstractPrefix,
271
- resolver: this.config.extractingOptions.discriminatorAbstractResolver,
272
- });
273
- const component = this.schemaComponentsMap.createComponent(
274
- this.schemaComponentsMap.createRef(["components", "schemas", typeName]),
275
- {
276
- ...schema,
277
- internal: true,
278
- },
279
- );
280
- const content = this.schemaParserFabric
281
- .createSchemaParser({ schema: component, schemaPath: this.schemaPath })
282
- .getInlineParseContent();
283
-
284
- return {
285
- typeName,
286
- component,
287
- content,
288
- };
289
- };
290
-
291
- createComplexSchemaStruct = () => {
292
- const ts = this.config.Ts;
293
- const complexType = this.schemaUtils.getComplexType(this.schema);
294
-
295
- if (complexType === SCHEMA_TYPES.COMPLEX_UNKNOWN) return null;
296
-
297
- return {
298
- content: ts.ExpressionGroup(
299
- this.schemaParser._complexSchemaParsers[complexType](this.schema),
300
- ),
301
- };
302
- };
303
- }
304
-
305
- module.exports = {
306
- DiscriminatorSchemaParser,
307
- };