swagger-typescript-api 13.0.9 → 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.
- package/dist/chunk-3S356DIU.cjs +105 -0
- package/dist/chunk-3S356DIU.cjs.map +1 -0
- package/dist/chunk-MTWJNW6B.js +65 -0
- package/dist/chunk-MTWJNW6B.js.map +1 -0
- package/dist/cli.cjs +43 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.js +36 -0
- package/dist/cli.js.map +1 -0
- package/dist/lib.cjs +21 -0
- package/dist/lib.cjs.map +1 -0
- package/dist/lib.js +4 -0
- package/dist/lib.js.map +1 -0
- package/package.json +25 -14
- package/cli/constants.js +0 -6
- package/cli/execute.js +0 -180
- package/cli/index.js +0 -94
- package/cli/operations/display-help.js +0 -177
- package/cli/operations/display-version.js +0 -5
- package/cli/parse-args.js +0 -24
- package/cli/process-option.js +0 -75
- package/index.js +0 -344
- package/src/code-formatter.js +0 -114
- package/src/code-gen-process.js +0 -569
- package/src/commands/generate-templates/configuration.js +0 -31
- package/src/commands/generate-templates/index.js +0 -10
- package/src/commands/generate-templates/templates-gen-process.js +0 -205
- package/src/component-type-name-resolver.js +0 -42
- package/src/configuration.js +0 -445
- package/src/constants.js +0 -77
- package/src/index.js +0 -16
- package/src/schema-components-map.js +0 -76
- package/src/schema-parser/base-schema-parsers/array.js +0 -41
- package/src/schema-parser/base-schema-parsers/complex.js +0 -49
- package/src/schema-parser/base-schema-parsers/discriminator.js +0 -305
- package/src/schema-parser/base-schema-parsers/enum.js +0 -156
- package/src/schema-parser/base-schema-parsers/object.js +0 -103
- package/src/schema-parser/base-schema-parsers/primitive.js +0 -61
- package/src/schema-parser/complex-schema-parsers/all-of.js +0 -26
- package/src/schema-parser/complex-schema-parsers/any-of.js +0 -27
- package/src/schema-parser/complex-schema-parsers/not.js +0 -9
- package/src/schema-parser/complex-schema-parsers/one-of.js +0 -27
- package/src/schema-parser/mono-schema-parser.js +0 -46
- package/src/schema-parser/schema-formatters.js +0 -164
- package/src/schema-parser/schema-parser-fabric.js +0 -130
- package/src/schema-parser/schema-parser.js +0 -295
- package/src/schema-parser/schema-utils.js +0 -319
- package/src/schema-parser/util/enum-key-resolver.js +0 -24
- package/src/schema-routes/schema-routes.js +0 -1208
- package/src/schema-routes/util/specific-arg-name-resolver.js +0 -24
- package/src/schema-walker.js +0 -91
- package/src/swagger-schema-resolver.js +0 -195
- package/src/templates-worker.js +0 -243
- package/src/translators/javascript.js +0 -81
- package/src/translators/translator.js +0 -33
- package/src/type-name-formatter.js +0 -111
- package/src/util/file-system.js +0 -95
- package/src/util/id.js +0 -7
- package/src/util/internal-case.js +0 -7
- package/src/util/logger.js +0 -142
- package/src/util/name-resolver.js +0 -103
- package/src/util/object-assign.js +0 -17
- package/src/util/pascal-case.js +0 -7
- package/src/util/random.js +0 -11
- package/src/util/request.js +0 -63
- package/src/util/sort-by-property.js +0 -15
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import { SCHEMA_TYPES } from "../../constants.js";
|
|
3
|
-
import { MonoSchemaParser } from "../mono-schema-parser.js";
|
|
4
|
-
import { EnumKeyResolver } from "../util/enum-key-resolver.js";
|
|
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?.$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
|
-
export { EnumSchemaParser };
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import { SCHEMA_TYPES } from "../../constants.js";
|
|
3
|
-
import { MonoSchemaParser } from "../mono-schema-parser.js";
|
|
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
|
-
export { ObjectSchemaParser };
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import { SCHEMA_TYPES } from "../../constants.js";
|
|
3
|
-
import { MonoSchemaParser } from "../mono-schema-parser.js";
|
|
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
|
-
export { PrimitiveSchemaParser };
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import { MonoSchemaParser } from "../mono-schema-parser.js";
|
|
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
|
-
export { AllOfSchemaParser };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import { MonoSchemaParser } from "../mono-schema-parser.js";
|
|
3
|
-
|
|
4
|
-
// T1 | T2
|
|
5
|
-
class AnyOfSchemaParser extends MonoSchemaParser {
|
|
6
|
-
parse() {
|
|
7
|
-
const ignoreTypes = [this.config.Ts.Keyword.Any];
|
|
8
|
-
const combined = _.map(this.schema.anyOf, (childSchema) =>
|
|
9
|
-
this.schemaParserFabric.getInlineParseContent(
|
|
10
|
-
this.schemaUtils.makeAddRequiredToChildSchema(this.schema, childSchema),
|
|
11
|
-
null,
|
|
12
|
-
this.schemaPath,
|
|
13
|
-
),
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
const filtered = this.schemaUtils.filterSchemaContents(
|
|
17
|
-
combined,
|
|
18
|
-
(content) => !ignoreTypes.includes(content),
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
const type = this.config.Ts.UnionType(filtered);
|
|
22
|
-
|
|
23
|
-
return this.schemaUtils.safeAddNullToType(this.schema, type);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export { AnyOfSchemaParser };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import { MonoSchemaParser } from "../mono-schema-parser.js";
|
|
3
|
-
|
|
4
|
-
// T1 | T2
|
|
5
|
-
class OneOfSchemaParser extends MonoSchemaParser {
|
|
6
|
-
parse() {
|
|
7
|
-
const ignoreTypes = [this.config.Ts.Keyword.Any];
|
|
8
|
-
const combined = _.map(this.schema.oneOf, (childSchema) =>
|
|
9
|
-
this.schemaParserFabric.getInlineParseContent(
|
|
10
|
-
this.schemaUtils.makeAddRequiredToChildSchema(this.schema, childSchema),
|
|
11
|
-
null,
|
|
12
|
-
this.schemaPath,
|
|
13
|
-
),
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
const filtered = this.schemaUtils.filterSchemaContents(
|
|
17
|
-
combined,
|
|
18
|
-
(content) => !ignoreTypes.includes(content),
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
const type = this.config.Ts.UnionType(filtered);
|
|
22
|
-
|
|
23
|
-
return this.schemaUtils.safeAddNullToType(this.schema, type);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export { OneOfSchemaParser };
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
class MonoSchemaParser {
|
|
2
|
-
schema;
|
|
3
|
-
typeName;
|
|
4
|
-
schemaPath;
|
|
5
|
-
|
|
6
|
-
/** @type {Logger} */
|
|
7
|
-
logger;
|
|
8
|
-
/** @type {SchemaParser} */
|
|
9
|
-
schemaParser;
|
|
10
|
-
/** @type {SchemaParserFabric} */
|
|
11
|
-
schemaParserFabric;
|
|
12
|
-
/** @type {TypeNameFormatter} */
|
|
13
|
-
typeNameFormatter;
|
|
14
|
-
/** @type {SchemaComponentsMap} */
|
|
15
|
-
schemaComponentsMap;
|
|
16
|
-
/** @type {SchemaUtils} */
|
|
17
|
-
schemaUtils;
|
|
18
|
-
/** @type {CodeGenConfig} */
|
|
19
|
-
config;
|
|
20
|
-
/** @type {SchemaFormatters} */
|
|
21
|
-
schemaFormatters;
|
|
22
|
-
|
|
23
|
-
constructor(schemaParser, schema, typeName = null, schemaPath = []) {
|
|
24
|
-
this.schemaParser = schemaParser;
|
|
25
|
-
this.schemaParserFabric = schemaParser.schemaParserFabric;
|
|
26
|
-
this.logger = schemaParser.logger;
|
|
27
|
-
this.schema = schema;
|
|
28
|
-
this.typeName = typeName;
|
|
29
|
-
this.typeNameFormatter = schemaParser.typeNameFormatter;
|
|
30
|
-
this.schemaPath = schemaPath;
|
|
31
|
-
this.schemaComponentsMap = this.schemaParser.schemaComponentsMap;
|
|
32
|
-
this.schemaUtils = this.schemaParser.schemaUtils;
|
|
33
|
-
this.config = this.schemaParser.config;
|
|
34
|
-
this.schemaFormatters = this.schemaParser.schemaFormatters;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
parse() {
|
|
38
|
-
throw new Error("not implemented");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
buildTypeNameFromPath = () => {
|
|
42
|
-
return this.schemaUtils.buildTypeNameFromPath(this.schemaPath);
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { MonoSchemaParser };
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import { SCHEMA_TYPES } from "../constants.js";
|
|
3
|
-
|
|
4
|
-
class SchemaFormatters {
|
|
5
|
-
/** @type {CodeGenConfig} */
|
|
6
|
-
config;
|
|
7
|
-
/** @type {Logger} */
|
|
8
|
-
logger;
|
|
9
|
-
/** @type {TemplatesWorker} */
|
|
10
|
-
templatesWorker;
|
|
11
|
-
/** @type {SchemaUtils} */
|
|
12
|
-
schemaUtils;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @param schemaParser {SchemaParser | SchemaParserFabric}
|
|
16
|
-
*/
|
|
17
|
-
constructor(schemaParser) {
|
|
18
|
-
this.config = schemaParser.config;
|
|
19
|
-
this.logger = schemaParser.logger;
|
|
20
|
-
this.schemaUtils = schemaParser.schemaUtils;
|
|
21
|
-
this.templatesWorker = schemaParser.templatesWorker;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
base = {
|
|
25
|
-
[SCHEMA_TYPES.ENUM]: (parsedSchema) => {
|
|
26
|
-
if (this.config.generateUnionEnums) {
|
|
27
|
-
return {
|
|
28
|
-
...parsedSchema,
|
|
29
|
-
$content: parsedSchema.content,
|
|
30
|
-
content: this.config.Ts.UnionType(
|
|
31
|
-
_.map(parsedSchema.content, ({ value }) => value),
|
|
32
|
-
),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
...parsedSchema,
|
|
38
|
-
$content: parsedSchema.content,
|
|
39
|
-
content: this.config.Ts.EnumFieldsWrapper(parsedSchema.content),
|
|
40
|
-
};
|
|
41
|
-
},
|
|
42
|
-
[SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
|
|
43
|
-
if (parsedSchema.nullable)
|
|
44
|
-
return this.inline[SCHEMA_TYPES.OBJECT](parsedSchema);
|
|
45
|
-
return {
|
|
46
|
-
...parsedSchema,
|
|
47
|
-
$content: parsedSchema.content,
|
|
48
|
-
content: this.formatObjectContent(parsedSchema.content),
|
|
49
|
-
};
|
|
50
|
-
},
|
|
51
|
-
[SCHEMA_TYPES.PRIMITIVE]: (parsedSchema) => {
|
|
52
|
-
return {
|
|
53
|
-
...parsedSchema,
|
|
54
|
-
$content: parsedSchema.content,
|
|
55
|
-
};
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
inline = {
|
|
59
|
-
[SCHEMA_TYPES.ENUM]: (parsedSchema) => {
|
|
60
|
-
return {
|
|
61
|
-
...parsedSchema,
|
|
62
|
-
content: parsedSchema.$ref
|
|
63
|
-
? parsedSchema.typeName
|
|
64
|
-
: this.config.Ts.UnionType(
|
|
65
|
-
_.compact([
|
|
66
|
-
..._.map(parsedSchema.content, ({ value }) => `${value}`),
|
|
67
|
-
parsedSchema.nullable && this.config.Ts.Keyword.Null,
|
|
68
|
-
]),
|
|
69
|
-
) || this.config.Ts.Keyword.Any,
|
|
70
|
-
};
|
|
71
|
-
},
|
|
72
|
-
[SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
|
|
73
|
-
if (_.isString(parsedSchema.content)) {
|
|
74
|
-
return {
|
|
75
|
-
...parsedSchema,
|
|
76
|
-
typeIdentifier: this.config.Ts.Keyword.Type,
|
|
77
|
-
content: this.schemaUtils.safeAddNullToType(parsedSchema.content),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return {
|
|
82
|
-
...parsedSchema,
|
|
83
|
-
typeIdentifier: this.config.Ts.Keyword.Type,
|
|
84
|
-
content: this.schemaUtils.safeAddNullToType(
|
|
85
|
-
parsedSchema,
|
|
86
|
-
parsedSchema.content.length
|
|
87
|
-
? this.config.Ts.ObjectWrapper(
|
|
88
|
-
this.formatObjectContent(parsedSchema.content),
|
|
89
|
-
)
|
|
90
|
-
: this.config.Ts.RecordType(
|
|
91
|
-
this.config.Ts.Keyword.String,
|
|
92
|
-
this.config.Ts.Keyword.Any,
|
|
93
|
-
),
|
|
94
|
-
),
|
|
95
|
-
};
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* @param parsedSchema {Record<string, any>}
|
|
101
|
-
* @param formatType {"base" | "inline"}
|
|
102
|
-
*/
|
|
103
|
-
formatSchema = (parsedSchema, formatType = "base") => {
|
|
104
|
-
const schemaType =
|
|
105
|
-
_.get(parsedSchema, ["schemaType"]) ||
|
|
106
|
-
_.get(parsedSchema, ["$parsed", "schemaType"]);
|
|
107
|
-
const formatterFn = _.get(this, [formatType, schemaType]);
|
|
108
|
-
return formatterFn?.(parsedSchema) || parsedSchema;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
formatDescription = (description, inline) => {
|
|
112
|
-
if (!description) return "";
|
|
113
|
-
|
|
114
|
-
let prettified = description;
|
|
115
|
-
|
|
116
|
-
prettified = _.replace(prettified, /\*\//g, "*/");
|
|
117
|
-
|
|
118
|
-
const hasMultipleLines = _.includes(prettified, "\n");
|
|
119
|
-
|
|
120
|
-
if (!hasMultipleLines) return prettified;
|
|
121
|
-
|
|
122
|
-
if (inline) {
|
|
123
|
-
return _(prettified)
|
|
124
|
-
.split(/\n/g)
|
|
125
|
-
.map((part) => _.trim(part))
|
|
126
|
-
.compact()
|
|
127
|
-
.join(" ")
|
|
128
|
-
.valueOf();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return _.replace(prettified, /\n$/g, "");
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
formatObjectContent = (content) => {
|
|
135
|
-
const fields = [];
|
|
136
|
-
|
|
137
|
-
for (const part of content) {
|
|
138
|
-
const extraSpace = " ";
|
|
139
|
-
const result = `${extraSpace}${part.field},\n`;
|
|
140
|
-
|
|
141
|
-
const renderedJsDoc = this.templatesWorker.renderTemplate(
|
|
142
|
-
this.config.templatesToRender.dataContractJsDoc,
|
|
143
|
-
{
|
|
144
|
-
data: part,
|
|
145
|
-
},
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
const routeNameFromTemplate = renderedJsDoc
|
|
149
|
-
.split("\n")
|
|
150
|
-
.map((c) => `${extraSpace}${c}`)
|
|
151
|
-
.join("\n");
|
|
152
|
-
|
|
153
|
-
if (routeNameFromTemplate) {
|
|
154
|
-
fields.push(`${routeNameFromTemplate}${result}`);
|
|
155
|
-
} else {
|
|
156
|
-
fields.push(`${result}`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return fields.join("");
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export { SchemaFormatters };
|