openapi-ts-request 0.5.0 → 0.7.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 +10 -7
- package/dist/bin/openapi.js +6 -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 +9 -1
- package/dist/index.js +6 -4
- package/dist/parser-mock/util.d.ts +2 -2
- package/dist/parser-mock/util.js +5 -6
- package/dist/util.d.ts +3 -1
- package/dist/util.js +58 -6
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
- 支持自定义请求工具函数, 支持 Fetch、Axios、[UniApp-Request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Node.js、XHR 客户端
|
|
15
15
|
- 支持通过 tags 过滤生成结果
|
|
16
16
|
- 支持 JSON/YAML 定义文件
|
|
17
|
+
- 支持将中文 tag 名称翻译为英文 tag 名称
|
|
17
18
|
|
|
18
19
|
## 使用
|
|
19
20
|
|
|
@@ -133,8 +134,8 @@ $ 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
|
-
--allowedTags <string[]> generate
|
|
137
|
+
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch", default is "axios"
|
|
138
|
+
--allowedTags <string[]> generate code from allowed tags
|
|
138
139
|
--requestOptionsType <string> custom request method options parameter type (default: "{ [key:
|
|
139
140
|
string]: unknown }")
|
|
140
141
|
--requestImportStatement <string> custom request import statement, for example: "const request =
|
|
@@ -143,8 +144,10 @@ $ 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
|
|
148
|
+
--authorization <string> docs authorization
|
|
147
149
|
--nullable <boolean> null instead of optional (default: false)
|
|
150
|
+
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
|
|
148
151
|
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
|
|
149
152
|
-h, --help display help for command
|
|
150
153
|
```
|
|
@@ -161,15 +164,17 @@ openapi --i ./spec.json --o ./apis
|
|
|
161
164
|
| --- | --- | --- | --- | --- |
|
|
162
165
|
| schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
|
|
163
166
|
| serversPath | 否 | string | './src/apis' | 生成结果的文件夹路径 |
|
|
164
|
-
| requestLibPath | 否 | string |
|
|
165
|
-
| allowedTags | 否 | string[] | - | 根据指定的 tags
|
|
167
|
+
| requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
|
|
168
|
+
| allowedTags | 否 | string[] | - | 根据指定的 tags 生成代码 |
|
|
166
169
|
| requestOptionsType | 否 | string | '{ [key: string]: unknown }' | 自定义请求方法 options 参数类型 |
|
|
167
170
|
| requestImportStatement | 否 | string | - | 自定义请求方法表达式,例如:"const request = require('@/request')" |
|
|
168
171
|
| apiPrefix | 否 | string | - | api path的前缀,例如:'api'(动态变量), "'api'"(字符串) |
|
|
169
172
|
| isDisplayTypeLabel | 否 | boolean | false | 是否生成 type 对应的label |
|
|
170
173
|
| isGenJsonSchemas | 否 | boolean | false | 是否生成 JSON Schemas |
|
|
171
174
|
| mockFolder | 否 | string | './mocks' | mock文件路径 |
|
|
175
|
+
| authorization | 否 | string | - | 文档权限凭证 |
|
|
172
176
|
| nullable | 否 | boolean | false | 使用 null 代替可选 |
|
|
177
|
+
| isTranslateToEnglishTag | 否 | boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
|
|
173
178
|
| isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
|
|
174
179
|
| hook | 否 | [Custom Hook](#Custom-Hook) | - | 自定义 hook |
|
|
175
180
|
|
|
@@ -224,5 +229,3 @@ export declare function patchSchema<T extends object>(
|
|
|
224
229
|
## 感谢
|
|
225
230
|
|
|
226
231
|
- [openapi2typescript](https://github.com/chenshuai2144/openapi2typescript)
|
|
227
|
-
|
|
228
|
-
ps:由于 openapi2typescript 仓库作者不怎么维护这个工具,不会主动增加功能,有些激进的pr也不再合并,为了更大的自主性,也为了方便自己更好的维护此工具,所以基于此仓库重构代码并添加了很多功能,感谢原作者!
|
package/dist/bin/openapi.js
CHANGED
|
@@ -12,15 +12,17 @@ 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
|
+
.option('--authorization <string>', 'docs authorization')
|
|
23
24
|
.option('--nullable <boolean>', 'null instead of optional', false)
|
|
25
|
+
.option('--isTranslateToEnglishTag <boolean>', 'translate chinese tag name to english tag name', false)
|
|
24
26
|
.option('--isCamelCase <boolean>', 'camelCase naming of controller files and request client', true)
|
|
25
27
|
.parse(process.argv)
|
|
26
28
|
.opts();
|
|
@@ -46,7 +48,9 @@ function run() {
|
|
|
46
48
|
isDisplayTypeLabel: JSON.parse(params.isDisplayTypeLabel) === true,
|
|
47
49
|
isGenJsonSchemas: JSON.parse(params.isGenJsonSchemas) === true,
|
|
48
50
|
mockFolder: params.mockFolder,
|
|
51
|
+
authorization: params.authorization,
|
|
49
52
|
nullable: JSON.parse(params.nullable) === true,
|
|
53
|
+
isTranslateToEnglishTag: JSON.parse(params.isTranslateToEnglishTag) === true,
|
|
50
54
|
isCamelCase: JSON.parse(params.isCamelCase) === true,
|
|
51
55
|
});
|
|
52
56
|
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
|
@@ -56,10 +56,18 @@ export type GenerateServiceProps = {
|
|
|
56
56
|
* mock目录
|
|
57
57
|
*/
|
|
58
58
|
mockFolder?: string;
|
|
59
|
+
/**
|
|
60
|
+
* 文档权限凭证
|
|
61
|
+
*/
|
|
62
|
+
authorization?: string;
|
|
59
63
|
/**
|
|
60
64
|
* 默认为false,true时使用null代替可选值
|
|
61
65
|
*/
|
|
62
66
|
nullable?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* 是否将中文 tag 名称翻译成英文 tag 名称
|
|
69
|
+
*/
|
|
70
|
+
isTranslateToEnglishTag?: boolean;
|
|
63
71
|
/**
|
|
64
72
|
* 模板文件、请求函数采用小驼峰命名
|
|
65
73
|
*/
|
|
@@ -131,4 +139,4 @@ export type GenerateServiceProps = {
|
|
|
131
139
|
customFileNames?: (operationObject: OperationObject, apiPath: string, apiMethod: string) => string[] | null;
|
|
132
140
|
};
|
|
133
141
|
};
|
|
134
|
-
export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, ...rest }: GenerateServiceProps): Promise<void>;
|
|
142
|
+
export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, authorization, 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,14 +9,17 @@ 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, authorization, isTranslateToEnglishTag } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "allowedTags", "authorization", "isTranslateToEnglishTag"]);
|
|
13
13
|
if (!schemaPath) {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
|
-
const openAPI = (yield (0, util_1.getOpenAPIConfig)(schemaPath));
|
|
16
|
+
const openAPI = (yield (0, util_1.getOpenAPIConfig)(schemaPath, authorization));
|
|
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,4 +1,6 @@
|
|
|
1
1
|
import { OpenAPI } from 'openapi-types';
|
|
2
|
+
import { OpenAPIObject } from './type';
|
|
2
3
|
export declare const getImportStatement: (requestLibPath: string) => string;
|
|
3
|
-
export declare const getOpenAPIConfig: (schemaPath: string) => Promise<OpenAPI.Document<{}>>;
|
|
4
|
+
export declare const getOpenAPIConfig: (schemaPath: string, authorization?: string) => Promise<OpenAPI.Document<{}>>;
|
|
4
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,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
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"));
|
|
8
11
|
const yaml = tslib_1.__importStar(require("js-yaml"));
|
|
@@ -20,7 +23,7 @@ const getImportStatement = (requestLibPath) => {
|
|
|
20
23
|
return `import request from 'axios';`;
|
|
21
24
|
};
|
|
22
25
|
exports.getImportStatement = getImportStatement;
|
|
23
|
-
function getSchema(schemaPath) {
|
|
26
|
+
function getSchema(schemaPath, authorization) {
|
|
24
27
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
25
28
|
if (schemaPath.startsWith('http')) {
|
|
26
29
|
const isHttps = schemaPath.startsWith('https:');
|
|
@@ -31,7 +34,7 @@ function getSchema(schemaPath) {
|
|
|
31
34
|
});
|
|
32
35
|
const config = isHttps ? { httpsAgent: agent } : { httpAgent: agent };
|
|
33
36
|
const json = yield axios_1.default
|
|
34
|
-
.get(schemaPath, config)
|
|
37
|
+
.get(schemaPath, Object.assign(Object.assign({}, config), { headers: { authorization } }))
|
|
35
38
|
.then((res) => res.data);
|
|
36
39
|
return json;
|
|
37
40
|
}
|
|
@@ -77,8 +80,8 @@ function converterSwaggerToOpenApi(swagger) {
|
|
|
77
80
|
});
|
|
78
81
|
});
|
|
79
82
|
}
|
|
80
|
-
const getOpenAPIConfig = (schemaPath) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
-
const schema = yield getSchema(schemaPath);
|
|
83
|
+
const getOpenAPIConfig = (schemaPath, authorization) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
84
|
+
const schema = yield getSchema(schemaPath, authorization);
|
|
82
85
|
if (!schema) {
|
|
83
86
|
return;
|
|
84
87
|
}
|
|
@@ -110,7 +113,6 @@ function parseSwaggerOrOpenapi(content) {
|
|
|
110
113
|
return openapi;
|
|
111
114
|
});
|
|
112
115
|
}
|
|
113
|
-
exports.parseSwaggerOrOpenapi = parseSwaggerOrOpenapi;
|
|
114
116
|
function isJSONString(str) {
|
|
115
117
|
try {
|
|
116
118
|
JSON.parse(str);
|
|
@@ -120,3 +122,53 @@ function isJSONString(str) {
|
|
|
120
122
|
return false;
|
|
121
123
|
}
|
|
122
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.7.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,11 +23,12 @@
|
|
|
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",
|
|
31
32
|
"js-yaml": "^4.1.0",
|
|
32
33
|
"lodash": "^4.17.21",
|
|
33
34
|
"memoizee": "^0.4.17",
|
|
@@ -35,7 +36,7 @@
|
|
|
35
36
|
"nunjucks": "^3.2.4",
|
|
36
37
|
"prettier": "^3.3.2",
|
|
37
38
|
"reserved-words": "^0.1.2",
|
|
38
|
-
"rimraf": "^
|
|
39
|
+
"rimraf": "^6.0.1",
|
|
39
40
|
"swagger2openapi": "^7.0.8",
|
|
40
41
|
"tiny-pinyin": "^1.3.2",
|
|
41
42
|
"tslib": "^2.6.3"
|
|
@@ -55,12 +56,11 @@
|
|
|
55
56
|
"@types/swagger2openapi": "^7.0.4",
|
|
56
57
|
"@typescript-eslint/eslint-plugin": "^7.9.0",
|
|
57
58
|
"@typescript-eslint/parser": "^7.9.0",
|
|
58
|
-
"eslint": "^8.
|
|
59
|
+
"eslint": "^8.57.1",
|
|
59
60
|
"husky": "^9.0.11",
|
|
60
61
|
"lint-staged": "^15.2.5",
|
|
61
|
-
"np": "^10.0.6",
|
|
62
62
|
"openapi-types": "^12.1.3",
|
|
63
|
-
"typescript": "5.
|
|
63
|
+
"typescript": "5.6.2"
|
|
64
64
|
},
|
|
65
65
|
"keywords": [
|
|
66
66
|
"openapi",
|
|
@@ -77,7 +77,6 @@
|
|
|
77
77
|
"start": "tsc -w",
|
|
78
78
|
"build": "tsc",
|
|
79
79
|
"changeset": "changeset",
|
|
80
|
-
"prepublish:test": "npm run build && np --no-cleanup --yolo --no-publish --any-branch",
|
|
81
80
|
"lint": "eslint ./src --report-unused-disable-directives --max-warnings=0",
|
|
82
81
|
"lint:fix": "eslint ./src --report-unused-disable-directives --max-warnings=0 --fix",
|
|
83
82
|
"test": "rm -rf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
|