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.
- package/LICENSE +21 -21
- package/README.md +22 -12
- package/cli/constants.js +3 -3
- package/cli/execute.js +52 -31
- package/cli/index.d.ts +1 -2
- package/cli/index.js +18 -17
- package/cli/operations/display-help.js +51 -29
- package/cli/parse-args.js +3 -3
- package/cli/process-option.js +28 -20
- package/index.d.ts +113 -8
- package/index.js +158 -135
- package/package.json +35 -30
- package/src/code-formatter.js +28 -13
- package/src/code-gen-process.js +357 -259
- package/src/commands/generate-templates/configuration.js +2 -2
- package/src/commands/generate-templates/index.js +1 -2
- package/src/commands/generate-templates/templates-gen-process.js +62 -35
- package/src/component-type-name-resolver.js +44 -0
- package/src/configuration.js +167 -95
- package/src/constants.js +28 -22
- package/src/index.js +3 -4
- package/src/schema-components-map.js +39 -23
- package/src/schema-parser/base-schema-parsers/array.js +43 -0
- package/src/schema-parser/base-schema-parsers/complex.js +51 -0
- package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
- package/src/schema-parser/base-schema-parsers/enum.js +158 -0
- package/src/schema-parser/base-schema-parsers/object.js +105 -0
- package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
- package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
- package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
- package/src/schema-parser/complex-schema-parsers/not.js +9 -0
- package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
- package/src/schema-parser/mono-schema-parser.js +48 -0
- package/src/schema-parser/schema-formatters.js +69 -60
- package/src/schema-parser/schema-parser-fabric.js +131 -0
- package/src/schema-parser/schema-parser.js +208 -427
- package/src/schema-parser/schema-utils.js +123 -58
- package/src/schema-parser/util/enum-key-resolver.js +26 -0
- package/src/schema-routes/schema-routes.js +1225 -0
- package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
- package/src/schema-walker.js +93 -0
- package/src/swagger-schema-resolver.js +61 -28
- package/src/templates-worker.js +240 -0
- package/src/translators/javascript.js +83 -0
- package/src/translators/translator.js +35 -0
- package/src/type-name-formatter.js +33 -18
- package/src/util/file-system.js +30 -14
- package/src/util/id.js +2 -2
- package/src/util/internal-case.js +1 -1
- package/src/util/logger.js +46 -20
- package/src/util/name-resolver.js +52 -60
- package/src/util/object-assign.js +7 -3
- package/src/util/pascal-case.js +1 -1
- package/src/util/request.js +5 -5
- package/src/util/sort-by-property.js +17 -0
- package/templates/README.md +17 -17
- package/templates/base/README.md +7 -7
- package/templates/base/data-contract-jsdoc.ejs +37 -37
- package/templates/base/data-contracts.ejs +40 -27
- package/templates/base/enum-data-contract.ejs +12 -12
- package/templates/base/http-client.ejs +3 -3
- package/templates/base/http-clients/axios-http-client.ejs +139 -138
- package/templates/base/http-clients/fetch-http-client.ejs +224 -224
- package/templates/base/interface-data-contract.ejs +10 -10
- package/templates/base/object-field-jsdoc.ejs +28 -28
- package/templates/base/route-docs.ejs +30 -30
- package/templates/base/route-name.ejs +42 -42
- package/templates/base/route-type.ejs +22 -21
- package/templates/base/type-data-contract.ejs +15 -15
- package/templates/default/README.md +6 -6
- package/templates/default/api.ejs +69 -68
- package/templates/default/procedure-call.ejs +100 -100
- package/templates/default/route-types.ejs +32 -32
- package/templates/modular/README.md +6 -6
- package/templates/modular/api.ejs +28 -28
- package/templates/modular/procedure-call.ejs +100 -100
- package/templates/modular/route-types.ejs +18 -18
- package/src/schema-parser/schema-processor.js +0 -79
- package/src/schema-parser/schema-routes.js +0 -950
- package/src/templates.js +0 -182
- package/src/translators/JavaScript.js +0 -60
|
@@ -1,47 +1,58 @@
|
|
|
1
|
-
const _ = require(
|
|
2
|
-
const { SCHEMA_TYPES } = require(
|
|
3
|
-
const { internalCase } = require(
|
|
4
|
-
const { pascalCase } = require(
|
|
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;
|
|
15
|
-
/**
|
|
16
|
-
* @type {TypeNameFormatter}
|
|
17
|
-
*/
|
|
12
|
+
/** @type {TypeNameFormatter} */
|
|
18
13
|
typeNameFormatter;
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
/** @type {SchemaWalker} */
|
|
15
|
+
schemaWalker;
|
|
16
|
+
|
|
17
|
+
constructor({
|
|
18
|
+
config,
|
|
19
|
+
schemaComponentsMap,
|
|
20
|
+
typeNameFormatter,
|
|
21
|
+
schemaWalker,
|
|
22
|
+
}) {
|
|
21
23
|
this.config = config;
|
|
22
24
|
this.schemaComponentsMap = schemaComponentsMap;
|
|
23
25
|
this.typeNameFormatter = typeNameFormatter;
|
|
26
|
+
this.schemaWalker = schemaWalker;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
getRequiredProperties = (schema) => {
|
|
27
|
-
return _.uniq(
|
|
30
|
+
return _.uniq(
|
|
31
|
+
(schema && _.isArray(schema.required) && schema.required) || [],
|
|
32
|
+
);
|
|
28
33
|
};
|
|
29
34
|
|
|
30
35
|
isRefSchema = (schema) => {
|
|
31
|
-
return !!(schema && schema[
|
|
36
|
+
return !!(schema && schema['$ref']);
|
|
32
37
|
};
|
|
33
38
|
|
|
34
39
|
getEnumNames = (schema) => {
|
|
35
|
-
return
|
|
40
|
+
return (
|
|
41
|
+
schema['x-enumNames'] ||
|
|
42
|
+
schema['xEnumNames'] ||
|
|
43
|
+
schema['x-enumnames'] ||
|
|
44
|
+
schema['x-enum-varnames']
|
|
45
|
+
);
|
|
36
46
|
};
|
|
37
47
|
|
|
38
48
|
getSchemaRefType = (schema) => {
|
|
39
49
|
if (!this.isRefSchema(schema)) return null;
|
|
50
|
+
// const resolved = this.schemaWalker.findByRef(schema.$ref);
|
|
40
51
|
return this.schemaComponentsMap.get(schema.$ref);
|
|
41
52
|
};
|
|
42
53
|
|
|
43
54
|
isPropertyRequired = (name, propertySchema, rootSchema) => {
|
|
44
|
-
if (propertySchema[
|
|
55
|
+
if (propertySchema['x-omitempty'] === false) {
|
|
45
56
|
return true;
|
|
46
57
|
}
|
|
47
58
|
|
|
@@ -62,7 +73,9 @@ class SchemaUtils {
|
|
|
62
73
|
isNullMissingInType = (schema, type) => {
|
|
63
74
|
const { nullable, type: schemaType } = schema || {};
|
|
64
75
|
return (
|
|
65
|
-
(nullable ||
|
|
76
|
+
(nullable ||
|
|
77
|
+
!!_.get(schema, 'x-nullable') ||
|
|
78
|
+
schemaType === this.config.Ts.Keyword.Null) &&
|
|
66
79
|
_.isString(type) &&
|
|
67
80
|
!type.includes(` ${this.config.Ts.Keyword.Null}`) &&
|
|
68
81
|
!type.includes(`${this.config.Ts.Keyword.Null} `)
|
|
@@ -91,7 +104,7 @@ class SchemaUtils {
|
|
|
91
104
|
if (_.keys(schema.properties).length) {
|
|
92
105
|
return SCHEMA_TYPES.OBJECT;
|
|
93
106
|
}
|
|
94
|
-
if (
|
|
107
|
+
if (schema.items) {
|
|
95
108
|
return SCHEMA_TYPES.ARRAY;
|
|
96
109
|
}
|
|
97
110
|
|
|
@@ -99,16 +112,21 @@ class SchemaUtils {
|
|
|
99
112
|
};
|
|
100
113
|
|
|
101
114
|
checkAndAddRequiredKeys = (schema, resultType) => {
|
|
102
|
-
if (
|
|
115
|
+
if ('$$requiredKeys' in schema && schema.$$requiredKeys.length) {
|
|
103
116
|
this.config.update({
|
|
104
117
|
internalTemplateOptions: {
|
|
105
118
|
addUtilRequiredKeysType: true,
|
|
106
119
|
},
|
|
107
120
|
});
|
|
108
|
-
return this.config.Ts.TypeWithGeneric(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
);
|
|
112
130
|
}
|
|
113
131
|
|
|
114
132
|
return resultType;
|
|
@@ -117,13 +135,20 @@ class SchemaUtils {
|
|
|
117
135
|
makeAddRequiredToChildSchema = (parentSchema, childSchema) => {
|
|
118
136
|
if (!childSchema) return childSchema;
|
|
119
137
|
|
|
120
|
-
const required = _.uniq([
|
|
138
|
+
const required = _.uniq([
|
|
139
|
+
...this.getRequiredProperties(parentSchema),
|
|
140
|
+
...this.getRequiredProperties(childSchema),
|
|
141
|
+
]);
|
|
121
142
|
|
|
122
143
|
const refData = this.getSchemaRefType(childSchema);
|
|
123
144
|
|
|
124
145
|
if (refData) {
|
|
125
|
-
const refObjectProperties = _.keys(
|
|
126
|
-
|
|
146
|
+
const refObjectProperties = _.keys(
|
|
147
|
+
(refData.rawTypeData && refData.rawTypeData.properties) || {},
|
|
148
|
+
);
|
|
149
|
+
const existedRequiredKeys = refObjectProperties.filter((key) =>
|
|
150
|
+
required.includes(key),
|
|
151
|
+
);
|
|
127
152
|
|
|
128
153
|
if (!existedRequiredKeys.length) return childSchema;
|
|
129
154
|
|
|
@@ -133,12 +158,17 @@ class SchemaUtils {
|
|
|
133
158
|
};
|
|
134
159
|
} else if (childSchema.properties) {
|
|
135
160
|
const childSchemaProperties = _.keys(childSchema.properties);
|
|
136
|
-
const existedRequiredKeys = childSchemaProperties.filter((key) =>
|
|
161
|
+
const existedRequiredKeys = childSchemaProperties.filter((key) =>
|
|
162
|
+
required.includes(key),
|
|
163
|
+
);
|
|
137
164
|
|
|
138
165
|
if (!existedRequiredKeys.length) return childSchema;
|
|
139
166
|
|
|
140
167
|
return {
|
|
141
|
-
required: _.uniq([
|
|
168
|
+
required: _.uniq([
|
|
169
|
+
...this.getRequiredProperties(childSchema),
|
|
170
|
+
...existedRequiredKeys,
|
|
171
|
+
]),
|
|
142
172
|
...childSchema,
|
|
143
173
|
};
|
|
144
174
|
}
|
|
@@ -146,6 +176,33 @@ class SchemaUtils {
|
|
|
146
176
|
return childSchema;
|
|
147
177
|
};
|
|
148
178
|
|
|
179
|
+
filterSchemaContents = (contents, filterFn) => {
|
|
180
|
+
return _.uniq(_.filter(contents, (type) => filterFn(type)));
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
resolveTypeName = (
|
|
184
|
+
typeName,
|
|
185
|
+
{ suffixes, resolver, prefixes, shouldReserve = true },
|
|
186
|
+
) => {
|
|
187
|
+
if (resolver) {
|
|
188
|
+
return this.config.componentTypeNameResolver.resolve(null, (reserved) => {
|
|
189
|
+
return resolver(pascalCase(typeName), reserved);
|
|
190
|
+
});
|
|
191
|
+
} else {
|
|
192
|
+
return this.config.componentTypeNameResolver.resolve(
|
|
193
|
+
[
|
|
194
|
+
...(prefixes || []).map((prefix) =>
|
|
195
|
+
pascalCase(`${prefix} ${typeName}`),
|
|
196
|
+
),
|
|
197
|
+
...(suffixes || []).map((suffix) =>
|
|
198
|
+
pascalCase(`${typeName} ${suffix}`),
|
|
199
|
+
),
|
|
200
|
+
],
|
|
201
|
+
shouldReserve,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
149
206
|
getComplexType = (schema) => {
|
|
150
207
|
if (schema.oneOf) return SCHEMA_TYPES.COMPLEX_ONE_OF;
|
|
151
208
|
if (schema.allOf) return SCHEMA_TYPES.COMPLEX_ALL_OF;
|
|
@@ -157,15 +214,26 @@ class SchemaUtils {
|
|
|
157
214
|
};
|
|
158
215
|
|
|
159
216
|
getInternalSchemaType = (schema) => {
|
|
160
|
-
if (!_.isEmpty(schema.enum) || !_.isEmpty(this.getEnumNames(schema)))
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (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
|
+
}
|
|
164
232
|
|
|
165
233
|
return SCHEMA_TYPES.PRIMITIVE;
|
|
166
234
|
};
|
|
167
235
|
|
|
168
|
-
getSchemaType =
|
|
236
|
+
getSchemaType = (schema) => {
|
|
169
237
|
if (!schema) return this.config.Ts.Keyword.Any;
|
|
170
238
|
|
|
171
239
|
const refTypeInfo = this.getSchemaRefType(schema);
|
|
@@ -173,7 +241,10 @@ class SchemaUtils {
|
|
|
173
241
|
if (refTypeInfo) {
|
|
174
242
|
return this.checkAndAddRequiredKeys(
|
|
175
243
|
schema,
|
|
176
|
-
this.safeAddNullToType(
|
|
244
|
+
this.safeAddNullToType(
|
|
245
|
+
schema,
|
|
246
|
+
this.typeNameFormatter.format(refTypeInfo.typeName),
|
|
247
|
+
),
|
|
177
248
|
);
|
|
178
249
|
}
|
|
179
250
|
|
|
@@ -183,41 +254,35 @@ class SchemaUtils {
|
|
|
183
254
|
|
|
184
255
|
let resultType;
|
|
185
256
|
|
|
186
|
-
/**
|
|
187
|
-
* @type {string | (() => Promise<string>)}
|
|
188
|
-
*/
|
|
189
257
|
const typeAlias =
|
|
190
258
|
_.get(this.config.primitiveTypes, [primitiveType, schema.format]) ||
|
|
191
|
-
_.get(this.config.primitiveTypes, [primitiveType,
|
|
259
|
+
_.get(this.config.primitiveTypes, [primitiveType, '$default']) ||
|
|
192
260
|
this.config.primitiveTypes[primitiveType];
|
|
193
261
|
|
|
194
262
|
if (_.isFunction(typeAlias)) {
|
|
195
|
-
resultType =
|
|
263
|
+
resultType = typeAlias(schema, this);
|
|
196
264
|
} else {
|
|
197
265
|
resultType = typeAlias || primitiveType;
|
|
198
266
|
}
|
|
199
267
|
|
|
200
268
|
if (!resultType) return this.config.Ts.Keyword.Any;
|
|
201
269
|
|
|
202
|
-
return this.checkAndAddRequiredKeys(
|
|
270
|
+
return this.checkAndAddRequiredKeys(
|
|
271
|
+
schema,
|
|
272
|
+
this.safeAddNullToType(schema, resultType),
|
|
273
|
+
);
|
|
203
274
|
};
|
|
204
275
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
};
|
|
276
|
+
buildTypeNameFromPath = (schemaPath) => {
|
|
277
|
+
schemaPath = _.uniq(_.compact(schemaPath));
|
|
208
278
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
} else {
|
|
217
|
-
return this.config.componentTypeNameResolver.resolve(
|
|
218
|
-
suffixes.map((suffix) => pascalCase(`${typeName} ${suffix}`)),
|
|
219
|
-
);
|
|
220
|
-
}
|
|
279
|
+
if (!schemaPath || !schemaPath[0]) return null;
|
|
280
|
+
|
|
281
|
+
return pascalCase(
|
|
282
|
+
camelCase(
|
|
283
|
+
_.uniq([schemaPath[0], schemaPath[schemaPath.length - 1]]).join('_'),
|
|
284
|
+
),
|
|
285
|
+
);
|
|
221
286
|
};
|
|
222
287
|
}
|
|
223
288
|
|
|
@@ -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
|
+
};
|