openapi-ts-request 1.9.2 → 1.10.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 +12 -7
- package/dist/bin/cli.js +38 -7
- package/dist/bin/openapi.js +32 -14
- package/dist/generator/serviceGenarator.js +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +7 -0
- package/dist/log.js +2 -2
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -45,19 +45,20 @@ pnpm i openapi-ts-request -D
|
|
|
45
45
|
> 配置文件还支持 **_.openapi-ts-request.ts_**, **_openapi-ts-request.config.cjs_** 等格式,参考 [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig?tab=readme-ov-file#cosmiconfig)
|
|
46
46
|
|
|
47
47
|
```ts
|
|
48
|
-
import
|
|
48
|
+
import { defineConfig } from 'openapi-ts-request';
|
|
49
49
|
|
|
50
|
-
export default {
|
|
50
|
+
export default defineConfig({
|
|
51
51
|
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
|
|
52
|
-
|
|
52
|
+
serversPath: './src/apis',
|
|
53
|
+
});
|
|
53
54
|
```
|
|
54
55
|
|
|
55
56
|
支持传入数组配置进行生成
|
|
56
57
|
|
|
57
58
|
```ts
|
|
58
|
-
import
|
|
59
|
+
import { defineConfig } from 'openapi-ts-request';
|
|
59
60
|
|
|
60
|
-
export default [
|
|
61
|
+
export default defineConfig([
|
|
61
62
|
{
|
|
62
63
|
schemaPath: 'http://app.swagger.io/v2/swagger.json',
|
|
63
64
|
serversPath: './src/apis/app',
|
|
@@ -66,7 +67,7 @@ export default [
|
|
|
66
67
|
schemaPath: 'http://auth.swagger.io/v2/swagger.json',
|
|
67
68
|
serversPath: './src/apis/auth',
|
|
68
69
|
},
|
|
69
|
-
]
|
|
70
|
+
]);
|
|
70
71
|
```
|
|
71
72
|
|
|
72
73
|
在 `package.json` 的 `script` 中添加命令: `"openapi": "openapi-ts",`
|
|
@@ -227,6 +228,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
227
228
|
| serversPath | 否 | string | './src/apis' | 运行结果文件夹路径 |
|
|
228
229
|
| requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
|
|
229
230
|
| full | 否 | boolean | true | 是否全量替换 |
|
|
231
|
+
| describe | 否 | string | - | 描述信息,在用 cli 可交互运行方式时会用到 |
|
|
230
232
|
| enableLogging | 否 | boolean | false | 是否开启日志 |
|
|
231
233
|
| priorityRule | 否 | string | 'include' | 模式规则,可选include/exclude/both |
|
|
232
234
|
| filterCaseInsensitive | 否 | boolean | false | 执行 includeTags、includePaths、excludeTags、excludePaths 过滤时是否忽略大小写 |
|
|
@@ -266,6 +268,8 @@ openapi --i ./spec.json --o ./apis
|
|
|
266
268
|
| customTemplates | {<br>[TypescriptFileType.serviceController]?: <T, U>(item: T, context: U) => string;<br>} | 自定义模板,详情请看源码 |
|
|
267
269
|
| customRenderTemplateData | {<br>[TypescriptFileType]?: (list: any[], context: {fileName: string, params: Record<string, unknown>}) => any[]<br>} | 自定义文件生成时的 list 参数处理,支持对不同文件类型进行精细化控制 |
|
|
268
270
|
|
|
271
|
+
[hooks 示例](https://github.com/openapi-ui/openapi-ts-request/blob/main/agents.md#-advanced-customization-hooks)
|
|
272
|
+
|
|
269
273
|
## Apifox-Config
|
|
270
274
|
|
|
271
275
|
| 属性 | 类型 | 说明 | 必填 |
|
|
@@ -343,7 +347,8 @@ export default {
|
|
|
343
347
|
4. 确保你的代码可以通过所有测试用例(新增功能需要添加新的功能测试用例):`pnpm test:unit`
|
|
344
348
|
5. 创建 changeset 文件通过命令:`pnpm changeset`
|
|
345
349
|
6. 使用 commit 提交你的修改(需遵循 commitlint 规范)
|
|
346
|
-
7.
|
|
350
|
+
7. 如果涉及文档,请同步更新 README.md、READMD-en_US.md、agents.md
|
|
351
|
+
8. 发起 Pull Request
|
|
347
352
|
|
|
348
353
|
## 感谢
|
|
349
354
|
|
package/dist/bin/cli.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const
|
|
5
|
+
const prompts_1 = require("@clack/prompts");
|
|
6
6
|
const commander_1 = require("commander");
|
|
7
7
|
const index_1 = require("../index");
|
|
8
|
+
const log_1 = require("../log");
|
|
8
9
|
const readConfig_1 = require("../readConfig");
|
|
9
10
|
commander_1.program
|
|
10
11
|
.option('-cfn, --configFileName <string>', 'config file name')
|
|
@@ -12,6 +13,11 @@ commander_1.program
|
|
|
12
13
|
.option('-u, --uniqueKey <string>', 'unique key');
|
|
13
14
|
commander_1.program.parse();
|
|
14
15
|
const options = commander_1.program.opts();
|
|
16
|
+
/**
|
|
17
|
+
* 1. 执行 cli 命令读取配置文件,已经使用 openapi.ts 替代了 cli.ts,后期会废弃 cli.ts
|
|
18
|
+
* 2. 如果配置文件中有 uniqueKey,则根据 uniqueKey 生成 service
|
|
19
|
+
* 3. 如果配置文件中没有 uniqueKey,且有多个 service,则交互式选择要生成的 service
|
|
20
|
+
*/
|
|
15
21
|
function run() {
|
|
16
22
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
17
23
|
const config = yield (0, readConfig_1.readConfig)({
|
|
@@ -25,30 +31,55 @@ function run() {
|
|
|
25
31
|
let configs = Array.isArray(config)
|
|
26
32
|
? config
|
|
27
33
|
: [config];
|
|
34
|
+
/** 是否交互式 */
|
|
35
|
+
let isInteractive = false;
|
|
28
36
|
if (options.uniqueKey) {
|
|
29
37
|
configs = configs.filter((config) => config.uniqueKey === options.uniqueKey);
|
|
30
38
|
}
|
|
39
|
+
else if (configs.length > 1) {
|
|
40
|
+
// 如果没有指定 uniqueKey,并且有多个配置,则交互式选择
|
|
41
|
+
isInteractive = true;
|
|
42
|
+
console.log(''); // 添加一个空行
|
|
43
|
+
(0, prompts_1.intro)('🎉 欢迎使用 openapi-ts-request 生成器');
|
|
44
|
+
const selected = yield (0, prompts_1.multiselect)({
|
|
45
|
+
message: '请选择要生成的 service',
|
|
46
|
+
options: configs.map((config) => ({
|
|
47
|
+
value: config,
|
|
48
|
+
label: config.describe || config.schemaPath,
|
|
49
|
+
})),
|
|
50
|
+
});
|
|
51
|
+
if ((0, prompts_1.isCancel)(selected)) {
|
|
52
|
+
(0, prompts_1.cancel)('👋 Has cancelled');
|
|
53
|
+
process.exit(0);
|
|
54
|
+
}
|
|
55
|
+
configs = selected;
|
|
56
|
+
}
|
|
31
57
|
for (const config of configs) {
|
|
32
58
|
tasks.push((0, index_1.generateService)(config));
|
|
33
59
|
}
|
|
34
60
|
const results = yield Promise.allSettled(tasks);
|
|
35
|
-
const errors = results.filter((result) => result.status === 'rejected');
|
|
36
61
|
let errorMsg = '';
|
|
37
|
-
for (let i = 0; i <
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
62
|
+
for (let i = 0; i < results.length; i++) {
|
|
63
|
+
const result = results[i];
|
|
64
|
+
if (result.status === 'rejected') {
|
|
65
|
+
const cnf = configs[i];
|
|
66
|
+
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${result.reason}\n`;
|
|
67
|
+
}
|
|
41
68
|
}
|
|
42
69
|
if (errorMsg) {
|
|
43
70
|
throw new Error(errorMsg);
|
|
44
71
|
}
|
|
72
|
+
if (isInteractive && !errorMsg) {
|
|
73
|
+
(0, prompts_1.outro)('🎉 All done!');
|
|
74
|
+
}
|
|
45
75
|
}
|
|
46
76
|
else {
|
|
47
77
|
throw new Error('config is not found');
|
|
48
78
|
}
|
|
49
79
|
}
|
|
50
80
|
catch (error) {
|
|
51
|
-
|
|
81
|
+
(0, log_1.logError)(error);
|
|
82
|
+
process.exit(1);
|
|
52
83
|
}
|
|
53
84
|
});
|
|
54
85
|
}
|
package/dist/bin/openapi.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const prompts_1 = require("@clack/prompts");
|
|
5
6
|
const commander_1 = require("commander");
|
|
6
7
|
const lodash_1 = require("lodash");
|
|
7
8
|
const path_1 = require("path");
|
|
@@ -99,33 +100,50 @@ function run() {
|
|
|
99
100
|
if (cnf) {
|
|
100
101
|
const tasks = [];
|
|
101
102
|
let configs = Array.isArray(cnf) ? cnf : [cnf];
|
|
103
|
+
/** 是否交互式 */
|
|
104
|
+
let isInteractive = false;
|
|
102
105
|
if (params.uniqueKey) {
|
|
103
106
|
configs = configs.filter((config) => config.uniqueKey === params.uniqueKey);
|
|
104
107
|
}
|
|
108
|
+
else if (configs.length > 1) {
|
|
109
|
+
// 如果没有指定 uniqueKey,并且有多个配置,则交互式选择
|
|
110
|
+
isInteractive = true;
|
|
111
|
+
console.log(''); // 添加一个空行
|
|
112
|
+
(0, prompts_1.intro)('🎉 欢迎使用 openapi-ts-request 生成器');
|
|
113
|
+
const selected = yield (0, prompts_1.multiselect)({
|
|
114
|
+
message: '请选择要生成的 service',
|
|
115
|
+
options: configs.map((config) => ({
|
|
116
|
+
value: config,
|
|
117
|
+
label: config.describe || config.schemaPath,
|
|
118
|
+
})),
|
|
119
|
+
});
|
|
120
|
+
if ((0, prompts_1.isCancel)(selected)) {
|
|
121
|
+
(0, prompts_1.cancel)('👋 Has cancelled');
|
|
122
|
+
process.exit(0);
|
|
123
|
+
}
|
|
124
|
+
configs = selected;
|
|
125
|
+
}
|
|
105
126
|
for (const config of configs) {
|
|
106
127
|
tasks.push((0, index_1.generateService)(config));
|
|
107
128
|
}
|
|
108
129
|
const results = yield Promise.allSettled(tasks);
|
|
109
|
-
const errors = results.filter((result) => result.status === 'rejected');
|
|
110
130
|
let errorMsg = '';
|
|
111
|
-
for (let i = 0; i <
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
131
|
+
for (let i = 0; i < results.length; i++) {
|
|
132
|
+
const result = results[i];
|
|
133
|
+
if (result.status === 'rejected') {
|
|
134
|
+
const cnf = configs[i];
|
|
135
|
+
errorMsg += `${cnf.uniqueKey}${cnf.uniqueKey && ':'}${result.reason}\n`;
|
|
136
|
+
}
|
|
115
137
|
}
|
|
116
138
|
if (errorMsg) {
|
|
117
|
-
|
|
118
|
-
|
|
139
|
+
throw new Error(errorMsg);
|
|
140
|
+
}
|
|
141
|
+
if (isInteractive && !errorMsg) {
|
|
142
|
+
(0, prompts_1.outro)('🎉 All done!');
|
|
119
143
|
}
|
|
120
144
|
}
|
|
121
145
|
else {
|
|
122
|
-
|
|
123
|
-
(0, log_1.logError)('Please provide either input/output options or a configuration file path and name.');
|
|
124
|
-
process.exit(1);
|
|
125
|
-
}
|
|
126
|
-
const options = baseGenerate(params);
|
|
127
|
-
yield (0, index_1.generateService)((0, lodash_1.pickBy)(options, (value) => value !== null && value !== undefined && value !== ''));
|
|
128
|
-
process.exit(0);
|
|
146
|
+
throw new Error('Please provide either input/output options or a configuration file path and name.');
|
|
129
147
|
}
|
|
130
148
|
}
|
|
131
149
|
catch (error) {
|
|
@@ -249,7 +249,7 @@ class ServiceGenerator {
|
|
|
249
249
|
displayTypeLabelFileName: config_2.displayTypeLabelFileName,
|
|
250
250
|
});
|
|
251
251
|
// 打印日志
|
|
252
|
-
(0, log_1.default)('✅ 成功生成 api 文件目录-> ', this.config.serversPath);
|
|
252
|
+
(0, log_1.default)('✅ 成功生成 api 文件目录-> ', ` ${this.config.serversPath}`);
|
|
253
253
|
}
|
|
254
254
|
getInterfaceTPConfigs() {
|
|
255
255
|
var _a, _b, _c;
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,10 @@ export type GenerateServiceProps = {
|
|
|
23
23
|
* 是否全量替换, 默认: true, 如果为false, 则进行增量替换
|
|
24
24
|
*/
|
|
25
25
|
full?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* 描述信息,在用 cli 可交互运行方式时会用到
|
|
28
|
+
*/
|
|
29
|
+
describe?: string;
|
|
26
30
|
/**
|
|
27
31
|
* 开启日志
|
|
28
32
|
*/
|
|
@@ -280,3 +284,7 @@ export type GenerateServiceProps = {
|
|
|
280
284
|
};
|
|
281
285
|
};
|
|
282
286
|
export declare function generateService({ requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, includePaths, excludePaths, authorization, isTranslateToEnglishTag, priorityRule, timeout, reactQueryMode, apifoxConfig, ...rest }: GenerateServiceProps): Promise<void>;
|
|
287
|
+
/**
|
|
288
|
+
* Defines the configuration for openapi-ts-request.
|
|
289
|
+
*/
|
|
290
|
+
export declare function defineConfig(config: GenerateServiceProps | GenerateServiceProps[]): GenerateServiceProps | GenerateServiceProps[];
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateService = generateService;
|
|
4
|
+
exports.defineConfig = defineConfig;
|
|
4
5
|
const tslib_1 = require("tslib");
|
|
5
6
|
const lodash_1 = require("lodash");
|
|
6
7
|
const config_1 = require("./config");
|
|
@@ -53,3 +54,9 @@ function generateService(_a) {
|
|
|
53
54
|
}
|
|
54
55
|
});
|
|
55
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Defines the configuration for openapi-ts-request.
|
|
59
|
+
*/
|
|
60
|
+
function defineConfig(config) {
|
|
61
|
+
return config;
|
|
62
|
+
}
|
package/dist/log.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.logError = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
6
|
-
const log = (...rest) => console.log(
|
|
7
|
-
const logError = (...rest) => console.error(
|
|
6
|
+
const log = (...rest) => console.log(` ${chalk_1.default.blue('[openAPI]')}: ${rest.join('\n')}`);
|
|
7
|
+
const logError = (...rest) => console.error(` ${chalk_1.default.red('❌ [openAPI]')}: ${rest.join('\n')}`);
|
|
8
8
|
exports.logError = logError;
|
|
9
9
|
exports.default = log;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=18.0.0",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"prettier.config.cjs"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
+
"@clack/prompts": "^0.11.0",
|
|
27
28
|
"@prettier/sync": "^0.6.1",
|
|
28
29
|
"@trivago/prettier-plugin-sort-imports": "^5.2.1",
|
|
29
30
|
"axios": "^1.7.2",
|