openapi-ts-request 1.2.0 → 1.3.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 +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/dist/util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getOpenAPIConfig = exports.getImportStatement = void 0;
|
|
3
|
+
exports.camelCase = exports.getOpenAPIConfig = exports.getOpenAPIConfigByApifox = exports.getImportStatement = void 0;
|
|
4
4
|
exports.parseSwaggerOrOpenapi = parseSwaggerOrOpenapi;
|
|
5
5
|
exports.translateChineseModuleNodeToEnglish = translateChineseModuleNodeToEnglish;
|
|
6
6
|
const tslib_1 = require("tslib");
|
|
@@ -12,7 +12,7 @@ const yaml = tslib_1.__importStar(require("js-yaml"));
|
|
|
12
12
|
const lodash_1 = require("lodash");
|
|
13
13
|
const node_fs_1 = require("node:fs");
|
|
14
14
|
const swagger2openapi_1 = tslib_1.__importDefault(require("swagger2openapi"));
|
|
15
|
-
const log_1 = tslib_1.
|
|
15
|
+
const log_1 = tslib_1.__importStar(require("./log"));
|
|
16
16
|
const getImportStatement = (requestLibPath) => {
|
|
17
17
|
if (requestLibPath) {
|
|
18
18
|
if (requestLibPath.startsWith('import')) {
|
|
@@ -23,7 +23,78 @@ const getImportStatement = (requestLibPath) => {
|
|
|
23
23
|
return `import { request } from 'axios';`;
|
|
24
24
|
};
|
|
25
25
|
exports.getImportStatement = getImportStatement;
|
|
26
|
-
|
|
26
|
+
const getApifoxIncludeTags = (tags) => {
|
|
27
|
+
let _tags_ = '*';
|
|
28
|
+
if (tags && Array.isArray(tags)) {
|
|
29
|
+
if (!tags.length) {
|
|
30
|
+
return '*';
|
|
31
|
+
}
|
|
32
|
+
_tags_ = [];
|
|
33
|
+
for (const tag of tags) {
|
|
34
|
+
if (typeof tag === 'string') {
|
|
35
|
+
if (tag === '*') {
|
|
36
|
+
_tags_ = '*';
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (tag instanceof RegExp) {
|
|
41
|
+
_tags_ = '*';
|
|
42
|
+
break;
|
|
43
|
+
// TODO:后期添加支持判断字符串是否为正则
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
_tags_.push(tag);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else if (tags) {
|
|
51
|
+
_tags_ = [tags];
|
|
52
|
+
}
|
|
53
|
+
return _tags_;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* 通过 apifox 获取 openapi 文档
|
|
57
|
+
* @param params {object}
|
|
58
|
+
* @param params.projectId {string} 项目 id
|
|
59
|
+
* @param params.locale {string} 语言
|
|
60
|
+
* @param params.apifoxVersion {string} apifox 版本 目前固定为 2024-03-28 可通过 https://api.apifox.com/v1/versions 获取最新版本
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
63
|
+
const getSchemaByApifox = (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ projectId, locale = 'zh-CN', apifoxVersion = '2024-03-28', includeTags, excludeTags = [], apifoxToken, oasVersion = '3.0', exportFormat = 'JSON', includeApifoxExtensionProperties = false, addFoldersToTags = false, }) {
|
|
64
|
+
try {
|
|
65
|
+
const body = {
|
|
66
|
+
scope: {
|
|
67
|
+
excludeTags,
|
|
68
|
+
},
|
|
69
|
+
options: {
|
|
70
|
+
includeApifoxExtensionProperties,
|
|
71
|
+
addFoldersToTags,
|
|
72
|
+
},
|
|
73
|
+
oasVersion,
|
|
74
|
+
exportFormat,
|
|
75
|
+
};
|
|
76
|
+
const tags = getApifoxIncludeTags(includeTags);
|
|
77
|
+
if (tags === '*') {
|
|
78
|
+
body.scope.type = 'ALL';
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
body.scope.type = 'SELECTED_TAGS';
|
|
82
|
+
body.scope.includeTags = tags;
|
|
83
|
+
}
|
|
84
|
+
const res = yield axios_1.default.post(`https://api.apifox.com/v1/projects/${projectId}/export-openapi?locale=${locale}`, body, {
|
|
85
|
+
headers: {
|
|
86
|
+
'X-Apifox-Api-Version': apifoxVersion,
|
|
87
|
+
Authorization: `Bearer ${apifoxToken}`,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
return res.data;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
(0, log_1.logError)('fetch openapi error:', error);
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
function getSchema(schemaPath, authorization, timeout) {
|
|
27
98
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
28
99
|
if (schemaPath.startsWith('http')) {
|
|
29
100
|
const isHttps = schemaPath.startsWith('https:');
|
|
@@ -34,7 +105,7 @@ function getSchema(schemaPath, authorization) {
|
|
|
34
105
|
});
|
|
35
106
|
const config = isHttps ? { httpsAgent: agent } : { httpAgent: agent };
|
|
36
107
|
const json = yield axios_1.default
|
|
37
|
-
.get(schemaPath, Object.assign(Object.assign({}, config), { headers: { authorization } }))
|
|
108
|
+
.get(schemaPath, Object.assign(Object.assign({}, config), { headers: { authorization }, timeout }))
|
|
38
109
|
.then((res) => res.data);
|
|
39
110
|
return json;
|
|
40
111
|
}
|
|
@@ -80,8 +151,16 @@ function converterSwaggerToOpenApi(swagger) {
|
|
|
80
151
|
});
|
|
81
152
|
});
|
|
82
153
|
}
|
|
83
|
-
const
|
|
84
|
-
const schema = yield
|
|
154
|
+
const getOpenAPIConfigByApifox = (props) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
155
|
+
const schema = yield getSchemaByApifox(props);
|
|
156
|
+
if (!schema) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
return yield parseSwaggerOrOpenapi(schema);
|
|
160
|
+
});
|
|
161
|
+
exports.getOpenAPIConfigByApifox = getOpenAPIConfigByApifox;
|
|
162
|
+
const getOpenAPIConfig = (schemaPath, authorization, timeout) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
163
|
+
const schema = yield getSchema(schemaPath, authorization, timeout);
|
|
85
164
|
if (!schema) {
|
|
86
165
|
return;
|
|
87
166
|
}
|
|
@@ -143,7 +222,7 @@ function translateChineseModuleNodeToEnglish(openAPI) {
|
|
|
143
222
|
if (tagName && /[\u3220-\uFA29]/.test(tagName)) {
|
|
144
223
|
void (0, bing_translate_api_1.translate)(tagName, null, 'en')
|
|
145
224
|
.then((translateRes) => {
|
|
146
|
-
const text = (0,
|
|
225
|
+
const text = (0, exports.camelCase)(translateRes === null || translateRes === void 0 ? void 0 : translateRes.translation);
|
|
147
226
|
if (text) {
|
|
148
227
|
translateMap[tagName] = text;
|
|
149
228
|
resolve(text);
|
|
@@ -174,3 +253,21 @@ function translateChineseModuleNodeToEnglish(openAPI) {
|
|
|
174
253
|
});
|
|
175
254
|
});
|
|
176
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* Converts a string to camelCase format, with an option to capitalize the first letter.
|
|
258
|
+
* 将字符串转换为驼峰格式,并可以选择将首字母大写。
|
|
259
|
+
*
|
|
260
|
+
* @param {string} str - The string to convert.
|
|
261
|
+
* @param {string} str - 要转换的字符串。
|
|
262
|
+
*
|
|
263
|
+
* @param {boolean} [upper=false] - Whether to capitalize the first letter of the resulting string.
|
|
264
|
+
* @param {boolean} [upper=false] - 是否将结果字符串的首字母大写。
|
|
265
|
+
*
|
|
266
|
+
* @returns {string} The camelCase formatted string, optionally with a capitalized first letter.
|
|
267
|
+
* @returns {string} 返回驼峰格式的字符串,可选择首字母大写。
|
|
268
|
+
*/
|
|
269
|
+
const camelCase = (str, upper = false) => {
|
|
270
|
+
const res = (0, lodash_1.camelCase)(str);
|
|
271
|
+
return upper ? res.charAt(0).toUpperCase() + res.slice(1) : res;
|
|
272
|
+
};
|
|
273
|
+
exports.camelCase = camelCase;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
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",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"rimraf": "^6.0.1",
|
|
45
45
|
"swagger2openapi": "^7.0.8",
|
|
46
46
|
"tiny-pinyin": "^1.3.2",
|
|
47
|
+
"ts-morph": "^25.0.1",
|
|
47
48
|
"tslib": "^2.6.3"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
@@ -68,7 +69,7 @@
|
|
|
68
69
|
"lint-staged": "^15.3.0",
|
|
69
70
|
"openapi-types": "^12.1.3",
|
|
70
71
|
"ts-node": "^10.9.2",
|
|
71
|
-
"typescript": "5.
|
|
72
|
+
"typescript": "5.8.2"
|
|
72
73
|
},
|
|
73
74
|
"keywords": [
|
|
74
75
|
"openapi",
|
|
@@ -92,6 +93,7 @@
|
|
|
92
93
|
"lint": "eslint ./src --report-unused-disable-directives --max-warnings=0",
|
|
93
94
|
"lint:fix": "eslint ./src --report-unused-disable-directives --max-warnings=0 --fix",
|
|
94
95
|
"test": "rm -rf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
|
|
96
|
+
"test:new": "npm run build && cd ./test && node ./test.js && cd ..",
|
|
95
97
|
"test:windows": "rimraf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
|
|
96
98
|
"openapi-ts-request": "openapi-ts"
|
|
97
99
|
}
|