@goast/typescript 0.2.18 → 0.2.20
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.
- package/cjs/lib/ast/nodes/tuple.js +5 -0
- package/cjs/lib/ast/nodes/variable.js +2 -0
- package/cjs/lib/generators/models/model-generator.js +27 -2
- package/cjs/lib/generators/models/models-generator.js +7 -1
- package/esm/lib/ast/nodes/tuple.js +5 -0
- package/esm/lib/ast/nodes/variable.js +2 -0
- package/esm/lib/generators/models/model-generator.js +27 -2
- package/esm/lib/generators/models/models-generator.js +7 -1
- package/package.json +1 -1
- package/types/lib/ast/nodes/tuple.d.ts +2 -0
- package/types/lib/ast/nodes/types.d.ts +1 -1
- package/types/lib/common-results.d.ts +1 -0
- package/types/lib/generators/models/model-generator.d.ts +1 -0
|
@@ -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 ');
|
|
@@ -11,13 +11,22 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
11
11
|
if (this.shouldGenerateTypeDeclaration(ctx, ctx.schema)) {
|
|
12
12
|
const name = this.getDeclarationTypeName(ctx, ctx.schema);
|
|
13
13
|
const filePath = this.getFilePath(ctx, ctx.schema);
|
|
14
|
+
const fileContent = this.getFileContent(ctx);
|
|
14
15
|
file_builder_1.TypeScriptFileBuilder.generate({
|
|
15
16
|
logName: `model ${name}`,
|
|
16
17
|
filePath,
|
|
17
18
|
options: ctx.config,
|
|
18
|
-
generator: (b) => b.append(
|
|
19
|
+
generator: (b) => b.append(fileContent),
|
|
19
20
|
});
|
|
20
|
-
return {
|
|
21
|
+
return {
|
|
22
|
+
component: name,
|
|
23
|
+
filePath,
|
|
24
|
+
imports: [{ kind: 'file', name, modulePath: filePath }],
|
|
25
|
+
additionalExports: fileContent.values
|
|
26
|
+
.filter((x) => typeof x === 'object' && x !== null && 'export' in x && x.export === true && 'name' in x)
|
|
27
|
+
.filter((x) => x.name !== name)
|
|
28
|
+
.map((x) => x.name),
|
|
29
|
+
};
|
|
21
30
|
}
|
|
22
31
|
else {
|
|
23
32
|
const builder = new file_builder_1.TypeScriptFileBuilder(undefined, ctx.config);
|
|
@@ -45,6 +54,9 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
45
54
|
}
|
|
46
55
|
else {
|
|
47
56
|
content.values.push(this.getTypeAlias(ctx, ctx.schema));
|
|
57
|
+
if (ctx.schema.enum) {
|
|
58
|
+
content.values.push(this.getEnumValuesVariable(ctx, ctx.schema));
|
|
59
|
+
}
|
|
48
60
|
}
|
|
49
61
|
return content;
|
|
50
62
|
}
|
|
@@ -109,6 +121,19 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
109
121
|
}
|
|
110
122
|
return result;
|
|
111
123
|
}
|
|
124
|
+
getEnumValuesVariable(ctx, schema) {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
const enumTypeName = this.getDeclarationTypeName(ctx, schema);
|
|
127
|
+
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 : [];
|
|
128
|
+
return ast_1.ts.variable((0, core_1.toCasing)(`${enumTypeName}-Values`, ctx.config.constantNameCasing), {
|
|
129
|
+
doc: ast_1.ts.doc({
|
|
130
|
+
description: `All possible values of the enum \`${enumTypeName}\`.`,
|
|
131
|
+
}),
|
|
132
|
+
export: true,
|
|
133
|
+
readonly: true,
|
|
134
|
+
value: ast_1.ts.tuple(values, { asConst: true }),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
112
137
|
getIndexer(ctx, schema) {
|
|
113
138
|
var _a;
|
|
114
139
|
return schema.additionalProperties
|
|
@@ -47,7 +47,13 @@ class TypeScriptModelsGenerator extends core_1.OpenApiSchemasGenerationProviderB
|
|
|
47
47
|
return ctx.config.indexFilePath ? (0, path_1.resolve)(ctx.config.outputDir, ctx.config.indexFilePath) : null;
|
|
48
48
|
}
|
|
49
49
|
getIndexFileContent(ctx) {
|
|
50
|
-
return (0, core_1.appendValueGroup)(Object.values(ctx.output.models).
|
|
50
|
+
return (0, core_1.appendValueGroup)(Object.values(ctx.output.models).flatMap((x) => {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
const { filePath } = x;
|
|
53
|
+
if (!filePath)
|
|
54
|
+
return [];
|
|
55
|
+
return [ast_1.ts.export(x.component, filePath), ...((_b = (_a = x.additionalExports) === null || _a === void 0 ? void 0 : _a.map((e) => ast_1.ts.export(e, filePath))) !== null && _b !== void 0 ? _b : [])];
|
|
56
|
+
}));
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
59
|
exports.TypeScriptModelsGenerator = TypeScriptModelsGenerator;
|
|
@@ -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 ');
|
|
@@ -8,13 +8,22 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
8
8
|
if (this.shouldGenerateTypeDeclaration(ctx, ctx.schema)) {
|
|
9
9
|
const name = this.getDeclarationTypeName(ctx, ctx.schema);
|
|
10
10
|
const filePath = this.getFilePath(ctx, ctx.schema);
|
|
11
|
+
const fileContent = this.getFileContent(ctx);
|
|
11
12
|
TypeScriptFileBuilder.generate({
|
|
12
13
|
logName: `model ${name}`,
|
|
13
14
|
filePath,
|
|
14
15
|
options: ctx.config,
|
|
15
|
-
generator: (b) => b.append(
|
|
16
|
+
generator: (b) => b.append(fileContent),
|
|
16
17
|
});
|
|
17
|
-
return {
|
|
18
|
+
return {
|
|
19
|
+
component: name,
|
|
20
|
+
filePath,
|
|
21
|
+
imports: [{ kind: 'file', name, modulePath: filePath }],
|
|
22
|
+
additionalExports: fileContent.values
|
|
23
|
+
.filter((x) => typeof x === 'object' && x !== null && 'export' in x && x.export === true && 'name' in x)
|
|
24
|
+
.filter((x) => x.name !== name)
|
|
25
|
+
.map((x) => x.name),
|
|
26
|
+
};
|
|
18
27
|
}
|
|
19
28
|
else {
|
|
20
29
|
const builder = new TypeScriptFileBuilder(undefined, ctx.config);
|
|
@@ -42,6 +51,9 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
42
51
|
}
|
|
43
52
|
else {
|
|
44
53
|
content.values.push(this.getTypeAlias(ctx, ctx.schema));
|
|
54
|
+
if (ctx.schema.enum) {
|
|
55
|
+
content.values.push(this.getEnumValuesVariable(ctx, ctx.schema));
|
|
56
|
+
}
|
|
45
57
|
}
|
|
46
58
|
return content;
|
|
47
59
|
}
|
|
@@ -106,6 +118,19 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
106
118
|
}
|
|
107
119
|
return result;
|
|
108
120
|
}
|
|
121
|
+
getEnumValuesVariable(ctx, schema) {
|
|
122
|
+
var _a, _b;
|
|
123
|
+
const enumTypeName = this.getDeclarationTypeName(ctx, schema);
|
|
124
|
+
const values = (_b = (_a = schema.enum) === null || _a === void 0 ? void 0 : _a.map((x) => ts.toNode(x))) !== null && _b !== void 0 ? _b : [];
|
|
125
|
+
return ts.variable(toCasing(`${enumTypeName}-Values`, ctx.config.constantNameCasing), {
|
|
126
|
+
doc: ts.doc({
|
|
127
|
+
description: `All possible values of the enum \`${enumTypeName}\`.`,
|
|
128
|
+
}),
|
|
129
|
+
export: true,
|
|
130
|
+
readonly: true,
|
|
131
|
+
value: ts.tuple(values, { asConst: true }),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
109
134
|
getIndexer(ctx, schema) {
|
|
110
135
|
var _a;
|
|
111
136
|
return schema.additionalProperties
|
|
@@ -44,6 +44,12 @@ export class TypeScriptModelsGenerator extends OpenApiSchemasGenerationProviderB
|
|
|
44
44
|
return ctx.config.indexFilePath ? resolve(ctx.config.outputDir, ctx.config.indexFilePath) : null;
|
|
45
45
|
}
|
|
46
46
|
getIndexFileContent(ctx) {
|
|
47
|
-
return appendValueGroup(Object.values(ctx.output.models).
|
|
47
|
+
return appendValueGroup(Object.values(ctx.output.models).flatMap((x) => {
|
|
48
|
+
var _a, _b;
|
|
49
|
+
const { filePath } = x;
|
|
50
|
+
if (!filePath)
|
|
51
|
+
return [];
|
|
52
|
+
return [ts.export(x.component, filePath), ...((_b = (_a = x.additionalExports) === null || _a === void 0 ? void 0 : _a.map((e) => ts.export(e, filePath))) !== null && _b !== void 0 ? _b : [])];
|
|
53
|
+
}));
|
|
48
54
|
}
|
|
49
55
|
}
|
package/package.json
CHANGED
|
@@ -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?: {
|