@goast/typescript 0.2.3 → 0.2.5

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, { skipSchemas: 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 result = ast_1.ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = this.getType(ctx, schema, true)) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
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, { skipSchemas: 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 (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
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,
@@ -92,11 +94,14 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
92
94
  });
93
95
  });
94
96
  }
95
- getType(ctx, schema, skipSchemas = false) {
97
+ getType(ctx, schema, options) {
96
98
  if (!schema)
97
99
  return this.getAnyType(ctx);
98
100
  schema = (0, core_1.getSchemaReference)(schema, ['description']);
99
- if (!skipSchemas && this.shouldGenerateTypeDeclaration(ctx, schema)) {
101
+ if ((options === null || options === void 0 ? void 0 : options.useBaseType) && ctx.schema.inheritedSchemas.some((x) => x.id === schema.id)) {
102
+ return ast_1.ts.reference(this.getBaseTypeName(ctx, schema), this.getFilePath(ctx, schema));
103
+ }
104
+ if (!(options === null || options === void 0 ? void 0 : options.skipSchemas) && this.shouldGenerateTypeDeclaration(ctx, schema)) {
100
105
  if (schema.id === ctx.schema.id) {
101
106
  return null;
102
107
  }
@@ -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
- const type = ast_1.ts.intersectionType(parts);
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
- type,
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,15 +170,18 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
163
170
  }), this.getDiscriminatorGenericParamName(ctx, schema)),
164
171
  ]);
165
172
  }
166
- return type;
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) {
170
179
  return this.getAnyType(ctx);
171
180
  }
181
+ const useBaseType = schema.id === ctx.schema.id;
172
182
  return ast_1.ts.intersectionType([
173
- ...schema.allOf.map((x) => this.getType(ctx, x)),
174
- ...schema.anyOf.map((x) => ast_1.ts.reference('Partial', null, { generics: [this.getType(ctx, x)] })),
183
+ ...schema.allOf.map((x) => this.getType(ctx, x, { useBaseType })),
184
+ ...schema.anyOf.map((x) => ast_1.ts.reference('Partial', null, { generics: [this.getType(ctx, x, { useBaseType })] })),
175
185
  ]);
176
186
  }
177
187
  getOneOfType(ctx, schema) {
@@ -241,6 +251,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
241
251
  getDiscriminatorTypeName(ctx, schema) {
242
252
  return (0, core_1.toCasing)(schema.name + '_Discriminator', ctx.config.typeNameCasing);
243
253
  }
254
+ getBaseTypeName(ctx, schema) {
255
+ return (0, core_1.toCasing)(schema.name + '_Base', ctx.config.typeNameCasing);
256
+ }
244
257
  getInheritedSchemas(ctx, schema) {
245
258
  return schema.inheritedSchemas
246
259
  .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, { skipSchemas: 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 result = ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = this.getType(ctx, schema, true)) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
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, { skipSchemas: 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 (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
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,
@@ -89,11 +91,14 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
89
91
  });
90
92
  });
91
93
  }
92
- getType(ctx, schema, skipSchemas = false) {
94
+ getType(ctx, schema, options) {
93
95
  if (!schema)
94
96
  return this.getAnyType(ctx);
95
97
  schema = getSchemaReference(schema, ['description']);
96
- if (!skipSchemas && this.shouldGenerateTypeDeclaration(ctx, schema)) {
98
+ if ((options === null || options === void 0 ? void 0 : options.useBaseType) && ctx.schema.inheritedSchemas.some((x) => x.id === schema.id)) {
99
+ return ts.reference(this.getBaseTypeName(ctx, schema), this.getFilePath(ctx, schema));
100
+ }
101
+ if (!(options === null || options === void 0 ? void 0 : options.skipSchemas) && this.shouldGenerateTypeDeclaration(ctx, schema)) {
97
102
  if (schema.id === ctx.schema.id) {
98
103
  return null;
99
104
  }
@@ -143,11 +148,13 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
143
148
  if (parts.length === 0) {
144
149
  parts.push(ts.objectType());
145
150
  }
146
- const type = ts.intersectionType(parts);
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
- type,
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,15 +167,18 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
160
167
  }), this.getDiscriminatorGenericParamName(ctx, schema)),
161
168
  ]);
