@goast/typescript 0.2.3 → 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,10 +40,10 @@ 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
|
-
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)))));
|
|
45
|
-
}
|
|
46
47
|
content.values.push(this.getTypeAlias(ctx, ctx.schema));
|
|
47
48
|
}
|
|
48
49
|
return content;
|
|
@@ -63,11 +64,12 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
63
64
|
}
|
|
64
65
|
getTypeAlias(ctx, schema) {
|
|
65
66
|
var _a;
|
|
66
|
-
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), {
|
|
67
69
|
export: true,
|
|
68
70
|
doc: ast_1.ts.doc({ description: schema.description }),
|
|
69
71
|
});
|
|
70
|
-
if (
|
|
72
|
+
if (isDiscriminated) {
|
|
71
73
|
const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
|
|
72
74
|
result.generics.push(ast_1.ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
|
|
73
75
|
constraint: propertyValue,
|
|
@@ -96,6 +98,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
96
98
|
if (!schema)
|
|
97
99
|
return this.getAnyType(ctx);
|
|
98
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
|
+
}
|
|
99
104
|
if (!skipSchemas && this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
100
105
|
if (schema.id === ctx.schema.id) {
|
|
101
106
|
return null;
|
|
@@ -146,11 +151,13 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
146
151
|
if (parts.length === 0) {
|
|
147
152
|
parts.push(ast_1.ts.objectType());
|
|
148
153
|
}
|
|
149
|
-
|
|
154
|
+
return ast_1.ts.intersectionType(parts);
|
|
155
|
+
}
|
|
156
|
+
getDiscriminatorType(ctx, schema) {
|
|
150
157
|
if (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
|
|
151
158
|
const discriminator = schema.discriminator;
|
|
152
159
|
return ast_1.ts.intersectionType([
|
|
153
|
-
|
|
160
|
+
this.getBaseTypeName(ctx, schema),
|
|
154
161
|
ast_1.ts.lookupType(ast_1.ts.objectType({
|
|
155
162
|
members: Object.entries(discriminator.mapping).map(([key, value]) => ast_1.ts.property(key, {
|
|
156
163
|
type: ast_1.ts.intersectionType([
|
|
@@ -163,7 +170,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
163
170
|
}), this.getDiscriminatorGenericParamName(ctx, schema)),
|
|
164
171
|
]);
|
|
165
172
|
}
|
|
166
|
-
|
|
173
|
+
else {
|
|
174
|
+
return ast_1.ts.refs.unknown();
|
|
175
|
+
}
|
|
167
176
|
}
|
|
168
177
|
getCombinedType(ctx, schema) {
|
|
169
178
|
if (schema.allOf.length === 0 && schema.anyOf.length === 0) {
|
|
@@ -241,6 +250,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
241
250
|
getDiscriminatorTypeName(ctx, schema) {
|
|
242
251
|
return (0, core_1.toCasing)(schema.name + '_Discriminator', ctx.config.typeNameCasing);
|
|
243
252
|
}
|
|
253
|
+
getBaseTypeName(ctx, schema) {
|
|
254
|
+
return (0, core_1.toCasing)(schema.name + '_Base', ctx.config.typeNameCasing);
|
|
255
|
+
}
|
|
244
256
|
getInheritedSchemas(ctx, schema) {
|
|
245
257
|
return schema.inheritedSchemas
|
|
246
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,10 +37,10 @@ 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
|
-
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)))));
|
|
42
|
-
}
|
|
43
44
|
content.values.push(this.getTypeAlias(ctx, ctx.schema));
|
|
44
45
|
}
|
|
45
46
|
return content;
|
|
@@ -60,11 +61,12 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
60
61
|
}
|
|
61
62
|
getTypeAlias(ctx, schema) {
|
|
62
63
|
var _a;
|
|
63
|
-
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), {
|
|
64
66
|
export: true,
|
|
65
67
|
doc: ts.doc({ description: schema.description }),
|
|
66
68
|
});
|
|
67
|
-
if (
|
|
69
|
+
if (isDiscriminated) {
|
|
68
70
|
const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
|
|
69
71
|
result.generics.push(ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
|
|
70
72
|
constraint: propertyValue,
|
|
@@ -93,6 +95,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
93
95
|
if (!schema)
|
|
94
96
|
return this.getAnyType(ctx);
|
|
95
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
|
+
}
|
|
96
101
|
if (!skipSchemas && this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
97
102
|
if (schema.id === ctx.schema.id) {
|
|
98
103
|
return null;
|
|
@@ -143,11 +148,13 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
143
148
|
if (parts.length === 0) {
|
|
144
149
|
parts.push(ts.objectType());
|
|
145
150
|
}
|
|
146
|
-
|
|
151
|
+
return ts.intersectionType(parts);
|
|
152
|
+
}
|
|
153
|
+
getDiscriminatorType(ctx, schema) {
|
|
147
154
|
if (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
|
|
148
155
|
const discriminator = schema.discriminator;
|
|
149
156
|
return ts.intersectionType([
|
|
150
|
-
|
|
157
|
+
this.getBaseTypeName(ctx, schema),
|
|
151
158
|
ts.lookupType(ts.objectType({
|
|
152
159
|
members: Object.entries(discriminator.mapping).map(([key, value]) => ts.property(key, {
|
|
153
160
|
type: ts.intersectionType([
|
|
@@ -160,7 +167,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
160
167
|
}), this.getDiscriminatorGenericParamName(ctx, schema)),
|
|
161
168
|
]);
|
|
162
169
|
}
|
|
163
|
-
|
|
170
|
+
else {
|
|
171
|
+
return ts.refs.unknown();
|
|
172
|
+
}
|
|
164
173
|
}
|
|
165
174
|
getCombinedType(ctx, schema) {
|
|
166
175
|
if (schema.allOf.length === 0 && schema.anyOf.length === 0) {
|
|
@@ -238,6 +247,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
238
247
|
getDiscriminatorTypeName(ctx, schema) {
|
|
239
248
|
return toCasing(schema.name + '_Discriminator', ctx.config.typeNameCasing);
|
|
240
249
|
}
|
|
250
|
+
getBaseTypeName(ctx, schema) {
|
|
251
|
+
return toCasing(schema.name + '_Base', ctx.config.typeNameCasing);
|
|
252
|
+
}
|
|
241
253
|
getInheritedSchemas(ctx, schema) {
|
|
242
254
|
return schema.inheritedSchemas
|
|
243
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>;
|
|
@@ -28,6 +29,7 @@ export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGener
|
|
|
28
29
|
protected getDeclarationTypeName(ctx: Context, schema: ApiSchema): string;
|
|
29
30
|
protected getDiscriminatorGenericParamName(ctx: Context, schema: ApiSchema): string;
|
|
30
31
|
protected getDiscriminatorTypeName(ctx: Context, schema: ApiSchema): string;
|
|
32
|
+
protected getBaseTypeName(ctx: Context, schema: ApiSchema): string;
|
|
31
33
|
protected getInheritedSchemas(ctx: Context, schema: ApiSchema): (ApiSchema<import("@goast/core").ApiSchemaKind> & {
|
|
32
34
|
discriminator: import("@goast/core").ApiSchemaDiscriminator;
|
|
33
35
|
})[];
|