@goast/typescript 0.0.1
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/README.md +11 -0
- package/assets/client/fetch/types.ts +9 -0
- package/assets/client/fetch/url-builder.ts +36 -0
- package/assets/service/angular/api-configuration.ts +18 -0
- package/assets/service/angular/base-service.ts +29 -0
- package/assets/service/angular/request-builder.ts +368 -0
- package/assets/service/angular/response-handlers.ts +127 -0
- package/assets/service/angular/strict-http-response.ts +8 -0
- package/cjs/index.js +9 -0
- package/cjs/lib/common-results.js +2 -0
- package/cjs/lib/config.js +15 -0
- package/cjs/lib/file-builder.js +244 -0
- package/cjs/lib/generators/file-generator.js +26 -0
- package/cjs/lib/generators/index.js +7 -0
- package/cjs/lib/generators/models/index.js +6 -0
- package/cjs/lib/generators/models/model-generator.js +317 -0
- package/cjs/lib/generators/models/models-generator.js +67 -0
- package/cjs/lib/generators/models/models.js +6 -0
- package/cjs/lib/generators/services/angular-services/angular-service-generator.js +360 -0
- package/cjs/lib/generators/services/angular-services/angular-services-generator.js +122 -0
- package/cjs/lib/generators/services/angular-services/index.js +6 -0
- package/cjs/lib/generators/services/angular-services/models.js +5 -0
- package/cjs/lib/generators/services/fetch-clients/fetch-client-generator.js +240 -0
- package/cjs/lib/generators/services/fetch-clients/fetch-clients-generator.js +83 -0
- package/cjs/lib/generators/services/fetch-clients/index.js +6 -0
- package/cjs/lib/generators/services/fetch-clients/models.js +5 -0
- package/cjs/lib/import-collection.js +116 -0
- package/cjs/lib/utils.js +56 -0
- package/cjs/package.json +1 -0
- package/esm/index.js +6 -0
- package/esm/lib/common-results.js +1 -0
- package/esm/lib/config.js +12 -0
- package/esm/lib/file-builder.js +240 -0
- package/esm/lib/generators/file-generator.js +22 -0
- package/esm/lib/generators/index.js +4 -0
- package/esm/lib/generators/models/index.js +3 -0
- package/esm/lib/generators/models/model-generator.js +313 -0
- package/esm/lib/generators/models/models-generator.js +63 -0
- package/esm/lib/generators/models/models.js +3 -0
- package/esm/lib/generators/services/angular-services/angular-service-generator.js +356 -0
- package/esm/lib/generators/services/angular-services/angular-services-generator.js +118 -0
- package/esm/lib/generators/services/angular-services/index.js +3 -0
- package/esm/lib/generators/services/angular-services/models.js +2 -0
- package/esm/lib/generators/services/fetch-clients/fetch-client-generator.js +236 -0
- package/esm/lib/generators/services/fetch-clients/fetch-clients-generator.js +79 -0
- package/esm/lib/generators/services/fetch-clients/index.js +3 -0
- package/esm/lib/generators/services/fetch-clients/models.js +2 -0
- package/esm/lib/import-collection.js +112 -0
- package/esm/lib/utils.js +48 -0
- package/package.json +29 -0
- package/types/index.d.ts +6 -0
- package/types/lib/common-results.d.ts +11 -0
- package/types/lib/config.d.ts +13 -0
- package/types/lib/file-builder.d.ts +110 -0
- package/types/lib/generators/file-generator.d.ts +11 -0
- package/types/lib/generators/index.d.ts +4 -0
- package/types/lib/generators/models/index.d.ts +3 -0
- package/types/lib/generators/models/model-generator.d.ts +40 -0
- package/types/lib/generators/models/models-generator.d.ts +23 -0
- package/types/lib/generators/models/models.d.ts +29 -0
- package/types/lib/generators/services/angular-services/angular-service-generator.d.ts +53 -0
- package/types/lib/generators/services/angular-services/angular-services-generator.d.ts +29 -0
- package/types/lib/generators/services/angular-services/index.d.ts +3 -0
- package/types/lib/generators/services/angular-services/models.d.ts +41 -0
- package/types/lib/generators/services/fetch-clients/fetch-client-generator.d.ts +43 -0
- package/types/lib/generators/services/fetch-clients/fetch-clients-generator.d.ts +24 -0
- package/types/lib/generators/services/fetch-clients/index.d.ts +3 -0
- package/types/lib/generators/services/fetch-clients/models.d.ts +34 -0
- package/types/lib/import-collection.d.ts +21 -0
- package/types/lib/utils.d.ts +6 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultTypeScriptFetchClientGenerator = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const fs_extra_1 = require("fs-extra");
|
|
6
|
+
const core_1 = require("@goast/core");
|
|
7
|
+
const file_builder_1 = require("../../../file-builder");
|
|
8
|
+
const file_generator_1 = require("../../file-generator");
|
|
9
|
+
class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptFileGenerator {
|
|
10
|
+
generate(ctx) {
|
|
11
|
+
const result = {};
|
|
12
|
+
let builder;
|
|
13
|
+
if (this.shouldGenerateInterface(ctx)) {
|
|
14
|
+
const filePath = this.getInterfaceFilePath(ctx);
|
|
15
|
+
const name = this.getInterfaceName(ctx);
|
|
16
|
+
console.log(`Generating interface ${name} in ${filePath}...`);
|
|
17
|
+
(0, fs_extra_1.ensureDirSync)((0, path_1.dirname)(filePath));
|
|
18
|
+
builder = new file_builder_1.TypeScriptFileBuilder(filePath, ctx.config);
|
|
19
|
+
this.generateInterface(ctx, builder);
|
|
20
|
+
(0, fs_extra_1.writeFileSync)(filePath, builder.toString());
|
|
21
|
+
result.interface = { filePath, component: name, imports: [{ kind: 'file', name, modulePath: filePath }] };
|
|
22
|
+
}
|
|
23
|
+
if (this.shouldGenerateClass(ctx)) {
|
|
24
|
+
const filePath = this.getClassFilePath(ctx);
|
|
25
|
+
const name = this.getClassName(ctx);
|
|
26
|
+
console.log(`Generating class ${name} in ${filePath}...`);
|
|
27
|
+
(0, fs_extra_1.ensureDirSync)((0, path_1.dirname)(filePath));
|
|
28
|
+
if (!builder || builder.filePath !== filePath) {
|
|
29
|
+
builder = new file_builder_1.TypeScriptFileBuilder(filePath, ctx.config);
|
|
30
|
+
}
|
|
31
|
+
this.generateDefaultOptions(ctx, builder);
|
|
32
|
+
this.generateClass(ctx, builder);
|
|
33
|
+
(0, fs_extra_1.writeFileSync)(filePath, builder.toString());
|
|
34
|
+
result.class = { filePath, component: name, imports: [{ kind: 'file', name, modulePath: filePath }] };
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
generateInterface(ctx, builder) {
|
|
39
|
+
builder
|
|
40
|
+
.ensurePreviousLineEmpty()
|
|
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
|
+
}
|
|
52
|
+
}
|
|
53
|
+
generateInterfaceSignature(ctx, builder) {
|
|
54
|
+
builder.append('export interface ').append(this.getInterfaceName(ctx));
|
|
55
|
+
}
|
|
56
|
+
generateInterfaceContent(ctx, builder) {
|
|
57
|
+
builder.forEach(ctx.service.endpoints, (builder, endpoint) => this.generateInterfaceServiceMethod(ctx, builder, endpoint));
|
|
58
|
+
}
|
|
59
|
+
generateInterfaceServiceMethod(ctx, builder, endpoint) {
|
|
60
|
+
builder
|
|
61
|
+
.ensurePreviousLineEmpty()
|
|
62
|
+
.append((builder) => this.generateServiceMethodDocumentation(ctx, builder, endpoint))
|
|
63
|
+
.ensureCurrentLineEmpty()
|
|
64
|
+
.append((builder) => this.generateServiceMethodSignature(ctx, builder, endpoint))
|
|
65
|
+
.appendLine(';');
|
|
66
|
+
}
|
|
67
|
+
generateDefaultOptions(ctx, builder) {
|
|
68
|
+
const baseUrl = ctx.service.endpoints
|
|
69
|
+
.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
|
+
.find((x) => !!x);
|
|
71
|
+
builder
|
|
72
|
+
.ensurePreviousLineEmpty()
|
|
73
|
+
.addImport('FetchClientOptions', this.getUtilPath(ctx, 'types.ts'))
|
|
74
|
+
.append('export const ', this.getDefaultOptionsConstantName(ctx), ': FetchClientOptions = ')
|
|
75
|
+
.parenthesize('{}', (builder) => builder.appendLineIf((0, core_1.notNullish)(baseUrl), 'baseUrl: ', this.toStringLiteral(ctx, baseUrl !== null && baseUrl !== void 0 ? baseUrl : ''), ','), { indent: true, multiline: true });
|
|
76
|
+
}
|
|
77
|
+
generateClass(ctx, builder) {
|
|
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.addImport(this.getInterfaceName(ctx), this.getInterfaceFilePath(ctx)))
|
|
97
|
+
.append(' implements ', this.getInterfaceName(ctx));
|
|
98
|
+
}
|
|
99
|
+
generateClassContent(ctx, builder) {
|
|
100
|
+
builder
|
|
101
|
+
.append((builder) => this.generateClassFields(ctx, builder))
|
|
102
|
+
.append((builder) => this.generateClassConstructor(ctx, builder))
|
|
103
|
+
.forEach(ctx.service.endpoints, (builder, endpoint) => this.generateClassServiceMethod(ctx, builder, endpoint));
|
|
104
|
+
}
|
|
105
|
+
generateClassFields(ctx, builder) {
|
|
106
|
+
builder.ensureCurrentLineEmpty().appendLine('private readonly _options: FetchClientOptions;');
|
|
107
|
+
}
|
|
108
|
+
generateClassConstructor(ctx, builder) {
|
|
109
|
+
builder
|
|
110
|
+
.ensurePreviousLineEmpty()
|
|
111
|
+
.append('constructor(options?: FetchClientOptions)')
|
|
112
|
+
.parenthesize('{}', (builder) => this.generateClassConstructorContent(ctx, builder), {
|
|
113
|
+
indent: true,
|
|
114
|
+
multiline: true,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
generateClassConstructorContent(ctx, builder) {
|
|
118
|
+
builder.appendLine('this._options = { ...', this.getDefaultOptionsConstantName(ctx), ', ...options };');
|
|
119
|
+
}
|
|
120
|
+
generateClassServiceMethod(ctx, builder, endpoint) {
|
|
121
|
+
builder
|
|
122
|
+
.ensurePreviousLineEmpty()
|
|
123
|
+
.append((builder) => this.generateServiceMethodDocumentation(ctx, builder, endpoint))
|
|
124
|
+
.ensureCurrentLineEmpty()
|
|
125
|
+
.append((builder) => this.generateServiceMethodSignature(ctx, builder, endpoint))
|
|
126
|
+
.append(' ')
|
|
127
|
+
.parenthesize('{}', (builder) => this.generateServiceMethodContent(ctx, builder, endpoint), {
|
|
128
|
+
indent: true,
|
|
129
|
+
multiline: true,
|
|
130
|
+
})
|
|
131
|
+
.appendLine();
|
|
132
|
+
}
|
|
133
|
+
generateServiceMethodDocumentation(ctx, builder, endpoint) {
|
|
134
|
+
var _a;
|
|
135
|
+
const hasParams = endpoint.parameters.some((p) => p.target === 'path' || p.target === 'query') ||
|
|
136
|
+
(!!endpoint.requestBody && ((_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content.length) > 0);
|
|
137
|
+
builder.appendComment('/***/', (builder) => {
|
|
138
|
+
var _a;
|
|
139
|
+
return builder
|
|
140
|
+
.appendLine((_a = endpoint.description) !== null && _a !== void 0 ? _a : '[No description was provided by the API]')
|
|
141
|
+
.appendLine(`@see ${(0, core_1.getEndpointUrlPreview)(endpoint)}`)
|
|
142
|
+
.appendLineIf(hasParams, `@param params Parameters for the endpoint.`)
|
|
143
|
+
.append(`@returns The response of the call to the endpoint.`);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
generateServiceMethodSignature(ctx, builder, endpoint) {
|
|
147
|
+
builder
|
|
148
|
+
.append((0, core_1.toCasing)(endpoint.name, ctx.config.methodCasing))
|
|
149
|
+
.parenthesize('()', (builder) => this.generateServiceMethodParameters(ctx, builder, endpoint))
|
|
150
|
+
.append(': ')
|
|
151
|
+
.append((builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint));
|
|
152
|
+
}
|
|
153
|
+
generateServiceMethodParameters(ctx, builder, endpoint) {
|
|
154
|
+
var _a, _b, _c;
|
|
155
|
+
const params = endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query');
|
|
156
|
+
const bodySchemaId = (_c = (_b = (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) === null || _b === void 0 ? void 0 : _b.schema) === null || _c === void 0 ? void 0 : _c.id;
|
|
157
|
+
builder
|
|
158
|
+
.appendLineIf(params.length > 0 && !!bodySchemaId)
|
|
159
|
+
.if(params.length > 0, (builder) => builder
|
|
160
|
+
.append('params: ')
|
|
161
|
+
.parenthesize('{}', (builder) => builder.forEach(params, (builder, parameter) => {
|
|
162
|
+
var _a;
|
|
163
|
+
return builder.appendLine(`${this.toPropertyName(ctx, parameter.name)}: ${this.getTypeName(ctx, builder, (_a = parameter.schema) === null || _a === void 0 ? void 0 : _a.id)};`);
|
|
164
|
+
}), { indent: true, multiline: true }))
|
|
165
|
+
.appendLineIf(params.length > 0 && !!bodySchemaId, ',')
|
|
166
|
+
.appendIf(!!bodySchemaId, (builder) => builder.append(`body: ${this.getTypeName(ctx, builder, bodySchemaId)}`))
|
|
167
|
+
.appendLineIf(params.length > 0 && !!bodySchemaId);
|
|
168
|
+
}
|
|
169
|
+
generateServiceMethodReturnValue(ctx, builder, endpoint) {
|
|
170
|
+
var _a, _b, _c;
|
|
171
|
+
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);
|
|
172
|
+
const schema = (_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;
|
|
173
|
+
builder.append('Promise').parenthesize('<>', (builder) => this.generateTypedResponse(ctx, builder, schema));
|
|
174
|
+
}
|
|
175
|
+
generateTypedResponse(ctx, builder, schema) {
|
|
176
|
+
builder
|
|
177
|
+
.addImport('TypedResponse', this.getUtilPath(ctx, 'types.ts'))
|
|
178
|
+
.append('TypedResponse')
|
|
179
|
+
.parenthesize('<>', (builder) => builder.append(this.getTypeName(ctx, builder, schema === null || schema === void 0 ? void 0 : schema.id)));
|
|
180
|
+
}
|
|
181
|
+
generateServiceMethodContent(ctx, builder, endpoint) {
|
|
182
|
+
builder
|
|
183
|
+
.addImport('UrlBuilder', this.getUtilPath(ctx, 'url-builder.ts'))
|
|
184
|
+
.appendLine('const url = new UrlBuilder(this._options.baseUrl)')
|
|
185
|
+
.indent((builder) => builder
|
|
186
|
+
.appendLine(`.withPath(${this.toStringLiteral(ctx, endpoint.path)})`)
|
|
187
|
+
.forEach(endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query'), (builder, parameter) => builder
|
|
188
|
+
.appendIf(parameter.target === 'path', `.withPathParam`)
|
|
189
|
+
.appendIf(parameter.target === 'query', `.withQueryParam`)
|
|
190
|
+
.parenthesize('()', `${this.toStringLiteral(ctx, parameter.name)}, params.${this.toPropertyName(ctx, parameter.name)}`)
|
|
191
|
+
.appendLine())
|
|
192
|
+
.appendLine('.build();'))
|
|
193
|
+
.append('return (this._options.fetch ?? fetch)')
|
|
194
|
+
.parenthesize('()', (builder) => builder.append('url, ').parenthesize('{}', (builder) => {
|
|
195
|
+
var _a, _b;
|
|
196
|
+
return builder
|
|
197
|
+
.appendLine('method: ', this.toStringLiteral(ctx, (0, core_1.toCasing)(endpoint.method, 'all-upper')), ',')
|
|
198
|
+
.appendLine('headers: this._options.headers,')
|
|
199
|
+
.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),');
|
|
200
|
+
}, { indent: true, multiline: true }), { indent: false })
|
|
201
|
+
.appendLine(' as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
|
|
202
|
+
}
|
|
203
|
+
getTypeName(ctx, builder, schemaId) {
|
|
204
|
+
if (!schemaId) {
|
|
205
|
+
return this.getAnyType(ctx);
|
|
206
|
+
}
|
|
207
|
+
const modelInfo = ctx.input.models[schemaId];
|
|
208
|
+
if (!modelInfo) {
|
|
209
|
+
return this.getAnyType(ctx);
|
|
210
|
+
}
|
|
211
|
+
modelInfo.imports.forEach((x) => builder.addImport(x.name, x.modulePath));
|
|
212
|
+
return modelInfo.component;
|
|
213
|
+
}
|
|
214
|
+
shouldGenerateInterface(ctx) {
|
|
215
|
+
return ctx.config.clientFileKind === 'interface' || ctx.config.clientFileKind === 'class-and-interface';
|
|
216
|
+
}
|
|
217
|
+
shouldGenerateClass(ctx) {
|
|
218
|
+
return ctx.config.clientFileKind === 'class' || ctx.config.clientFileKind === 'class-and-interface';
|
|
219
|
+
}
|
|
220
|
+
getInterfaceName(ctx) {
|
|
221
|
+
return (0, core_1.toCasing)(ctx.service.name, ctx.config.interfaceNameCasing);
|
|
222
|
+
}
|
|
223
|
+
getClassName(ctx) {
|
|
224
|
+
return (0, core_1.toCasing)(ctx.service.name, ctx.config.classNameCasing);
|
|
225
|
+
}
|
|
226
|
+
getDefaultOptionsConstantName(ctx) {
|
|
227
|
+
return (0, core_1.toCasing)(this.getClassName(ctx) + '-default-options', ctx.config.constantCasing);
|
|
228
|
+
}
|
|
229
|
+
getInterfaceFilePath(ctx) {
|
|
230
|
+
var _a, _b;
|
|
231
|
+
return (0, path_1.resolve)(ctx.config.outputDir, (_a = ctx.config.clientInterfaceDirPath) !== null && _a !== void 0 ? _a : ctx.config.clientDirPath, `${(0, core_1.toCasing)(ctx.service.name, (_b = ctx.config.interfaceFileNameCasing) !== null && _b !== void 0 ? _b : ctx.config.fileNameCasing)}.ts`);
|
|
232
|
+
}
|
|
233
|
+
getClassFilePath(ctx) {
|
|
234
|
+
return (0, path_1.resolve)(ctx.config.outputDir, ctx.config.clientDirPath, `${(0, core_1.toCasing)(ctx.service.name, ctx.config.fileNameCasing)}.ts`);
|
|
235
|
+
}
|
|
236
|
+
getUtilPath(ctx, fileName) {
|
|
237
|
+
return (0, path_1.resolve)(ctx.config.outputDir, ctx.config.utilsDirPath, fileName);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.DefaultTypeScriptFetchClientGenerator = DefaultTypeScriptFetchClientGenerator;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypeScriptClientsGenerator = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const fs_extra_1 = require("fs-extra");
|
|
6
|
+
const core_1 = require("@goast/core");
|
|
7
|
+
const fetch_client_generator_1 = require("./fetch-client-generator");
|
|
8
|
+
const models_1 = require("./models");
|
|
9
|
+
const file_builder_1 = require("../../../file-builder");
|
|
10
|
+
const import_collection_1 = require("../../../import-collection");
|
|
11
|
+
class TypeScriptClientsGenerator extends core_1.OpenApiServicesGenerationProviderBase {
|
|
12
|
+
constructor(clientGeneratorFactory) {
|
|
13
|
+
super();
|
|
14
|
+
this._clientGeneratorFactory =
|
|
15
|
+
clientGeneratorFactory !== null && clientGeneratorFactory !== void 0 ? clientGeneratorFactory : core_1.Factory.fromValue(new fetch_client_generator_1.DefaultTypeScriptFetchClientGenerator());
|
|
16
|
+
}
|
|
17
|
+
initResult() {
|
|
18
|
+
return {
|
|
19
|
+
clients: {},
|
|
20
|
+
clientIndexFilePath: undefined,
|
|
21
|
+
clientInterfaceIndexFilePath: undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
buildContext(context, config) {
|
|
25
|
+
return this.getProviderContext(context, config, models_1.defaultTypeScriptFetchClientsGeneratorConfig);
|
|
26
|
+
}
|
|
27
|
+
onGenerate(ctx) {
|
|
28
|
+
this.copyUtilsFiles(ctx);
|
|
29
|
+
const output = super.onGenerate(ctx);
|
|
30
|
+
output.clientIndexFilePath = this.generateIndexFile(ctx);
|
|
31
|
+
return output;
|
|
32
|
+
}
|
|
33
|
+
generateService(ctx, service) {
|
|
34
|
+
const clientGenerator = this._clientGeneratorFactory.create();
|
|
35
|
+
return clientGenerator.generate(Object.assign(Object.assign({}, ctx), { service }));
|
|
36
|
+
}
|
|
37
|
+
addServiceResult(ctx, service, result) {
|
|
38
|
+
ctx.output.clients[service.id] = result;
|
|
39
|
+
}
|
|
40
|
+
generateIndexFile(ctx) {
|
|
41
|
+
if (!this.shouldGenerateIndexFile(ctx)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const filePath = this.getIndexFilePath(ctx);
|
|
45
|
+
console.log(`Generating index file to ${filePath}...`);
|
|
46
|
+
(0, fs_extra_1.ensureDirSync)((0, path_1.dirname)(filePath));
|
|
47
|
+
const builder = new file_builder_1.TypeScriptFileBuilder(filePath, ctx.config);
|
|
48
|
+
this.generateIndexFileContent(ctx, builder);
|
|
49
|
+
(0, fs_extra_1.writeFileSync)(filePath, builder.toString());
|
|
50
|
+
return filePath;
|
|
51
|
+
}
|
|
52
|
+
getIndexFilePath(ctx) {
|
|
53
|
+
var _a;
|
|
54
|
+
return (0, path_1.resolve)(ctx.config.outputDir, (_a = ctx.config.indexFilePath) !== null && _a !== void 0 ? _a : 'clients.ts');
|
|
55
|
+
}
|
|
56
|
+
shouldGenerateIndexFile(ctx) {
|
|
57
|
+
return ctx.config.indexFilePath !== null;
|
|
58
|
+
}
|
|
59
|
+
generateIndexFileContent(ctx, builder) {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
const exports = new import_collection_1.ImportExportCollection();
|
|
62
|
+
for (const clientId in ctx.output.clients) {
|
|
63
|
+
const client = ctx.output.clients[clientId];
|
|
64
|
+
if ((_a = client.class) === null || _a === void 0 ? void 0 : _a.filePath) {
|
|
65
|
+
exports.addExport(client.class.component, client.class.filePath);
|
|
66
|
+
}
|
|
67
|
+
if ((_b = client.interface) === null || _b === void 0 ? void 0 : _b.filePath) {
|
|
68
|
+
exports.addExport(client.interface.component, client.interface.filePath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.writeTo(builder);
|
|
72
|
+
}
|
|
73
|
+
copyUtilsFiles(ctx) {
|
|
74
|
+
const sourceDir = (0, path_1.resolve)((0, path_1.dirname)(require.resolve('@goast/typescript')), '../assets/client/fetch');
|
|
75
|
+
const targetDir = (0, path_1.resolve)(ctx.config.outputDir, ctx.config.utilsDirPath);
|
|
76
|
+
(0, fs_extra_1.ensureDirSync)(targetDir);
|
|
77
|
+
const files = (0, fs_extra_1.readdirSync)(sourceDir);
|
|
78
|
+
for (const file of files) {
|
|
79
|
+
(0, fs_extra_1.copyFileSync)((0, path_1.resolve)(sourceDir, file), (0, path_1.resolve)(targetDir, file));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.TypeScriptClientsGenerator = TypeScriptClientsGenerator;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./fetch-client-generator"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./fetch-clients-generator"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./models"), exports);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultTypeScriptFetchClientsGeneratorConfig = void 0;
|
|
4
|
+
const config_1 = require("../../../config");
|
|
5
|
+
exports.defaultTypeScriptFetchClientsGeneratorConfig = Object.assign(Object.assign({}, config_1.defaultTypeScriptGeneratorConfig), { fileNameCasing: { casing: 'kebab', suffix: '-client' }, interfaceFileNameCasing: null, classNameCasing: { casing: 'pascal', suffix: 'Client' }, interfaceNameCasing: { prefix: 'I', casing: 'pascal', suffix: 'Client' }, methodCasing: 'camel', clientFileKind: 'class', clientDirPath: 'clients', clientInterfaceDirPath: 'clients/interfaces', indexFilePath: 'clients.ts', interfaceIndexFilePath: 'clients.ts', utilsDirPath: 'utils' });
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImportExportCollection = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const core_1 = require("@goast/core");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
class ImportExportCollection {
|
|
8
|
+
constructor() {
|
|
9
|
+
this._imports = new Map();
|
|
10
|
+
this._exports = new Map();
|
|
11
|
+
}
|
|
12
|
+
get hasImports() {
|
|
13
|
+
return this._imports.size > 0;
|
|
14
|
+
}
|
|
15
|
+
get hasExports() {
|
|
16
|
+
return this._exports.size > 0;
|
|
17
|
+
}
|
|
18
|
+
get imports() {
|
|
19
|
+
return Array.from(this._imports.entries())
|
|
20
|
+
.map(([fromModule, importNames]) => Array.from(importNames).map((importName) => ({
|
|
21
|
+
kind: this.getImportKind(fromModule),
|
|
22
|
+
modulePath: fromModule,
|
|
23
|
+
name: importName,
|
|
24
|
+
})))
|
|
25
|
+
.flat();
|
|
26
|
+
}
|
|
27
|
+
addImport(importName, fromModule) {
|
|
28
|
+
const existingImport = this._imports.get(fromModule);
|
|
29
|
+
if (existingImport) {
|
|
30
|
+
existingImport.add(importName);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
this._imports.set(fromModule, new Set([importName]));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
addExport(exportName, fromModule) {
|
|
37
|
+
const existingExport = this._exports.get(fromModule);
|
|
38
|
+
if (existingExport) {
|
|
39
|
+
existingExport.add(exportName);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this._exports.set(fromModule, new Set([exportName]));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
clear() {
|
|
46
|
+
this._imports.clear();
|
|
47
|
+
this._exports.clear();
|
|
48
|
+
}
|
|
49
|
+
toString(options, filePath, importModuleTransformer) {
|
|
50
|
+
const builder = new core_1.StringBuilder(options);
|
|
51
|
+
this.writeTo(builder, filePath, importModuleTransformer);
|
|
52
|
+
return builder.toString();
|
|
53
|
+
}
|
|
54
|
+
writeTo(builder, filePath, importModuleTransformer) {
|
|
55
|
+
if (!filePath) {
|
|
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
|
+
}
|
|
64
|
+
if (this._imports.size > 0) {
|
|
65
|
+
const sortedImports = this.sortAndResolve(this._imports, filePath, importModuleTransformer);
|
|
66
|
+
this.writeImportsExports(builder, 'import', sortedImports);
|
|
67
|
+
}
|
|
68
|
+
if (this._imports.size > 0 && this._exports.size > 0) {
|
|
69
|
+
builder.appendLine();
|
|
70
|
+
}
|
|
71
|
+
if (this._exports.size > 0) {
|
|
72
|
+
const sortedExports = this.sortAndResolve(this._exports, filePath, importModuleTransformer);
|
|
73
|
+
this.writeImportsExports(builder, 'export', sortedExports);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
writeImportsExports(builder, keyword, data) {
|
|
77
|
+
const moduleData = [];
|
|
78
|
+
const fileData = [];
|
|
79
|
+
for (const x of data) {
|
|
80
|
+
if (x[1] === 'module') {
|
|
81
|
+
moduleData.push(x);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
fileData.push(x);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
for (const [fromModule, _, names] of moduleData) {
|
|
88
|
+
builder.appendLine(`${keyword} { ${names.join(', ')} } from '${fromModule}';`);
|
|
89
|
+
}
|
|
90
|
+
if (moduleData.length > 0 && fileData.length > 0) {
|
|
91
|
+
builder.appendLine();
|
|
92
|
+
}
|
|
93
|
+
for (const [fromModule, _, names] of fileData) {
|
|
94
|
+
builder.appendLine(`${keyword} { ${names.join(', ')} } from '${fromModule}';`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
sortAndResolve(data, filePath, importModuleTransformer) {
|
|
98
|
+
return Array.from(data.entries())
|
|
99
|
+
.sort(([fromModuleA], [fromModuleB]) => fromModuleA.localeCompare(fromModuleB))
|
|
100
|
+
.map(([fromModule, names]) => [...this.resolveModulePath(fromModule, filePath, importModuleTransformer), Array.from(names).sort()]);
|
|
101
|
+
}
|
|
102
|
+
getImportKind(fromModule) {
|
|
103
|
+
const extName = (0, path_1.extname)(fromModule).toUpperCase();
|
|
104
|
+
return extName === '.TS' || extName === '.JS' || extName === '.JSON' ? 'file' : 'module';
|
|
105
|
+
}
|
|
106
|
+
resolveModulePath(fromModule, filePath, importModuleTransformer) {
|
|
107
|
+
const kind = this.getImportKind(fromModule);
|
|
108
|
+
return [
|
|
109
|
+
filePath && kind === 'file'
|
|
110
|
+
? (0, utils_1.getModulePathRelativeToFile)(filePath, fromModule, importModuleTransformer)
|
|
111
|
+
: fromModule,
|
|
112
|
+
kind,
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.ImportExportCollection = ImportExportCollection;
|
package/cjs/lib/utils.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modifyString = exports.getModulePathRelativeToDirectory = exports.getModulePathRelativeToFile = exports.toTypeScriptPropertyName = exports.toTypeScriptStringLiteral = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
function doubleQuotedToSingleQuoted(value) {
|
|
6
|
+
return `'${value.slice(1, -1).replace(/'/g, "\\'")}'`;
|
|
7
|
+
}
|
|
8
|
+
function toTypeScriptStringLiteral(value, useSingleQuotes) {
|
|
9
|
+
return useSingleQuotes ? doubleQuotedToSingleQuoted(JSON.stringify(value)) : JSON.stringify(value);
|
|
10
|
+
}
|
|
11
|
+
exports.toTypeScriptStringLiteral = toTypeScriptStringLiteral;
|
|
12
|
+
function toTypeScriptPropertyName(value, useSingleQuotes) {
|
|
13
|
+
if (value.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*$/)) {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
return useSingleQuotes ? doubleQuotedToSingleQuoted(JSON.stringify(value)) : JSON.stringify(value);
|
|
17
|
+
}
|
|
18
|
+
exports.toTypeScriptPropertyName = toTypeScriptPropertyName;
|
|
19
|
+
function getModulePathRelativeToFile(from, to, transformer) {
|
|
20
|
+
return transformModulePath((0, path_1.relative)((0, path_1.dirname)(from), to).replace(/\\/g, '/'), transformer);
|
|
21
|
+
}
|
|
22
|
+
exports.getModulePathRelativeToFile = getModulePathRelativeToFile;
|
|
23
|
+
function getModulePathRelativeToDirectory(from, to, transformer) {
|
|
24
|
+
return transformModulePath((0, path_1.relative)(from, to).replace(/\\/g, '/'), transformer);
|
|
25
|
+
}
|
|
26
|
+
exports.getModulePathRelativeToDirectory = getModulePathRelativeToDirectory;
|
|
27
|
+
function transformModulePath(modulePath, transformer) {
|
|
28
|
+
if (!modulePath.startsWith('.')) {
|
|
29
|
+
modulePath = `./${modulePath}`;
|
|
30
|
+
}
|
|
31
|
+
if (transformer === 'omit-extension') {
|
|
32
|
+
return modulePath.replace(/\.[^/.]+$/, '');
|
|
33
|
+
}
|
|
34
|
+
else if (transformer === 'js-extension') {
|
|
35
|
+
return modulePath.replace(/\.[^/.]+$/, '.js');
|
|
36
|
+
}
|
|
37
|
+
else if (typeof transformer === 'function') {
|
|
38
|
+
return transformer(modulePath);
|
|
39
|
+
}
|
|
40
|
+
return modulePath;
|
|
41
|
+
}
|
|
42
|
+
function modifyString(value, modifier, ...args) {
|
|
43
|
+
var _a;
|
|
44
|
+
if (typeof modifier === 'string') {
|
|
45
|
+
return modifier;
|
|
46
|
+
}
|
|
47
|
+
if (modifier instanceof RegExp) {
|
|
48
|
+
const match = value.match(modifier);
|
|
49
|
+
return (_a = match === null || match === void 0 ? void 0 : match[0]) !== null && _a !== void 0 ? _a : value;
|
|
50
|
+
}
|
|
51
|
+
if (typeof modifier === 'function') {
|
|
52
|
+
return modifier(value, ...args);
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
exports.modifyString = modifyString;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const defaultTypeScriptGeneratorConfig = {
|
|
2
|
+
charsTreatedAsEmptyLine: ['{'],
|
|
3
|
+
indent: { type: 'spaces', count: 2 },
|
|
4
|
+
importModuleTransformer: 'omit-extension',
|
|
5
|
+
typeNameCasing: 'pascal',
|
|
6
|
+
methodNameCasing: 'camel',
|
|
7
|
+
propertyNameCasing: 'camel',
|
|
8
|
+
enumValueNameCasing: 'pascal',
|
|
9
|
+
constantCasing: { casing: 'snake', wordCasing: 'all-upper' },
|
|
10
|
+
preferUnknown: true,
|
|
11
|
+
useSingleQuotes: true,
|
|
12
|
+
};
|