openapi-ts-request 0.4.11 → 0.6.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 +7 -6
- package/dist/bin/openapi.js +4 -2
- package/dist/generator/file.js +2 -2
- package/dist/generator/patchSchema.js +1 -2
- package/dist/generator/util.js +16 -17
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -3
- package/dist/parser-mock/util.d.ts +2 -2
- package/dist/parser-mock/util.js +5 -6
- package/dist/util.d.ts +5 -1
- package/dist/util.js +103 -6
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
- 支持通过 npx、CLI、Nodejs 的方式使用
|
|
14
14
|
- 支持自定义请求工具函数, 支持 Fetch、Axios、[UniApp-Request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Node.js、XHR 客户端
|
|
15
15
|
- 支持通过 tags 过滤生成结果
|
|
16
|
-
- 支持 JSON 定义文件
|
|
16
|
+
- 支持 JSON/YAML 定义文件
|
|
17
|
+
- 支持将中文 tag 名称翻译为英文 tag 名称
|
|
17
18
|
|
|
18
19
|
## 使用
|
|
19
20
|
|
|
@@ -133,7 +134,7 @@ $ openapi --help
|
|
|
133
134
|
-V, --version output the version number
|
|
134
135
|
-i, --input <string> OpenAPI specification, can be a path, url (required)
|
|
135
136
|
-o, --output <string> output directory (required)
|
|
136
|
-
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch"
|
|
137
|
+
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch", default is "axios"
|
|
137
138
|
--allowedTags <string[]> generate results from allowed tags
|
|
138
139
|
--requestOptionsType <string> custom request method options parameter type (default: "{ [key:
|
|
139
140
|
string]: unknown }")
|
|
@@ -143,8 +144,9 @@ $ openapi --help
|
|
|
143
144
|
"'api'"(string)
|
|
144
145
|
--isDisplayTypeLabel <boolean> generate label matching type field (default: false)
|
|
145
146
|
--isGenJsonSchemas <boolean> generate JSON Schemas (default: false)
|
|
146
|
-
--mockFolder <string> mock file path
|
|
147
|
+
--mockFolder <string> mock file path
|
|
147
148
|
--nullable <boolean> null instead of optional (default: false)
|
|
149
|
+
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
|
|
148
150
|
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
|
|
149
151
|
-h, --help display help for command
|
|
150
152
|
```
|
|
@@ -161,7 +163,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
161
163
|
| --- | --- | --- | --- | --- |
|
|
162
164
|
| schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
|
|
163
165
|
| serversPath | 否 | string | './src/apis' | 生成结果的文件夹路径 |
|
|
164
|
-
| requestLibPath | 否 | string |
|
|
166
|
+
| requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
|
|
165
167
|
| allowedTags | 否 | string[] | - | 根据指定的 tags 生成结果 |
|
|
166
168
|
| requestOptionsType | 否 | string | '{ [key: string]: unknown }' | 自定义请求方法 options 参数类型 |
|
|
167
169
|
| requestImportStatement | 否 | string | - | 自定义请求方法表达式,例如:"const request = require('@/request')" |
|
|
@@ -170,6 +172,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
170
172
|
| isGenJsonSchemas | 否 | boolean | false | 是否生成 JSON Schemas |
|
|
171
173
|
| mockFolder | 否 | string | './mocks' | mock文件路径 |
|
|
172
174
|
| nullable | 否 | boolean | false | 使用 null 代替可选 |
|
|
175
|
+
| isTranslateToEnglishTag | 否 | boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
|
|
173
176
|
| isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
|
|
174
177
|
| hook | 否 | [Custom Hook](#Custom-Hook) | - | 自定义 hook |
|
|
175
178
|
|
|
@@ -224,5 +227,3 @@ export declare function patchSchema<T extends object>(
|
|
|
224
227
|
## 感谢
|
|
225
228
|
|
|
226
229
|
- [openapi2typescript](https://github.com/chenshuai2144/openapi2typescript)
|
|
227
|
-
|
|
228
|
-
ps:由于 openapi2typescript 仓库作者不怎么维护这个工具,不会主动增加功能,有些激进的pr也不再合并,为了更大的自主性,也为了方便自己更好的维护此工具,所以基于此仓库重构代码并添加了很多功能,感谢原作者!
|
package/dist/bin/openapi.js
CHANGED
|
@@ -12,15 +12,16 @@ const params = commander_1.program
|
|
|
12
12
|
.version(pkg.version)
|
|
13
13
|
.requiredOption('-i, --input <string>', 'OpenAPI specification, can be a path, url (required)')
|
|
14
14
|
.requiredOption('-o, --output <string>', 'output directory (required)')
|
|
15
|
-
.option('--requestLibPath <string>', 'custom request lib path, for example: "@/request", "node-fetch"')
|
|
15
|
+
.option('--requestLibPath <string>', 'custom request lib path, for example: "@/request", "node-fetch", default is "axios"')
|
|
16
16
|
.option('--allowedTags <string[]>', 'generate results from allowed tags')
|
|
17
17
|
.option('--requestOptionsType <string>', 'custom request method options parameter type (default: "{ [key: string]: unknown }")')
|
|
18
18
|
.option('--requestImportStatement <string>', `custom request import statement, for example: "const request = require('@/request')"`)
|
|
19
19
|
.option('--apiPrefix <string>', `custom the prefix of the api path, for example: "api"(variable), "'api'"(string)`)
|
|
20
20
|
.option('--isDisplayTypeLabel <boolean>', 'generate label matching type field', false)
|
|
21
21
|
.option('--isGenJsonSchemas <boolean>', 'generate JSON Schemas', false)
|
|
22
|
-
.option('--mockFolder <string>', '
|
|
22
|
+
.option('--mockFolder <string>', 'mock file path')
|
|
23
23
|
.option('--nullable <boolean>', 'null instead of optional', false)
|
|
24
|
+
.option('--isTranslateToEnglishTag <boolean>', 'translate chinese tag name to english tag name', false)
|
|
24
25
|
.option('--isCamelCase <boolean>', 'camelCase naming of controller files and request client', true)
|
|
25
26
|
.parse(process.argv)
|
|
26
27
|
.opts();
|
|
@@ -47,6 +48,7 @@ function run() {
|
|
|
47
48
|
isGenJsonSchemas: JSON.parse(params.isGenJsonSchemas) === true,
|
|
48
49
|
mockFolder: params.mockFolder,
|
|
49
50
|
nullable: JSON.parse(params.nullable) === true,
|
|
51
|
+
isTranslateToEnglishTag: JSON.parse(params.isTranslateToEnglishTag) === true,
|
|
50
52
|
isCamelCase: JSON.parse(params.isCamelCase) === true,
|
|
51
53
|
});
|
|
52
54
|
process.exit(0);
|
package/dist/generator/file.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.prettierFile = void 0;
|
|
4
|
+
exports.writeFile = writeFile;
|
|
4
5
|
const tslib_1 = require("tslib");
|
|
5
6
|
const sync_1 = require("@prettier/sync");
|
|
6
7
|
const fs_1 = require("fs");
|
|
@@ -33,4 +34,3 @@ function writeFile(folderPath, fileName, content) {
|
|
|
33
34
|
});
|
|
34
35
|
return hasError;
|
|
35
36
|
}
|
|
36
|
-
exports.writeFile = writeFile;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.patchSchema =
|
|
3
|
+
exports.patchSchema = patchSchema;
|
|
4
4
|
const lodash_1 = require("lodash");
|
|
5
5
|
const util_1 = require("./util");
|
|
6
6
|
function patchSchema(schema, schemas) {
|
|
@@ -48,4 +48,3 @@ function patchSchema(schema, schemas) {
|
|
|
48
48
|
}
|
|
49
49
|
return schema;
|
|
50
50
|
}
|
|
51
|
-
exports.patchSchema = patchSchema;
|
package/dist/generator/util.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.stripDot = stripDot;
|
|
4
|
+
exports.resolveTypeName = resolveTypeName;
|
|
5
|
+
exports.getDefaultType = getDefaultType;
|
|
6
|
+
exports.getDefaultFileTag = getDefaultFileTag;
|
|
7
|
+
exports.handleDuplicateTypeNames = handleDuplicateTypeNames;
|
|
8
|
+
exports.getBasePrefix = getBasePrefix;
|
|
9
|
+
exports.genDefaultFunctionName = genDefaultFunctionName;
|
|
10
|
+
exports.getFinalFileName = getFinalFileName;
|
|
11
|
+
exports.replaceDot = replaceDot;
|
|
12
|
+
exports.resolveFunctionName = resolveFunctionName;
|
|
13
|
+
exports.markAllowSchema = markAllowSchema;
|
|
14
|
+
exports.isReferenceObject = isReferenceObject;
|
|
15
|
+
exports.isSchemaObject = isSchemaObject;
|
|
16
|
+
exports.isNonArraySchemaObject = isNonArraySchemaObject;
|
|
17
|
+
exports.isArraySchemaObject = isArraySchemaObject;
|
|
18
|
+
exports.isBinaryArraySchemaObject = isBinaryArraySchemaObject;
|
|
4
19
|
const tslib_1 = require("tslib");
|
|
5
20
|
const lodash_1 = require("lodash");
|
|
6
21
|
const reserved_words_1 = tslib_1.__importDefault(require("reserved-words"));
|
|
@@ -10,7 +25,6 @@ const config_1 = require("./config");
|
|
|
10
25
|
function stripDot(str) {
|
|
11
26
|
return str.replace(/[-_ .](\w)/g, (_all, letter) => letter.toUpperCase());
|
|
12
27
|
}
|
|
13
|
-
exports.stripDot = stripDot;
|
|
14
28
|
// 兼容C#泛型的typeLastName取法
|
|
15
29
|
function getTypeLastName(typeName) {
|
|
16
30
|
var _a, _b, _c, _d, _e;
|
|
@@ -55,7 +69,6 @@ function resolveTypeName(typeName) {
|
|
|
55
69
|
const noBlankName = name.replace(/ +/g, '');
|
|
56
70
|
return tiny_pinyin_1.default.convertToPinyin(noBlankName, '', true);
|
|
57
71
|
}
|
|
58
|
-
exports.resolveTypeName = resolveTypeName;
|
|
59
72
|
function getRefName(refObject) {
|
|
60
73
|
if (!isReferenceObject(refObject)) {
|
|
61
74
|
return refObject;
|
|
@@ -175,7 +188,6 @@ function getDefaultType(schemaObject, namespace = '', schemas) {
|
|
|
175
188
|
}
|
|
176
189
|
return 'unknown';
|
|
177
190
|
}
|
|
178
|
-
exports.getDefaultType = getDefaultType;
|
|
179
191
|
function getDefaultFileTag(operationObject, apiPath) {
|
|
180
192
|
return operationObject['x-swagger-router-controller']
|
|
181
193
|
? [operationObject['x-swagger-router-controller']]
|
|
@@ -183,7 +195,6 @@ function getDefaultFileTag(operationObject, apiPath) {
|
|
|
183
195
|
apiPath.replace('/', '').split('/')[1],
|
|
184
196
|
];
|
|
185
197
|
}
|
|
186
|
-
exports.getDefaultFileTag = getDefaultFileTag;
|
|
187
198
|
function findDuplicateTypeNames(arr) {
|
|
188
199
|
const counts = (0, lodash_1.countBy)(arr);
|
|
189
200
|
const duplicates = (0, lodash_1.filter)((0, lodash_1.keys)(counts), (key) => counts[key] > 1);
|
|
@@ -205,7 +216,6 @@ function handleDuplicateTypeNames(interfaceTPConfigs) {
|
|
|
205
216
|
});
|
|
206
217
|
}
|
|
207
218
|
}
|
|
208
|
-
exports.handleDuplicateTypeNames = handleDuplicateTypeNames;
|
|
209
219
|
// 检测所有path重复区域(prefix)
|
|
210
220
|
function getBasePrefix(paths) {
|
|
211
221
|
const arr = [];
|
|
@@ -231,7 +241,6 @@ function getBasePrefix(paths) {
|
|
|
231
241
|
});
|
|
232
242
|
return `${res.join('/')}/`;
|
|
233
243
|
}
|
|
234
|
-
exports.getBasePrefix = getBasePrefix;
|
|
235
244
|
// 将地址path路径转为大驼峰
|
|
236
245
|
function genDefaultFunctionName(path, pathBasePrefix) {
|
|
237
246
|
// 首字母转大写
|
|
@@ -253,18 +262,15 @@ function genDefaultFunctionName(path, pathBasePrefix) {
|
|
|
253
262
|
return toUpperFirstLetter(s);
|
|
254
263
|
}).join('');
|
|
255
264
|
}
|
|
256
|
-
exports.genDefaultFunctionName = genDefaultFunctionName;
|
|
257
265
|
function getFinalFileName(s) {
|
|
258
266
|
// 支持下划线、中划线和空格分隔符,注意分隔符枚举值的顺序不能改变,否则正则匹配会报错
|
|
259
267
|
return s.replace(/[-_ ](\w)/g, (_all, letter) => letter.toUpperCase());
|
|
260
268
|
}
|
|
261
|
-
exports.getFinalFileName = getFinalFileName;
|
|
262
269
|
function replaceDot(s) {
|
|
263
270
|
return s
|
|
264
271
|
.replace(/\./g, '_')
|
|
265
272
|
.replace(/[-_ ](\w)/g, (_all, letter) => letter.toUpperCase());
|
|
266
273
|
}
|
|
267
|
-
exports.replaceDot = replaceDot;
|
|
268
274
|
function resolveFunctionName(functionName, methodName) {
|
|
269
275
|
// 类型声明过滤关键字
|
|
270
276
|
if (reserved_words_1.default.check(functionName)) {
|
|
@@ -272,7 +278,6 @@ function resolveFunctionName(functionName, methodName) {
|
|
|
272
278
|
}
|
|
273
279
|
return functionName;
|
|
274
280
|
}
|
|
275
|
-
exports.resolveFunctionName = resolveFunctionName;
|
|
276
281
|
// 标记引用的 $ref 对应的schema
|
|
277
282
|
function markAllowSchema(schemaStr, schemas) {
|
|
278
283
|
const refs = schemaStr === null || schemaStr === void 0 ? void 0 : schemaStr.match(/#\/components\/schemas\/([A-Za-z0-9._-]+)/g);
|
|
@@ -285,30 +290,24 @@ function markAllowSchema(schemaStr, schemas) {
|
|
|
285
290
|
}
|
|
286
291
|
});
|
|
287
292
|
}
|
|
288
|
-
exports.markAllowSchema = markAllowSchema;
|
|
289
293
|
function isReferenceObject(schema) {
|
|
290
294
|
return (schema === null || schema === void 0 ? void 0 : schema.$ref) !== undefined;
|
|
291
295
|
}
|
|
292
|
-
exports.isReferenceObject = isReferenceObject;
|
|
293
296
|
function isSchemaObject(schema) {
|
|
294
297
|
return (schema === null || schema === void 0 ? void 0 : schema.properties) !== undefined;
|
|
295
298
|
}
|
|
296
|
-
exports.isSchemaObject = isSchemaObject;
|
|
297
299
|
function isNonArraySchemaObject(schema) {
|
|
298
300
|
return ((schema === null || schema === void 0 ? void 0 : schema.type) === 'object' &&
|
|
299
301
|
(schema === null || schema === void 0 ? void 0 : schema.properties) !== undefined);
|
|
300
302
|
}
|
|
301
|
-
exports.isNonArraySchemaObject = isNonArraySchemaObject;
|
|
302
303
|
function isArraySchemaObject(schema) {
|
|
303
304
|
return (((schema === null || schema === void 0 ? void 0 : schema.type) === 'array' ||
|
|
304
305
|
(schema === null || schema === void 0 ? void 0 : schema.type) === 'stringArray') &&
|
|
305
306
|
(schema === null || schema === void 0 ? void 0 : schema.items) !== undefined);
|
|
306
307
|
}
|
|
307
|
-
exports.isArraySchemaObject = isArraySchemaObject;
|
|
308
308
|
function isBinaryArraySchemaObject(schema) {
|
|
309
309
|
var _a, _b;
|
|
310
310
|
return (isArraySchemaObject(schema) &&
|
|
311
311
|
(((_a = schema.items) === null || _a === void 0 ? void 0 : _a.format) === 'binary' ||
|
|
312
312
|
((_b = schema.items) === null || _b === void 0 ? void 0 : _b.format) === 'base64'));
|
|
313
313
|
}
|
|
314
|
-
exports.isBinaryArraySchemaObject = isBinaryArraySchemaObject;
|
package/dist/index.d.ts
CHANGED
|
@@ -60,6 +60,10 @@ export type GenerateServiceProps = {
|
|
|
60
60
|
* 默认为false,true时使用null代替可选值
|
|
61
61
|
*/
|
|
62
62
|
nullable?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* 是否将中文 tag 名称翻译成英文 tag 名称
|
|
65
|
+
*/
|
|
66
|
+
isTranslateToEnglishTag?: boolean;
|
|
63
67
|
/**
|
|
64
68
|
* 模板文件、请求函数采用小驼峰命名
|
|
65
69
|
*/
|
|
@@ -131,4 +135,4 @@ export type GenerateServiceProps = {
|
|
|
131
135
|
customFileNames?: (operationObject: OperationObject, apiPath: string, apiMethod: string) => string[] | null;
|
|
132
136
|
};
|
|
133
137
|
};
|
|
134
|
-
export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, ...rest }: GenerateServiceProps): Promise<void>;
|
|
138
|
+
export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, isTranslateToEnglishTag, ...rest }: GenerateServiceProps): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateService =
|
|
3
|
+
exports.generateService = generateService;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const lodash_1 = require("lodash");
|
|
6
6
|
const mockGenarator_1 = require("./generator/mockGenarator");
|
|
@@ -9,7 +9,7 @@ const util_1 = require("./util");
|
|
|
9
9
|
tslib_1.__exportStar(require("./generator/patchSchema"), exports);
|
|
10
10
|
function generateService(_a) {
|
|
11
11
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
12
|
-
var { requestLibPath, schemaPath, mockFolder, allowedTags } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "allowedTags"]);
|
|
12
|
+
var { requestLibPath, schemaPath, mockFolder, allowedTags, isTranslateToEnglishTag } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "allowedTags", "isTranslateToEnglishTag"]);
|
|
13
13
|
if (!schemaPath) {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
@@ -17,6 +17,9 @@ function generateService(_a) {
|
|
|
17
17
|
if ((0, lodash_1.isEmpty)(openAPI)) {
|
|
18
18
|
return;
|
|
19
19
|
}
|
|
20
|
+
if (isTranslateToEnglishTag) {
|
|
21
|
+
yield (0, util_1.translateChineseModuleNodeToEnglish)(openAPI);
|
|
22
|
+
}
|
|
20
23
|
const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
|
|
21
24
|
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', nullable: false, isCamelCase: true, isDisplayTypeLabel: false, isGenJsonSchemas: false, allowedTags: allowedTags
|
|
22
25
|
? (0, lodash_1.map)(allowedTags, (item) => item.toLowerCase())
|
|
@@ -30,4 +33,3 @@ function generateService(_a) {
|
|
|
30
33
|
}
|
|
31
34
|
});
|
|
32
35
|
}
|
|
33
|
-
exports.generateService = generateService;
|
|
@@ -32,7 +32,7 @@ export declare function inferSchema(thing: ParameterObject | SchemaObject | Refe
|
|
|
32
32
|
externalDocs?: OpenAPIV3.ExternalDocumentationObject;
|
|
33
33
|
example?: any;
|
|
34
34
|
deprecated?: boolean;
|
|
35
|
-
format?: "
|
|
35
|
+
format?: import("../type").ISchemaObjectFormat;
|
|
36
36
|
additionalProperties?: boolean | import("../type").ISchemaObject;
|
|
37
37
|
properties?: {
|
|
38
38
|
[name: string]: import("../type").ISchemaObject;
|
|
@@ -74,7 +74,7 @@ export declare function inferSchema(thing: ParameterObject | SchemaObject | Refe
|
|
|
74
74
|
externalDocs?: OpenAPIV3.ExternalDocumentationObject;
|
|
75
75
|
example?: any;
|
|
76
76
|
deprecated?: boolean;
|
|
77
|
-
format?: "
|
|
77
|
+
format?: import("../type").ISchemaObjectFormat;
|
|
78
78
|
additionalProperties?: boolean | import("../type").ISchemaObject;
|
|
79
79
|
properties?: {
|
|
80
80
|
[name: string]: import("../type").ISchemaObject;
|
package/dist/parser-mock/util.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.objectify = objectify;
|
|
4
|
+
exports.get = get;
|
|
5
|
+
exports.normalizeArray = normalizeArray;
|
|
6
|
+
exports.inferSchema = inferSchema;
|
|
7
|
+
exports.getRandomInt = getRandomInt;
|
|
4
8
|
const lodash_1 = require("lodash");
|
|
5
9
|
const util_1 = require("../generator/util");
|
|
6
10
|
function objectify(thing) {
|
|
@@ -8,7 +12,6 @@ function objectify(thing) {
|
|
|
8
12
|
return {};
|
|
9
13
|
return thing;
|
|
10
14
|
}
|
|
11
|
-
exports.objectify = objectify;
|
|
12
15
|
function get(openAPI, path) {
|
|
13
16
|
var _a, _b;
|
|
14
17
|
const refPaths = path.split('/');
|
|
@@ -18,13 +21,11 @@ function get(openAPI, path) {
|
|
|
18
21
|
}
|
|
19
22
|
return schema;
|
|
20
23
|
}
|
|
21
|
-
exports.get = get;
|
|
22
24
|
function normalizeArray(arr) {
|
|
23
25
|
if ((0, lodash_1.isArray)(arr))
|
|
24
26
|
return arr;
|
|
25
27
|
return [arr];
|
|
26
28
|
}
|
|
27
|
-
exports.normalizeArray = normalizeArray;
|
|
28
29
|
function isParameterObject(thing) {
|
|
29
30
|
return (thing === null || thing === void 0 ? void 0 : thing.schema) !== undefined;
|
|
30
31
|
}
|
|
@@ -37,10 +38,8 @@ function inferSchema(thing) {
|
|
|
37
38
|
}
|
|
38
39
|
return thing;
|
|
39
40
|
}
|
|
40
|
-
exports.inferSchema = inferSchema;
|
|
41
41
|
function getRandomInt(min, max) {
|
|
42
42
|
const minCeiled = Math.ceil(min);
|
|
43
43
|
const maxFloored = Math.floor(max);
|
|
44
44
|
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
|
|
45
45
|
}
|
|
46
|
-
exports.getRandomInt = getRandomInt;
|
package/dist/util.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
+
import { OpenAPI } from 'openapi-types';
|
|
2
|
+
import { OpenAPIObject } from './type';
|
|
1
3
|
export declare const getImportStatement: (requestLibPath: string) => string;
|
|
2
|
-
export declare const getOpenAPIConfig: (schemaPath: string) => Promise<
|
|
4
|
+
export declare const getOpenAPIConfig: (schemaPath: string) => Promise<OpenAPI.Document<{}>>;
|
|
5
|
+
export declare function parseSwaggerOrOpenapi(content: string | OpenAPI.Document): Promise<OpenAPI.Document<{}>>;
|
|
6
|
+
export declare function translateChineseModuleNodeToEnglish(openAPI: OpenAPIObject): Promise<unknown>;
|
package/dist/util.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getOpenAPIConfig = exports.getImportStatement = void 0;
|
|
4
|
+
exports.parseSwaggerOrOpenapi = parseSwaggerOrOpenapi;
|
|
5
|
+
exports.translateChineseModuleNodeToEnglish = translateChineseModuleNodeToEnglish;
|
|
4
6
|
const tslib_1 = require("tslib");
|
|
5
7
|
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
8
|
+
const bing_translate_api_1 = require("bing-translate-api");
|
|
6
9
|
const http_1 = tslib_1.__importDefault(require("http"));
|
|
7
10
|
const https_1 = tslib_1.__importDefault(require("https"));
|
|
11
|
+
const yaml = tslib_1.__importStar(require("js-yaml"));
|
|
12
|
+
const lodash_1 = require("lodash");
|
|
13
|
+
const node_fs_1 = require("node:fs");
|
|
8
14
|
const swagger2openapi_1 = tslib_1.__importDefault(require("swagger2openapi"));
|
|
9
15
|
const log_1 = tslib_1.__importDefault(require("./log"));
|
|
10
16
|
const getImportStatement = (requestLibPath) => {
|
|
@@ -40,14 +46,22 @@ function getSchema(schemaPath) {
|
|
|
40
46
|
if (require.cache[schemaPath]) {
|
|
41
47
|
delete require.cache[schemaPath];
|
|
42
48
|
}
|
|
43
|
-
|
|
49
|
+
let schema = '';
|
|
50
|
+
try {
|
|
51
|
+
schema = (yield require(schemaPath));
|
|
52
|
+
}
|
|
53
|
+
catch (_a) {
|
|
54
|
+
try {
|
|
55
|
+
schema = (0, node_fs_1.readFileSync)(schemaPath, 'utf8');
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
console.error('Error reading schema file:', error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
44
61
|
return schema;
|
|
45
62
|
});
|
|
46
63
|
}
|
|
47
64
|
function converterSwaggerToOpenApi(swagger) {
|
|
48
|
-
if (!swagger.swagger) {
|
|
49
|
-
return swagger;
|
|
50
|
-
}
|
|
51
65
|
return new Promise((resolve, reject) => {
|
|
52
66
|
const convertOptions = {
|
|
53
67
|
patch: true,
|
|
@@ -69,9 +83,92 @@ function converterSwaggerToOpenApi(swagger) {
|
|
|
69
83
|
const getOpenAPIConfig = (schemaPath) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
70
84
|
const schema = yield getSchema(schemaPath);
|
|
71
85
|
if (!schema) {
|
|
72
|
-
return
|
|
86
|
+
return;
|
|
73
87
|
}
|
|
74
|
-
const openAPI = yield
|
|
88
|
+
const openAPI = yield parseSwaggerOrOpenapi(schema);
|
|
75
89
|
return openAPI;
|
|
76
90
|
});
|
|
77
91
|
exports.getOpenAPIConfig = getOpenAPIConfig;
|
|
92
|
+
function parseSwaggerOrOpenapi(content) {
|
|
93
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
let openapi = {};
|
|
95
|
+
if ((0, lodash_1.isObject)(content)) {
|
|
96
|
+
openapi = content;
|
|
97
|
+
// if is swagger2.0 json, covert swagger2.0 to openapi3.0
|
|
98
|
+
if (openapi.swagger) {
|
|
99
|
+
openapi = yield converterSwaggerToOpenApi(openapi);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
if (isJSONString(content)) {
|
|
104
|
+
openapi = JSON.parse(content);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
openapi = yaml.load(content);
|
|
108
|
+
}
|
|
109
|
+
if (openapi.swagger) {
|
|
110
|
+
openapi = yield converterSwaggerToOpenApi(openapi);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return openapi;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function isJSONString(str) {
|
|
117
|
+
try {
|
|
118
|
+
JSON.parse(str);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function translateChineseModuleNodeToEnglish(openAPI) {
|
|
126
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
const translateMap = {};
|
|
129
|
+
const operations = [];
|
|
130
|
+
let tags = [];
|
|
131
|
+
(0, lodash_1.forEach)((0, lodash_1.keys)(openAPI.paths), (path) => {
|
|
132
|
+
const pathItemObject = openAPI.paths[path];
|
|
133
|
+
(0, lodash_1.forEach)((0, lodash_1.keys)(pathItemObject), (method) => {
|
|
134
|
+
if (pathItemObject[method]) {
|
|
135
|
+
const operation = pathItemObject[method];
|
|
136
|
+
operations.push(operation);
|
|
137
|
+
tags = tags.concat(operation.tags);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
void Promise.all((0, lodash_1.map)((0, lodash_1.uniq)(tags), (tagName) => {
|
|
142
|
+
return new Promise((resolve) => {
|
|
143
|
+
void (0, bing_translate_api_1.translate)(tagName, null, 'en')
|
|
144
|
+
.then((translateRes) => {
|
|
145
|
+
const text = (0, lodash_1.camelCase)(translateRes === null || translateRes === void 0 ? void 0 : translateRes.translation);
|
|
146
|
+
if (text) {
|
|
147
|
+
translateMap[tagName] = text;
|
|
148
|
+
resolve(text);
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
.catch(() => {
|
|
152
|
+
resolve(tagName);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}))
|
|
156
|
+
.then(() => {
|
|
157
|
+
(0, lodash_1.map)(operations, (operation) => {
|
|
158
|
+
var _a;
|
|
159
|
+
const tagName = (_a = operation.tags) === null || _a === void 0 ? void 0 : _a[0];
|
|
160
|
+
if (tagName && /[\u3220-\uFA29]/.test(tagName)) {
|
|
161
|
+
operation.tags = [
|
|
162
|
+
translateMap[tagName],
|
|
163
|
+
...operation.tags.slice(1),
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
resolve(true);
|
|
168
|
+
})
|
|
169
|
+
.catch(() => {
|
|
170
|
+
reject(false);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Swagger2/OpenAPI3 to TypeScript, request client, request mock service, enum, type field label, JSON Schemas",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,18 +23,20 @@
|
|
|
23
23
|
"@prettier/sync": "^0.5.2",
|
|
24
24
|
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
25
25
|
"axios": "^1.7.2",
|
|
26
|
+
"bing-translate-api": "^4.0.2",
|
|
26
27
|
"chalk": "^4.1.2",
|
|
27
28
|
"commander": "^12.1.0",
|
|
28
29
|
"cosmiconfig": "^9.0.0",
|
|
29
30
|
"cosmiconfig-typescript-loader": "^5.0.0",
|
|
30
|
-
"glob": "^
|
|
31
|
+
"glob": "^11.0.0",
|
|
32
|
+
"js-yaml": "^4.1.0",
|
|
31
33
|
"lodash": "^4.17.21",
|
|
32
34
|
"memoizee": "^0.4.17",
|
|
33
35
|
"mockjs": "^1.1.0",
|
|
34
36
|
"nunjucks": "^3.2.4",
|
|
35
37
|
"prettier": "^3.3.2",
|
|
36
38
|
"reserved-words": "^0.1.2",
|
|
37
|
-
"rimraf": "^
|
|
39
|
+
"rimraf": "^6.0.1",
|
|
38
40
|
"swagger2openapi": "^7.0.8",
|
|
39
41
|
"tiny-pinyin": "^1.3.2",
|
|
40
42
|
"tslib": "^2.6.3"
|
|
@@ -44,6 +46,7 @@
|
|
|
44
46
|
"@changesets/cli": "^2.27.6",
|
|
45
47
|
"@commitlint/cli": "^19.2.1",
|
|
46
48
|
"@commitlint/config-conventional": "^19.2.2",
|
|
49
|
+
"@types/js-yaml": "^4.0.9",
|
|
47
50
|
"@types/lodash": "^4.17.5",
|
|
48
51
|
"@types/memoizee": "^0.4.11",
|
|
49
52
|
"@types/mockjs": "^1.0.10",
|
|
@@ -53,12 +56,11 @@
|
|
|
53
56
|
"@types/swagger2openapi": "^7.0.4",
|
|
54
57
|
"@typescript-eslint/eslint-plugin": "^7.9.0",
|
|
55
58
|
"@typescript-eslint/parser": "^7.9.0",
|
|
56
|
-
"eslint": "^8.
|
|
59
|
+
"eslint": "^8.57.1",
|
|
57
60
|
"husky": "^9.0.11",
|
|
58
61
|
"lint-staged": "^15.2.5",
|
|
59
|
-
"np": "^10.0.6",
|
|
60
62
|
"openapi-types": "^12.1.3",
|
|
61
|
-
"typescript": "5.
|
|
63
|
+
"typescript": "5.6.2"
|
|
62
64
|
},
|
|
63
65
|
"keywords": [
|
|
64
66
|
"openapi",
|
|
@@ -75,7 +77,6 @@
|
|
|
75
77
|
"start": "tsc -w",
|
|
76
78
|
"build": "tsc",
|
|
77
79
|
"changeset": "changeset",
|
|
78
|
-
"prepublish:test": "npm run build && np --no-cleanup --yolo --no-publish --any-branch",
|
|
79
80
|
"lint": "eslint ./src --report-unused-disable-directives --max-warnings=0",
|
|
80
81
|
"lint:fix": "eslint ./src --report-unused-disable-directives --max-warnings=0 --fix",
|
|
81
82
|
"test": "rm -rf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
|