@goast/typescript 0.2.17 → 0.2.19

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.
@@ -6,8 +6,10 @@ const node_1 = require("../node");
6
6
  const write_ts_nodes_1 = require("../utils/write-ts-nodes");
7
7
  class TsTuple extends node_1.TsNode {
8
8
  constructor(options) {
9
+ var _a;
9
10
  super(options);
10
11
  this.elements = options.elements.filter(core_1.notNullish);
12
+ this.asConst = (_a = options.asConst) !== null && _a !== void 0 ? _a : false;
11
13
  }
12
14
  onWrite(builder) {
13
15
  if (this.elements.length === 0) {
@@ -19,6 +21,9 @@ class TsTuple extends node_1.TsNode {
19
21
  multiline,
20
22
  });
21
23
  }
24
+ if (this.asConst) {
25
+ builder.append(' as const');
26
+ }
22
27
  }
23
28
  }
24
29
  exports.TsTuple = TsTuple;
@@ -15,6 +15,8 @@ class TsVariable extends node_1.TsNode {
15
15
  this.readonly = (_e = options.readonly) !== null && _e !== void 0 ? _e : false;
16
16
  }
17
17
  onWrite(builder) {
18
+ if (this.doc)
19
+ (0, write_ts_nodes_1.writeTsNode)(builder, this.doc);
18
20
  if (this.export)
19
21
  builder.append('export ');
20
22
  builder.append(this.readonly ? 'const ' : 'let ');
@@ -45,6 +45,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
45
45
  }
46
46
  else {
47
47
  content.values.push(this.getTypeAlias(ctx, ctx.schema));
48
+ if (ctx.schema.enum) {
49
+ content.values.push(this.getEnumValuesVariable(ctx, ctx.schema));
50
+ }
48
51
  }
49
52
  return content;
50
53
  }
@@ -109,6 +112,19 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
109
112
  }
110
113
  return result;
111
114
  }
115
+ getEnumValuesVariable(ctx, schema) {
116
+ var _a, _b;
117
+ const enumTypeName = this.getDeclarationTypeName(ctx, schema);
118
+ const values = (_b = (_a = schema.enum) === null || _a === void 0 ? void 0 : _a.map((x) => ast_1.ts.toNode(x))) !== null && _b !== void 0 ? _b : [];
119
+ return ast_1.ts.variable((0, core_1.toCasing)(`${enumTypeName}-Values`, ctx.config.constantNameCasing), {
120
+ doc: ast_1.ts.doc({
121
+ description: `All possible values of the enum \`${enumTypeName}\`.`,
122
+ }),
123
+ export: true,
124
+ readonly: true,
125
+ value: ast_1.ts.tuple(values, { asConst: true }),
126
+ });
127
+ }
112
128
  getIndexer(ctx, schema) {
113
129
  var _a;
114
130
  return schema.additionalProperties
@@ -272,6 +288,10 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
272
288
  schema.kind !== 'oneOf') {
273
289
  return false;
274
290
  }
291
+ // Dynamically generated schemas do not have its own type declaration
292
+ if (!ctx.data.schemas.some((x) => x.id === schema.id)) {
293
+ return false;
294
+ }
275
295
  // For all other types, check if the user has enabled inline unnamed schemas
276
296
  return !ctx.config.inlineUnnamedSchemas;
277
297
  }
@@ -3,8 +3,10 @@ import { TsNode } from '../node';
3
3
  import { writeTsNodes } from '../utils/write-ts-nodes';
4
4
  export class TsTuple extends TsNode {
5
5
  constructor(options) {
6
+ var _a;
6
7
  super(options);
7
8
  this.elements = options.elements.filter(notNullish);
9
+ this.asConst = (_a = options.asConst) !== null && _a !== void 0 ? _a : false;
8
10
  }
9
11
  onWrite(builder) {
10
12
  if (this.elements.length === 0) {
@@ -16,6 +18,9 @@ export class TsTuple extends TsNode {
16
18
  multiline,
17
19
  });
18
20
  }
21
+ if (this.asConst) {
22
+ builder.append(' as const');
23
+ }
19
24
  }
20
25
  }
21
26
  const createTuple = (elements, options) => new TsTuple(Object.assign(Object.assign({}, options), { elements }));
@@ -12,6 +12,8 @@ export class TsVariable extends TsNode {
12
12
  this.readonly = (_e = options.readonly) !== null && _e !== void 0 ? _e : false;
13
13
  }
14
14
  onWrite(builder) {
15
+ if (this.doc)
16
+ writeTsNode(builder, this.doc);
15
17
  if (this.export)
16
18
  builder.append('export ');
17
19
  builder.append(this.readonly ? 'const ' : 'let ');
@@ -42,6 +42,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
42
42
  }
43
43
  else {
44
44
  content.values.push(this.getTypeAlias(ctx, ctx.schema));
45
+ if (ctx.schema.enum) {
46
+ content.values.push(this.getEnumValuesVariable(ctx, ctx.schema));
47
+ }
45
48
  }
46
49
  return content;
47
50
  }
@@ -106,6 +109,19 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
106
109
  }
107
110
  return result;
108
111
  }
