swagger-typescript-api 13.0.0-experimental-1 → 13.0.1

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 (81) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +26 -12
  3. package/cli/constants.js +3 -3
  4. package/cli/execute.js +52 -31
  5. package/cli/index.d.ts +1 -2
  6. package/cli/index.js +18 -17
  7. package/cli/operations/display-help.js +51 -29
  8. package/cli/parse-args.js +3 -3
  9. package/cli/process-option.js +28 -20
  10. package/index.d.ts +113 -8
  11. package/index.js +164 -135
  12. package/package.json +36 -30
  13. package/src/code-formatter.js +28 -13
  14. package/src/code-gen-process.js +367 -259
  15. package/src/commands/generate-templates/configuration.js +2 -2
  16. package/src/commands/generate-templates/index.js +1 -2
  17. package/src/commands/generate-templates/templates-gen-process.js +62 -35
  18. package/src/component-type-name-resolver.js +44 -0
  19. package/src/configuration.js +172 -96
  20. package/src/constants.js +28 -22
  21. package/src/index.js +3 -4
  22. package/src/schema-components-map.js +43 -25
  23. package/src/schema-parser/base-schema-parsers/array.js +43 -0
  24. package/src/schema-parser/base-schema-parsers/complex.js +51 -0
  25. package/src/schema-parser/base-schema-parsers/discriminator.js +304 -0
  26. package/src/schema-parser/base-schema-parsers/enum.js +158 -0
  27. package/src/schema-parser/base-schema-parsers/object.js +105 -0
  28. package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
  29. package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
  30. package/src/schema-parser/complex-schema-parsers/any-of.js +27 -0
  31. package/src/schema-parser/complex-schema-parsers/not.js +9 -0
  32. package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
  33. package/src/schema-parser/mono-schema-parser.js +48 -0
  34. package/src/schema-parser/schema-formatters.js +69 -60
  35. package/src/schema-parser/schema-parser-fabric.js +131 -0
  36. package/src/schema-parser/schema-parser.js +234 -425
  37. package/src/schema-parser/schema-utils.js +165 -67
  38. package/src/schema-parser/util/enum-key-resolver.js +26 -0
  39. package/src/schema-routes/schema-routes.js +1222 -0
  40. package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
  41. package/src/schema-walker.js +93 -0
  42. package/src/swagger-schema-resolver.js +61 -28
  43. package/src/templates-worker.js +240 -0
  44. package/src/translators/javascript.js +83 -0
  45. package/src/translators/translator.js +35 -0
  46. package/src/type-name-formatter.js +43 -22
  47. package/src/util/file-system.js +30 -14
  48. package/src/util/id.js +2 -2
  49. package/src/util/internal-case.js +1 -1
  50. package/src/util/logger.js +46 -20
  51. package/src/util/name-resolver.js +52 -60
  52. package/src/util/object-assign.js +7 -3
  53. package/src/util/pascal-case.js +1 -1
  54. package/src/util/request.js +5 -5
  55. package/src/util/sort-by-property.js +17 -0
  56. package/templates/README.md +17 -17
  57. package/templates/base/README.md +7 -7
  58. package/templates/base/data-contract-jsdoc.ejs +37 -37
  59. package/templates/base/data-contracts.ejs +40 -27
  60. package/templates/base/enum-data-contract.ejs +12 -12
  61. package/templates/base/http-client.ejs +3 -3
  62. package/templates/base/http-clients/axios-http-client.ejs +139 -138
  63. package/templates/base/http-clients/fetch-http-client.ejs +224 -224
  64. package/templates/base/interface-data-contract.ejs +10 -10
  65. package/templates/base/object-field-jsdoc.ejs +28 -28
  66. package/templates/base/route-docs.ejs +30 -30
  67. package/templates/base/route-name.ejs +42 -42
  68. package/templates/base/route-type.ejs +22 -21
  69. package/templates/base/type-data-contract.ejs +15 -15
  70. package/templates/default/README.md +6 -6
  71. package/templates/default/api.ejs +69 -68
  72. package/templates/default/procedure-call.ejs +100 -100
  73. package/templates/default/route-types.ejs +32 -32
  74. package/templates/modular/README.md +6 -6
  75. package/templates/modular/api.ejs +28 -28
  76. package/templates/modular/procedure-call.ejs +100 -100
  77. package/templates/modular/route-types.ejs +18 -18
  78. package/src/schema-parser/schema-processor.js +0 -79
  79. package/src/schema-parser/schema-routes.js +0 -950
  80. package/src/templates.js +0 -182
  81. package/src/translators/JavaScript.js +0 -60
@@ -0,0 +1,43 @@
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
+ };
@@ -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,304 @@
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 mappingTypeNameRef = this.schemaComponentsMap.createRef([
78
+ 'components',
79
+ 'schemas',
80
+ this.schemaUtils.resolveTypeName(
81
+ `${abstractSchemaStruct.typeName} ${discriminator.propertyName}`,
82
+ {
83
+ suffixes: this.config.extractingOptions.discriminatorMappingSuffix,
84
+ resolver:
85
+ this.config.extractingOptions.discriminatorMappingNameResolver,
86
+ },
87
+ ),
88
+ ]);
89
+ const mappingTypeNameComponent =
90
+ this.schemaComponentsMap.createComponent(mappingTypeNameRef);
91
+ const mappingTypeNameSchema = this.schemaParserFabric.createSchema({
92
+ linkedComponent: mappingTypeNameComponent,
93
+ content: ts.IntersectionType([
94
+ ts.ObjectWrapper(
95
+ ts.TypeField({
96
+ key: ts.StringValue(discriminator.propertyName),
97
+ value: 'Key',
98
+ }),
99
+ ),
100
+ 'Type',
101
+ ]),
102
+ genericArgs: [{ name: 'Key' }, { name: 'Type' }],
103
+ internal: true,
104
+ });
105
+
106
+ mappingTypeName = mappingTypeNameSchema.typeData.name;
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 schemaIsEmpty = !_.keys(schema).length;
263
+
264
+ if (schemaIsEmpty) return null;
265
+
266
+ const typeName = this.schemaUtils.resolveTypeName(this.typeName, {
267
+ prefixes: this.config.extractingOptions.discriminatorAbstractPrefix,
268
+ resolver: this.config.extractingOptions.discriminatorAbstractResolver,
269
+ });
270
+ const component = this.schemaComponentsMap.createComponent(
271
+ this.schemaComponentsMap.createRef(['components', 'schemas', typeName]),
272
+ {
273
+ ...schema,
274
+ internal: true,
275
+ },
276
+ );
277
+ const content = this.schemaParserFabric
278
+ .createSchemaParser({ schema: component, schemaPath: this.schemaPath })
279
+ .getInlineParseContent();
280
+
281
+ return {
282
+ typeName,
283
+ component,
284
+ content,
285
+ };
286
+ };
287
+
288
+ createComplexSchemaStruct = () => {
289
+ const ts = this.config.Ts;
290
+ const complexType = this.schemaUtils.getComplexType(this.schema);
291
+
292
+ if (complexType === SCHEMA_TYPES.COMPLEX_UNKNOWN) return null;
293
+
294
+ return {
295
+ content: ts.ExpressionGroup(
296
+ this.schemaParser._complexSchemaParsers[complexType](this.schema),
297
+ ),
298
+ };
299
+ };
300
+ }
301
+
302
+ module.exports = {
303
+ DiscriminatorSchemaParser,
304
+ };
@@ -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
+ };