@goast/typescript 0.2.2 → 0.2.3

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.
@@ -40,6 +40,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
40
40
  content.values.push(this.getEnum(ctx));
41
41
  }
42
42
  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
+ }
43
46
  content.values.push(this.getTypeAlias(ctx, ctx.schema));
44
47
  }
45
48
  return content;
@@ -65,7 +68,7 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
65
68
  doc: ast_1.ts.doc({ description: schema.description }),
66
69
  });
67
70
  if (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
68
- const propertyValue = ast_1.ts.unionType(Object.keys(schema.discriminator.mapping).map((x) => ast_1.ts.string(x)));
71
+ const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
69
72
  result.generics.push(ast_1.ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
70
73
  constraint: propertyValue,
71
74
  default: propertyValue,
@@ -99,15 +102,6 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
99
102
  }
100
103
  return ast_1.ts.reference(this.getDeclarationTypeName(ctx, schema), this.getFilePath(ctx, schema));
101
104
  }
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
105
  if (schema.enum) {
112
106
  return ast_1.ts.unionType(schema.enum.map((x) => ast_1.ts.toNode(x)));
113
107
  }
@@ -163,7 +157,7 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
163
157
  ast_1.ts.objectType({
164
158
  members: [ast_1.ts.property(discriminator.propertyName, { type: ast_1.ts.string(key) })],
165
159
  }),
166
- this.getType(ctx, value, true),
160
+ this.getType(ctx, value),
167
161
  ]),
168
162
  })),
169
163
  }), this.getDiscriminatorGenericParamName(ctx, schema)),
@@ -244,6 +238,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
244
238
  var _a;
245
239
  return (0, core_1.toCasing)((_a = schema.discriminator) === null || _a === void 0 ? void 0 : _a.propertyName, ctx.config.genericParamNameCasing);
246
240
  }
241
+ getDiscriminatorTypeName(ctx, schema) {
242
+ return (0, core_1.toCasing)(schema.name + '_Discriminator', ctx.config.typeNameCasing);
243
+ }
247
244
  getInheritedSchemas(ctx, schema) {
248
245
  return schema.inheritedSchemas
249
246
  .filter((schema) => this.shouldGenerateTypeDeclaration(ctx, schema) && !schema.isNameGenerated)
@@ -37,6 +37,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
37
37
  content.values.push(this.getEnum(ctx));
38
38
  }
39
39
  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
+ }
40
43
  content.values.push(this.getTypeAlias(ctx, ctx.schema));
41
44
  }
42
45
  return content;
@@ -62,7 +65,7 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
62
65
  doc: ts.doc({ description: schema.description }),
63
66
  });
64
67
  if (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
65
- const propertyValue = ts.unionType(Object.keys(schema.discriminator.mapping).map((x) => ts.string(x)));
68
+ const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
66
69
  result.generics.push(ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
67
70
  constraint: propertyValue,
68
71
  default: propertyValue,
@@ -96,15 +99,6 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
96
99
  }
97
100
  return ts.reference(this.getDeclarationTypeName(ctx, schema), this.getFilePath(ctx, schema));
98
101
  }
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
102
  if (schema.enum) {
109
103
  return ts.unionType(schema.enum.map((x) => ts.toNode(x)));
110
104
  }
@@ -160,7 +154,7 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
160
154
  ts.objectType({
161
155
  members: [ts.property(discriminator.propertyName, { type: ts.string(key) })],
162
156
  }),
163
- this.getType(ctx, value, true),
157
+ this.getType(ctx, value),
164
158
  ]),
165
159
  })),
166
160
  }), this.getDiscriminatorGenericParamName(ctx, schema)),
@@ -241,6 +235,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
241
235
  var _a;
242
236
  return toCasing((_a = schema.discriminator) === null || _a === void 0 ? void 0 : _a.propertyName, ctx.config.genericParamNameCasing);
243
237
  }
238
+ getDiscriminatorTypeName(ctx, schema) {
239
+ return toCasing(schema.name + '_Discriminator', ctx.config.typeNameCasing);
240
+ }
244
241
  getInheritedSchemas(ctx, schema) {
245
242
  return schema.inheritedSchemas
246
243
  .filter((schema) => this.shouldGenerateTypeDeclaration(ctx, schema) && !schema.isNameGenerated)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/typescript",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -27,6 +27,7 @@ export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGener
27
27
  protected resolveInterfaceSchema(ctx: Context): ApiSchema<'object'> | undefined;
28
28
  protected getDeclarationTypeName(ctx: Context, schema: ApiSchema): string;
29
29
  protected getDiscriminatorGenericParamName(ctx: Context, schema: ApiSchema): string;
30
+ protected getDiscriminatorTypeName(ctx: Context, schema: ApiSchema): string;
30
31
  protected getInheritedSchemas(ctx: Context, schema: ApiSchema): (ApiSchema<import("@goast/core").ApiSchemaKind> & {
31
32
  discriminator: import("@goast/core").ApiSchemaDiscriminator;
32
33
  })[];