112
+ getEnumValuesVariable(ctx, schema) {
113
+ var _a, _b;
114
+ const enumTypeName = this.getDeclarationTypeName(ctx, schema);
115
+ const values = (_b = (_a = schema.enum) === null || _a === void 0 ? void 0 : _a.map((x) => ts.toNode(x))) !== null && _b !== void 0 ? _b : [];
116
+ return ts.variable(toCasing(`${enumTypeName}-Values`, ctx.config.constantNameCasing), {
117
+ doc: ts.doc({
118
+ description: `All possible values of the enum \`${enumTypeName}\`.`,
119
+ }),
120
+ export: true,
121
+ readonly: true,
122
+ value: ts.tuple(values, { asConst: true }),
123
+ });
124
+ }
109
125
  getIndexer(ctx, schema) {
110
126
  var _a;
111
127
  return schema.additionalProperties
@@ -269,6 +285,10 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
269
285
  schema.kind !== 'oneOf') {
270
286
  return false;
271
287
  }
288
+ // Dynamically generated schemas do not have its own type declaration
289
+ if (!ctx.data.schemas.some((x) => x.id === schema.id)) {
290
+ return false;
291
+ }
272
292
  // For all other types, check if the user has enabled inline unnamed schemas
273
293
  return !ctx.config.inlineUnnamedSchemas;
274
294
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/typescript",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -9,11 +9,7 @@
9
9
  "license": "MIT",
10
10
  "type": "module",
11
11
  "dependencies": {
12
- "fs-extra": "^11.2.0",
13
- "node-fetch": "^2.7.0",
14
- "yaml": "^2.4.1",
15
- "@goast/core": "0.2.1",
16
- "tslib": "2.6.2"
12
+ "@goast/core": "0.2.2"
17
13
  },
18
14
  "module": "./esm/index.js",
19
15
  "main": "./cjs/index.js",
@@ -5,9 +5,11 @@ import { writeTsNodes } from '../utils/write-ts-nodes';
5
5
  type Injects = never;
6
6
  type Options<TBuilder extends SourceBuilder, TInjects extends string = never> = AstNodeOptions<typeof TsNode<TBuilder, TInjects | Injects>, {
7
7
  elements: Nullable<TsType<TBuilder> | TsValue<TBuilder>>[];
8
+ asConst?: Nullable<boolean>;
8
9
  }>;
9
10
  export declare class TsTuple<TBuilder extends SourceBuilder, TInjects extends string = never> extends TsNode<TBuilder, TInjects | Injects> {
10
11
  elements: (TsType<TBuilder> | TsValue<TBuilder>)[];
12
+ asConst: boolean;
11
13
  constructor(options: Options<TBuilder, TInjects>);
12
14
  protected onWrite(builder: TBuilder): void;
13
15
  }
@@ -14,5 +14,5 @@ import type { TsTuple } from './tuple';
14
14
  import type { TsTypeof } from './typeof';
15
15
  import type { TsUnionType } from './union-type';
16
16
  import type { BasicAppendValue, SourceBuilder } from '@goast/core';
17
- export type TsType<TBuilder extends SourceBuilder> = BasicAppendValue<TBuilder> | TsIntersectionType<TBuilder> | TsUnionType<TBuilder> | TsArrayType<TBuilder> | TsFunctionType<TBuilder> | TsObjectType<TBuilder> | TsString<TBuilder> | TsLookupType<TBuilder> | TsTypeof<TBuilder> | TsReference<TBuilder>;
17
+ export type TsType<TBuilder extends SourceBuilder> = BasicAppendValue<TBuilder> | TsIntersectionType<TBuilder> | TsUnionType<TBuilder> | TsArrayType<TBuilder> | TsTuple<TBuilder> | TsFunctionType<TBuilder> | TsObjectType<TBuilder> | TsString<TBuilder> | TsLookupType<TBuilder> | TsTypeof<TBuilder> | TsReference<TBuilder>;
18
18
  export type TsValue<TBuilder extends SourceBuilder> = BasicAppendValue<TBuilder> | TsObject<TBuilder> | TsTuple<TBuilder> | TsString<TBuilder> | TsArrowFunction<TBuilder> | TsFunction<TBuilder> | TsClass<TBuilder> | TsCall<TBuilder> | TsReference<TBuilder>;
@@ -15,6 +15,7 @@ export declare class DefaultTypeScriptModelGenerator extends TypeScriptFileGener
15
15
  protected getEnum(ctx: Context): ts.Enum<import("@goast/core").SourceBuilder<never>, never>;
16
16
  protected getInterface(ctx: Context, schema: ObjectLikeApiSchema): ts.Interface<Builder, never>;
17
17
  protected getTypeAlias(ctx: Context, schema: ApiSchema): ts.TypeAlias<Builder>;
18
+ protected getEnumValuesVariable(ctx: Context, schema: ApiSchema): ts.Variable<Builder>;
18
19
  protected getIndexer(ctx: Context, schema: ObjectLikeApiSchema): ts.Indexer<Builder, never> | null;
19
20
  protected getProperties(ctx: Context, schema: ObjectLikeApiSchema): (import("../../ast/nodes/property").TsAccessorProperty<Builder, ""> | import("../../ast/nodes/property").TsSimpleProperty<Builder, "">)[];
20
21
  protected getType(ctx: Context, schema: Nullable<ApiSchema>, options?: {