@goast/typescript 0.2.2 → 0.2.4
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.
|
@@ -28,6 +28,7 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
getFileContent(ctx) {
|
|
31
|
+
var _a;
|
|
31
32
|
const content = (0, core_1.appendValueGroup)([], '\n');
|
|
32
33
|
const interfaceSchema = this.resolveInterfaceSchema(ctx);
|
|
33
34
|
if (interfaceSchema) {
|
|
@@ -39,6 +40,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
39
40
|
ctx.schema.enum.length > 0) {
|
|
40
41
|
content.values.push(this.getEnum(ctx));
|
|
41
42
|
}
|
|
43
|
+
else if (ctx.schema.discriminator && Object.keys(ctx.schema.discriminator.mapping).length > 0) {
|
|
44
|
+
content.values.push(ast_1.ts.typeAlias(this.getDiscriminatorTypeName(ctx, ctx.schema), ast_1.ts.unionType(Object.keys(ctx.schema.discriminator.mapping).map((x) => ast_1.ts.string(x)))), ast_1.ts.typeAlias(this.getBaseTypeName(ctx, ctx.schema), (_a = this.getType(ctx, ctx.schema, true)) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), { export: true }), this.getTypeAlias(ctx, ctx.schema));
|
|
45
|
+
}
|
|
42
46
|
else {
|
|
43
47
|
content.values.push(this.getTypeAlias(ctx, ctx.schema));
|
|
44
48
|
}
|
|
@@ -60,12 +64,13 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
60
64
|
}
|
|
61
65
|
getTypeAlias(ctx, schema) {
|
|
62
66
|
var _a;
|
|
63
|
-
const
|
|
67
|
+
const isDiscriminated = schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0;
|
|
68
|
+
const result = ast_1.ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = (isDiscriminated ? this.getDiscriminatorType(ctx, schema) : this.getType(ctx, schema, true))) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
|
|
64
69
|
export: true,
|
|
65
70
|
doc: ast_1.ts.doc({ description: schema.description }),
|
|
66
71
|
});
|
|
67
|
-
if (
|
|
68
|
-
const propertyValue =
|
|
72
|
+
if (isDiscriminated) {
|
|
73
|
+
const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
|
|
69
74
|
result.generics.push(ast_1.ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
|
|
70
75
|
constraint: propertyValue,
|
|
71
76
|
default: propertyValue,
|
|
@@ -93,21 +98,15 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
93
98
|
if (!schema)
|
|
94
99
|
return this.getAnyType(ctx);
|
|
95
100
|
schema = (0, core_1.getSchemaReference)(schema, ['description']);
|
|
101
|
+
if (ctx.schema.inheritedSchemas.some((x) => x.id === schema.id)) {
|
|
102
|
+
return ast_1.ts.reference(this.getBaseTypeName(ctx, schema), this.getFilePath(ctx, schema));
|
|
103
|
+
}
|
|
96
104
|
if (!skipSchemas && this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
97
105
|
if (schema.id === ctx.schema.id) {
|
|
98
106
|
return null;
|
|
99
107
|
}
|
|
100
108
|
return ast_1.ts.reference(this.getDeclarationTypeName(ctx, schema), this.getFilePath(ctx, schema));
|
|
101
109
|
}
|
|
102
|
-
if (schema.id === ctx.schema.id && schema.inheritedSchemas.length > 0) {
|
|
103
|
-
return ast_1.ts.intersectionType(this.getInheritedSchemas(ctx, schema).map((x) => {
|
|
104
|
-
var _a;
|
|
105
|
-
const mappingValue = (_a = Object.entries(x.discriminator.mapping).find(([_, value]) => value.id === schema.id)) === null || _a === void 0 ? void 0 : _a[0];
|
|
106
|
-
return ast_1.ts.reference(this.getDeclarationTypeName(ctx, x), this.getFilePath(ctx, x), {
|
|
107
|
-
generics: [mappingValue !== undefined ? ast_1.ts.string(mappingValue) : null],
|
|
108
|
-
});
|
|
109
|
-
}));
|
|
110
|
-
}
|
|
111
110
|
if (schema.enum) {
|
|
112
111
|
return ast_1.ts.unionType(schema.enum.map((x) => ast_1.ts.toNode(x)));
|
|
113
112
|
}
|
|
@@ -152,24 +151,28 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
152
151
|
if (parts.length === 0) {
|
|
153
152
|
parts.push(ast_1.ts.objectType());
|
|
154
153
|
}
|
|
155
|
-
|
|
154
|
+
return ast_1.ts.intersectionType(parts);
|
|
155
|
+
}
|
|
156
|
+
getDiscriminatorType(ctx, schema) {
|
|
156
157
|
if (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
|
|
157
158
|
const discriminator = schema.discriminator;
|
|
158
159
|
return ast_1.ts.intersectionType([
|
|
159
|
-
|
|
160
|
+
this.getBaseTypeName(ctx, schema),
|
|
160
161
|
ast_1.ts.lookupType(ast_1.ts.objectType({
|
|
161
162
|
members: Object.entries(discriminator.mapping).map(([key, value]) => ast_1.ts.property(key, {
|
|
162
163
|
type: ast_1.ts.intersectionType([
|
|
163
164
|
ast_1.ts.objectType({
|
|
164
165
|
members: [ast_1.ts.property(discriminator.propertyName, { type: ast_1.ts.string(key) })],
|
|
165
166
|
}),
|
|
166
|
-
this.getType(ctx, value
|
|
167
|
+
this.getType(ctx, value),
|
|
167
168
|
]),
|
|
168
169
|
})),
|
|
169
170
|
}), this.getDiscriminatorGenericParamName(ctx, schema)),
|
|
170
171
|
]);
|
|
171
172
|
}
|
|
172
|
-
|
|
173
|
+
else {
|
|
174
|
+
return ast_1.ts.refs.unknown();
|
|
175
|
+
}
|
|
173
176
|
}
|
|
174
177
|
getCombinedType(ctx, schema) {
|
|
175
178
|
if (schema.allOf.length === 0 && schema.anyOf.length === 0) {
|
|
@@ -244,6 +247,12 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
244
247
|
var _a;
|
|
245
248
|
return (0, core_1.toCasing)((_a = schema.discriminator) === null || _a === void 0 ? void 0 : _a.propertyName, ctx.config.genericParamNameCasing);
|
|
246
249
|
}
|
|
250
|
+
getDiscriminatorTypeName(ctx, schema) {
|
|
251
|
+
return (0, core_1.toCasing)(schema.name + '_Discriminator', ctx.config.typeNameCasing);
|
|
252
|
+
}
|
|
253
|
+
getBaseTypeName(ctx, schema) {
|
|
254
|
+
return (0, core_1.toCasing)(schema.name + '_Base', ctx.config.typeNameCasing);
|
|
255
|
+
}
|
|
247
256
|
getInheritedSchemas(ctx, schema) {
|
|
248
257
|
return schema.inheritedSchemas
|
|
249
258
|
.filter((schema) => this.shouldGenerateTypeDeclaration(ctx, schema) && !schema.isNameGenerated)
|
|
@@ -25,6 +25,7 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
getFileContent(ctx) {
|
|
28
|
+
var _a;
|
|
28
29
|
const content = appendValueGroup([], '\n');
|
|
29
30
|
const interfaceSchema = this.resolveInterfaceSchema(ctx);
|
|
30
31
|
if (interfaceSchema) {
|
|
@@ -36,6 +37,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
36
37
|
ctx.schema.enum.length > 0) {
|
|
37
38
|
content.values.push(this.getEnum(ctx));
|
|
38
39
|
}
|
|
40
|
+
else if (ctx.schema.discriminator && Object.keys(ctx.schema.discriminator.mapping).length > 0) {
|
|
41
|
+
content.values.push(ts.typeAlias(this.getDiscriminatorTypeName(ctx, ctx.schema), ts.unionType(Object.keys(ctx.schema.discriminator.mapping).map((x) => ts.string(x)))), ts.typeAlias(this.getBaseTypeName(ctx, ctx.schema), (_a = this.getType(ctx, ctx.schema, true)) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), { export: true }), this.getTypeAlias(ctx, ctx.schema));
|
|
42
|
+
}
|
|
39
43
|
else {
|
|
40
44
|
content.values.push(this.getTypeAlias(ctx, ctx.schema));
|
|
41
45
|
}
|
|
@@ -57,12 +61,13 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
57
61
|
}
|
|
58
62
|
getTypeAlias(ctx, schema) {
|
|
59
63
|
var _a;
|
|
60
|
-
const
|
|
64
|
+
const isDiscriminated = schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0;
|
|
65
|
+
const result = ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = (isDiscriminated ? this.getDiscriminatorType(ctx, schema) : this.getType(ctx, schema, true))) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
|
|
61
66
|
export: true,
|
|
62
67
|
doc: ts.doc({ description: schema.description }),
|
|
63
68
|
});
|
|
64
|
-
if (
|
|
65
|
-
const propertyValue =
|
|
69
|
+
if (isDiscriminated) {
|
|
70
|
+
const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
|
|
66
71
|
result.generics.push(ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
|
|
67
72
|
constraint: propertyValue,
|
|
68
73
|
default: propertyValue,
|
|
@@ -90,21 +95,15 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
90
95
|
if (!schema)
|
|
91
96
|
return this.getAnyType(ctx);
|
|
92
97
|
schema = getSchemaReference(schema, ['description']);
|
|
98
|
+
if (ctx.schema.inheritedSchemas.some((x) => x.id === schema.id)) {
|
|
99
|
+
return ts.reference(this.getBaseTypeName(ctx, schema), this.getFilePath(ctx, schema));
|
|
100
|
+
}
|
|
93
101
|
if (!skipSchemas && this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
94
102
|
if (schema.id === ctx.schema.id) {
|
|
95
103
|
return null;
|
|
96
104
|
}
|
|
97
105
|
return ts.reference(this.getDeclarationTypeName(ctx, schema), this.getFilePath(ctx, schema));
|
|
98
106
|
}
|
|
99
|
-
if (schema.id === ctx.schema.id && schema.inheritedSchemas.length > 0) {
|
|
100
|
-
return ts.intersectionType(this.getInheritedSchemas(ctx, schema).map((x) => {
|
|
101
|
-
var _a;
|
|
102
|
-
const mappingValue = (_a = Object.entries(x.discriminator.mapping).find(([_, value]) => value.id === schema.id)) === null || _a === void 0 ? void 0 : _a[0];
|
|
103
|
-
return ts.reference(this.getDeclarationTypeName(ctx, x), this.getFilePath(ctx, x), {
|
|
104
|
-
generics: [mappingValue !== undefined ? ts.string(mappingValue) : null],
|
|
105
|
-
});
|
|
106
|
-
}));
|
|
107
|
-
}
|
|
108
107
|
if (schema.enum) {
|
|
109
108
|
return ts.unionType(schema.enum.map((x) => ts.toNode(x)));
|
|
110
109
|
}
|
|
@@ -149,24 +148,28 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
149
148
|
if (parts.length === 0) {
|
|
150
149
|
parts.push(ts.objectType());
|
|
151
150
|
}
|
|
152
|
-
|
|
151
|
+
return ts.intersectionType(parts);
|
|
152
|
+
}
|
|
153
|
+
getDiscriminatorType(ctx, schema) {
|
|
153
154
|
if (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
|
|
154
155
|
const discriminator = schema.discriminator;
|
|
155
156
|
return ts.intersectionType([
|
|
156
|
-
|
|
157
|
+
this.getBaseTypeName(ctx, schema),
|
|
157
158
|
ts.lookupType(ts.objectType({
|
|
158
159
|
members: Object.entries(discriminator.mapping).map(([key, value]) => ts.property(key, {
|
|
159
160
|
type: ts.intersectionType([
|
|
160
161
|
ts.objectType({
|
|
161
162
|
members: [ts.property(discriminator.propertyName, { type: ts.string(key) })],
|
|
162
163
|
}),
|
|
163
|
-
this.getType(ctx, value
|
|
164
|
+
this.getType(ctx, value),
|
|
164
165
|
]),
|
|
165
166
|
})),
|
|
166
167
|
}), this.getDiscriminatorGenericParamName(ctx, schema)),
|
|
167
168
|
]);
|
|
168
169
|
}
|
|
169
|
-
|
|
170
|
+
else {
|
|
171
|
+
return ts.refs.unknown();
|
|
172
|
+
}
|
|
170
173
|
}
|
|
171
174
|
getCombinedType(ctx, schema) {
|
|
172
175
|
if (schema.allOf.length === 0 && schema.anyOf.length === 0) {
|
|
@@ -241,6 +244,12 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
241
244
|
var _a;
|
|
242
245
|
return toCasing((_a = schema.discriminator) === null || _a === void 0 ? void 0 : _a.propertyName, ctx.config.genericParamNameCasing);
|
|
243
246
|
}
|
|
247
|
+
getDiscriminatorTypeName(ctx, schema) {
|
|
248
|
+
return toCasing(schema.name + '_Discriminator', ctx.config.typeNameCasing);
|
|
249
|
+
}
|
|
250
|
+
getBaseTypeName(ctx, schema) {
|
|
251
|
+
return toCasing(schema.name + '_Base', ctx.config.typeNameCasing);
|
|
252
|
+
}
|
|
244
253
|
getInheritedSchemas(ctx, schema) {
|
|
245
254
|
return schema.inheritedSchemas
|
|
246
255
|
.filter((schema) => this.shouldGenerateTypeDeclaration(ctx, schema) && !schema.isNameGenerated)
|
package/package.json
CHANGED
|
@@ -20,6 +20,7 @@ export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGener
|
|
|
20
20
|
protected getType(ctx: Context, schema: Nullable<ApiSchema>, skipSchemas?: boolean): ts.Type<Builder> | null;
|
|
21
21
|
protected getArrayType(ctx: Context, schema: ArrayLikeApiSchema): ts.Type<Builder>;
|
|
22
22
|
protected getObjectType(ctx: Context, schema: ObjectLikeApiSchema): ts.Type<Builder>;
|
|
23
|
+
protected getDiscriminatorType(ctx: Context, schema: ApiSchema): ts.Type<Builder>;
|
|
23
24
|
protected getCombinedType(ctx: Context, schema: CombinedLikeApiSchema): ts.Type<Builder>;
|
|
24
25
|
protected getOneOfType(ctx: Context, schema: ApiSchema<'oneOf'>): ts.Type<Builder>;
|
|
25
26
|
protected getMultiType(ctx: Context, schema: ApiSchema<'multi-type'>): ts.Type<Builder>;
|
|
@@ -27,6 +28,8 @@ export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGener
|
|
|
27
28
|
protected resolveInterfaceSchema(ctx: Context): ApiSchema<'object'> | undefined;
|
|
28
29
|
protected getDeclarationTypeName(ctx: Context, schema: ApiSchema): string;
|
|
29
30
|
protected getDiscriminatorGenericParamName(ctx: Context, schema: ApiSchema): string;
|
|
31
|
+
protected getDiscriminatorTypeName(ctx: Context, schema: ApiSchema): string;
|
|
32
|
+
protected getBaseTypeName(ctx: Context, schema: ApiSchema): string;
|
|
30
33
|
protected getInheritedSchemas(ctx: Context, schema: ApiSchema): (ApiSchema<import("@goast/core").ApiSchemaKind> & {
|
|
31
34
|
discriminator: import("@goast/core").ApiSchemaDiscriminator;
|
|
32
35
|
})[];
|