@goast/typescript 0.0.4 → 0.2.0
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/assets/{service → client}/angular/request-builder.ts +6 -2
- package/assets/client/fetch/{url-builder.ts → fetch-client.utils.ts} +15 -0
- package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +235 -0
- package/cjs/lib/ast/_index.js +108 -0
- package/cjs/lib/ast/common.js +2 -0
- package/cjs/lib/ast/index.js +5 -0
- package/cjs/lib/ast/node.js +10 -0
- package/cjs/lib/ast/nodes/argument.js +20 -0
- package/cjs/lib/ast/nodes/array-type.js +27 -0
- package/cjs/lib/ast/nodes/arrow-function.js +51 -0
- package/cjs/lib/ast/nodes/call.js +26 -0
- package/cjs/lib/ast/nodes/class.js +70 -0
- package/cjs/lib/ast/nodes/constructor-parameter.js +46 -0
- package/cjs/lib/ast/nodes/constructor.js +42 -0
- package/cjs/lib/ast/nodes/decorator.js +38 -0
- package/cjs/lib/ast/nodes/doc-tag.js +64 -0
- package/cjs/lib/ast/nodes/doc.js +57 -0
- package/cjs/lib/ast/nodes/enum-value.js +35 -0
- package/cjs/lib/ast/nodes/enum.js +46 -0
- package/cjs/lib/ast/nodes/export.js +27 -0
- package/cjs/lib/ast/nodes/function-type.js +35 -0
- package/cjs/lib/ast/nodes/function.js +60 -0
- package/cjs/lib/ast/nodes/generic-parameter.js +40 -0
- package/cjs/lib/ast/nodes/indexer.js +32 -0
- package/cjs/lib/ast/nodes/interface.js +60 -0
- package/cjs/lib/ast/nodes/intersection-type.js +44 -0
- package/cjs/lib/ast/nodes/lookup-type.js +30 -0
- package/cjs/lib/ast/nodes/method.js +82 -0
- package/cjs/lib/ast/nodes/object-type.js +24 -0
- package/cjs/lib/ast/nodes/object.js +24 -0
- package/cjs/lib/ast/nodes/parameter.js +45 -0
- package/cjs/lib/ast/nodes/property-accessor.js +128 -0
- package/cjs/lib/ast/nodes/property.js +119 -0
- package/cjs/lib/ast/nodes/reference.js +63 -0
- package/cjs/lib/ast/nodes/string.js +30 -0
- package/cjs/lib/ast/nodes/tuple.js +28 -0
- package/cjs/lib/ast/nodes/type-alias.js +36 -0
- package/cjs/lib/ast/nodes/typeof.js +23 -0
- package/cjs/lib/ast/nodes/types.js +2 -0
- package/cjs/lib/ast/nodes/union-type.js +44 -0
- package/cjs/lib/ast/nodes/variable.js +38 -0
- package/cjs/lib/ast/references/angular.js +12 -0
- package/cjs/lib/ast/references/easy-network-stub.js +8 -0
- package/cjs/lib/ast/references/index.js +7 -0
- package/cjs/lib/ast/references/rxjs.js +9 -0
- package/cjs/lib/ast/references/typescript.js +33 -0
- package/cjs/lib/ast/utils/get-type-script-builder-options.js +19 -0
- package/cjs/lib/ast/utils/to-ts-node.js +24 -0
- package/cjs/lib/ast/utils/write-ts-generic-parameters.js +11 -0
- package/cjs/lib/ast/utils/write-ts-members.js +18 -0
- package/cjs/lib/ast/utils/write-ts-nodes.js +24 -0
- package/cjs/lib/ast/utils/write-ts-parameters.js +14 -0
- package/cjs/lib/config.js +5 -3
- package/cjs/lib/file-builder.js +45 -202
- package/cjs/lib/generators/file-generator.js +2 -18
- package/cjs/lib/generators/index.js +1 -0
- package/cjs/lib/generators/models/model-generator.js +144 -205
- package/cjs/lib/generators/models/models-generator.js +12 -26
- package/cjs/lib/generators/models/models.js +1 -1
- package/cjs/lib/generators/services/angular-services/angular-service-generator.js +183 -306
- package/cjs/lib/generators/services/angular-services/angular-services-generator.js +168 -76
- package/cjs/lib/generators/services/angular-services/models.js +10 -1
- package/cjs/lib/generators/services/angular-services/refs.js +24 -0
- package/cjs/lib/generators/services/easy-network-stub/easy-network-stub-generator.js +169 -0
- package/cjs/lib/generators/services/easy-network-stub/easy-network-stubs-generator.js +73 -0
- package/cjs/lib/generators/services/easy-network-stub/index.js +6 -0
- package/cjs/lib/generators/services/easy-network-stub/models.js +10 -0
- package/cjs/lib/generators/services/easy-network-stub/refs.js +21 -0
- package/cjs/lib/generators/services/fetch-clients/fetch-client-generator.js +126 -180
- package/cjs/lib/generators/services/fetch-clients/fetch-clients-generator.js +5 -1
- package/cjs/lib/generators/services/fetch-clients/refs.js +17 -0
- package/cjs/lib/import-collection.js +18 -19
- package/esm/lib/ast/_index.js +37 -0
- package/esm/lib/ast/common.js +1 -0
- package/esm/lib/ast/index.js +2 -0
- package/esm/lib/ast/node.js +6 -0
- package/esm/lib/ast/nodes/argument.js +16 -0
- package/esm/lib/ast/nodes/array-type.js +23 -0
- package/esm/lib/ast/nodes/arrow-function.js +47 -0
- package/esm/lib/ast/nodes/call.js +22 -0
- package/esm/lib/ast/nodes/class.js +66 -0
- package/esm/lib/ast/nodes/constructor-parameter.js +42 -0
- package/esm/lib/ast/nodes/constructor.js +38 -0
- package/esm/lib/ast/nodes/decorator.js +34 -0
- package/esm/lib/ast/nodes/doc-tag.js +60 -0
- package/esm/lib/ast/nodes/doc.js +53 -0
- package/esm/lib/ast/nodes/enum-value.js +31 -0
- package/esm/lib/ast/nodes/enum.js +42 -0
- package/esm/lib/ast/nodes/export.js +22 -0
- package/esm/lib/ast/nodes/function-type.js +31 -0
- package/esm/lib/ast/nodes/function.js +56 -0
- package/esm/lib/ast/nodes/generic-parameter.js +36 -0
- package/esm/lib/ast/nodes/indexer.js +28 -0
- package/esm/lib/ast/nodes/interface.js +56 -0
- package/esm/lib/ast/nodes/intersection-type.js +40 -0
- package/esm/lib/ast/nodes/lookup-type.js +26 -0
- package/esm/lib/ast/nodes/method.js +77 -0
- package/esm/lib/ast/nodes/object-type.js +20 -0
- package/esm/lib/ast/nodes/object.js +20 -0
- package/esm/lib/ast/nodes/parameter.js +41 -0
- package/esm/lib/ast/nodes/property-accessor.js +121 -0
- package/esm/lib/ast/nodes/property.js +113 -0
- package/esm/lib/ast/nodes/reference.js +59 -0
- package/esm/lib/ast/nodes/string.js +26 -0
- package/esm/lib/ast/nodes/tuple.js +24 -0
- package/esm/lib/ast/nodes/type-alias.js +32 -0
- package/esm/lib/ast/nodes/typeof.js +18 -0
- package/esm/lib/ast/nodes/types.js +1 -0
- package/esm/lib/ast/nodes/union-type.js +40 -0
- package/esm/lib/ast/nodes/variable.js +34 -0
- package/esm/lib/ast/references/angular.js +9 -0
- package/esm/lib/ast/references/easy-network-stub.js +5 -0
- package/esm/lib/ast/references/index.js +5 -0
- package/esm/lib/ast/references/rxjs.js +6 -0
- package/esm/lib/ast/references/typescript.js +30 -0
- package/esm/lib/ast/utils/get-type-script-builder-options.js +15 -0
- package/esm/lib/ast/utils/to-ts-node.js +20 -0
- package/esm/lib/ast/utils/write-ts-generic-parameters.js +7 -0
- package/esm/lib/ast/utils/write-ts-members.js +14 -0
- package/esm/lib/ast/utils/write-ts-nodes.js +19 -0
- package/esm/lib/ast/utils/write-ts-parameters.js +10 -0
- package/esm/lib/config.js +5 -3
- package/esm/lib/file-builder.js +44 -202
- package/esm/lib/generators/file-generator.js +2 -18
- package/esm/lib/generators/index.js +1 -0
- package/esm/lib/generators/models/model-generator.js +149 -210
- package/esm/lib/generators/models/models-generator.js +15 -29
- package/esm/lib/generators/models/models.js +1 -1
- package/esm/lib/generators/services/angular-services/angular-service-generator.js +186 -309
- package/esm/lib/generators/services/angular-services/angular-services-generator.js +170 -78
- package/esm/lib/generators/services/angular-services/models.js +10 -1
- package/esm/lib/generators/services/angular-services/refs.js +20 -0
- package/esm/lib/generators/services/easy-network-stub/easy-network-stub-generator.js +165 -0
- package/esm/lib/generators/services/easy-network-stub/easy-network-stubs-generator.js +69 -0
- package/esm/lib/generators/services/easy-network-stub/index.js +3 -0
- package/esm/lib/generators/services/easy-network-stub/models.js +7 -0
- package/esm/lib/generators/services/easy-network-stub/refs.js +17 -0
- package/esm/lib/generators/services/fetch-clients/fetch-client-generator.js +128 -182
- package/esm/lib/generators/services/fetch-clients/fetch-clients-generator.js +5 -1
- package/esm/lib/generators/services/fetch-clients/refs.js +13 -0
- package/esm/lib/import-collection.js +17 -18
- package/package.json +6 -6
- package/types/lib/ast/_index.d.ts +38 -0
- package/types/lib/ast/common.d.ts +1 -0
- package/types/lib/ast/index.d.ts +1 -0
- package/types/lib/ast/node.d.ts +4 -0
- package/types/lib/ast/nodes/argument.d.ts +17 -0
- package/types/lib/ast/nodes/array-type.d.ts +19 -0
- package/types/lib/ast/nodes/arrow-function.d.ts +29 -0
- package/types/lib/ast/nodes/call.d.ts +20 -0
- package/types/lib/ast/nodes/class.d.ts +41 -0
- package/types/lib/ast/nodes/constructor-parameter.d.ts +19 -0
- package/types/lib/ast/nodes/constructor.d.ts +25 -0
- package/types/lib/ast/nodes/decorator.d.ts +22 -0
- package/types/lib/ast/nodes/doc-tag.d.ts +73 -0
- package/types/lib/ast/nodes/doc.d.ts +28 -0
- package/types/lib/ast/nodes/enum-value.d.ts +21 -0
- package/types/lib/ast/nodes/enum.d.ts +26 -0
- package/types/lib/ast/nodes/export.d.ts +19 -0
- package/types/lib/ast/nodes/function-type.d.ts +23 -0
- package/types/lib/ast/nodes/function.d.ts +35 -0
- package/types/lib/ast/nodes/generic-parameter.d.ts +25 -0
- package/types/lib/ast/nodes/indexer.d.ts +23 -0
- package/types/lib/ast/nodes/interface.d.ts +36 -0
- package/types/lib/ast/nodes/intersection-type.d.ts +17 -0
- package/types/lib/ast/nodes/lookup-type.d.ts +19 -0
- package/types/lib/ast/nodes/method.d.ts +45 -0
- package/types/lib/ast/nodes/object-type.d.ts +20 -0
- package/types/lib/ast/nodes/object.d.ts +19 -0
- package/types/lib/ast/nodes/parameter.d.ts +28 -0
- package/types/lib/ast/nodes/property-accessor.d.ts +53 -0
- package/types/lib/ast/nodes/property.d.ts +84 -0
- package/types/lib/ast/nodes/reference.d.ts +46 -0
- package/types/lib/ast/nodes/string.d.ts +18 -0
- package/types/lib/ast/nodes/tuple.d.ts +17 -0
- package/types/lib/ast/nodes/type-alias.d.ts +27 -0
- package/types/lib/ast/nodes/typeof.d.ts +18 -0
- package/types/lib/ast/nodes/types.d.ts +18 -0
- package/types/lib/ast/nodes/union-type.d.ts +17 -0
- package/types/lib/ast/nodes/variable.d.ts +28 -0
- package/types/lib/ast/references/angular.d.ts +37 -0
- package/types/lib/ast/references/easy-network-stub.d.ts +26 -0
- package/types/lib/ast/references/index.d.ts +3 -0
- package/types/lib/ast/references/rxjs.d.ts +30 -0
- package/types/lib/ast/references/typescript.d.ts +133 -0
- package/types/lib/ast/utils/get-type-script-builder-options.d.ts +3 -0
- package/types/lib/ast/utils/to-ts-node.d.ts +8 -0
- package/types/lib/ast/utils/write-ts-generic-parameters.d.ts +3 -0
- package/types/lib/ast/utils/write-ts-members.d.ts +5 -0
- package/types/lib/ast/utils/write-ts-nodes.d.ts +6 -0
- package/types/lib/ast/utils/write-ts-parameters.d.ts +3 -0
- package/types/lib/common-results.d.ts +4 -0
- package/types/lib/config.d.ts +5 -2
- package/types/lib/file-builder.d.ts +21 -98
- package/types/lib/generators/file-generator.d.ts +3 -7
- package/types/lib/generators/index.d.ts +1 -0
- package/types/lib/generators/models/model-generator.d.ts +18 -23
- package/types/lib/generators/models/models-generator.d.ts +4 -5
- package/types/lib/generators/models/models.d.ts +4 -4
- package/types/lib/generators/services/angular-services/angular-service-generator.d.ts +15 -35
- package/types/lib/generators/services/angular-services/angular-services-generator.d.ts +12 -10
- package/types/lib/generators/services/angular-services/models.d.ts +67 -22
- package/types/lib/generators/services/angular-services/refs.d.ts +34 -0
- package/types/lib/generators/services/easy-network-stub/easy-network-stub-generator.d.ts +32 -0
- package/types/lib/generators/services/easy-network-stub/easy-network-stubs-generator.d.ts +25 -0
- package/types/lib/generators/services/easy-network-stub/index.d.ts +3 -0
- package/types/lib/generators/services/easy-network-stub/models.d.ts +51 -0
- package/types/lib/generators/services/easy-network-stub/refs.d.ts +49 -0
- package/types/lib/generators/services/fetch-clients/fetch-client-generator.d.ts +11 -22
- package/types/lib/generators/services/fetch-clients/models.d.ts +4 -1
- package/types/lib/generators/services/fetch-clients/refs.d.ts +23 -0
- package/types/lib/import-collection.d.ts +13 -5
- package/assets/client/fetch/types.ts +0 -9
- package/assets/service/angular/api-configuration.ts +0 -18
- package/assets/service/angular/base-service.ts +0 -29
- package/assets/service/angular/response-handlers.ts +0 -127
- package/assets/service/angular/strict-http-response.ts +0 -8
|
@@ -1,281 +1,211 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { resolve } from 'path';
|
|
2
|
+
import { appendValueGroup, getSchemaReference, notNullish, resolveAnyOfAndAllOf, toCasing, } from '@goast/core';
|
|
3
|
+
import { ts } from '../../ast';
|
|
4
4
|
import { TypeScriptFileBuilder } from '../../file-builder';
|
|
5
|
-
import { toTypeScriptPropertyName } from '../../utils';
|
|
6
5
|
import { TypeScriptFileGenerator } from '../file-generator';
|
|
7
6
|
export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
8
7
|
generate(ctx) {
|
|
9
8
|
if (this.shouldGenerateTypeDeclaration(ctx, ctx.schema)) {
|
|
10
9
|
const name = this.getDeclarationTypeName(ctx, ctx.schema);
|
|
11
10
|
const filePath = this.getFilePath(ctx, ctx.schema);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
TypeScriptFileBuilder.generate({
|
|
12
|
+
logName: `model ${name}`,
|
|
13
|
+
filePath,
|
|
14
|
+
options: ctx.config,
|
|
15
|
+
generator: (b) => b.append(this.getFileContent(ctx)),
|
|
16
|
+
});
|
|
17
17
|
return { component: name, filePath, imports: [{ kind: 'file', name, modulePath: filePath }] };
|
|
18
18
|
}
|
|
19
19
|
else {
|
|
20
20
|
const builder = new TypeScriptFileBuilder(undefined, ctx.config);
|
|
21
|
-
this.
|
|
21
|
+
builder.append(this.getType(ctx, ctx.schema));
|
|
22
22
|
const imports = builder.imports.imports;
|
|
23
23
|
builder.imports.clear();
|
|
24
24
|
return { component: builder.toString(false), imports };
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
getFileContent(ctx) {
|
|
28
|
+
const content = appendValueGroup([], '\n');
|
|
28
29
|
const interfaceSchema = this.resolveInterfaceSchema(ctx);
|
|
29
30
|
if (interfaceSchema) {
|
|
30
|
-
this.
|
|
31
|
+
content.values.push(this.getInterface(ctx, interfaceSchema));
|
|
31
32
|
}
|
|
32
33
|
else if (ctx.config.enumGeneration !== 'union' &&
|
|
33
34
|
ctx.schema.kind === 'string' &&
|
|
34
35
|
ctx.schema.enum !== undefined &&
|
|
35
36
|
ctx.schema.enum.length > 0) {
|
|
36
|
-
this.
|
|
37
|
+
content.values.push(this.getEnum(ctx));
|
|
37
38
|
}
|
|
38
39
|
else {
|
|
39
|
-
this.
|
|
40
|
+
content.values.push(this.getTypeAlias(ctx, ctx.schema));
|
|
40
41
|
}
|
|
42
|
+
return content;
|
|
41
43
|
}
|
|
42
|
-
|
|
43
|
-
builder
|
|
44
|
-
.append((builder) => this.generateTypeDocumentation(ctx, builder))
|
|
45
|
-
.ensureCurrentLineEmpty()
|
|
46
|
-
.append((builder) => this.generateEnumSignature(ctx, builder))
|
|
47
|
-
.parenthesize('{}', (builder) => this.generateEnumValues(ctx, builder), { multiline: true })
|
|
48
|
-
.ensureCurrentLineEmpty();
|
|
49
|
-
}
|
|
50
|
-
generateEnumSignature(ctx, builder) {
|
|
51
|
-
builder.append('export enum ').append(this.getDeclarationTypeName(ctx, ctx.schema));
|
|
52
|
-
}
|
|
53
|
-
generateEnumValues(ctx, builder) {
|
|
44
|
+
getEnum(ctx) {
|
|
54
45
|
var _a, _b;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.append((builder) => this.generateTypeDocumentation(ctx, builder))
|
|
60
|
-
.ensureCurrentLineEmpty()
|
|
61
|
-
.append((builder) => this.generateInterfaceSignature(ctx, builder))
|
|
62
|
-
.parenthesize('{}', (builder) => builder.append((builder) => this.generateInterfaceContent(ctx, builder, schema)), { multiline: true })
|
|
63
|
-
.ensureCurrentLineEmpty();
|
|
64
|
-
}
|
|
65
|
-
generateInterfaceSignature(ctx, builder) {
|
|
66
|
-
builder.append('export interface ').append(this.getDeclarationTypeName(ctx, ctx.schema));
|
|
67
|
-
}
|
|
68
|
-
generateInterfaceContent(ctx, builder, schema) {
|
|
69
|
-
this.generateObjectTypeProperties(ctx, builder, schema);
|
|
70
|
-
if (schema.additionalProperties) {
|
|
71
|
-
builder.append('[key: string]: ');
|
|
72
|
-
if (schema.additionalProperties === true) {
|
|
73
|
-
builder.append(this.getAnyType(ctx));
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
this.generateTypeUsage(ctx, builder, schema.additionalProperties);
|
|
77
|
-
}
|
|
78
|
-
builder.appendLine(';');
|
|
79
|
-
}
|
|
46
|
+
return ts.enum(this.getDeclarationTypeName(ctx, ctx.schema), {
|
|
47
|
+
export: true,
|
|
48
|
+
members: ((_b = (_a = ctx.schema.enum) === null || _a === void 0 ? void 0 : _a.map((x) => String(x))) !== null && _b !== void 0 ? _b : []).map((x) => ts.enumValue(toCasing(x, ctx.config.enumValueNameCasing), { value: ts.string(x) })),
|
|
49
|
+
});
|
|
80
50
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
.
|
|
85
|
-
.
|
|
86
|
-
|
|
87
|
-
.append((builder) => this.generateType(ctx, builder, ctx.schema))
|
|
88
|
-
.appendLine(';');
|
|
51
|
+
getInterface(ctx, schema) {
|
|
52
|
+
return ts.interface(this.getDeclarationTypeName(ctx, ctx.schema), {
|
|
53
|
+
export: true,
|
|
54
|
+
doc: ts.doc({ description: schema.description }),
|
|
55
|
+
members: [...this.getProperties(ctx, schema), this.getIndexer(ctx, schema)],
|
|
56
|
+
});
|
|
89
57
|
}
|
|
90
|
-
|
|
91
|
-
|
|
58
|
+
getTypeAlias(ctx, schema) {
|
|
59
|
+
var _a;
|
|
60
|
+
const result = ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = this.getType(ctx, schema, true)) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
|
|
61
|
+
export: true,
|
|
62
|
+
doc: ts.doc({ description: schema.description }),
|
|
63
|
+
});
|
|
64
|
+
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)));
|
|
66
|
+
result.generics.push(ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
|
|
67
|
+
constraint: propertyValue,
|
|
68
|
+
default: propertyValue,
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
getIndexer(ctx, schema) {
|
|
74
|
+
var _a;
|
|
75
|
+
return schema.additionalProperties
|
|
76
|
+
? ts.indexer('string', (_a = (schema.additionalProperties === true ? null : this.getType(ctx, schema.additionalProperties))) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), { readonly: ctx.config.immutableTypes })
|
|
77
|
+
: null;
|
|
78
|
+
}
|
|
79
|
+
getProperties(ctx, schema) {
|
|
80
|
+
return Array.from(schema.properties.values()).map((property) => {
|
|
81
|
+
return ts.property(property.name, {
|
|
82
|
+
doc: ts.doc({ description: property.schema.description }),
|
|
83
|
+
readonly: ctx.config.immutableTypes,
|
|
84
|
+
optional: !schema.required.has(property.name),
|
|
85
|
+
type: ts.unionType([this.getType(ctx, property.schema), property.schema.nullable ? 'null' : null]),
|
|
86
|
+
});
|
|
87
|
+
});
|
|
92
88
|
}
|
|
93
|
-
|
|
89
|
+
getType(ctx, schema, skipSchemas = false) {
|
|
90
|
+
if (!schema)
|
|
91
|
+
return this.getAnyType(ctx);
|
|
94
92
|
schema = getSchemaReference(schema, ['description']);
|
|
95
|
-
if (this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (filePath) {
|
|
99
|
-
builder.addImport(name, filePath);
|
|
93
|
+
if (!skipSchemas && this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
94
|
+
if (schema.id === ctx.schema.id) {
|
|
95
|
+
return null;
|
|
100
96
|
}
|
|
101
|
-
|
|
97
|
+
return ts.reference(this.getDeclarationTypeName(ctx, schema), this.getFilePath(ctx, schema));
|
|
102
98
|
}
|
|
103
|
-
|
|
104
|
-
this.
|
|
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
|
+
}));
|
|
105
107
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (schema.enum !== undefined && schema.enum.length > 0) {
|
|
109
|
-
this.generateEnumType(ctx, builder, schema);
|
|
110
|
-
return;
|
|
108
|
+
if (schema.enum) {
|
|
109
|
+
return ts.unionType(schema.enum.map((x) => ts.toNode(x)));
|
|
111
110
|
}
|
|
112
111
|
switch (schema.kind) {
|
|
113
112
|
case 'boolean':
|
|
114
|
-
|
|
115
|
-
break;
|
|
113
|
+
return 'boolean';
|
|
116
114
|
case 'integer':
|
|
117
115
|
case 'number':
|
|
118
|
-
|
|
119
|
-
break;
|
|
116
|
+
return 'number';
|
|
120
117
|
case 'string':
|
|
121
|
-
|
|
122
|
-
break;
|
|
118
|
+
return 'string';
|
|
123
119
|
case 'null':
|
|
124
|
-
|
|
125
|
-
break;
|
|
120
|
+
return 'null';
|
|
126
121
|
case 'unknown':
|
|
127
|
-
|
|
128
|
-
break;
|
|
122
|
+
return this.getAnyType(ctx);
|
|
129
123
|
case 'array':
|
|
130
|
-
this.
|
|
131
|
-
break;
|
|
124
|
+
return this.getArrayType(ctx, schema);
|
|
132
125
|
case 'object':
|
|
133
|
-
this.
|
|
134
|
-
break;
|
|
126
|
+
return this.getObjectType(ctx, schema);
|
|
135
127
|
case 'combined':
|
|
136
|
-
this.
|
|
137
|
-
break;
|
|
128
|
+
return this.getCombinedType(ctx, schema);
|
|
138
129
|
case 'multi-type':
|
|
139
|
-
this.
|
|
140
|
-
break;
|
|
130
|
+
return this.getMultiType(ctx, schema);
|
|
141
131
|
case 'oneOf':
|
|
142
|
-
this.
|
|
143
|
-
break;
|
|
132
|
+
return this.getOneOfType(ctx, schema);
|
|
144
133
|
default:
|
|
145
|
-
|
|
146
|
-
break;
|
|
134
|
+
return this.getAnyType(ctx);
|
|
147
135
|
}
|
|
148
136
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
137
|
+
getArrayType(ctx, schema) {
|
|
138
|
+
var _a;
|
|
139
|
+
return ts.arrayType((_a = this.getType(ctx, schema.items)) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
|
|
140
|
+
readonly: ctx.config.immutableTypes,
|
|
141
|
+
});
|
|
154
142
|
}
|
|
155
|
-
|
|
143
|
+
getObjectType(ctx, schema) {
|
|
144
|
+
const parts = [
|
|
145
|
+
schema.properties.size > 0 ? ts.objectType({ members: this.getProperties(ctx, schema) }) : null,
|
|
146
|
+
schema.additionalProperties ? ts.objectType({ members: [this.getIndexer(ctx, schema)] }) : null,
|
|
147
|
+
schema.allOf.length > 0 || schema.anyOf.length > 0 ? this.getCombinedType(ctx, schema) : null,
|
|
148
|
+
].filter(notNullish);
|
|
149
|
+
if (parts.length === 0) {
|
|
150
|
+
parts.push(ts.objectType());
|
|
151
|
+
}
|
|
152
|
+
const type = ts.intersectionType(parts);
|
|
153
|
+
if (schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0) {
|
|
154
|
+
const discriminator = schema.discriminator;
|
|
155
|
+
return ts.intersectionType([
|
|
156
|
+
type,
|
|
157
|
+
ts.lookupType(ts.objectType({
|
|
158
|
+
members: Object.entries(discriminator.mapping).map(([key, value]) => ts.property(key, {
|
|
159
|
+
type: ts.intersectionType([
|
|
160
|
+
ts.objectType({
|
|
161
|
+
members: [ts.property(discriminator.propertyName, { type: ts.string(key) })],
|
|
162
|
+
}),
|
|
163
|
+
this.getType(ctx, value, true),
|
|
164
|
+
]),
|
|
165
|
+
})),
|
|
166
|
+
}), this.getDiscriminatorGenericParamName(ctx, schema)),
|
|
167
|
+
]);
|
|
168
|
+
}
|
|
169
|
+
return type;
|
|
170
|
+
}
|
|
171
|
+
getCombinedType(ctx, schema) {
|
|
156
172
|
if (schema.allOf.length === 0 && schema.anyOf.length === 0) {
|
|
157
|
-
|
|
158
|
-
return;
|
|
173
|
+
return this.getAnyType(ctx);
|
|
159
174
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
.forEach(schema.anyOf, (builder, schema) => builder
|
|
165
|
-
.append('Partial')
|
|
166
|
-
.parenthesize('<>', (builder) => this.generateTypeUsage(ctx, builder, schema), { indent: false }), { separator: ' & ' }));
|
|
175
|
+
return ts.intersectionType([
|
|
176
|
+
...schema.allOf.map((x) => this.getType(ctx, x)),
|
|
177
|
+
...schema.anyOf.map((x) => ts.reference('Partial', null, { generics: [this.getType(ctx, x)] })),
|
|
178
|
+
]);
|
|
167
179
|
}
|
|
168
|
-
|
|
180
|
+
getOneOfType(ctx, schema) {
|
|
169
181
|
if (schema.oneOf.length === 0) {
|
|
170
|
-
|
|
171
|
-
return;
|
|
182
|
+
return this.getAnyType(ctx);
|
|
172
183
|
}
|
|
173
|
-
|
|
174
|
-
separator: ' | ',
|
|
175
|
-
}));
|
|
184
|
+
return ts.unionType(schema.oneOf.map((x) => this.getType(ctx, x)));
|
|
176
185
|
}
|
|
177
|
-
|
|
186
|
+
getMultiType(ctx, schema) {
|
|
178
187
|
if (schema.type.length === 0) {
|
|
179
|
-
|
|
180
|
-
return;
|
|
188
|
+
return this.getAnyType(ctx);
|
|
181
189
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
.appendIf(parenthesize, (builder) => builder.appendLine().append('| '))
|
|
185
|
-
.forEach(schema.type, (builder, type) => {
|
|
186
|
-
switch (type) {
|
|
190
|
+
return ts.unionType(schema.type.map((x) => {
|
|
191
|
+
switch (x) {
|
|
187
192
|
case 'string':
|
|
188
|
-
|
|
189
|
-
break;
|
|
193
|
+
return 'string';
|
|
190
194
|
case 'number':
|
|
191
195
|
case 'integer':
|
|
192
|
-
|
|
193
|
-
break;
|
|
196
|
+
return 'number';
|
|
194
197
|
case 'boolean':
|
|
195
|
-
|
|
196
|
-
break;
|
|
198
|
+
return 'boolean';
|
|
197
199
|
case 'null':
|
|
198
|
-
|
|
199
|
-
break;
|
|
200
|
+
return 'null';
|
|
200
201
|
case 'array':
|
|
201
|
-
this.
|
|
202
|
-
break;
|
|
202
|
+
return this.getArrayType(ctx, schema);
|
|
203
203
|
case 'object':
|
|
204
|
-
this.
|
|
205
|
-
break;
|
|
204
|
+
return this.getObjectType(ctx, schema);
|
|
206
205
|
default:
|
|
207
|
-
|
|
208
|
-
break;
|
|
206
|
+
return 'never';
|
|
209
207
|
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
generateObjectType(ctx, builder, schema) {
|
|
213
|
-
if (schema.properties.size === 0 &&
|
|
214
|
-
!schema.additionalProperties &&
|
|
215
|
-
schema.allOf.length === 0 &&
|
|
216
|
-
schema.anyOf.length === 0 &&
|
|
217
|
-
schema.discriminator === undefined) {
|
|
218
|
-
builder.append('{}');
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
// properties
|
|
222
|
-
if (schema.properties.size > 0) {
|
|
223
|
-
builder
|
|
224
|
-
.parenthesize('{}', (builder) => builder.appendLine().append((builder) => this.generateObjectTypeProperties(ctx, builder, schema)))
|
|
225
|
-
.appendIf(!!schema.additionalProperties || schema.allOf.length > 0 || schema.anyOf.length > 0, ' & ');
|
|
226
|
-
}
|
|
227
|
-
// additional properties
|
|
228
|
-
if (schema.additionalProperties) {
|
|
229
|
-
this.generateObjectTypeAdditionalProperties(ctx, builder, schema);
|
|
230
|
-
if (schema.allOf.length > 0 || schema.anyOf.length > 0) {
|
|
231
|
-
builder.append(' & ');
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
// allOf & anyOf
|
|
235
|
-
if (schema.allOf.length > 0 || schema.anyOf.length > 0) {
|
|
236
|
-
this.generateCombinedType(ctx, builder, schema);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
generateObjectTypeProperties(ctx, builder, schema) {
|
|
240
|
-
builder.forEach(schema.properties.values(), (builder, property) => {
|
|
241
|
-
this.generatePropertyDocumentation(ctx, builder, property);
|
|
242
|
-
builder
|
|
243
|
-
.appendIf(ctx.config.immutableTypes, 'readonly ')
|
|
244
|
-
.append(toTypeScriptPropertyName(property.name, ctx.config.useSingleQuotes))
|
|
245
|
-
.appendIf(!schema.required.has(property.name), '?')
|
|
246
|
-
.append(': ')
|
|
247
|
-
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
248
|
-
.appendIf(property.schema.nullable === true, ' | null')
|
|
249
|
-
.appendLine(';');
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
generateObjectTypeAdditionalProperties(ctx, builder, schema) {
|
|
253
|
-
if (!schema.additionalProperties)
|
|
254
|
-
return;
|
|
255
|
-
builder
|
|
256
|
-
.appendIf(ctx.config.immutableTypes, 'Readonly')
|
|
257
|
-
.parenthesizeIf(ctx.config.immutableTypes, '<>', (builder) => builder
|
|
258
|
-
.append('Record')
|
|
259
|
-
.appendGenericArguments('string', schema.additionalProperties === true
|
|
260
|
-
? this.getAnyType(ctx)
|
|
261
|
-
: (builder) => this.generateTypeUsage(ctx, builder, schema.additionalProperties)));
|
|
262
|
-
}
|
|
263
|
-
generateEnumType(ctx, builder, schema) {
|
|
264
|
-
builder.indent((builder) => {
|
|
265
|
-
var _a;
|
|
266
|
-
builder.forEach((_a = schema.enum) !== null && _a !== void 0 ? _a : [], (builder, item) => builder.append(this.toStringLiteral(ctx, String(item))), {
|
|
267
|
-
separator: ' | ',
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
generateTypeDocumentation(ctx, builder) {
|
|
272
|
-
this.generateDocumentation(ctx, builder, ctx.schema);
|
|
273
|
-
}
|
|
274
|
-
generatePropertyDocumentation(ctx, builder, property) {
|
|
275
|
-
this.generateDocumentation(ctx, builder, property.schema);
|
|
276
|
-
}
|
|
277
|
-
generateDocumentation(ctx, builder, schema) {
|
|
278
|
-
builder.appendComment('/***/', schema.description);
|
|
208
|
+
}));
|
|
279
209
|
}
|
|
280
210
|
shouldGenerateTypeDeclaration(ctx, schema) {
|
|
281
211
|
// All named schemas should have its own type declaration
|
|
@@ -283,7 +213,7 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
283
213
|
return true;
|
|
284
214
|
}
|
|
285
215
|
// All enum types should have its own type declaration
|
|
286
|
-
if (schema.
|
|
216
|
+
if (schema.enum !== undefined && schema.enum.length > 0) {
|
|
287
217
|
return true;
|
|
288
218
|
}
|
|
289
219
|
// All primitive types already exist and do not need its own type declaration
|
|
@@ -307,6 +237,15 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
307
237
|
getDeclarationTypeName(ctx, schema) {
|
|
308
238
|
return toCasing(schema.name, ctx.config.typeNameCasing);
|
|
309
239
|
}
|
|
240
|
+
getDiscriminatorGenericParamName(ctx, schema) {
|
|
241
|
+
var _a;
|
|
242
|
+
return toCasing((_a = schema.discriminator) === null || _a === void 0 ? void 0 : _a.propertyName, ctx.config.genericParamNameCasing);
|
|
243
|
+
}
|
|
244
|
+
getInheritedSchemas(ctx, schema) {
|
|
245
|
+
return schema.inheritedSchemas
|
|
246
|
+
.filter((schema) => this.shouldGenerateTypeDeclaration(ctx, schema) && !schema.isNameGenerated)
|
|
247
|
+
.filter((item, index, self) => self.indexOf(item) === index);
|
|
248
|
+
}
|
|
310
249
|
getFilePath(ctx, schema) {
|
|
311
250
|
return resolve(ctx.config.outputDir, ctx.config.modelsDirPath, `${toCasing(schema.name, ctx.config.fileNameCasing)}.ts`);
|
|
312
251
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { OpenApiSchemasGenerationProviderBase, Factory } from '@goast/core';
|
|
1
|
+
import { resolve } from 'path';
|
|
2
|
+
import { OpenApiSchemasGenerationProviderBase, Factory, appendValueGroup, } from '@goast/core';
|
|
4
3
|
import { DefaultTypeScriptModelGenerator } from './model-generator';
|
|
5
4
|
import { defaultTypeScriptModelsGeneratorConfig, } from './models';
|
|
5
|
+
import { ts } from '../../ast';
|
|
6
6
|
import { TypeScriptFileBuilder } from '../../file-builder';
|
|
7
|
-
import { ImportExportCollection } from '../../import-collection';
|
|
8
7
|
export class TypeScriptModelsGenerator extends OpenApiSchemasGenerationProviderBase {
|
|
9
8
|
constructor(modelGeneratorFactory) {
|
|
10
9
|
super();
|
|
@@ -13,7 +12,7 @@ export class TypeScriptModelsGenerator extends OpenApiSchemasGenerationProviderB
|
|
|
13
12
|
initResult() {
|
|
14
13
|
return {
|
|
15
14
|
models: {},
|
|
16
|
-
|
|
15
|
+
indexFiles: { models: undefined },
|
|
17
16
|
};
|
|
18
17
|
}
|
|
19
18
|
buildContext(context, config) {
|
|
@@ -21,7 +20,7 @@ export class TypeScriptModelsGenerator extends OpenApiSchemasGenerationProviderB
|
|
|
21
20
|
}
|
|
22
21
|
onGenerate(ctx) {
|
|
23
22
|
const output = super.onGenerate(ctx);
|
|
24
|
-
output.
|
|
23
|
+
output.indexFiles.models = this.generateIndexFile(ctx);
|
|
25
24
|
return output;
|
|
26
25
|
}
|
|
27
26
|
generateSchema(ctx, schema) {
|
|
@@ -32,32 +31,19 @@ export class TypeScriptModelsGenerator extends OpenApiSchemasGenerationProviderB
|
|
|
32
31
|
ctx.output.models[schema.id] = result;
|
|
33
32
|
}
|
|
34
33
|
generateIndexFile(ctx) {
|
|
35
|
-
if (!this.shouldGenerateIndexFile(ctx)) {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
34
|
const filePath = this.getIndexFilePath(ctx);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
35
|
+
TypeScriptFileBuilder.tryGenerate({
|
|
36
|
+
logName: 'model index file',
|
|
37
|
+
filePath,
|
|
38
|
+
options: ctx.config,
|
|
39
|
+
generator: (b) => b.append(this.getIndexFileContent(ctx)),
|
|
40
|
+
});
|
|
44
41
|
return filePath;
|
|
45
42
|
}
|
|
46
43
|
getIndexFilePath(ctx) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return ctx.config.indexFilePath !== null;
|
|
52
|
-
}
|
|
53
|
-
generateIndexFileContent(ctx, builder) {
|
|
54
|
-
const exports = new ImportExportCollection();
|
|
55
|
-
for (const modelId in ctx.output.models) {
|
|
56
|
-
const model = ctx.output.models[modelId];
|
|
57
|
-
if (model.filePath) {
|
|
58
|
-
exports.addExport(model.component, model.filePath);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
exports.writeTo(builder);
|
|
44
|
+
return ctx.config.indexFilePath ? resolve(ctx.config.outputDir, ctx.config.indexFilePath) : null;
|
|
45
|
+
}
|
|
46
|
+
getIndexFileContent(ctx) {
|
|
47
|
+
return appendValueGroup(Object.values(ctx.output.models).map((x) => (x.filePath ? ts.export(x.component, x.filePath) : null)));
|
|
62
48
|
}
|
|
63
49
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { defaultTypeScriptGeneratorConfig } from '../../config';
|
|
2
|
-
export const defaultTypeScriptModelGeneratorConfig = Object.assign(Object.assign({}, defaultTypeScriptGeneratorConfig), {
|
|
2
|
+
export const defaultTypeScriptModelGeneratorConfig = Object.assign(Object.assign({}, defaultTypeScriptGeneratorConfig), { enumGeneration: 'union', immutableTypes: false, inlineUnnamedSchemas: true, modelsDirPath: 'models', typeDeclaration: 'type' });
|
|
3
3
|
export const defaultTypeScriptModelsGeneratorConfig = Object.assign(Object.assign({}, defaultTypeScriptModelGeneratorConfig), { indexFilePath: 'models.ts' });
|