@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
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DefaultTypeScriptFetchClientGenerator = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
|
-
const fs_extra_1 = require("fs-extra");
|
|
6
5
|
const core_1 = require("@goast/core");
|
|
6
|
+
const ast_1 = require("../../../ast");
|
|
7
7
|
const file_builder_1 = require("../../../file-builder");
|
|
8
8
|
const file_generator_1 = require("../../file-generator");
|
|
9
9
|
class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptFileGenerator {
|
|
@@ -13,204 +13,145 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
13
13
|
if (this.shouldGenerateInterface(ctx)) {
|
|
14
14
|
const filePath = this.getInterfaceFilePath(ctx);
|
|
15
15
|
const name = this.getInterfaceName(ctx);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
file_builder_1.TypeScriptFileBuilder.generate({
|
|
17
|
+
logName: `interface ${name}`,
|
|
18
|
+
filePath,
|
|
19
|
+
options: ctx.config,
|
|
20
|
+
generator: (b) => b.append(this.getInterfaceFileContent(ctx)),
|
|
21
|
+
});
|
|
21
22
|
result.interface = { filePath, component: name, imports: [{ kind: 'file', name, modulePath: filePath }] };
|
|
22
23
|
}
|
|
23
24
|
if (this.shouldGenerateClass(ctx)) {
|
|
24
25
|
const filePath = this.getClassFilePath(ctx);
|
|
25
26
|
const name = this.getClassName(ctx);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.generateClass(ctx, builder);
|
|
33
|
-
(0, fs_extra_1.writeFileSync)(filePath, builder.toString());
|
|
27
|
+
file_builder_1.TypeScriptFileBuilder.generate({
|
|
28
|
+
logName: `class ${name}`,
|
|
29
|
+
filePath,
|
|
30
|
+
options: ctx.config,
|
|
31
|
+
generator: (b) => b.append(this.getClassFileContent(ctx)),
|
|
32
|
+
});
|
|
34
33
|
result.class = { filePath, component: name, imports: [{ kind: 'file', name, modulePath: filePath }] };
|
|
35
34
|
}
|
|
36
35
|
return result;
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.append((builder) => this.generateInterfaceDocumentation(ctx, builder))
|
|
42
|
-
.ensureCurrentLineEmpty()
|
|
43
|
-
.append((builder) => this.generateInterfaceSignature(ctx, builder))
|
|
44
|
-
.append(' ')
|
|
45
|
-
.parenthesize('{}', (builder) => this.generateInterfaceContent(ctx, builder))
|
|
46
|
-
.appendLine();
|
|
47
|
-
}
|
|
48
|
-
generateInterfaceDocumentation(ctx, builder) {
|
|
49
|
-
if (ctx.service.description) {
|
|
50
|
-
builder.appendLine('/**').appendLineWithLinePrefix(' *', ctx.service.description).appendLine(' */');
|
|
51
|
-
}
|
|
37
|
+
// #region Interface
|
|
38
|
+
getInterfaceFileContent(ctx) {
|
|
39
|
+
return (0, core_1.appendValueGroup)([this.getInterface(ctx)], '\n');
|
|
52
40
|
}
|
|
53
|
-
|
|
54
|
-
|
|
41
|
+
getInterface(ctx) {
|
|
42
|
+
return ast_1.ts.interface(this.getInterfaceName(ctx), {
|
|
43
|
+
doc: ast_1.ts.doc({ description: ctx.service.description }),
|
|
44
|
+
export: true,
|
|
45
|
+
members: ctx.service.endpoints.map((endpoint) => this.getInterfaceEndpointMethod(ctx, endpoint)),
|
|
46
|
+
});
|
|
55
47
|
}
|
|
56
|
-
|
|
57
|
-
|
|
48
|
+
getInterfaceEndpointMethod(ctx, endpoint) {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
const params = endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query');
|
|
51
|
+
const bodySchema = (_b = (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) === null || _b === void 0 ? void 0 : _b.schema;
|
|
52
|
+
const responseSchema = this.getResponseSchema(ctx, endpoint);
|
|
53
|
+
return ast_1.ts.method((0, core_1.toCasing)(endpoint.name, ctx.config.methodCasing), {
|
|
54
|
+
doc: ast_1.ts.doc({ description: endpoint.description }),
|
|
55
|
+
parameters: [
|
|
56
|
+
params.length > 0
|
|
57
|
+
? ast_1.ts.parameter('params', {
|
|
58
|
+
description: 'Parameters for the endpoint.',
|
|
59
|
+
type: ast_1.ts.objectType({
|
|
60
|
+
members: params.map((x) => ast_1.ts.property((0, core_1.toCasing)(x.name, ctx.config.propertyNameCasing), {
|
|
61
|
+
type: this.getSchemaType(ctx, x.schema),
|
|
62
|
+
optional: !x.required,
|
|
63
|
+
})),
|
|
64
|
+
}),
|
|
65
|
+
})
|
|
66
|
+
: null,
|
|
67
|
+
bodySchema &&
|
|
68
|
+
ast_1.ts.parameter('body', {
|
|
69
|
+
description: 'Body for the endpoint.',
|
|
70
|
+
type: this.getSchemaType(ctx, bodySchema),
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
returnType: ast_1.ts.refs.promise([ctx.refs.typedResponse([this.getSchemaType(ctx, responseSchema, ast_1.ts.refs.void_())])]),
|
|
74
|
+
});
|
|
58
75
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.ensureCurrentLineEmpty()
|
|
64
|
-
.append((builder) => this.generateServiceMethodSignature(ctx, builder, endpoint))
|
|
65
|
-
.appendLine(';');
|
|
76
|
+
// #endregion
|
|
77
|
+
// #region Class
|
|
78
|
+
getClassFileContent(ctx) {
|
|
79
|
+
return (0, core_1.appendValueGroup)([this.getDefaultOptionsConstant(ctx), this.getClass(ctx)], '\n');
|
|
66
80
|
}
|
|
67
|
-
|
|
81
|
+
getDefaultOptionsConstant(ctx) {
|
|
68
82
|
const baseUrl = ctx.service.endpoints
|
|
69
83
|
.map((x) => { var _a, _b, _c; return (_c = (_b = (_a = x.$src) === null || _a === void 0 ? void 0 : _a.document.servers) === null || _b === void 0 ? void 0 : _b.find((x) => x.url)) === null || _c === void 0 ? void 0 : _c.url; })
|
|
70
84
|
.find((x) => !!x);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
builder
|
|
79
|
-
.ensurePreviousLineEmpty()
|
|
80
|
-
.append((builder) => this.generateClassDocumentation(ctx, builder))
|
|
81
|
-
.ensureCurrentLineEmpty()
|
|
82
|
-
.append((builder) => this.generateClassSignature(ctx, builder))
|
|
83
|
-
.append(' ')
|
|
84
|
-
.parenthesize('{}', (builder) => this.generateClassContent(ctx, builder))
|
|
85
|
-
.appendLine();
|
|
86
|
-
}
|
|
87
|
-
generateClassDocumentation(ctx, builder) {
|
|
88
|
-
if (ctx.service.description) {
|
|
89
|
-
builder.appendLine('/**').appendLineWithLinePrefix(' *', ctx.service.description).appendLine(' */');
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
generateClassSignature(ctx, builder) {
|
|
93
|
-
builder
|
|
94
|
-
.append('export class ')
|
|
95
|
-
.append(this.getClassName(ctx))
|
|
96
|
-
.appendIf(this.shouldGenerateInterface(ctx), (builder) => builder
|
|
97
|
-
.addImport(this.getInterfaceName(ctx), this.getInterfaceFilePath(ctx))
|
|
98
|
-
.append(' implements ', this.getInterfaceName(ctx)));
|
|
99
|
-
}
|
|
100
|
-
generateClassContent(ctx, builder) {
|
|
101
|
-
builder
|
|
102
|
-
.append((builder) => this.generateClassFields(ctx, builder))
|
|
103
|
-
.append((builder) => this.generateClassConstructor(ctx, builder))
|
|
104
|
-
.forEach(ctx.service.endpoints, (builder, endpoint) => this.generateClassServiceMethod(ctx, builder, endpoint));
|
|
105
|
-
}
|
|
106
|
-
generateClassFields(ctx, builder) {
|
|
107
|
-
builder.ensureCurrentLineEmpty().appendLine('public options: FetchClientOptions;');
|
|
108
|
-
}
|
|
109
|
-
generateClassConstructor(ctx, builder) {
|
|
110
|
-
builder
|
|
111
|
-
.ensurePreviousLineEmpty()
|
|
112
|
-
.append('constructor(options?: Partial<FetchClientOptions>)')
|
|
113
|
-
.parenthesize('{}', (builder) => this.generateClassConstructorContent(ctx, builder), {
|
|
114
|
-
indent: true,
|
|
115
|
-
multiline: true,
|
|
85
|
+
return ast_1.ts.variable(this.getDefaultOptionsConstantName(ctx), {
|
|
86
|
+
export: true,
|
|
87
|
+
readonly: true,
|
|
88
|
+
type: ctx.refs.fetchClientOptions(),
|
|
89
|
+
value: ast_1.ts.object({
|
|
90
|
+
members: [baseUrl && ast_1.ts.property('baseUrl', { value: ast_1.ts.string(baseUrl) })],
|
|
91
|
+
}),
|
|
116
92
|
});
|
|
117
93
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
.appendLine(`@see ${(0, core_1.getEndpointUrlPreview)(endpoint)}`)
|
|
143
|
-
.appendLineIf(hasParams, `@param params Parameters for the endpoint.`)
|
|
144
|
-
.append(`@returns The response of the call to the endpoint.`);
|
|
94
|
+
getClass(ctx) {
|
|
95
|
+
return ast_1.ts.class(this.getClassName(ctx), {
|
|
96
|
+
doc: ast_1.ts.doc({ description: ctx.service.description }),
|
|
97
|
+
export: true,
|
|
98
|
+
implements: this.shouldGenerateInterface(ctx)
|
|
99
|
+
? [ast_1.ts.reference(this.getInterfaceName(ctx), this.getInterfaceFilePath(ctx))]
|
|
100
|
+
: undefined,
|
|
101
|
+
members: [
|
|
102
|
+
ast_1.ts.property('options', {
|
|
103
|
+
doc: ast_1.ts.doc({ description: 'Options for the fetch client.' }),
|
|
104
|
+
accessModifier: 'public',
|
|
105
|
+
type: ctx.refs.fetchClientOptions(),
|
|
106
|
+
}),
|
|
107
|
+
ast_1.ts.constructor({
|
|
108
|
+
parameters: [
|
|
109
|
+
ast_1.ts.constructorParameter('options', {
|
|
110
|
+
type: ast_1.ts.refs.partial([ctx.refs.fetchClientOptions()]),
|
|
111
|
+
optional: true,
|
|
112
|
+
}),
|
|
113
|
+
],
|
|
114
|
+
body: (0, core_1.appendValueGroup)([(0, core_1.builderTemplate) `this.options = { ...${this.getDefaultOptionsConstantName(ctx)}, ...options };`], '\n'),
|
|
115
|
+
}),
|
|
116
|
+
...ctx.service.endpoints.map((endpoint) => this.getServiceMethod(ctx, endpoint)),
|
|
117
|
+
],
|
|
145
118
|
});
|
|
146
119
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
generateServiceMethodContent(ctx, builder, endpoint) {
|
|
183
|
-
builder
|
|
184
|
-
.addImport('UrlBuilder', this.getUtilPath(ctx, 'url-builder.ts'))
|
|
185
|
-
.appendLine('const url = new UrlBuilder(this.options.baseUrl)')
|
|
186
|
-
.indent((builder) => builder
|
|
187
|
-
.appendLine(`.withPath(${this.toStringLiteral(ctx, endpoint.path)})`)
|
|
188
|
-
.forEach(endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query'), (builder, parameter) => builder
|
|
189
|
-
.appendIf(parameter.target === 'path', `.withPathParam`)
|
|
190
|
-
.appendIf(parameter.target === 'query', `.withQueryParam`)
|
|
191
|
-
.parenthesize('()', `${this.toStringLiteral(ctx, parameter.name)}, params.${this.toPropertyName(ctx, parameter.name)}`)
|
|
192
|
-
.appendLine())
|
|
193
|
-
.appendLine('.build();'))
|
|
194
|
-
.append('return (this.options.fetch ?? fetch)')
|
|
195
|
-
.parenthesize('()', (builder) => builder.append('url, ').parenthesize('{}', (builder) => {
|
|
196
|
-
var _a, _b;
|
|
197
|
-
return builder
|
|
198
|
-
.appendLine('method: ', this.toStringLiteral(ctx, (0, core_1.toCasing)(endpoint.method, 'all-upper')), ',')
|
|
199
|
-
.appendLine('headers: this.options.headers,')
|
|
200
|
-
.appendLineIf(!!((_b = (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) === null || _b === void 0 ? void 0 : _b.schema), 'body: JSON.stringify(body),');
|
|
201
|
-
}, { indent: true, multiline: true }), { indent: false })
|
|
202
|
-
.appendLine(' as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
|
|
203
|
-
}
|
|
204
|
-
getTypeName(ctx, builder, schemaId) {
|
|
205
|
-
if (!schemaId) {
|
|
206
|
-
return this.getAnyType(ctx);
|
|
207
|
-
}
|
|
208
|
-
const modelInfo = ctx.input.models[schemaId];
|
|
209
|
-
if (!modelInfo) {
|
|
210
|
-
return this.getAnyType(ctx);
|
|
211
|
-
}
|
|
212
|
-
modelInfo.imports.forEach((x) => builder.addImport(x.name, x.modulePath));
|
|
213
|
-
return modelInfo.component;
|
|
120
|
+
getServiceMethod(ctx, endpoint) {
|
|
121
|
+
var _a, _b;
|
|
122
|
+
const method = this.getInterfaceEndpointMethod(ctx, endpoint);
|
|
123
|
+
method.accessModifier = 'public';
|
|
124
|
+
method.body = (0, core_1.appendValueGroup)([
|
|
125
|
+
(0, core_1.builderTemplate) `const url = new ${ctx.refs.urlBuilder()}(this.options.baseUrl)${core_1.builderTemplate.indent `
|
|
126
|
+
.withPath(${ast_1.ts.string(endpoint.path)})${(b) => b.forEach(endpoint.parameters, (b, p) => {
|
|
127
|
+
switch (p.target) {
|
|
128
|
+
case 'path':
|
|
129
|
+
b.append((0, core_1.builderTemplate) `\n.withPathParam(${ast_1.ts.string(p.name)}, params.${(0, core_1.toCasing)(p.name, ctx.config.paramNameCasing)})`);
|
|
130
|
+
break;
|
|
131
|
+
case 'query':
|
|
132
|
+
b.append((0, core_1.builderTemplate) `\n.withQueryParam(${ast_1.ts.string(p.name)}, params.${(0, core_1.toCasing)(p.name, ctx.config.paramNameCasing)})`);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
})}
|
|
136
|
+
.build();`}`,
|
|
137
|
+
(0, core_1.builderTemplate) `const response = (this.options.fetch ?? fetch)(url, ${ast_1.ts.object({
|
|
138
|
+
members: [
|
|
139
|
+
ast_1.ts.property('method', { value: ast_1.ts.string((0, core_1.toCasing)(endpoint.method, 'all-upper')) }),
|
|
140
|
+
ast_1.ts.property('headers', { value: 'this.options.headers' }),
|
|
141
|
+
((_b = (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) === null || _b === void 0 ? void 0 : _b.schema) ? ast_1.ts.property('body', { value: 'JSON.stringify(body)' }) : null,
|
|
142
|
+
],
|
|
143
|
+
})});`,
|
|
144
|
+
(0, core_1.builderTemplate) `Object.defineProperty(response, 'isVoidResponse', { value: ${this.getResponseSchema(ctx, endpoint) ? 'false' : 'true'} });`,
|
|
145
|
+
(0, core_1.builderTemplate) `return response as unknown as ${method.returnType};`,
|
|
146
|
+
], '\n');
|
|
147
|
+
return method;
|
|
148
|
+
}
|
|
149
|
+
// #endregion
|
|
150
|
+
getSchemaType(ctx, schema, fallback) {
|
|
151
|
+
const output = schema && ctx.input.models[schema.id];
|
|
152
|
+
if (!output)
|
|
153
|
+
return fallback !== null && fallback !== void 0 ? fallback : this.getAnyType(ctx);
|
|
154
|
+
return (b) => b.appendModelUsage(output);
|
|
214
155
|
}
|
|
215
156
|
shouldGenerateInterface(ctx) {
|
|
216
157
|
return ctx.config.clientFileKind === 'interface' || ctx.config.clientFileKind === 'class-and-interface';
|
|
@@ -225,7 +166,7 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
225
166
|
return (0, core_1.toCasing)(ctx.service.name, ctx.config.classNameCasing);
|
|
226
167
|
}
|
|
227
168
|
getDefaultOptionsConstantName(ctx) {
|
|
228
|
-
return (0, core_1.toCasing)(this.getClassName(ctx) + '-default-options', ctx.config.
|
|
169
|
+
return (0, core_1.toCasing)(this.getClassName(ctx) + '-default-options', ctx.config.constantNameCasing);
|
|
229
170
|
}
|
|
230
171
|
getInterfaceFilePath(ctx) {
|
|
231
172
|
var _a, _b;
|
|
@@ -237,5 +178,10 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
237
178
|
getUtilPath(ctx, fileName) {
|
|
238
179
|
return (0, path_1.resolve)(ctx.config.outputDir, ctx.config.utilsDirPath, fileName);
|
|
239
180
|
}
|
|
181
|
+
getResponseSchema(ctx, endpoint) {
|
|
182
|
+
var _a, _b, _c;
|
|
183
|
+
const successResponse = (_a = endpoint.responses.find((x) => x.statusCode === 200)) !== null && _a !== void 0 ? _a : endpoint.responses.find((x) => x.statusCode && x.statusCode > 200 && x.statusCode < 300);
|
|
184
|
+
return (_c = (_b = successResponse === null || successResponse === void 0 ? void 0 : successResponse.contentOptions) === null || _b === void 0 ? void 0 : _b.find((x) => x.schema !== undefined)) === null || _c === void 0 ? void 0 : _c.schema;
|
|
185
|
+
}
|
|
240
186
|
}
|
|
241
187
|
exports.DefaultTypeScriptFetchClientGenerator = DefaultTypeScriptFetchClientGenerator;
|
|
@@ -6,6 +6,7 @@ const fs_extra_1 = require("fs-extra");
|
|
|
6
6
|
const core_1 = require("@goast/core");
|
|
7
7
|
const fetch_client_generator_1 = require("./fetch-client-generator");
|
|
8
8
|
const models_1 = require("./models");
|
|
9
|
+
const refs_1 = require("./refs");
|
|
9
10
|
const file_builder_1 = require("../../../file-builder");
|
|
10
11
|
const import_collection_1 = require("../../../import-collection");
|
|
11
12
|
class TypeScriptClientsGenerator extends core_1.OpenApiServicesGenerationProviderBase {
|
|
@@ -22,7 +23,10 @@ class TypeScriptClientsGenerator extends core_1.OpenApiServicesGenerationProvide
|
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
25
|
buildContext(context, config) {
|
|
25
|
-
|
|
26
|
+
const providerContext = this.getProviderContext(context, config, models_1.defaultTypeScriptFetchClientsGeneratorConfig);
|
|
27
|
+
return Object.assign(providerContext, {
|
|
28
|
+
refs: (0, refs_1.getReferenceFactories)(providerContext.config),
|
|
29
|
+
});
|
|
26
30
|
}
|
|
27
31
|
onGenerate(ctx) {
|
|
28
32
|
this.copyUtilsFiles(ctx);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getReferenceFactories = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const ast_1 = require("../../../ast");
|
|
6
|
+
// See packages/typescript/assets/client/fetch for reference
|
|
7
|
+
function getReferenceFactories(options) {
|
|
8
|
+
const utilsDirPath = (0, path_1.resolve)(options.outputDir, options.utilsDirPath);
|
|
9
|
+
const fetchClientUtilsPath = (0, path_1.resolve)(utilsDirPath, 'fetch-client.utils.ts');
|
|
10
|
+
return {
|
|
11
|
+
// fetch-client.utils.ts
|
|
12
|
+
typedResponse: ast_1.ts.reference.genericFactory('TypedResponse', fetchClientUtilsPath),
|
|
13
|
+
fetchClientOptions: ast_1.ts.reference.factory('FetchClientOptions', fetchClientUtilsPath),
|
|
14
|
+
urlBuilder: ast_1.ts.reference.factory('UrlBuilder', fetchClientUtilsPath),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
exports.getReferenceFactories = getReferenceFactories;
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ImportExportCollection = void 0;
|
|
3
|
+
exports.ImportExportCollection = exports.defaultImportExportCollectionOptions = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
5
|
const core_1 = require("@goast/core");
|
|
6
|
+
const config_1 = require("./config");
|
|
6
7
|
const utils_1 = require("./utils");
|
|
8
|
+
exports.defaultImportExportCollectionOptions = {
|
|
9
|
+
importModuleTransformer: config_1.defaultTypeScriptGeneratorConfig.importModuleTransformer,
|
|
10
|
+
useSingleQuotes: config_1.defaultTypeScriptGeneratorConfig.useSingleQuotes,
|
|
11
|
+
};
|
|
7
12
|
class ImportExportCollection {
|
|
8
|
-
constructor() {
|
|
13
|
+
constructor(options) {
|
|
9
14
|
this._imports = new Map();
|
|
10
15
|
this._exports = new Map();
|
|
16
|
+
this._options = Object.assign(Object.assign({}, exports.defaultImportExportCollectionOptions), options);
|
|
11
17
|
}
|
|
12
18
|
get hasImports() {
|
|
13
19
|
return this._imports.size > 0;
|
|
@@ -46,34 +52,26 @@ class ImportExportCollection {
|
|
|
46
52
|
this._imports.clear();
|
|
47
53
|
this._exports.clear();
|
|
48
54
|
}
|
|
49
|
-
toString(options
|
|
55
|
+
toString(options) {
|
|
50
56
|
const builder = new core_1.StringBuilder(options);
|
|
51
|
-
this.writeTo(builder
|
|
57
|
+
this.writeTo(builder);
|
|
52
58
|
return builder.toString();
|
|
53
59
|
}
|
|
54
|
-
writeTo(builder
|
|
55
|
-
|
|
56
|
-
filePath = builder.filePath;
|
|
57
|
-
}
|
|
58
|
-
if (!importModuleTransformer) {
|
|
59
|
-
importModuleTransformer = builder.options.importModuleTransformer;
|
|
60
|
-
if (!importModuleTransformer) {
|
|
61
|
-
throw new Error('importModuleTransformer is required');
|
|
62
|
-
}
|
|
63
|
-
}
|
|
60
|
+
writeTo(builder) {
|
|
61
|
+
const { filePath, importModuleTransformer, useSingleQuotes } = this._options;
|
|
64
62
|
if (this._imports.size > 0) {
|
|
65
63
|
const sortedImports = this.sortAndResolve(this._imports, filePath, importModuleTransformer);
|
|
66
|
-
this.writeImportsExports(builder, 'import', sortedImports);
|
|
64
|
+
this.writeImportsExports(builder, 'import', sortedImports, { useSingleQuotes });
|
|
67
65
|
}
|
|
68
66
|
if (this._imports.size > 0 && this._exports.size > 0) {
|
|
69
67
|
builder.appendLine();
|
|
70
68
|
}
|
|
71
69
|
if (this._exports.size > 0) {
|
|
72
70
|
const sortedExports = this.sortAndResolve(this._exports, filePath, importModuleTransformer);
|
|
73
|
-
this.writeImportsExports(builder, 'export', sortedExports);
|
|
71
|
+
this.writeImportsExports(builder, 'export', sortedExports, { useSingleQuotes });
|
|
74
72
|
}
|
|
75
73
|
}
|
|
76
|
-
writeImportsExports(builder, keyword, data) {
|
|
74
|
+
writeImportsExports(builder, keyword, data, options) {
|
|
77
75
|
const moduleData = [];
|
|
78
76
|
const fileData = [];
|
|
79
77
|
for (const x of data) {
|
|
@@ -84,14 +82,15 @@ class ImportExportCollection {
|
|
|
84
82
|
fileData.push(x);
|
|
85
83
|
}
|
|
86
84
|
}
|
|
85
|
+
const quote = options.useSingleQuotes ? "'" : '"';
|
|
87
86
|
for (const [fromModule, _, names] of moduleData) {
|
|
88
|
-
builder.appendLine(`${keyword} { ${names.join(', ')} } from
|
|
87
|
+
builder.appendLine(`${keyword} { ${names.join(', ')} } from ${quote}${fromModule}${quote};`);
|
|
89
88
|
}
|
|
90
89
|
if (moduleData.length > 0 && fileData.length > 0) {
|
|
91
90
|
builder.appendLine();
|
|
92
91
|
}
|
|
93
92
|
for (const [fromModule, _, names] of fileData) {
|
|
94
|
-
builder.appendLine(`${keyword} { ${names.join(', ')} } from
|
|
93
|
+
builder.appendLine(`${keyword} { ${names.join(', ')} } from ${quote}${fromModule}${quote};`);
|
|
95
94
|
}
|
|
96
95
|
}
|
|
97
96
|
sortAndResolve(data, filePath, importModuleTransformer) {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export { TsNode as Node } from './node';
|
|
2
|
+
export { TsArgument as Argument, tsArgument as argument } from './nodes/argument';
|
|
3
|
+
export { TsArrayType as ArrayType, tsArrayType as arrayType } from './nodes/array-type';
|
|
4
|
+
export { TsArrowFunction as ArrowFunction, tsArrowFunction as arrowFunction } from './nodes/arrow-function';
|
|
5
|
+
export { TsCall as Call, tsCall as call } from './nodes/call';
|
|
6
|
+
export { TsClass as Class, tsClass as class } from './nodes/class';
|
|
7
|
+
export { TsConstructor as Constructor, tsConstructor as constructor } from './nodes/constructor';
|
|
8
|
+
export { TsConstructorParameter as ConstructorParameter, tsConstructorParameter as constructorParameter, } from './nodes/constructor-parameter';
|
|
9
|
+
export { TsDecorator as Decorator, tsDecorator as decorator } from './nodes/decorator';
|
|
10
|
+
export { TsDoc as Doc, tsDoc as doc } from './nodes/doc';
|
|
11
|
+
export { TsDocTag as DocTag, tsDocTag as docTag } from './nodes/doc-tag';
|
|
12
|
+
export { TsEnum as Enum, tsEnum as enum } from './nodes/enum';
|
|
13
|
+
export { TsEnumValue as EnumValue, tsEnumValue as enumValue } from './nodes/enum-value';
|
|
14
|
+
export { TsExport as Export, tsExport as export } from './nodes/export';
|
|
15
|
+
export { TsFunction as Function, tsFunction as function } from './nodes/function';
|
|
16
|
+
export { TsFunctionType as FunctionType, tsFunctionType as functionType } from './nodes/function-type';
|
|
17
|
+
export { TsGenericParameter as GenericParameter, tsGenericParameter as genericParameter, } from './nodes/generic-parameter';
|
|
18
|
+
export { TsIndexer as Indexer, tsIndexer as indexer } from './nodes/indexer';
|
|
19
|
+
export { TsInterface as Interface, tsInterface as interface } from './nodes/interface';
|
|
20
|
+
export { TsIntersectionType as IntersectionType, tsIntersectionType as intersectionType, } from './nodes/intersection-type';
|
|
21
|
+
export { TsLookupType as LookupType, tsLookupType as lookupType } from './nodes/lookup-type';
|
|
22
|
+
export { TsMethod as Method, tsMethod as method } from './nodes/method';
|
|
23
|
+
export { TsObject as Object, tsObject as object } from './nodes/object';
|
|
24
|
+
export { TsObjectType as ObjectType, tsObjectType as objectType } from './nodes/object-type';
|
|
25
|
+
export { TsParameter as Parameter, tsParameter as parameter } from './nodes/parameter';
|
|
26
|
+
export { TsProperty as Property, tsProperty as property } from './nodes/property';
|
|
27
|
+
export { TsReference as Reference, tsReference as reference } from './nodes/reference';
|
|
28
|
+
export { TsString as String, tsString as string } from './nodes/string';
|
|
29
|
+
export { TsTuple as Tuple, tsTuple as tuple } from './nodes/tuple';
|
|
30
|
+
export { TsTypeAlias as TypeAlias, tsTypeAlias as typeAlias } from './nodes/type-alias';
|
|
31
|
+
export { TsUnionType as UnionType, tsUnionType as unionType } from './nodes/union-type';
|
|
32
|
+
export { TsVariable as Variable, tsVariable as variable } from './nodes/variable';
|
|
33
|
+
export { TsTypeof as Typeof, tsTypeof as typeof } from './nodes/typeof';
|
|
34
|
+
import * as refs_1 from './references';
|
|
35
|
+
export { refs_1 as refs };
|
|
36
|
+
export { getTypeScriptBuilderOptions as getBuilderOptions } from './utils/get-type-script-builder-options';
|
|
37
|
+
export { toTsNode as toNode } from './utils/to-ts-node';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TsNode } from '../node';
|
|
2
|
+
import { writeTsNode } from '../utils/write-ts-nodes';
|
|
3
|
+
import { writeTsParameters } from '../utils/write-ts-parameters';
|
|
4
|
+
export class TsArgument extends TsNode {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super(options);
|
|
7
|
+
this.value = options.value;
|
|
8
|
+
}
|
|
9
|
+
onWrite(builder) {
|
|
10
|
+
writeTsNode(builder, this.value);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const createArgument = (value, options) => new TsArgument(Object.assign(Object.assign({}, options), { value }));
|
|
14
|
+
export const tsArgument = Object.assign(createArgument, {
|
|
15
|
+
write: writeTsParameters,
|
|
16
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { TsNode } from '../node';
|
|
2
|
+
import { writeTsNode, writeTsNodes } from '../utils/write-ts-nodes';
|
|
3
|
+
export class TsArrayType extends TsNode {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
var _a;
|
|
6
|
+
super(options);
|
|
7
|
+
this.readonly = (_a = options.readonly) !== null && _a !== void 0 ? _a : false;
|
|
8
|
+
this.type = options.type;
|
|
9
|
+
}
|
|
10
|
+
onWrite(builder) {
|
|
11
|
+
if (this.readonly)
|
|
12
|
+
builder.append('readonly ');
|
|
13
|
+
builder
|
|
14
|
+
.append(this.inject.beforeType)
|
|
15
|
+
.parenthesize('()', (b) => writeTsNode(b, this.type))
|
|
16
|
+
.append(this.inject.afterType);
|
|
17
|
+
builder.append('[]');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const createArrayType = (type, options) => new TsArrayType(Object.assign(Object.assign({}, options), { type }));
|
|
21
|
+
export const tsArrayType = Object.assign(createArrayType, {
|
|
22
|
+
write: writeTsNodes,
|
|
23
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { notNullish } from '@goast/core';
|
|
2
|
+
import { tsGenericParameter } from './generic-parameter';
|
|
3
|
+
import { tsParameter } from './parameter';
|
|
4
|
+
import { TsNode } from '../node';
|
|
5
|
+
import { writeTsNode, writeTsNodes } from '../utils/write-ts-nodes';
|
|
6
|
+
export class TsArrowFunction extends TsNode {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
9
|
+
super(options);
|
|
10
|
+
this.async = (_a = options.async) !== null && _a !== void 0 ? _a : false;
|
|
11
|
+
this.generics = (_c = (_b = options.generics) === null || _b === void 0 ? void 0 : _b.filter(notNullish)) !== null && _c !== void 0 ? _c : [];
|
|
12
|
+
this.parameters = (_e = (_d = options.parameters) === null || _d === void 0 ? void 0 : _d.filter(notNullish)) !== null && _e !== void 0 ? _e : [];
|
|
13
|
+
this.returnType = (_f = options.returnType) !== null && _f !== void 0 ? _f : null;
|
|
14
|
+
this.singleExpression = (_g = options.singleExpression) !== null && _g !== void 0 ? _g : false;
|
|
15
|
+
this.body = (_h = options.body) !== null && _h !== void 0 ? _h : null;
|
|
16
|
+
}
|
|
17
|
+
onWrite(builder) {
|
|
18
|
+
if (this.async) {
|
|
19
|
+
builder.append('async ');
|
|
20
|
+
}
|
|
21
|
+
builder.append(this.inject.beforeGenerics);
|
|
22
|
+
tsGenericParameter.write(builder, this.generics);
|
|
23
|
+
builder.append(this.inject.afterGenerics);
|
|
24
|
+
builder.append(this.inject.beforeParams);
|
|
25
|
+
tsParameter.write(builder, this.parameters);
|
|
26
|
+
builder.append(this.inject.afterParams);
|
|
27
|
+
if (this.returnType) {
|
|
28
|
+
builder.append(': ');
|
|
29
|
+
builder.append(this.inject.beforeReturnType);
|
|
30
|
+
writeTsNode(builder, this.returnType);
|
|
31
|
+
builder.append(this.inject.afterReturnType);
|
|
32
|
+
}
|
|
33
|
+
builder.append(' => ');
|
|
34
|
+
builder.append(this.inject.beforeBody);
|
|
35
|
+
if (this.singleExpression && this.body) {
|
|
36
|
+
builder.append(this.body);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
builder.parenthesize('{}', this.body, { multiline: !!this.body });
|
|
40
|
+
}
|
|
41
|
+
builder.append(this.inject.afterBody);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const createArrowFunction = (options) => new TsArrowFunction(options !== null && options !== void 0 ? options : {});
|
|
45
|
+
export const tsArrowFunction = Object.assign(createArrowFunction, {
|
|
46
|
+
write: writeTsNodes,
|
|
47
|
+
});
|