162
169
  }
163
- return type;
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) {
167
176
  return this.getAnyType(ctx);
168
177
  }
178
+ const useBaseType = schema.id === ctx.schema.id;
169
179
  return ts.intersectionType([
170
- ...schema.allOf.map((x) => this.getType(ctx, x)),
171
- ...schema.anyOf.map((x) => ts.reference('Partial', null, { generics: [this.getType(ctx, x)] })),
180
+ ...schema.allOf.map((x) => this.getType(ctx, x, { useBaseType })),
181
+ ...schema.anyOf.map((x) => ts.reference('Partial', null, { generics: [this.getType(ctx, x, { useBaseType })] })),
172
182
  ]);
173
183
  }
174
184
  getOneOfType(ctx, schema) {
@@ -238,6 +248,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
238
248
  getDiscriminatorTypeName(ctx, schema) {
239
249
  return toCasing(schema.name + '_Discriminator', ctx.config.typeNameCasing);
240
250
  }
251
+ getBaseTypeName(ctx, schema) {
252
+ return toCasing(schema.name + '_Base', ctx.config.typeNameCasing);
253
+ }
241
254
  getInheritedSchemas(ctx, schema) {
242
255
  return schema.inheritedSchemas
243
256
  .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.3",
3
+ "version": "0.2.5",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -17,9 +17,13 @@ export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGener
17
17
  protected getTypeAlias(ctx: Context, schema: ApiSchema): ts.TypeAlias<Builder>;
18
18
  protected getIndexer(ctx: Context, schema: ObjectLikeApiSchema): ts.Indexer<Builder, never> | null;
19
19
  protected getProperties(ctx: Context, schema: ObjectLikeApiSchema): (import("../../ast/nodes/property").TsAccessorProperty<Builder, ""> | import("../../ast/nodes/property").TsSimpleProperty<Builder, "">)[];
20
- protected getType(ctx: Context, schema: Nullable<ApiSchema>, skipSchemas?: boolean): ts.Type<Builder> | null;
20
+ protected getType(ctx: Context, schema: Nullable<ApiSchema>, options?: {
21
+ skipSchemas?: boolean;
22
+ useBaseType?: boolean;
23
+ }): ts.Type<Builder> | null;
21
24
  protected getArrayType(ctx: Context, schema: ArrayLikeApiSchema): ts.Type<Builder>;
22
25
  protected getObjectType(ctx: Context, schema: ObjectLikeApiSchema): ts.Type<Builder>;
26
+ protected getDiscriminatorType(ctx: Context, schema: ApiSchema): ts.Type<Builder>;
23
27
  protected getCombinedType(ctx: Context, schema: CombinedLikeApiSchema): ts.Type<Builder>;
24
28
  protected getOneOfType(ctx: Context, schema: ApiSchema<'oneOf'>): ts.Type<Builder>;
25
29
  protected getMultiType(ctx: Context, schema: ApiSchema<'multi-type'>): ts.Type<Builder>;
@@ -28,6 +32,7 @@ export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGener
28
32
  protected getDeclarationTypeName(ctx: Context, schema: ApiSchema): string;
29
33
  protected getDiscriminatorGenericParamName(ctx: Context, schema: ApiSchema): string;
30
34
  protected getDiscriminatorTypeName(ctx: Context, schema: ApiSchema): string;
35
+ protected getBaseTypeName(ctx: Context, schema: ApiSchema): string;
31
36
  protected getInheritedSchemas(ctx: Context, schema: ApiSchema): (ApiSchema<import("@goast/core").ApiSchemaKind> & {
32
37
  discriminator: import("@goast/core").ApiSchemaDiscriminator;
33
38
  })[];