-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathutil.ts
More file actions
370 lines (320 loc) · 9.58 KB
/
util.ts
File metadata and controls
370 lines (320 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import axios from 'axios';
import { translate } from 'bing-translate-api';
import http from 'http';
import https from 'https';
import * as yaml from 'js-yaml';
import {
camelCase as _camelCase_,
forEach,
isEmpty,
isObject,
keys,
map,
uniq,
} from 'lodash';
import { existsSync, readFileSync } from 'node:fs';
import { writeFile } from 'node:fs/promises';
import type { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';
import converter from 'swagger2openapi';
import log, { logError } from './log';
import type { OpenAPIObject, OperationObject } from './type';
import { type APIFoxBody, type GetSchemaByApifoxProps } from './type';
export const getImportStatement = (requestLibPath: string) => {
if (requestLibPath) {
if (requestLibPath.startsWith('import')) {
return requestLibPath;
}
return `import request from '${requestLibPath}';`;
}
return `import request from 'axios';`;
};
/**
* 通过 apifox 获取 openapi 文档
* @param params {object}
* @param params.projectId {string} 项目 id
* @param params.locale {string} 语言
* @param params.apifoxVersion {string} apifox 版本 目前固定为 2024-03-28 可通过 https://api.apifox.com/v1/versions 获取最新版本
* @returns
*/
const getSchemaByApifox = async ({
projectId,
locale = 'zh-CN',
apifoxVersion = '2024-03-28',
selectedTags,
excludedByTags = [],
apifoxToken,
oasVersion = '3.0',
exportFormat = 'JSON',
includeApifoxExtensionProperties = false,
addFoldersToTags = false,
branchId,
moduleId,
}: GetSchemaByApifoxProps): Promise<OpenAPI.Document | null> => {
try {
const body: APIFoxBody = {
scope: {
excludedByTags,
},
options: {
includeApifoxExtensionProperties,
addFoldersToTags,
},
oasVersion,
exportFormat,
};
if (branchId !== undefined) {
body.branchId = branchId;
}
if (moduleId !== undefined) {
body.moduleId = moduleId;
}
const tags = !isEmpty(selectedTags) ? selectedTags : '*';
if (tags === '*') {
body.scope.type = 'ALL';
} else {
body.scope.type = 'SELECTED_TAGS';
body.scope.selectedTags = tags;
}
const res = await axios.post(
`https://api.apifox.com/v1/projects/${projectId}/export-openapi?locale=${locale}`,
body,
{
headers: {
'X-Apifox-Api-Version': apifoxVersion,
Authorization: `Bearer ${apifoxToken}`,
},
}
);
return res.data as OpenAPI.Document;
} catch (error) {
logError('fetch openapi error:', error);
return null;
}
};
async function getSchema(
schemaPath: string,
authorization?: string,
timeout?: number
) {
if (schemaPath.startsWith('http')) {
const isHttps = schemaPath.startsWith('https:');
const protocol = isHttps ? https : http;
try {
const agent = new protocol.Agent({
rejectUnauthorized: false,
});
const config = isHttps ? { httpsAgent: agent } : { httpAgent: agent };
const json = await axios
.get(schemaPath, {
...config,
headers: { authorization },
timeout,
})
.then((res) => res.data as OpenAPI.Document);
return json;
} catch (error) {
console.log('fetch openapi error:', error);
}
return;
}
if (require.cache[schemaPath]) {
delete require.cache[schemaPath];
}
let schema: string | OpenAPI.Document = '';
try {
schema = (await require(schemaPath)) as OpenAPI.Document;
} catch {
try {
schema = readFileSync(schemaPath, 'utf8');
} catch (error) {
console.error('Error reading schema file:', error);
}
}
return schema;
}
function converterSwaggerToOpenApi(swagger: OpenAPI.Document) {
return new Promise<OpenAPIV3.Document>((resolve, reject) => {
const convertOptions = {
patch: true,
warnOnly: true,
resolveInternal: true,
};
// options.patch = true; // fix up small errors in the source definition
// options.warnOnly = true; // Do not throw on non-patchable errors
// options.warnOnly = true; // enable resolution of internal $refs, also disables deduplication of requestBodies
converter.convertObj(
swagger as OpenAPIV2.Document,
convertOptions,
(err, options) => {
log(['💺 将 Swagger 转化为 openAPI']);
if (err) {
return reject(err);
}
resolve(options.openapi);
}
);
});
}
export const getOpenAPIConfigByApifox = async (
props: GetSchemaByApifoxProps
) => {
const schema = await getSchemaByApifox(props);
if (!schema) {
return;
}
return await parseSwaggerOrOpenapi(schema);
};
export const getOpenAPIConfig = async (
schemaPath: string,
authorization?: string,
timeout?: number
) => {
const schema = await getSchema(schemaPath, authorization, timeout);
if (!schema) {
return;
}
const openAPI = await parseSwaggerOrOpenapi(schema);
return openAPI;
};
export async function parseSwaggerOrOpenapi(
content: string | OpenAPI.Document
) {
let openapi = {} as OpenAPI.Document;
if (isObject(content)) {
openapi = content;
// if is swagger2.0 json, covert swagger2.0 to openapi3.0
if ((openapi as OpenAPIV2.Document).swagger) {
openapi = await converterSwaggerToOpenApi(openapi);
}
} else {
if (isJSONString(content)) {
openapi = JSON.parse(content) as OpenAPI.Document;
} else {
openapi = yaml.load(content) as OpenAPI.Document;
}
if ((openapi as OpenAPIV2.Document).swagger) {
openapi = await converterSwaggerToOpenApi(openapi);
}
}
return openapi;
}
function isJSONString(str: string) {
try {
JSON.parse(str);
return true;
} catch (error) {
return false;
}
}
function readFileSafelySync(filePath: string) {
if (!existsSync(filePath)) {
logError(`文件 ${filePath} 不存在`);
return null;
}
try {
return readFileSync(filePath, 'utf-8');
} catch (error) {
logError(`读取文件 ${filePath} 时出错:`, error);
return null;
}
}
async function writeFileAsync(filePath: string, content: string) {
try {
await writeFile(filePath, content, 'utf8');
} catch (error) {
logError(`文件 ${filePath} 写入失败`);
}
}
export async function translateChineseModuleNodeToEnglish(
openAPI: OpenAPIObject
) {
const content = readFileSafelySync(
process.cwd() + '/openapi-ts-request.cache.json'
);
let i18n: Record<string, string> | null = {};
if (content !== null) {
if (isJSONString(content)) {
i18n = JSON.parse(content) as Record<string, string>;
}
}
return new Promise<Record<string, string> | boolean>((resolve, reject) => {
const translateMap: Record<string, string> = i18n;
const operations = [] as OperationObject[];
let tags: string[] = [];
forEach(keys(openAPI.paths), (path) => {
const pathItemObject = openAPI.paths[path];
forEach(keys(pathItemObject), (method: string) => {
if (pathItemObject[method]) {
const operation = pathItemObject[method] as OperationObject;
operations.push(operation);
tags = tags.concat(operation.tags);
}
});
});
void Promise.all(
map(uniq(tags), (tagName) => {
return new Promise((resolve) => {
if (tagName && /[\u3220-\uFA29]/.test(tagName) && !i18n[tagName]) {
void translate(tagName, null, 'en')
.then((translateRes) => {
const text = camelCase(translateRes?.translation);
if (text) {
translateMap[tagName] = text;
resolve(text);
}
})
.catch(() => {
resolve(tagName);
});
} else {
resolve(tagName);
}
});
})
)
.then(() => {
forEach(operations, (operation) => {
forEach(operation.tags, (tagName, index) => {
if (translateMap[tagName]) {
operation.tags[index] = translateMap[tagName];
}
});
});
resolve(translateMap);
// 在写入前再次读取缓存,合并多个任务的翻译结果
const existingContent = readFileSafelySync(
process.cwd() + '/openapi-ts-request.cache.json'
);
let existingCache: Record<string, string> = {};
if (existingContent !== null && isJSONString(existingContent)) {
existingCache = JSON.parse(existingContent) as Record<string, string>;
}
// 合并现有缓存和新的翻译结果(新结果优先)
const mergedCache = { ...existingCache, ...translateMap };
void writeFileAsync(
process.cwd() + '/openapi-ts-request.cache.json',
JSON.stringify(mergedCache, null, 2)
);
})
.catch(() => {
reject(false);
});
});
}
/**
* Converts a string to camelCase format, with an option to capitalize the first letter.
* 将字符串转换为驼峰格式,并可以选择将首字母大写。
*
* @param {string} str - The string to convert.
* @param {string} str - 要转换的字符串。
*
* @param {boolean} [upper=false] - Whether to capitalize the first letter of the resulting string.
* @param {boolean} [upper=false] - 是否将结果字符串的首字母大写。
*
* @returns {string} The camelCase formatted string, optionally with a capitalized first letter.
* @returns {string} 返回驼峰格式的字符串,可选择首字母大写。
*/
export const camelCase = (str: string, upper: boolean = false) => {
const res = _camelCase_(str);
return upper ? res.charAt(0).toUpperCase() + res.slice(1) : res;
};