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
package/src/constants.js DELETED
@@ -1,77 +0,0 @@
1
- import { createRequire } from "node:module";
2
-
3
- const require = createRequire(import.meta.url);
4
- const packageJson = require("../package.json");
5
-
6
- const DEFAULT_BODY_ARG_NAME = "data";
7
-
8
- const FILE_PREFIX = `/* eslint-disable */
9
- /* tslint:disable */
10
- /*
11
- * ---------------------------------------------------------------
12
- * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
13
- * ## ##
14
- * ## AUTHOR: acacode ##
15
- * ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
16
- * ---------------------------------------------------------------
17
- */
18
-
19
- `;
20
-
21
- const HTTP_CLIENT = {
22
- FETCH: "fetch",
23
- AXIOS: "axios",
24
- };
25
-
26
- const PRETTIER_OPTIONS = {
27
- printWidth: 120,
28
- tabWidth: 2,
29
- trailingComma: "all",
30
- parser: "typescript",
31
- };
32
-
33
- const PROJECT_VERSION = packageJson.version;
34
-
35
- const RESERVED_BODY_ARG_NAMES = ["data", "body", "reqBody"];
36
-
37
- const RESERVED_HEADER_ARG_NAMES = ["headers", "headersParams"];
38
-
39
- const RESERVED_PATH_ARG_NAMES = ["path", "pathParams"];
40
-
41
- const RESERVED_QUERY_ARG_NAMES = ["query", "queryParams", "queryArg"];
42
-
43
- const RESERVED_REQ_PARAMS_ARG_NAMES = [
44
- "params",
45
- "requestParams",
46
- "reqParams",
47
- "httpParams",
48
- ];
49
-
50
- const SCHEMA_TYPES = {
51
- ARRAY: "array",
52
- OBJECT: "object",
53
- ENUM: "enum",
54
- REF: "$ref",
55
- PRIMITIVE: "primitive",
56
- COMPLEX: "complex",
57
- DISCRIMINATOR: "discriminator",
58
- COMPLEX_ONE_OF: "oneOf",
59
- COMPLEX_ANY_OF: "anyOf",
60
- COMPLEX_ALL_OF: "allOf",
61
- COMPLEX_NOT: "not",
62
- COMPLEX_UNKNOWN: "__unknown",
63
- };
64
-
65
- export {
66
- DEFAULT_BODY_ARG_NAME,
67
- FILE_PREFIX,
68
- HTTP_CLIENT,
69
- PRETTIER_OPTIONS,
70
- PROJECT_VERSION,
71
- RESERVED_BODY_ARG_NAMES,
72
- RESERVED_HEADER_ARG_NAMES,
73
- RESERVED_PATH_ARG_NAMES,
74
- RESERVED_QUERY_ARG_NAMES,
75
- RESERVED_REQ_PARAMS_ARG_NAMES,
76
- SCHEMA_TYPES,
77
- };
package/src/index.js DELETED
@@ -1,16 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { CodeGenProcess } from "./code-gen-process.js";
4
- import { generateTemplates } from "./commands/generate-templates/index.js";
5
- import * as constants from "./constants.js";
6
-
7
- async function generateApi({ name, prettier, ...config }) {
8
- const codeGenProcess = new CodeGenProcess({
9
- ...config,
10
- fileName: name,
11
- prettierOptions: prettier,
12
- });
13
- return await codeGenProcess.start();
14
- }
15
-
16
- export { constants, generateApi, generateTemplates };
@@ -1,76 +0,0 @@
1
- import _ from "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
- export { SchemaComponentsMap };
@@ -1,41 +0,0 @@
1
- import _ from "lodash";
2
- import { SCHEMA_TYPES } from "../../constants.js";
3
- import { MonoSchemaParser } from "../mono-schema-parser.js";
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
- export { ArraySchemaParser };
@@ -1,49 +0,0 @@
1
- import _ from "lodash";
2
- import { SCHEMA_TYPES } from "../../constants.js";
3
- import { MonoSchemaParser } from "../mono-schema-parser.js";
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
- export { ComplexSchemaParser };
@@ -1,305 +0,0 @@
1
- import _ from "lodash";
2
- import { SCHEMA_TYPES } from "../../constants.js";
3
- import { MonoSchemaParser } from "../mono-schema-parser.js";
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
- export { DiscriminatorSchemaParser };