openapi-ts-request 1.2.0 → 1.3.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/README.md +50 -2
- package/dist/bin/cli.js +30 -11
- package/dist/bin/openapi.js +80 -31
- package/dist/generator/merge.d.ts +6 -0
- package/dist/generator/merge.js +267 -0
- package/dist/generator/mockGenarator.js +1 -1
- package/dist/generator/serviceGenarator.js +86 -59
- package/dist/generator/type.d.ts +16 -1
- package/dist/generator/type.js +6 -0
- package/dist/index.d.ts +19 -3
- package/dist/index.js +11 -5
- package/dist/log.d.ts +1 -0
- package/dist/log.js +3 -0
- package/dist/readConfig.d.ts +2 -0
- package/dist/readConfig.js +31 -0
- package/dist/type.d.ts +42 -0
- package/dist/util.d.ts +17 -2
- package/dist/util.js +104 -7
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
<!-- TODO:需要修改文档, 添加参数, 添加apifox的配置支持 -->
|
|
2
|
+
|
|
1
3
|
## 介绍
|
|
2
4
|
|
|
3
5
|
[](https://github.com/openapi-ui/openapi-ts-request) [](https://www.npmjs.com/package/openapi-ts-request) 
|
|
@@ -13,6 +15,7 @@
|
|
|
13
15
|
- react-query/vue-query
|
|
14
16
|
- 类型字段翻译
|
|
15
17
|
- JSON Schemas
|
|
18
|
+
- Apifox Config
|
|
16
19
|
|
|
17
20
|
文档:[使用手册](https://github.com/openapi-ui/openapi-ts-request/issues/100)
|
|
18
21
|
|
|
@@ -25,6 +28,7 @@
|
|
|
25
28
|
- 支持通过 tags 过滤生成结果
|
|
26
29
|
- 支持 JSON/YAML 定义文件
|
|
27
30
|
- 支持将中文 tag 名称翻译为英文 tag 名称
|
|
31
|
+
- 支持直接配置`apifox`的`token`和`projectId`直接生成
|
|
28
32
|
|
|
29
33
|
## 使用
|
|
30
34
|
|
|
@@ -181,9 +185,13 @@ $ openapi --help
|
|
|
181
185
|
|
|
182
186
|
Options:
|
|
183
187
|
-V, --version output the version number
|
|
184
|
-
-i, --input <string> OpenAPI specification, can be a path, url
|
|
185
|
-
-o, --output <string> output directory
|
|
188
|
+
-i, --input <string> OpenAPI specification, can be a path, url
|
|
189
|
+
-o, --output <string> output directory
|
|
190
|
+
-cfn, --configFileName <string> config file name
|
|
191
|
+
-cfp, --configFilePath <string> config file path
|
|
192
|
+
-u, --uniqueKey <string> unique key
|
|
186
193
|
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch" (default: "axios")
|
|
194
|
+
-f, --full <boolean> full replacement (default: true)
|
|
187
195
|
--enableLogging <boolean> open the log (default: false)
|
|
188
196
|
--priorityRule <string> priority rule, include/exclude/both (default: "include")
|
|
189
197
|
--includeTags <(string|RegExp)[]> generate code from include tags
|
|
@@ -221,6 +229,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
221
229
|
| schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
|
|
222
230
|
| serversPath | 否 | string | './src/apis' | 运行结果文件夹路径 |
|
|
223
231
|
| requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
|
|
232
|
+
| full | 否 | boolean | true | 是否全量替换 |
|
|
224
233
|
| enableLogging | 否 | boolean | false | 是否开启日志 |
|
|
225
234
|
| priorityRule | 否 | string | 'include' | 模式规则,可选include/exclude/both |
|
|
226
235
|
| includeTags | 否 | (string\|RegExp)[] | - | 根据指定的 tags 生成代码, priorityRule=include则必填 |
|
|
@@ -237,6 +246,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
237
246
|
| isGenJsonSchemas | 否 | boolean | false | 是否生成 JSON Schemas |
|
|
238
247
|
| mockFolder | 否 | string | - | mock文件路径,例如:'./mocks' |
|
|
239
248
|
| authorization | 否 | string | - | 文档权限凭证 |
|
|
249
|
+
| apifoxConfig | 否 | [Apifox Config](#Apifox-Config) | - | apifox 配置 |
|
|
240
250
|
| nullable | 否 | boolean | false | 使用 null 代替可选 |
|
|
241
251
|
| isTranslateToEnglishTag | 否 | boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
|
|
242
252
|
| isOnlyGenTypeScriptType | 否 | boolean | false | 仅生成 typescript 类型 |
|
|
@@ -255,6 +265,17 @@ openapi --i ./spec.json --o ./apis
|
|
|
255
265
|
| customType | ({<br>schemaObject: SchemaObject \| ReferenceObject,<br>namespace: string,<br>originGetType:(schemaObject: SchemaObject \| ReferenceObject, namespace: string, schemas?: ComponentsObject['schemas']) => string,<br>schemas?: ComponentsObject['schemas'],<br>}) => string | 自定义类型 <br> _返回非字符串将使用默认方法获取type_ |
|
|
256
266
|
| customFileNames | (<br>operationObject: OperationObject,<br>apiPath: string,<br>apiMethod: string,<br>) => string[] | 自定义生成的请求客户端文件名称,可以返回多个文件名称的数组(表示生成多个文件). <br> _返回为空,则使用默认的方法获取_ |
|
|
257
267
|
|
|
268
|
+
## Apifox-Config
|
|
269
|
+
|
|
270
|
+
| 属性 | 类型 | 说明 | 必填 |
|
|
271
|
+
| --- | --- | --- | --- |
|
|
272
|
+
| projectId | string | 项目id | true |
|
|
273
|
+
| local | string | 语言(默认:zh-CN) | false |
|
|
274
|
+
| apifoxVersion | string | 默认: 2024-03-28, [获取当前版本](https://api.apifox.com/v1/versions) | false |
|
|
275
|
+
| includeTags | \* 或 string[] | 默认: \* | false |
|
|
276
|
+
| excludeTags | string[] | 默认: [] | false |
|
|
277
|
+
| apifoxToken | string | [获取](https://docs.apifox.com/doc-5723694) | true |
|
|
278
|
+
|
|
258
279
|
## JSON Schemas
|
|
259
280
|
|
|
260
281
|
- 默认生成 [components.schemas](https://spec.openapis.org/oas/latest.html#components-object) 下面的 JSON Schemas,[paths](https://spec.openapis.org/oas/latest.html#paths-object) 对应的 JSON Schemas 目前需自行解析
|
|
@@ -275,6 +296,33 @@ export declare function patchSchema<T extends object>(
|
|
|
275
296
|
|
|
276
297
|
适配 uniapp 推荐采用自定义 request 函数的方式,你也可以使用 `@uni-helper/axios-adapter` 适配器,详情见 [【使用手册 2.2】](https://github.com/openapi-ui/openapi-ts-request/issues/100)
|
|
277
298
|
|
|
299
|
+
## 旧版本升级注意事项
|
|
300
|
+
|
|
301
|
+
- 当前命名规范修改
|
|
302
|
+
- 当前版本已完成增量修改, 不会影响以前
|
|
303
|
+
- 可以弃用`openapi-ts`命令, 直接使用`openapi`
|
|
304
|
+
|
|
305
|
+
### 沿用旧版本命名规范配置如下
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import type { APIDataType } from 'openapi-ts-request/dist/generator/type';
|
|
309
|
+
import {
|
|
310
|
+
genDefaultFunctionName,
|
|
311
|
+
resolveFunctionName,
|
|
312
|
+
stripDot,
|
|
313
|
+
} from 'openapi-ts-request/dist/generator/util';
|
|
314
|
+
|
|
315
|
+
export default {
|
|
316
|
+
hook: {
|
|
317
|
+
customFunctionName(data: APIDataType, prefix: string) {
|
|
318
|
+
if (data.operationId)
|
|
319
|
+
return resolveFunctionName(stripDot(data.operationId), data.method);
|
|
320
|
+
return data.method + genDefaultFunctionName(data.path, prefix);
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
```
|
|
325
|
+
|
|
278
326
|
## 贡献
|
|
279
327
|
|
|
280
328
|
### 环境要求
|
package/dist/bin/cli.js
CHANGED
|
@@ -1,27 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var _a;
|
|
4
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
4
|
const tslib_1 = require("tslib");
|
|
6
5
|
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
7
|
-
const
|
|
8
|
-
const cosmiconfig_typescript_loader_1 = require("cosmiconfig-typescript-loader");
|
|
6
|
+
const commander_1 = require("commander");
|
|
9
7
|
const index_1 = require("../index");
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
const readConfig_1 = require("../readConfig");
|
|
9
|
+
commander_1.program
|
|
10
|
+
.option('-cfn, --configFileName <string>', 'config file name')
|
|
11
|
+
.option('-cfp, --configFilePath <string>', 'config file path')
|
|
12
|
+
.option('-u, --uniqueKey <string>', 'unique key');
|
|
13
|
+
commander_1.program.parse();
|
|
14
|
+
const options = commander_1.program.opts();
|
|
16
15
|
function run() {
|
|
17
16
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const config = yield (0, readConfig_1.readConfig)({
|
|
18
|
+
fallbackName: 'openapi-ts-request',
|
|
19
|
+
filePath: options.filePath,
|
|
20
|
+
fileName: options.fileName,
|
|
21
|
+
});
|
|
18
22
|
try {
|
|
23
|
+
const tasks = [];
|
|
19
24
|
if (config) {
|
|
20
|
-
|
|
25
|
+
let configs = Array.isArray(config)
|
|
21
26
|
? config
|
|
22
27
|
: [config];
|
|
28
|
+
if (options.uniqueKey) {
|
|
29
|
+
configs = configs.filter((config) => config.uniqueKey === options.uniqueKey);
|
|
30
|
+
}
|
|
23
31
|
for (const config of configs) {
|
|
24
|
-
|
|
32
|
+
tasks.push((0, index_1.generateService)(config));
|
|
33
|
+
}
|
|
34
|
+
const results = yield Promise.allSettled(tasks);
|
|
35
|
+
const errors = results.filter((result) => result.status === 'rejected');
|
|
36
|
+
let errorMsg = '';
|
|
37
|
+
for (let i = 0; i < errors.length; i++) {
|
|
38
|
+
const error = errors[i];
|
|
39
|
+
const cnf = configs[i];
|
|
40
|
+
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${error.reason}\n`;
|
|
41
|
+
}
|
|
42
|
+
if (errorMsg) {
|
|
43
|
+
throw new Error(errorMsg);
|
|
25
44
|
}
|
|
26
45
|
}
|
|
27
46
|
else {
|
package/dist/bin/openapi.js
CHANGED
|
@@ -7,13 +7,19 @@ const lodash_1 = require("lodash");
|
|
|
7
7
|
const path_1 = require("path");
|
|
8
8
|
const pkg = tslib_1.__importStar(require("../../package.json"));
|
|
9
9
|
const index_1 = require("../index");
|
|
10
|
+
const log_1 = require("../log");
|
|
11
|
+
const readConfig_1 = require("../readConfig");
|
|
10
12
|
const params = commander_1.program
|
|
11
13
|
.name('openapi')
|
|
12
14
|
.usage('[options]')
|
|
13
15
|
.version(pkg.version)
|
|
14
|
-
.
|
|
15
|
-
.
|
|
16
|
+
.option('-i, --input <string>', 'OpenAPI specification, can be a path, url')
|
|
17
|
+
.option('-o, --output <string>', 'output directory')
|
|
18
|
+
.option('-cfn, --configFileName <string>', 'config file name')
|
|
19
|
+
.option('-cfp, --configFilePath <string>', 'config file path')
|
|
20
|
+
.option('-u, --uniqueKey <string>', 'unique key')
|
|
16
21
|
.option('--requestLibPath <string>', 'custom request lib path, for example: "@/request", "node-fetch" (default: "axios")')
|
|
22
|
+
.option('-f, --full <boolean>', 'full replacement', true)
|
|
17
23
|
.option('--enableLogging <boolean>', 'open the log', false)
|
|
18
24
|
.option('--priorityRule <string>', 'priority rule, include/exclude/both (default: "include")')
|
|
19
25
|
.option('--includeTags <(string|RegExp)[]>', 'generate code from include tags')
|
|
@@ -44,41 +50,84 @@ function getPath(path) {
|
|
|
44
50
|
}
|
|
45
51
|
return (0, path_1.join)(process.cwd(), path);
|
|
46
52
|
}
|
|
53
|
+
const baseGenerate = (_params_) => {
|
|
54
|
+
const input = getPath(_params_.input);
|
|
55
|
+
const output = getPath(_params_.output);
|
|
56
|
+
const options = {
|
|
57
|
+
schemaPath: input,
|
|
58
|
+
serversPath: output,
|
|
59
|
+
requestLibPath: _params_.requestLibPath,
|
|
60
|
+
full: JSON.parse(_params_.full) === true,
|
|
61
|
+
enableLogging: JSON.parse(_params_.enableLogging) === true,
|
|
62
|
+
priorityRule: _params_.priorityRule,
|
|
63
|
+
includeTags: _params_.includeTags,
|
|
64
|
+
includePaths: _params_.includePaths,
|
|
65
|
+
excludeTags: _params_.excludeTags,
|
|
66
|
+
excludePaths: _params_.excludePaths,
|
|
67
|
+
requestOptionsType: _params_.requestOptionsType,
|
|
68
|
+
apiPrefix: _params_.apiPrefix,
|
|
69
|
+
isGenReactQuery: JSON.parse(_params_.isGenReactQuery) === true,
|
|
70
|
+
reactQueryMode: _params_.reactQueryMode,
|
|
71
|
+
isGenJavaScript: JSON.parse(_params_.isGenJavaScript) === true,
|
|
72
|
+
isDisplayTypeLabel: JSON.parse(_params_.isDisplayTypeLabel) === true,
|
|
73
|
+
isGenJsonSchemas: JSON.parse(_params_.isGenJsonSchemas) === true,
|
|
74
|
+
mockFolder: _params_.mockFolder,
|
|
75
|
+
authorization: _params_.authorization,
|
|
76
|
+
nullable: JSON.parse(_params_.nullable) === true,
|
|
77
|
+
isTranslateToEnglishTag: JSON.parse(_params_.isTranslateToEnglishTag) === true,
|
|
78
|
+
isOnlyGenTypeScriptType: JSON.parse(_params_.isOnlyGenTypeScriptType) === true,
|
|
79
|
+
isCamelCase: JSON.parse(_params_.isCamelCase) === true,
|
|
80
|
+
isSupportParseEnumDesc: JSON.parse(_params_.isSupportParseEnumDesc) === true,
|
|
81
|
+
};
|
|
82
|
+
return options;
|
|
83
|
+
};
|
|
47
84
|
function run() {
|
|
48
85
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
const output = getPath(params.output);
|
|
52
|
-
const options = {
|
|
53
|
-
schemaPath: input,
|
|
54
|
-
serversPath: output,
|
|
55
|
-
requestLibPath: params.requestLibPath,
|
|
56
|
-
enableLogging: JSON.parse(params.enableLogging) === true,
|
|
57
|
-
priorityRule: params.priorityRule,
|
|
58
|
-
includeTags: params.includeTags,
|
|
59
|
-
includePaths: params.includePaths,
|
|
60
|
-
excludeTags: params.excludeTags,
|
|
61
|
-
excludePaths: params.excludePaths,
|
|
62
|
-
requestOptionsType: params.requestOptionsType,
|
|
63
|
-
apiPrefix: params.apiPrefix,
|
|
64
|
-
isGenReactQuery: JSON.parse(params.isGenReactQuery) === true,
|
|
65
|
-
reactQueryMode: params.reactQueryMode,
|
|
66
|
-
isGenJavaScript: JSON.parse(params.isGenJavaScript) === true,
|
|
67
|
-
isDisplayTypeLabel: JSON.parse(params.isDisplayTypeLabel) === true,
|
|
68
|
-
isGenJsonSchemas: JSON.parse(params.isGenJsonSchemas) === true,
|
|
69
|
-
mockFolder: params.mockFolder,
|
|
70
|
-
authorization: params.authorization,
|
|
71
|
-
nullable: JSON.parse(params.nullable) === true,
|
|
72
|
-
isTranslateToEnglishTag: JSON.parse(params.isTranslateToEnglishTag) === true,
|
|
73
|
-
isOnlyGenTypeScriptType: JSON.parse(params.isOnlyGenTypeScriptType) === true,
|
|
74
|
-
isCamelCase: JSON.parse(params.isCamelCase) === true,
|
|
75
|
-
isSupportParseEnumDesc: JSON.parse(params.isSupportParseEnumDesc) === true,
|
|
76
|
-
};
|
|
86
|
+
if (params.input && params.output) {
|
|
87
|
+
const options = baseGenerate(params);
|
|
77
88
|
yield (0, index_1.generateService)((0, lodash_1.pickBy)(options, (value) => value !== null && value !== undefined && value !== ''));
|
|
78
89
|
process.exit(0);
|
|
79
90
|
}
|
|
91
|
+
const cnf = yield (0, readConfig_1.readConfig)({
|
|
92
|
+
fallbackName: 'openapi-ts-request',
|
|
93
|
+
filePath: params.configFilePath,
|
|
94
|
+
fileName: params.configFileName,
|
|
95
|
+
});
|
|
96
|
+
try {
|
|
97
|
+
if (cnf) {
|
|
98
|
+
const tasks = [];
|
|
99
|
+
let configs = Array.isArray(cnf) ? cnf : [cnf];
|
|
100
|
+
if (params.uniqueKey) {
|
|
101
|
+
configs = configs.filter((config) => config.uniqueKey === params.uniqueKey);
|
|
102
|
+
}
|
|
103
|
+
for (const config of configs) {
|
|
104
|
+
tasks.push((0, index_1.generateService)(config));
|
|
105
|
+
}
|
|
106
|
+
const results = yield Promise.allSettled(tasks);
|
|
107
|
+
const errors = results.filter((result) => result.status === 'rejected');
|
|
108
|
+
let errorMsg = '';
|
|
109
|
+
for (let i = 0; i < errors.length; i++) {
|
|
110
|
+
const error = errors[i];
|
|
111
|
+
const cnf = configs[i];
|
|
112
|
+
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${error.reason}\n`;
|
|
113
|
+
}
|
|
114
|
+
if (errorMsg) {
|
|
115
|
+
(0, log_1.logError)(errorMsg);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
if (!params.input || !params.output) {
|
|
121
|
+
(0, log_1.logError)('Please provide either input/output options or a configuration file path and name.');
|
|
122
|
+
process.exit(1);
|
|
123
|
+
}
|
|
124
|
+
const options = baseGenerate(params);
|
|
125
|
+
yield (0, index_1.generateService)((0, lodash_1.pickBy)(options, (value) => value !== null && value !== undefined && value !== ''));
|
|
126
|
+
process.exit(0);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
80
129
|
catch (error) {
|
|
81
|
-
|
|
130
|
+
(0, log_1.logError)(error);
|
|
82
131
|
process.exit(1);
|
|
83
132
|
}
|
|
84
133
|
});
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _Merger_instances, _Merger_project, _Merger_sourceFile, _Merger_mergeRule, _Merger_mergedFile, _Merger_sourceCode, _Merger_leadingCommentRanges, _Merger_mergeExport, _Merger_mergeClass, _Merger_mergeType, _Merger_mergeEnums, _Merger_mergeInterfaces, _Merger_mergeFunctions, _Merger_mergeVariables, _Merger_mergeImports;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Merger = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const ts_morph_1 = require("ts-morph");
|
|
7
|
+
const ts = tslib_1.__importStar(require("typescript"));
|
|
8
|
+
const type_1 = require("./type");
|
|
9
|
+
const sortMapByKey = (map) => {
|
|
10
|
+
return Array.from(map.entries())
|
|
11
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
12
|
+
.map(([k, v]) => ({ k, v }));
|
|
13
|
+
};
|
|
14
|
+
class Merger {
|
|
15
|
+
constructor({ srcPath, source, mergeRule = type_1.MergeRule.RIGHT, projectOptions = {}, }) {
|
|
16
|
+
_Merger_instances.add(this);
|
|
17
|
+
_Merger_project.set(this, void 0);
|
|
18
|
+
_Merger_sourceFile.set(this, void 0);
|
|
19
|
+
_Merger_mergeRule.set(this, void 0);
|
|
20
|
+
_Merger_mergedFile.set(this, void 0);
|
|
21
|
+
_Merger_sourceCode.set(this, void 0);
|
|
22
|
+
_Merger_leadingCommentRanges.set(this, []);
|
|
23
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_project, new ts_morph_1.Project(projectOptions), "f");
|
|
24
|
+
if (srcPath) {
|
|
25
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_sourceFile, tslib_1.__classPrivateFieldGet(this, _Merger_project, "f").addSourceFileAtPath(srcPath), "f");
|
|
26
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_sourceCode, tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getFullText().trim(), "f");
|
|
27
|
+
}
|
|
28
|
+
else if (source) {
|
|
29
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_sourceCode, source.trim(), "f");
|
|
30
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_sourceFile, tslib_1.__classPrivateFieldGet(this, _Merger_project, "f").createSourceFile('_source_.ts', source), "f");
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_sourceCode, "f")) {
|
|
34
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_leadingCommentRanges, ts
|
|
35
|
+
.getLeadingCommentRanges(tslib_1.__classPrivateFieldGet(this, _Merger_sourceCode, "f"), 0)
|
|
36
|
+
.map((range) => {
|
|
37
|
+
return tslib_1.__classPrivateFieldGet(this, _Merger_sourceCode, "f").slice(range.pos, range.end);
|
|
38
|
+
}), "f");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
// console.log(error, '>>>');
|
|
43
|
+
}
|
|
44
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_mergeRule, mergeRule, "f");
|
|
45
|
+
tslib_1.__classPrivateFieldSet(this, _Merger_mergedFile, tslib_1.__classPrivateFieldGet(this, _Merger_project, "f").createSourceFile('_merged_.ts'), "f");
|
|
46
|
+
}
|
|
47
|
+
merge({ srcPath, source }) {
|
|
48
|
+
let destFile;
|
|
49
|
+
if (srcPath) {
|
|
50
|
+
destFile = tslib_1.__classPrivateFieldGet(this, _Merger_project, "f").addSourceFileAtPath(srcPath);
|
|
51
|
+
}
|
|
52
|
+
else if (source) {
|
|
53
|
+
if (!source.trim()) {
|
|
54
|
+
return tslib_1.__classPrivateFieldGet(this, _Merger_sourceCode, "f");
|
|
55
|
+
}
|
|
56
|
+
destFile = tslib_1.__classPrivateFieldGet(this, _Merger_project, "f").createSourceFile('_dest_.ts', source);
|
|
57
|
+
}
|
|
58
|
+
if (!tslib_1.__classPrivateFieldGet(this, _Merger_sourceCode, "f")) {
|
|
59
|
+
return destFile.getFullText();
|
|
60
|
+
}
|
|
61
|
+
const destCode = destFile.getFullText().trim();
|
|
62
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_sourceCode, "f") === destCode) {
|
|
63
|
+
return tslib_1.__classPrivateFieldGet(this, _Merger_sourceCode, "f");
|
|
64
|
+
}
|
|
65
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeImports).call(this, destFile);
|
|
66
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeVariables).call(this, destFile);
|
|
67
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeClass).call(this, destFile);
|
|
68
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeType).call(this, destFile);
|
|
69
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeEnums).call(this, destFile);
|
|
70
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeInterfaces).call(this, destFile);
|
|
71
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeFunctions).call(this, destFile);
|
|
72
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_instances, "m", _Merger_mergeExport).call(this, destFile);
|
|
73
|
+
// console.log(source, '>>>>');
|
|
74
|
+
// if (srcPath?.includes('index.ts')) {
|
|
75
|
+
// console.log(source, '>>>>');
|
|
76
|
+
// }
|
|
77
|
+
const leadingComment = tslib_1.__classPrivateFieldGet(this, _Merger_leadingCommentRanges, "f").join('\n');
|
|
78
|
+
if (leadingComment) {
|
|
79
|
+
return `${leadingComment}\n${tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").getFullText()}`;
|
|
80
|
+
}
|
|
81
|
+
return tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").getFullText();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.Merger = Merger;
|
|
85
|
+
_Merger_project = new WeakMap(), _Merger_sourceFile = new WeakMap(), _Merger_mergeRule = new WeakMap(), _Merger_mergedFile = new WeakMap(), _Merger_sourceCode = new WeakMap(), _Merger_leadingCommentRanges = new WeakMap(), _Merger_instances = new WeakSet(), _Merger_mergeExport = function _Merger_mergeExport(destFile) {
|
|
86
|
+
var _a, _b;
|
|
87
|
+
const exportMap = new Map();
|
|
88
|
+
// const exportArray: ExportDeclarationStructure[] = [];
|
|
89
|
+
(_a = tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getExportDeclarations()) === null || _a === void 0 ? void 0 : _a.forEach((e) => {
|
|
90
|
+
const eStructure = e.getStructure();
|
|
91
|
+
const path = e.getModuleSpecifierValue();
|
|
92
|
+
if (path) {
|
|
93
|
+
exportMap.set(path, eStructure);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
(_b = destFile.getExportDeclarations()) === null || _b === void 0 ? void 0 : _b.forEach((e) => {
|
|
97
|
+
const eStructure = e.getStructure();
|
|
98
|
+
const path = e.getModuleSpecifierValue();
|
|
99
|
+
if (path) {
|
|
100
|
+
exportMap.set(path, eStructure);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
sortMapByKey(exportMap).forEach(({ v }) => {
|
|
104
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addExportDeclaration(v);
|
|
105
|
+
});
|
|
106
|
+
}, _Merger_mergeClass = function _Merger_mergeClass(destFile) {
|
|
107
|
+
const classMap = new Map();
|
|
108
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getClasses().forEach((c) => {
|
|
109
|
+
const cName = c.getName();
|
|
110
|
+
const cStructure = c.getStructure();
|
|
111
|
+
classMap.set(cName, cStructure);
|
|
112
|
+
});
|
|
113
|
+
destFile.getClasses().forEach((c) => {
|
|
114
|
+
const cName = c.getName();
|
|
115
|
+
if (classMap.has(cName)) {
|
|
116
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_mergeRule, "f") === type_1.MergeRule.RIGHT) {
|
|
117
|
+
const cStructure = c.getStructure();
|
|
118
|
+
classMap.set(cName, cStructure);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
const cStructure = c.getStructure();
|
|
123
|
+
classMap.set(cName, cStructure);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
sortMapByKey(classMap).forEach(({ v }) => {
|
|
127
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addClass(v);
|
|
128
|
+
});
|
|
129
|
+
}, _Merger_mergeType = function _Merger_mergeType(destFile) {
|
|
130
|
+
const typeMap = new Map();
|
|
131
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getTypeAliases().forEach((t) => {
|
|
132
|
+
const tName = t.getName();
|
|
133
|
+
const tStructure = t.getStructure();
|
|
134
|
+
typeMap.set(tName, tStructure);
|
|
135
|
+
});
|
|
136
|
+
destFile.getTypeAliases().forEach((t) => {
|
|
137
|
+
const tName = t.getName();
|
|
138
|
+
if (typeMap.has(tName)) {
|
|
139
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_mergeRule, "f") === type_1.MergeRule.RIGHT) {
|
|
140
|
+
const tStructure = t.getStructure();
|
|
141
|
+
typeMap.set(tName, tStructure);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const tStructure = t.getStructure();
|
|
146
|
+
typeMap.set(tName, tStructure);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
sortMapByKey(typeMap).forEach(({ v }) => {
|
|
150
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addTypeAlias(v);
|
|
151
|
+
});
|
|
152
|
+
}, _Merger_mergeEnums = function _Merger_mergeEnums(destFile) {
|
|
153
|
+
const enumMap = new Map();
|
|
154
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getEnums().forEach((e) => {
|
|
155
|
+
const eName = e.getName();
|
|
156
|
+
const eStructure = e.getStructure();
|
|
157
|
+
enumMap.set(eName, eStructure);
|
|
158
|
+
});
|
|
159
|
+
destFile.getEnums().forEach((e) => {
|
|
160
|
+
const eName = e.getName();
|
|
161
|
+
if (enumMap.has(eName)) {
|
|
162
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_mergeRule, "f") === type_1.MergeRule.RIGHT) {
|
|
163
|
+
const eStructure = e.getStructure();
|
|
164
|
+
enumMap.set(eName, eStructure);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
const eStructure = e.getStructure();
|
|
169
|
+
enumMap.set(eName, eStructure);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
sortMapByKey(enumMap).forEach(({ v }) => {
|
|
173
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addEnum(v);
|
|
174
|
+
});
|
|
175
|
+
}, _Merger_mergeInterfaces = function _Merger_mergeInterfaces(destFile) {
|
|
176
|
+
const interfaceMap = new Map();
|
|
177
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getInterfaces().forEach((i) => {
|
|
178
|
+
const iName = i.getName();
|
|
179
|
+
const iStructure = i.getStructure();
|
|
180
|
+
interfaceMap.set(iName, iStructure);
|
|
181
|
+
});
|
|
182
|
+
destFile.getInterfaces().forEach((i) => {
|
|
183
|
+
const iName = i.getName();
|
|
184
|
+
if (interfaceMap.has(iName)) {
|
|
185
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_mergeRule, "f") === type_1.MergeRule.RIGHT) {
|
|
186
|
+
const iStructure = i.getStructure();
|
|
187
|
+
interfaceMap.set(iName, iStructure);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const iStructure = i.getStructure();
|
|
192
|
+
interfaceMap.set(iName, iStructure);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
sortMapByKey(interfaceMap).forEach(({ v }) => {
|
|
196
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addInterface(v);
|
|
197
|
+
});
|
|
198
|
+
}, _Merger_mergeFunctions = function _Merger_mergeFunctions(destFile) {
|
|
199
|
+
const functionMap = new Map();
|
|
200
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getFunctions().forEach((f) => {
|
|
201
|
+
const fName = f.getName();
|
|
202
|
+
const fStructure = f.getStructure();
|
|
203
|
+
functionMap.set(fName, fStructure);
|
|
204
|
+
});
|
|
205
|
+
destFile.getFunctions().forEach((f) => {
|
|
206
|
+
const fName = f.getName();
|
|
207
|
+
if (functionMap.has(fName)) {
|
|
208
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_mergeRule, "f") === type_1.MergeRule.RIGHT) {
|
|
209
|
+
const fStructure = f.getStructure();
|
|
210
|
+
functionMap.set(fName, fStructure);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
const fStructure = f.getStructure();
|
|
215
|
+
functionMap.set(fName, fStructure);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
sortMapByKey(functionMap).forEach(({ v }) => {
|
|
219
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addFunction(v);
|
|
220
|
+
});
|
|
221
|
+
}, _Merger_mergeVariables = function _Merger_mergeVariables(destFile) {
|
|
222
|
+
const variableSet = new Map();
|
|
223
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getVariableStatements().forEach((v) => {
|
|
224
|
+
var _a;
|
|
225
|
+
const vStructure = v.getStructure();
|
|
226
|
+
const vName = (_a = vStructure.declarations[0]) === null || _a === void 0 ? void 0 : _a.name;
|
|
227
|
+
variableSet.set(vName, vStructure);
|
|
228
|
+
});
|
|
229
|
+
destFile.getVariableStatements().forEach((v) => {
|
|
230
|
+
var _a;
|
|
231
|
+
const vStructure = v.getStructure();
|
|
232
|
+
const vName = (_a = vStructure.declarations[0]) === null || _a === void 0 ? void 0 : _a.name;
|
|
233
|
+
if (variableSet.has(vName)) {
|
|
234
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_mergeRule, "f") === type_1.MergeRule.RIGHT) {
|
|
235
|
+
variableSet.set(vName, vStructure);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
const vStructure = v.getStructure();
|
|
240
|
+
variableSet.set(vName, vStructure);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
sortMapByKey(variableSet).forEach(({ v }) => {
|
|
244
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addVariableStatement(v);
|
|
245
|
+
});
|
|
246
|
+
}, _Merger_mergeImports = function _Merger_mergeImports(destFile) {
|
|
247
|
+
const importMap = new Map();
|
|
248
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_sourceFile, "f").getImportDeclarations().forEach((i) => {
|
|
249
|
+
const iStructure = i.getStructure();
|
|
250
|
+
importMap.set(iStructure.moduleSpecifier, iStructure);
|
|
251
|
+
});
|
|
252
|
+
destFile.getImportDeclarations().forEach((i) => {
|
|
253
|
+
const iStructure = i.getStructure();
|
|
254
|
+
const iModuleSpecifier = iStructure.moduleSpecifier;
|
|
255
|
+
if (importMap.has(iModuleSpecifier)) {
|
|
256
|
+
if (tslib_1.__classPrivateFieldGet(this, _Merger_mergeRule, "f") === type_1.MergeRule.RIGHT) {
|
|
257
|
+
importMap.set(iModuleSpecifier, iStructure);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
importMap.set(iModuleSpecifier, iStructure);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
sortMapByKey(importMap).forEach(({ v }) => {
|
|
265
|
+
tslib_1.__classPrivateFieldGet(this, _Merger_mergedFile, "f").addImportDeclaration(v);
|
|
266
|
+
});
|
|
267
|
+
};
|
|
@@ -137,7 +137,7 @@ const genByTemp = ({ method, path, parameters, status, data, }) => {
|
|
|
137
137
|
}`;
|
|
138
138
|
};
|
|
139
139
|
const genMockFiles = (mockFunction) => {
|
|
140
|
-
return (0, file_1.prettierFile)(`
|
|
140
|
+
return (0, file_1.prettierFile)(`
|
|
141
141
|
/* eslint-disable */
|
|
142
142
|
// @ts-ignore
|
|
143
143
|
import { Request, Response } from 'express';
|