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.
- package/LICENSE +21 -21
- package/README.md +1 -1
- 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 +99 -7
- package/index.js +158 -136
- package/package.json +35 -30
- package/src/code-formatter.js +28 -13
- package/src/code-gen-process.js +367 -264
- 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 +166 -98
- 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 +58 -44
- package/src/schema-parser/schema-parser-fabric.js +131 -0
- package/src/schema-parser/schema-parser.js +212 -361
- package/src/schema-parser/schema-utils.js +158 -33
- package/src/schema-parser/util/enum-key-resolver.js +26 -0
- package/src/{schema-parser → schema-routes}/schema-routes.js +472 -203
- 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.js → type-name-formatter.js} +35 -20
- 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 +50 -58
- 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/base/data-contract-jsdoc.ejs +37 -37
- package/templates/base/data-contracts.ejs +40 -28
- package/templates/base/enum-data-contract.ejs +12 -12
- package/templates/base/http-client.ejs +2 -2
- 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/api.ejs +69 -65
- package/templates/default/procedure-call.ejs +100 -100
- package/templates/default/route-types.ejs +32 -28
- 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/templates.js +0 -177
- package/src/translators/JavaScript.js +0 -60
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,301 @@
|
|
|
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 { discriminator, ...noDiscriminatorSchema } = this.schema;
|
|
8
|
+
|
|
9
|
+
if (!discriminator.mapping) {
|
|
10
|
+
return this.schemaParserFabric
|
|
11
|
+
.createSchemaParser({
|
|
12
|
+
schema: noDiscriminatorSchema,
|
|
13
|
+
typeName: this.typeName,
|
|
14
|
+
schemaPath: this.schemaPath,
|
|
15
|
+
})
|
|
16
|
+
.parseSchema();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// https://github.com/acacode/swagger-typescript-api/issues/456
|
|
20
|
+
// const skipMappingType = !!noDiscriminatorSchema.oneOf;
|
|
21
|
+
const skipMappingType = false;
|
|
22
|
+
|
|
23
|
+
const abstractSchemaStruct = this.createAbstractSchemaStruct();
|
|
24
|
+
// const complexSchemaStruct = this.createComplexSchemaStruct();
|
|
25
|
+
const discriminatorSchemaStruct = this.createDiscriminatorSchema({
|
|
26
|
+
skipMappingType,
|
|
27
|
+
abstractSchemaStruct,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const schemaContent = this.config.Ts.IntersectionType(
|
|
31
|
+
[
|
|
32
|
+
abstractSchemaStruct?.content,
|
|
33
|
+
discriminatorSchemaStruct?.content,
|
|
34
|
+
].filter(Boolean),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
...(_.isObject(this.schema) ? this.schema : {}),
|
|
39
|
+
$schemaPath: this.schemaPath.slice(),
|
|
40
|
+
$parsedSchema: true,
|
|
41
|
+
schemaType: SCHEMA_TYPES.COMPLEX,
|
|
42
|
+
type: SCHEMA_TYPES.PRIMITIVE,
|
|
43
|
+
typeIdentifier: this.config.Ts.Keyword.Type,
|
|
44
|
+
name: this.typeName,
|
|
45
|
+
description: this.schemaFormatters.formatDescription(
|
|
46
|
+
this.schema.description,
|
|
47
|
+
),
|
|
48
|
+
content: schemaContent,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
createDiscriminatorSchema = ({ skipMappingType, abstractSchemaStruct }) => {
|
|
53
|
+
const refPath = this.schemaComponentsMap.createRef([
|
|
54
|
+
'components',
|
|
55
|
+
'schemas',
|
|
56
|
+
this.typeName,
|
|
57
|
+
]);
|
|
58
|
+
const { discriminator } = this.schema;
|
|
59
|
+
const mappingEntries = _.entries(discriminator.mapping);
|
|
60
|
+
const ableToCreateMappingType =
|
|
61
|
+
!skipMappingType &&
|
|
62
|
+
!!(abstractSchemaStruct?.typeName && mappingEntries.length);
|
|
63
|
+
const mappingContents = [];
|
|
64
|
+
let mappingTypeName;
|
|
65
|
+
|
|
66
|
+
/** { mapping_key: SchemaEnum.MappingKey, ... } */
|
|
67
|
+
const mappingPropertySchemaEnumKeysMap =
|
|
68
|
+
this.createMappingPropertySchemaEnumKeys({
|
|
69
|
+
abstractSchemaStruct,
|
|
70
|
+
discPropertyName: discriminator.propertyName,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
if (ableToCreateMappingType) {
|
|
74
|
+
mappingTypeName = this.schemaUtils.resolveTypeName(
|
|
75
|
+
`${abstractSchemaStruct.typeName} ${discriminator.propertyName}`,
|
|
76
|
+
{
|
|
77
|
+
suffixes: this.config.extractingOptions.discriminatorMappingSuffix,
|
|
78
|
+
resolver:
|
|
79
|
+
this.config.extractingOptions.discriminatorMappingNameResolver,
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
this.schemaParserFabric.createSchema({
|
|
83
|
+
linkedComponent: this.schemaComponentsMap.createComponent(
|
|
84
|
+
this.schemaComponentsMap.createRef([
|
|
85
|
+
'components',
|
|
86
|
+
'schemas',
|
|
87
|
+
mappingTypeName,
|
|
88
|
+
]),
|
|
89
|
+
),
|
|
90
|
+
content: this.config.Ts.IntersectionType([
|
|
91
|
+
this.config.Ts.ObjectWrapper(
|
|
92
|
+
this.config.Ts.TypeField({
|
|
93
|
+
key: discriminator.propertyName,
|
|
94
|
+
value: 'Key',
|
|
95
|
+
}),
|
|
96
|
+
),
|
|
97
|
+
'Type',
|
|
98
|
+
]),
|
|
99
|
+
genericArgs: [{ name: 'Key' }, { name: 'Type' }],
|
|
100
|
+
internal: true,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** returns (GenericType<"mapping_key", MappingType>) or ({ discriminatorProperty: "mapping_key" } & MappingType) */
|
|
105
|
+
const createMappingContent = (mappingSchema, mappingKey) => {
|
|
106
|
+
const content = this.schemaParserFabric
|
|
107
|
+
.createSchemaParser({
|
|
108
|
+
schema: mappingSchema,
|
|
109
|
+
schemaPath: this.schemaPath,
|
|
110
|
+
})
|
|
111
|
+
.getInlineParseContent();
|
|
112
|
+
|
|
113
|
+
const mappingUsageKey =
|
|
114
|
+
mappingPropertySchemaEnumKeysMap[mappingKey] ||
|
|
115
|
+
this.config.Ts.StringValue(mappingKey);
|
|
116
|
+
|
|
117
|
+
if (ableToCreateMappingType) {
|
|
118
|
+
return this.config.Ts.TypeWithGeneric(mappingTypeName, [
|
|
119
|
+
mappingUsageKey,
|
|
120
|
+
content,
|
|
121
|
+
]);
|
|
122
|
+
} else {
|
|
123
|
+
return this.config.Ts.ExpressionGroup(
|
|
124
|
+
this.config.Ts.IntersectionType([
|
|
125
|
+
this.config.Ts.ObjectWrapper(
|
|
126
|
+
this.config.Ts.TypeField({
|
|
127
|
+
key: discriminator.propertyName,
|
|
128
|
+
value: mappingUsageKey,
|
|
129
|
+
}),
|
|
130
|
+
),
|
|
131
|
+
content,
|
|
132
|
+
]),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
for (const [mappingKey, schema] of mappingEntries) {
|
|
138
|
+
const mappingSchema =
|
|
139
|
+
typeof schema === 'string' ? { $ref: schema } : schema;
|
|
140
|
+
|
|
141
|
+
this.mutateMappingDependentSchema({
|
|
142
|
+
discPropertyName: discriminator.propertyName,
|
|
143
|
+
abstractSchemaStruct,
|
|
144
|
+
mappingSchema,
|
|
145
|
+
refPath,
|
|
146
|
+
mappingPropertySchemaEnumKeysMap,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
mappingContents.push(createMappingContent(mappingSchema, mappingKey));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (skipMappingType) return null;
|
|
153
|
+
|
|
154
|
+
const content = this.config.Ts.ExpressionGroup(
|
|
155
|
+
this.config.Ts.UnionType(mappingContents),
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
content,
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
createMappingPropertySchemaEnumKeys = ({
|
|
164
|
+
abstractSchemaStruct,
|
|
165
|
+
discPropertyName,
|
|
166
|
+
}) => {
|
|
167
|
+
let mappingPropertySchemaEnumKeysMap = {};
|
|
168
|
+
let mappingPropertySchema = _.get(
|
|
169
|
+
abstractSchemaStruct?.component?.rawTypeData,
|
|
170
|
+
['properties', discPropertyName],
|
|
171
|
+
);
|
|
172
|
+
if (this.schemaUtils.isRefSchema(mappingPropertySchema)) {
|
|
173
|
+
mappingPropertySchema = this.schemaUtils.getSchemaRefType(
|
|
174
|
+
mappingPropertySchema,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (
|
|
179
|
+
mappingPropertySchema?.rawTypeData?.$parsed?.type === SCHEMA_TYPES.ENUM
|
|
180
|
+
) {
|
|
181
|
+
mappingPropertySchemaEnumKeysMap = _.reduce(
|
|
182
|
+
mappingPropertySchema.rawTypeData.$parsed.enum,
|
|
183
|
+
(acc, key, index) => {
|
|
184
|
+
const enumKey =
|
|
185
|
+
mappingPropertySchema.rawTypeData.$parsed.content[index].key;
|
|
186
|
+
acc[key] = this.config.Ts.EnumUsageKey(
|
|
187
|
+
mappingPropertySchema.rawTypeData.$parsed.typeName,
|
|
188
|
+
enumKey,
|
|
189
|
+
);
|
|
190
|
+
return acc;
|
|
191
|
+
},
|
|
192
|
+
{},
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return mappingPropertySchemaEnumKeysMap;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
mutateMappingDependentSchema = ({
|
|
200
|
+
discPropertyName,
|
|
201
|
+
abstractSchemaStruct,
|
|
202
|
+
mappingSchema,
|
|
203
|
+
refPath,
|
|
204
|
+
mappingPropertySchemaEnumKeysMap,
|
|
205
|
+
}) => {
|
|
206
|
+
const complexSchemaKeys = _.keys(this.schemaParser._complexSchemaParsers);
|
|
207
|
+
// override parent dependencies
|
|
208
|
+
if (mappingSchema.$ref && abstractSchemaStruct?.component?.$ref) {
|
|
209
|
+
const mappingRefSchema =
|
|
210
|
+
this.schemaUtils.getSchemaRefType(mappingSchema)?.rawTypeData;
|
|
211
|
+
if (mappingRefSchema) {
|
|
212
|
+
complexSchemaKeys.forEach((schemaKey) => {
|
|
213
|
+
if (_.isArray(mappingRefSchema[schemaKey])) {
|
|
214
|
+
mappingRefSchema[schemaKey] = mappingRefSchema[schemaKey].map(
|
|
215
|
+
(schema) => {
|
|
216
|
+
if (schema.$ref === refPath) {
|
|
217
|
+
return {
|
|
218
|
+
...schema,
|
|
219
|
+
$ref: abstractSchemaStruct.component.$ref,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (
|
|
223
|
+
this.schemaUtils.getInternalSchemaType(schema) ===
|
|
224
|
+
SCHEMA_TYPES.OBJECT
|
|
225
|
+
) {
|
|
226
|
+
for (const schemaPropertyName in schema.properties) {
|
|
227
|
+
const schemaProperty =
|
|
228
|
+
schema.properties[schemaPropertyName];
|
|
229
|
+
if (
|
|
230
|
+
schemaPropertyName === discPropertyName &&
|
|
231
|
+
this.schemaUtils.getInternalSchemaType(schemaProperty) ===
|
|
232
|
+
SCHEMA_TYPES.ENUM &&
|
|
233
|
+
schemaProperty.enum.length === 1 &&
|
|
234
|
+
mappingPropertySchemaEnumKeysMap[schemaProperty.enum[0]]
|
|
235
|
+
) {
|
|
236
|
+
schema.properties[schemaPropertyName] =
|
|
237
|
+
this.schemaParserFabric.createSchema({
|
|
238
|
+
content:
|
|
239
|
+
mappingPropertySchemaEnumKeysMap[
|
|
240
|
+
schemaProperty.enum[0]
|
|
241
|
+
],
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return schema;
|
|
247
|
+
},
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
createAbstractSchemaStruct = () => {
|
|
256
|
+
// eslint-disable-next-line no-unused-vars
|
|
257
|
+
const { discriminator, ...noDiscriminatorSchema } = this.schema;
|
|
258
|
+
const complexSchemaKeys = _.keys(this.schemaParser._complexSchemaParsers);
|
|
259
|
+
const schema = _.omit(_.clone(noDiscriminatorSchema), complexSchemaKeys);
|
|
260
|
+
const schemaIsEmpty = !_.keys(schema).length;
|
|
261
|
+
|
|
262
|
+
if (schemaIsEmpty) return null;
|
|
263
|
+
|
|
264
|
+
const typeName = this.schemaUtils.resolveTypeName(this.typeName, {
|
|
265
|
+
prefixes: this.config.extractingOptions.discriminatorAbstractPrefix,
|
|
266
|
+
resolver: this.config.extractingOptions.discriminatorAbstractResolver,
|
|
267
|
+
});
|
|
268
|
+
const component = this.schemaComponentsMap.createComponent(
|
|
269
|
+
this.schemaComponentsMap.createRef(['components', 'schemas', typeName]),
|
|
270
|
+
{
|
|
271
|
+
...schema,
|
|
272
|
+
internal: true,
|
|
273
|
+
},
|
|
274
|
+
);
|
|
275
|
+
const content = this.schemaParserFabric
|
|
276
|
+
.createSchemaParser({ schema: component, schemaPath: this.schemaPath })
|
|
277
|
+
.getInlineParseContent();
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
typeName,
|
|
281
|
+
component,
|
|
282
|
+
content,
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
createComplexSchemaStruct = () => {
|
|
287
|
+
const complexType = this.schemaUtils.getComplexType(this.schema);
|
|
288
|
+
|
|
289
|
+
if (complexType === SCHEMA_TYPES.COMPLEX_UNKNOWN) return null;
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
content: this.config.Ts.ExpressionGroup(
|
|
293
|
+
this.schemaParser._complexSchemaParsers[complexType](this.schema),
|
|
294
|
+
),
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
module.exports = {
|
|
300
|
+
DiscriminatorSchemaParser,
|
|
301
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const { MonoSchemaParser } = require('../mono-schema-parser');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
const { SCHEMA_TYPES } = require('../../constants');
|
|
4
|
+
const { EnumKeyResolver } = require('../util/enum-key-resolver');
|
|
5
|
+
|
|
6
|
+
class EnumSchemaParser extends MonoSchemaParser {
|
|
7
|
+
/** @type {EnumKeyResolver} */
|
|
8
|
+
enumKeyResolver;
|
|
9
|
+
|
|
10
|
+
constructor(...args) {
|
|
11
|
+
super(...args);
|
|
12
|
+
this.enumKeyResolver = new EnumKeyResolver(this.config, this.logger, []);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
extractEnum = (pathTypeName) => {
|
|
16
|
+
const generatedTypeName = this.schemaUtils.resolveTypeName(pathTypeName, {
|
|
17
|
+
suffixes: this.config.extractingOptions.enumSuffix,
|
|
18
|
+
resolver: this.config.extractingOptions.enumNameResolver,
|
|
19
|
+
});
|
|
20
|
+
const customComponent = this.schemaComponentsMap.createComponent(
|
|
21
|
+
this.schemaComponentsMap.createRef([
|
|
22
|
+
'components',
|
|
23
|
+
'schemas',
|
|
24
|
+
generatedTypeName,
|
|
25
|
+
]),
|
|
26
|
+
{
|
|
27
|
+
...this.schema,
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
return this.schemaParserFabric.parseSchema(customComponent);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
parse() {
|
|
34
|
+
const pathTypeName = this.buildTypeNameFromPath();
|
|
35
|
+
|
|
36
|
+
if (this.config.extractEnums && !this.typeName && pathTypeName != null) {
|
|
37
|
+
return this.extractEnum(pathTypeName);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const refType = this.schemaUtils.getSchemaRefType(this.schema);
|
|
41
|
+
const $ref = (refType && refType.$ref) || null;
|
|
42
|
+
|
|
43
|
+
// fix schema when enum has length 1+ but value is []
|
|
44
|
+
if (Array.isArray(this.schema.enum)) {
|
|
45
|
+
this.schema.enum = this.schema.enum.filter((key) => key != null);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (Array.isArray(this.schema.enum) && Array.isArray(this.schema.enum[0])) {
|
|
49
|
+
return this.schemaParserFabric.parseSchema(
|
|
50
|
+
{
|
|
51
|
+
oneOf: this.schema.enum.map((enumNames) => ({
|
|
52
|
+
type: 'array',
|
|
53
|
+
items: enumNames.map((enumName) => ({
|
|
54
|
+
type: 'string',
|
|
55
|
+
enum: [enumName],
|
|
56
|
+
})),
|
|
57
|
+
})),
|
|
58
|
+
},
|
|
59
|
+
this.typeName,
|
|
60
|
+
this.schemaPath,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const keyType = this.schemaUtils.getSchemaType(this.schema);
|
|
65
|
+
const enumNames = this.schemaUtils.getEnumNames(this.schema);
|
|
66
|
+
let content = null;
|
|
67
|
+
|
|
68
|
+
const formatValue = (value) => {
|
|
69
|
+
if (value === null) {
|
|
70
|
+
return this.config.Ts.NullValue(value);
|
|
71
|
+
}
|
|
72
|
+
if (
|
|
73
|
+
_.includes(keyType, this.schemaUtils.getSchemaType({ type: 'number' }))
|
|
74
|
+
) {
|
|
75
|
+
return this.config.Ts.NumberValue(value);
|
|
76
|
+
}
|
|
77
|
+
if (
|
|
78
|
+
_.includes(keyType, this.schemaUtils.getSchemaType({ type: 'boolean' }))
|
|
79
|
+
) {
|
|
80
|
+
return this.config.Ts.BooleanValue(value);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return this.config.Ts.StringValue(value);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
if (_.isArray(enumNames) && _.size(enumNames)) {
|
|
87
|
+
content = _.map(enumNames, (enumName, index) => {
|
|
88
|
+
const enumValue = _.get(this.schema.enum, index);
|
|
89
|
+
const formattedKey = this.formatEnumKey({
|
|
90
|
+
key: enumName,
|
|
91
|
+
value: enumValue,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (this.config.enumNamesAsValues || _.isUndefined(enumValue)) {
|
|
95
|
+
return {
|
|
96
|
+
key: formattedKey,
|
|
97
|
+
type: this.config.Ts.Keyword.String,
|
|
98
|
+
value: this.config.Ts.StringValue(enumName),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
key: formattedKey,
|
|
104
|
+
type: keyType,
|
|
105
|
+
value: formatValue(enumValue),
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
content = _.map(this.schema.enum, (value) => {
|
|
110
|
+
return {
|
|
111
|
+
key: this.formatEnumKey({ value }),
|
|
112
|
+
type: keyType,
|
|
113
|
+
value: formatValue(value),
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
...(_.isObject(this.schema) ? this.schema : {}),
|
|
120
|
+
$ref: $ref,
|
|
121
|
+
typeName: this.typeName || ($ref && refType.typeName) || null,
|
|
122
|
+
$parsedSchema: true,
|
|
123
|
+
schemaType: SCHEMA_TYPES.ENUM,
|
|
124
|
+
type: SCHEMA_TYPES.ENUM,
|
|
125
|
+
keyType: keyType,
|
|
126
|
+
typeIdentifier: this.config.generateUnionEnums
|
|
127
|
+
? this.config.Ts.Keyword.Type
|
|
128
|
+
: this.config.Ts.Keyword.Enum,
|
|
129
|
+
name: this.typeName,
|
|
130
|
+
description: this.schemaFormatters.formatDescription(
|
|
131
|
+
this.schema.description,
|
|
132
|
+
),
|
|
133
|
+
content,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
formatEnumKey = ({ key, value }) => {
|
|
138
|
+
let formatted;
|
|
139
|
+
|
|
140
|
+
if (key) {
|
|
141
|
+
formatted = this.typeNameFormatter.format(key, {
|
|
142
|
+
type: 'enum-key',
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!formatted) {
|
|
147
|
+
formatted = this.typeNameFormatter.format(`${value}`, {
|
|
148
|
+
type: 'enum-key',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return this.enumKeyResolver.resolve([formatted]);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
module.exports = {
|
|
157
|
+
EnumSchemaParser,
|
|
158
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const { MonoSchemaParser } = require('../mono-schema-parser');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
const { SCHEMA_TYPES } = require('../../constants');
|
|
4
|
+
|
|
5
|
+
class ObjectSchemaParser extends MonoSchemaParser {
|
|
6
|
+
parse() {
|
|
7
|
+
const contentProperties = this.getObjectSchemaContent(this.schema);
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
...(_.isObject(this.schema) ? this.schema : {}),
|
|
11
|
+
$schemaPath: this.schemaPath.slice(),
|
|
12
|
+
$parsedSchema: true,
|
|
13
|
+
schemaType: SCHEMA_TYPES.OBJECT,
|
|
14
|
+
type: SCHEMA_TYPES.OBJECT,
|
|
15
|
+
typeIdentifier: this.config.Ts.Keyword.Interface,
|
|
16
|
+
name: this.typeName,
|
|
17
|
+
description: this.schemaFormatters.formatDescription(
|
|
18
|
+
this.schema.description,
|
|
19
|
+
),
|
|
20
|
+
allFieldsAreOptional: !_.some(
|
|
21
|
+
_.values(contentProperties),
|
|
22
|
+
(part) => part.isRequired,
|
|
23
|
+
),
|
|
24
|
+
content: contentProperties,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getObjectSchemaContent = (schema) => {
|
|
29
|
+
const { properties, additionalProperties } = schema || {};
|
|
30
|
+
|
|
31
|
+
const propertiesContent = _.map(properties, (property, name) => {
|
|
32
|
+
const required = this.schemaUtils.isPropertyRequired(
|
|
33
|
+
name,
|
|
34
|
+
property,
|
|
35
|
+
schema,
|
|
36
|
+
);
|
|
37
|
+
const rawTypeData = _.get(
|
|
38
|
+
this.schemaUtils.getSchemaRefType(property),
|
|
39
|
+
'rawTypeData',
|
|
40
|
+
{},
|
|
41
|
+
);
|
|
42
|
+
const nullable = !!(rawTypeData.nullable || property.nullable);
|
|
43
|
+
const fieldName = this.typeNameFormatter.isValidName(name)
|
|
44
|
+
? name
|
|
45
|
+
: this.config.Ts.StringValue(name);
|
|
46
|
+
const fieldValue = this.schemaParserFabric
|
|
47
|
+
.createSchemaParser({
|
|
48
|
+
schema: property,
|
|
49
|
+
schemaPath: [...this.schemaPath, name],
|
|
50
|
+
})
|
|
51
|
+
.getInlineParseContent();
|
|
52
|
+
const readOnly = property.readOnly;
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
...property,
|
|
56
|
+
$$raw: property,
|
|
57
|
+
title: property.title,
|
|
58
|
+
description:
|
|
59
|
+
property.description ||
|
|
60
|
+
_.compact(
|
|
61
|
+
_.map(
|
|
62
|
+
property[this.schemaUtils.getComplexType(property)],
|
|
63
|
+
'description',
|
|
64
|
+
),
|
|
65
|
+
)[0] ||
|
|
66
|
+
rawTypeData.description ||
|
|
67
|
+
_.compact(
|
|
68
|
+
_.map(
|
|
69
|
+
rawTypeData[this.schemaUtils.getComplexType(rawTypeData)],
|
|
70
|
+
'description',
|
|
71
|
+
),
|
|
72
|
+
)[0] ||
|
|
73
|
+
'',
|
|
74
|
+
isRequired: required,
|
|
75
|
+
isNullable: nullable,
|
|
76
|
+
name: fieldName,
|
|
77
|
+
value: fieldValue,
|
|
78
|
+
field: this.config.Ts.TypeField({
|
|
79
|
+
readonly: readOnly && this.config.addReadonly,
|
|
80
|
+
optional: !required,
|
|
81
|
+
key: fieldName,
|
|
82
|
+
value: fieldValue,
|
|
83
|
+
}),
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (additionalProperties) {
|
|
88
|
+
propertiesContent.push({
|
|
89
|
+
$$raw: { additionalProperties },
|
|
90
|
+
description: '',
|
|
91
|
+
isRequired: false,
|
|
92
|
+
field: this.config.Ts.InterfaceDynamicField(
|
|
93
|
+
this.config.Ts.Keyword.String,
|
|
94
|
+
this.config.Ts.Keyword.Any,
|
|
95
|
+
),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return propertiesContent;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
module.exports = {
|
|
104
|
+
ObjectSchemaParser,
|
|
105
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const { MonoSchemaParser } = require('../mono-schema-parser');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
const { SCHEMA_TYPES } = require('../../constants');
|
|
4
|
+
|
|
5
|
+
class PrimitiveSchemaParser extends MonoSchemaParser {
|
|
6
|
+
parse() {
|
|
7
|
+
let contentType = null;
|
|
8
|
+
const { additionalProperties, type, description, items } =
|
|
9
|
+
this.schema || {};
|
|
10
|
+
|
|
11
|
+
if (type === this.config.Ts.Keyword.Object && additionalProperties) {
|
|
12
|
+
const fieldType = _.isObject(additionalProperties)
|
|
13
|
+
? this.schemaParserFabric
|
|
14
|
+
.createSchemaParser({
|
|
15
|
+
schema: additionalProperties,
|
|
16
|
+
schemaPath: this.schemaPath,
|
|
17
|
+
})
|
|
18
|
+
.getInlineParseContent()
|
|
19
|
+
: this.config.Ts.Keyword.Any;
|
|
20
|
+
contentType = this.config.Ts.RecordType(
|
|
21
|
+
this.config.Ts.Keyword.String,
|
|
22
|
+
fieldType,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (_.isArray(type) && type.length) {
|
|
27
|
+
contentType = this.schemaParser._complexSchemaParsers.oneOf({
|
|
28
|
+
...(_.isObject(this.schema) ? this.schema : {}),
|
|
29
|
+
oneOf: type.map((type) => ({ type })),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (_.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
|
|
34
|
+
contentType = this.config.Ts.Tuple(
|
|
35
|
+
items.map((item) =>
|
|
36
|
+
this.schemaParserFabric
|
|
37
|
+
.createSchemaParser({ schema: item, schemaPath: this.schemaPath })
|
|
38
|
+
.getInlineParseContent(),
|
|
39
|
+
),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
...(_.isObject(this.schema) ? this.schema : {}),
|
|
45
|
+
$schemaPath: this.schemaPath.slice(),
|
|
46
|
+
$parsedSchema: true,
|
|
47
|
+
schemaType: SCHEMA_TYPES.PRIMITIVE,
|
|
48
|
+
type: SCHEMA_TYPES.PRIMITIVE,
|
|
49
|
+
typeIdentifier: this.config.Ts.Keyword.Type,
|
|
50
|
+
name: this.typeName,
|
|
51
|
+
description: this.schemaFormatters.formatDescription(description),
|
|
52
|
+
// TODO: probably it should be refactored. `type === 'null'` is not flexible
|
|
53
|
+
content:
|
|
54
|
+
type === this.config.Ts.Keyword.Null
|
|
55
|
+
? type
|
|
56
|
+
: contentType || this.schemaUtils.getSchemaType(this.schema),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = {
|
|
62
|
+
PrimitiveSchemaParser,
|
|
63
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { MonoSchemaParser } = require('../mono-schema-parser');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
|
|
4
|
+
// T1 & T2
|
|
5
|
+
class AllOfSchemaParser extends MonoSchemaParser {
|
|
6
|
+
parse() {
|
|
7
|
+
const ignoreTypes = [this.config.Ts.Keyword.Any];
|
|
8
|
+
const combined = _.map(this.schema.allOf, (childSchema) =>
|
|
9
|
+
this.schemaParserFabric.getInlineParseContent(
|
|
10
|
+
this.schemaUtils.makeAddRequiredToChildSchema(this.schema, childSchema),
|
|
11
|
+
null,
|
|
12
|
+
this.schemaPath,
|
|
13
|
+
),
|
|
14
|
+
);
|
|
15
|
+
const filtered = this.schemaUtils.filterSchemaContents(
|
|
16
|
+
combined,
|
|
17
|
+
(content) => !ignoreTypes.includes(content),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const type = this.config.Ts.IntersectionType(filtered);
|
|
21
|
+
|
|
22
|
+
return this.schemaUtils.safeAddNullToType(this.schema, type);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = { AllOfSchemaParser };
|