yapi-to-typescript2 1.0.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/LICENSE +21 -0
- package/README.md +22 -0
- package/lib/cjs/Generator.js +1135 -0
- package/lib/cjs/SwaggerToYApiServer.js +336 -0
- package/lib/cjs/cli.js +285 -0
- package/lib/cjs/helpers.js +230 -0
- package/lib/cjs/index.js +19 -0
- package/lib/cjs/swaggerJsonToYApiData.js +496 -0
- package/lib/cjs/types.js +100 -0
- package/lib/cjs/utils.js +766 -0
- package/lib/esm/Generator.js +1111 -0
- package/lib/esm/SwaggerToYApiServer.js +317 -0
- package/lib/esm/cli.js +257 -0
- package/lib/esm/helpers.js +216 -0
- package/lib/esm/index.d.ts +845 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/swaggerJsonToYApiData.js +483 -0
- package/lib/esm/types.js +79 -0
- package/lib/esm/utils.js +707 -0
- package/package.json +114 -0
package/lib/esm/utils.js
ADDED
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
2
|
+
import _createForOfIteratorHelperLoose from "@babel/runtime/helpers/esm/createForOfIteratorHelperLoose";
|
|
3
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
4
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
5
|
+
import fs from 'fs-extra';
|
|
6
|
+
import JSON5 from 'json5';
|
|
7
|
+
import nodeFetch from 'node-fetch';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import prettier from 'prettier';
|
|
10
|
+
import ProxyAgent from 'proxy-agent';
|
|
11
|
+
import toJsonSchema from 'to-json-schema';
|
|
12
|
+
import { castArray, cloneDeepFast, forOwn, isArray, isEmpty, isObject, mapKeys, memoize, run, traverse } from 'vtils';
|
|
13
|
+
import { compile } from 'json-schema-to-typescript';
|
|
14
|
+
import { FileData } from './helpers';
|
|
15
|
+
import { Method, RequestBodyType, RequestFormItemType, Required, ResponseBodyType } from './types';
|
|
16
|
+
import { URL } from 'url';
|
|
17
|
+
/**
|
|
18
|
+
* 抛出错误。
|
|
19
|
+
*
|
|
20
|
+
* @param msg 错误信息
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export function throwError() {
|
|
24
|
+
for (var _len = arguments.length, msg = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
25
|
+
msg[_key] = arguments[_key];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* istanbul ignore next */
|
|
29
|
+
throw new Error(msg.join(''));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 将路径统一为 unix 风格的路径。
|
|
33
|
+
*
|
|
34
|
+
* @param path 路径
|
|
35
|
+
* @returns unix 风格的路径
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export function toUnixPath(path) {
|
|
39
|
+
return path.replace(/[/\\]+/g, '/');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 获得规范化的相对路径。
|
|
43
|
+
*
|
|
44
|
+
* @param from 来源路径
|
|
45
|
+
* @param to 去向路径
|
|
46
|
+
* @returns 相对路径
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
export function getNormalizedRelativePath(from, to) {
|
|
50
|
+
return toUnixPath(path.relative(path.dirname(from), to)).replace(/^(?=[^.])/, './').replace(/\.(ts|js)x?$/i, '');
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 原地遍历 JSONSchema。
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
export function traverseJsonSchema(jsonSchema, cb, currentPath) {
|
|
57
|
+
if (currentPath === void 0) {
|
|
58
|
+
currentPath = [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* istanbul ignore if */
|
|
62
|
+
if (!isObject(jsonSchema)) return jsonSchema; // Mock.toJSONSchema 产生的 properties 为数组,然而 JSONSchema4 的 properties 为对象
|
|
63
|
+
|
|
64
|
+
if (isArray(jsonSchema.properties)) {
|
|
65
|
+
jsonSchema.properties = jsonSchema.properties.reduce(function (props, js) {
|
|
66
|
+
props[js.name] = js;
|
|
67
|
+
return props;
|
|
68
|
+
}, {});
|
|
69
|
+
} // 处理传入的 JSONSchema
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
cb(jsonSchema, currentPath); // 继续处理对象的子元素
|
|
73
|
+
|
|
74
|
+
if (jsonSchema.properties) {
|
|
75
|
+
forOwn(jsonSchema.properties, function (item, key) {
|
|
76
|
+
return traverseJsonSchema(item, cb, [].concat(currentPath, [key]));
|
|
77
|
+
});
|
|
78
|
+
} // 继续处理数组的子元素
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if (jsonSchema.items) {
|
|
82
|
+
castArray(jsonSchema.items).forEach(function (item, index) {
|
|
83
|
+
return traverseJsonSchema(item, cb, [].concat(currentPath, [index]));
|
|
84
|
+
});
|
|
85
|
+
} // 处理 oneOf
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if (jsonSchema.oneOf) {
|
|
89
|
+
jsonSchema.oneOf.forEach(function (item) {
|
|
90
|
+
return traverseJsonSchema(item, cb, currentPath);
|
|
91
|
+
});
|
|
92
|
+
} // 处理 anyOf
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
if (jsonSchema.anyOf) {
|
|
96
|
+
jsonSchema.anyOf.forEach(function (item) {
|
|
97
|
+
return traverseJsonSchema(item, cb, currentPath);
|
|
98
|
+
});
|
|
99
|
+
} // 处理 allOf
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
if (jsonSchema.allOf) {
|
|
103
|
+
jsonSchema.allOf.forEach(function (item) {
|
|
104
|
+
return traverseJsonSchema(item, cb, currentPath);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return jsonSchema;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 原地处理 JSONSchema。
|
|
112
|
+
*
|
|
113
|
+
* @param jsonSchema 待处理的 JSONSchema
|
|
114
|
+
* @returns 处理后的 JSONSchema
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
export function processJsonSchema(jsonSchema, customTypeMapping) {
|
|
118
|
+
return traverseJsonSchema(jsonSchema, function (jsonSchema) {
|
|
119
|
+
// 删除通过 swagger 导入时未剔除的 ref
|
|
120
|
+
delete jsonSchema.$ref;
|
|
121
|
+
delete jsonSchema.$$ref; // 数组只取第一个判断类型
|
|
122
|
+
|
|
123
|
+
if (jsonSchema.type === 'array' && Array.isArray(jsonSchema.items) && jsonSchema.items.length) {
|
|
124
|
+
jsonSchema.items = jsonSchema.items[0];
|
|
125
|
+
} // 处理类型名称为标准的 JSONSchema 类型名称
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
if (jsonSchema.type) {
|
|
129
|
+
// 类型映射表,键都为小写
|
|
130
|
+
var typeMapping = _extends({
|
|
131
|
+
byte: 'integer',
|
|
132
|
+
short: 'integer',
|
|
133
|
+
int: 'integer',
|
|
134
|
+
long: 'integer',
|
|
135
|
+
float: 'number',
|
|
136
|
+
double: 'number',
|
|
137
|
+
bigdecimal: 'number',
|
|
138
|
+
char: 'string',
|
|
139
|
+
void: 'null'
|
|
140
|
+
}, mapKeys(customTypeMapping, function (_, key) {
|
|
141
|
+
return key.toLowerCase();
|
|
142
|
+
}));
|
|
143
|
+
|
|
144
|
+
var isMultiple = Array.isArray(jsonSchema.type);
|
|
145
|
+
var types = castArray(jsonSchema.type).map(function (type) {
|
|
146
|
+
// 所有类型转成小写,如:String -> string
|
|
147
|
+
type = type.toLowerCase(); // 映射为标准的 JSONSchema 类型
|
|
148
|
+
|
|
149
|
+
type = typeMapping[type] || type;
|
|
150
|
+
return type;
|
|
151
|
+
});
|
|
152
|
+
jsonSchema.type = isMultiple ? types : types[0];
|
|
153
|
+
} // 移除字段名称首尾空格
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
if (jsonSchema.properties) {
|
|
157
|
+
forOwn(jsonSchema.properties, function (_, prop) {
|
|
158
|
+
var propDef = jsonSchema.properties[prop];
|
|
159
|
+
delete jsonSchema.properties[prop];
|
|
160
|
+
jsonSchema.properties[prop.trim()] = propDef;
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
if (Array.isArray(jsonSchema.required)) {
|
|
164
|
+
jsonSchema.required = jsonSchema.required.map(function (prop) {
|
|
165
|
+
return prop.trim();
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return jsonSchema;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 获取适用于 JSTT 的 JSONSchema。
|
|
175
|
+
*
|
|
176
|
+
* @param jsonSchema 待处理的 JSONSchema
|
|
177
|
+
* @returns 适用于 JSTT 的 JSONSchema
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
export function jsonSchemaToJSTTJsonSchema(jsonSchema, typeName) {
|
|
181
|
+
if (jsonSchema) {
|
|
182
|
+
// 去除最外层的 description 以防止 JSTT 提取它作为类型的注释
|
|
183
|
+
delete jsonSchema.description;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return traverseJsonSchema(jsonSchema, function (jsonSchema, currentPath) {
|
|
187
|
+
// 支持类型引用
|
|
188
|
+
var refValue = // YApi 低版本不支持配置 title,可以在 description 里配置
|
|
189
|
+
jsonSchema.title == null ? jsonSchema.description : jsonSchema.title;
|
|
190
|
+
|
|
191
|
+
if (refValue != null && refValue.startsWith('&')) {
|
|
192
|
+
var typeRelativePath = refValue.substring(1);
|
|
193
|
+
var typeAbsolutePath = toUnixPath(path.resolve(path.dirname(("/" + currentPath.join('/')).replace(/\/{2,}/g, '/')), typeRelativePath).replace(/^[a-z]+:/i, ''));
|
|
194
|
+
var typeAbsolutePathArr = typeAbsolutePath.split('/').filter(Boolean);
|
|
195
|
+
var tsTypeLeft = '';
|
|
196
|
+
var tsTypeRight = typeName;
|
|
197
|
+
|
|
198
|
+
for (var _iterator = _createForOfIteratorHelperLoose(typeAbsolutePathArr), _step; !(_step = _iterator()).done;) {
|
|
199
|
+
var key = _step.value;
|
|
200
|
+
tsTypeLeft += 'NonNullable<';
|
|
201
|
+
tsTypeRight += "[" + JSON.stringify(key) + "]>";
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
var tsType = "" + tsTypeLeft + tsTypeRight;
|
|
205
|
+
jsonSchema.tsType = tsType;
|
|
206
|
+
} // 去除 title 和 id,防止 json-schema-to-typescript 提取它们作为接口名
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
delete jsonSchema.title;
|
|
210
|
+
delete jsonSchema.id; // 忽略数组长度限制
|
|
211
|
+
|
|
212
|
+
delete jsonSchema.minItems;
|
|
213
|
+
delete jsonSchema.maxItems;
|
|
214
|
+
|
|
215
|
+
if (jsonSchema.type === 'object') {
|
|
216
|
+
// 将 additionalProperties 设为 false
|
|
217
|
+
jsonSchema.additionalProperties = false;
|
|
218
|
+
} // 删除 default,防止 json-schema-to-typescript 根据它推测类型
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
delete jsonSchema.default;
|
|
222
|
+
return jsonSchema;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* 将 JSONSchema 字符串转为 JSONSchema 对象。
|
|
227
|
+
*
|
|
228
|
+
* @param str 要转换的 JSONSchema 字符串
|
|
229
|
+
* @returns 转换后的 JSONSchema 对象
|
|
230
|
+
*/
|
|
231
|
+
|
|
232
|
+
export function jsonSchemaStringToJsonSchema(str, customTypeMapping) {
|
|
233
|
+
return processJsonSchema(JSON.parse(str), customTypeMapping);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* 获得 JSON 数据的 JSONSchema 对象。
|
|
237
|
+
*
|
|
238
|
+
* @param json JSON 数据
|
|
239
|
+
* @returns JSONSchema 对象
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
export function jsonToJsonSchema(json, customTypeMapping) {
|
|
243
|
+
var schema = toJsonSchema(json, {
|
|
244
|
+
required: false,
|
|
245
|
+
arrays: {
|
|
246
|
+
mode: 'first'
|
|
247
|
+
},
|
|
248
|
+
objects: {
|
|
249
|
+
additionalProperties: false
|
|
250
|
+
},
|
|
251
|
+
strings: {
|
|
252
|
+
detectFormat: false
|
|
253
|
+
},
|
|
254
|
+
postProcessFnc: function postProcessFnc(type, schema, value) {
|
|
255
|
+
if (!schema.description && !!value && type !== 'object') {
|
|
256
|
+
schema.description = JSON.stringify(value);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return schema;
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
delete schema.description;
|
|
263
|
+
return processJsonSchema(schema, customTypeMapping);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 获得 mockjs 模板的 JSONSchema 对象。
|
|
267
|
+
*
|
|
268
|
+
* @param template mockjs 模板
|
|
269
|
+
* @returns JSONSchema 对象
|
|
270
|
+
*/
|
|
271
|
+
|
|
272
|
+
export function mockjsTemplateToJsonSchema(template, customTypeMapping) {
|
|
273
|
+
var actions = []; // https://github.com/nuysoft/Mock/blob/refactoring/src/mock/constant.js#L27
|
|
274
|
+
|
|
275
|
+
var keyRe = /(.+)\|(?:\+(\d+)|([+-]?\d+-?[+-]?\d*)?(?:\.(\d+-?\d*))?)/; // https://github.com/nuysoft/Mock/wiki/Mock.Random
|
|
276
|
+
|
|
277
|
+
var numberPatterns = ['natural', 'integer', 'float', 'range', 'increment'];
|
|
278
|
+
var boolPatterns = ['boolean', 'bool'];
|
|
279
|
+
|
|
280
|
+
var normalizeValue = function normalizeValue(value) {
|
|
281
|
+
if (typeof value === 'string' && value.startsWith('@')) {
|
|
282
|
+
var pattern = value.slice(1);
|
|
283
|
+
|
|
284
|
+
if (numberPatterns.some(function (p) {
|
|
285
|
+
return pattern.startsWith(p);
|
|
286
|
+
})) {
|
|
287
|
+
return 1;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (boolPatterns.some(function (p) {
|
|
291
|
+
return pattern.startsWith(p);
|
|
292
|
+
})) {
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return value;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
traverse(template, function (value, key, parent) {
|
|
301
|
+
if (typeof key === 'string') {
|
|
302
|
+
actions.push(function () {
|
|
303
|
+
delete parent[key];
|
|
304
|
+
parent[// https://github.com/nuysoft/Mock/blob/refactoring/src/mock/schema/schema.js#L16
|
|
305
|
+
key.replace(keyRe, '$1')] = normalizeValue(value);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
actions.forEach(function (action) {
|
|
310
|
+
return action();
|
|
311
|
+
});
|
|
312
|
+
return jsonToJsonSchema(template, customTypeMapping);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* 获得属性定义列表的 JSONSchema 对象。
|
|
316
|
+
*
|
|
317
|
+
* @param propDefinitions 属性定义列表
|
|
318
|
+
* @returns JSONSchema 对象
|
|
319
|
+
*/
|
|
320
|
+
|
|
321
|
+
export function propDefinitionsToJsonSchema(propDefinitions, customTypeMapping) {
|
|
322
|
+
return processJsonSchema({
|
|
323
|
+
type: 'object',
|
|
324
|
+
required: propDefinitions.reduce(function (res, prop) {
|
|
325
|
+
if (prop.required) {
|
|
326
|
+
res.push(prop.name);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return res;
|
|
330
|
+
}, []),
|
|
331
|
+
properties: propDefinitions.reduce(function (res, prop) {
|
|
332
|
+
res[prop.name] = _extends({
|
|
333
|
+
type: prop.type,
|
|
334
|
+
description: prop.comment
|
|
335
|
+
}, prop.type === 'file' ? {
|
|
336
|
+
tsType: FileData.name
|
|
337
|
+
} : {});
|
|
338
|
+
return res;
|
|
339
|
+
}, {})
|
|
340
|
+
}, customTypeMapping);
|
|
341
|
+
}
|
|
342
|
+
var JSTTOptions = {
|
|
343
|
+
bannerComment: '',
|
|
344
|
+
style: {
|
|
345
|
+
bracketSpacing: false,
|
|
346
|
+
printWidth: 120,
|
|
347
|
+
semi: true,
|
|
348
|
+
singleQuote: true,
|
|
349
|
+
tabWidth: 2,
|
|
350
|
+
trailingComma: 'none',
|
|
351
|
+
useTabs: false
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
/**
|
|
355
|
+
* 根据 JSONSchema 对象生产 TypeScript 类型定义。
|
|
356
|
+
*
|
|
357
|
+
* @param jsonSchema JSONSchema 对象
|
|
358
|
+
* @param typeName 类型名称
|
|
359
|
+
* @returns TypeScript 类型定义
|
|
360
|
+
*/
|
|
361
|
+
|
|
362
|
+
export function jsonSchemaToType(_x, _x2) {
|
|
363
|
+
return _jsonSchemaToType.apply(this, arguments);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function _jsonSchemaToType() {
|
|
367
|
+
_jsonSchemaToType = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(jsonSchema, typeName) {
|
|
368
|
+
var fakeTypeName, code;
|
|
369
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
370
|
+
while (1) {
|
|
371
|
+
switch (_context.prev = _context.next) {
|
|
372
|
+
case 0:
|
|
373
|
+
if (!isEmpty(jsonSchema)) {
|
|
374
|
+
_context.next = 2;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return _context.abrupt("return", "export interface " + typeName + " {}");
|
|
379
|
+
|
|
380
|
+
case 2:
|
|
381
|
+
if (!jsonSchema.__is_any__) {
|
|
382
|
+
_context.next = 5;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
delete jsonSchema.__is_any__;
|
|
387
|
+
return _context.abrupt("return", "export type " + typeName + " = any");
|
|
388
|
+
|
|
389
|
+
case 5:
|
|
390
|
+
// JSTT 会转换 typeName,因此传入一个全大写的假 typeName,生成代码后再替换回真正的 typeName
|
|
391
|
+
fakeTypeName = 'THISISAFAKETYPENAME';
|
|
392
|
+
_context.next = 8;
|
|
393
|
+
return compile(jsonSchemaToJSTTJsonSchema(cloneDeepFast(jsonSchema), typeName), fakeTypeName, JSTTOptions);
|
|
394
|
+
|
|
395
|
+
case 8:
|
|
396
|
+
code = _context.sent;
|
|
397
|
+
return _context.abrupt("return", code.replace(fakeTypeName, typeName).trim());
|
|
398
|
+
|
|
399
|
+
case 10:
|
|
400
|
+
case "end":
|
|
401
|
+
return _context.stop();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}, _callee);
|
|
405
|
+
}));
|
|
406
|
+
return _jsonSchemaToType.apply(this, arguments);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export function getRequestDataJsonSchema(interfaceInfo, customTypeMapping) {
|
|
410
|
+
var jsonSchema; // 处理表单数据(仅 POST 类接口)
|
|
411
|
+
|
|
412
|
+
if (isPostLikeMethod(interfaceInfo.method)) {
|
|
413
|
+
switch (interfaceInfo.req_body_type) {
|
|
414
|
+
case RequestBodyType.form:
|
|
415
|
+
jsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_body_form.map(function (item) {
|
|
416
|
+
return {
|
|
417
|
+
name: item.name,
|
|
418
|
+
required: item.required === Required.true,
|
|
419
|
+
type: item.type === RequestFormItemType.file ? 'file' : 'string',
|
|
420
|
+
comment: item.desc
|
|
421
|
+
};
|
|
422
|
+
}), customTypeMapping);
|
|
423
|
+
break;
|
|
424
|
+
|
|
425
|
+
case RequestBodyType.json:
|
|
426
|
+
if (interfaceInfo.req_body_other) {
|
|
427
|
+
jsonSchema = interfaceInfo.req_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.req_body_other, customTypeMapping) : jsonToJsonSchema(JSON5.parse(interfaceInfo.req_body_other), customTypeMapping);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
break;
|
|
431
|
+
|
|
432
|
+
default:
|
|
433
|
+
/* istanbul ignore next */
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
} // 处理查询数据
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
if (isArray(interfaceInfo.req_query) && interfaceInfo.req_query.length) {
|
|
440
|
+
var queryJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_query.map(function (item) {
|
|
441
|
+
return {
|
|
442
|
+
name: item.name,
|
|
443
|
+
required: item.required === Required.true,
|
|
444
|
+
type: item.type || 'string',
|
|
445
|
+
comment: item.desc
|
|
446
|
+
};
|
|
447
|
+
}), customTypeMapping);
|
|
448
|
+
/* istanbul ignore else */
|
|
449
|
+
|
|
450
|
+
if (jsonSchema) {
|
|
451
|
+
jsonSchema.properties = _extends({}, jsonSchema.properties, queryJsonSchema.properties);
|
|
452
|
+
jsonSchema.required = [].concat(Array.isArray(jsonSchema.required) ? jsonSchema.required : [], Array.isArray(queryJsonSchema.required) ? queryJsonSchema.required : []);
|
|
453
|
+
} else {
|
|
454
|
+
jsonSchema = queryJsonSchema;
|
|
455
|
+
}
|
|
456
|
+
} // 处理路径参数
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
if (isArray(interfaceInfo.req_params) && interfaceInfo.req_params.length) {
|
|
460
|
+
var paramsJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_params.map(function (item) {
|
|
461
|
+
return {
|
|
462
|
+
name: item.name,
|
|
463
|
+
required: true,
|
|
464
|
+
type: item.type || 'string',
|
|
465
|
+
comment: item.desc
|
|
466
|
+
};
|
|
467
|
+
}), customTypeMapping);
|
|
468
|
+
/* istanbul ignore else */
|
|
469
|
+
|
|
470
|
+
if (jsonSchema) {
|
|
471
|
+
jsonSchema.properties = _extends({}, jsonSchema.properties, paramsJsonSchema.properties);
|
|
472
|
+
jsonSchema.required = [].concat(Array.isArray(jsonSchema.required) ? jsonSchema.required : [], Array.isArray(paramsJsonSchema.required) ? paramsJsonSchema.required : []);
|
|
473
|
+
} else {
|
|
474
|
+
jsonSchema = paramsJsonSchema;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return jsonSchema || {};
|
|
479
|
+
}
|
|
480
|
+
export function getResponseDataJsonSchema(interfaceInfo, customTypeMapping, dataKey) {
|
|
481
|
+
var jsonSchema = {};
|
|
482
|
+
|
|
483
|
+
switch (interfaceInfo.res_body_type) {
|
|
484
|
+
case ResponseBodyType.json:
|
|
485
|
+
if (interfaceInfo.res_body) {
|
|
486
|
+
jsonSchema = interfaceInfo.res_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.res_body, customTypeMapping) : mockjsTemplateToJsonSchema(JSON5.parse(interfaceInfo.res_body), customTypeMapping);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
break;
|
|
490
|
+
|
|
491
|
+
default:
|
|
492
|
+
jsonSchema = {
|
|
493
|
+
__is_any__: true
|
|
494
|
+
};
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (dataKey && jsonSchema) {
|
|
499
|
+
jsonSchema = reachJsonSchema(jsonSchema, dataKey);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
return jsonSchema;
|
|
503
|
+
}
|
|
504
|
+
export function reachJsonSchema(jsonSchema, path) {
|
|
505
|
+
var last = jsonSchema;
|
|
506
|
+
|
|
507
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(castArray(path)), _step2; !(_step2 = _iterator2()).done;) {
|
|
508
|
+
var _last$properties;
|
|
509
|
+
|
|
510
|
+
var segment = _step2.value;
|
|
511
|
+
|
|
512
|
+
var _last = (_last$properties = last.properties) == null ? void 0 : _last$properties[segment];
|
|
513
|
+
|
|
514
|
+
if (!_last) {
|
|
515
|
+
return jsonSchema;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
last = _last;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return last;
|
|
522
|
+
}
|
|
523
|
+
export function sortByWeights(list) {
|
|
524
|
+
list.sort(function (a, b) {
|
|
525
|
+
var _x$weights;
|
|
526
|
+
|
|
527
|
+
var x = a.weights.length > b.weights.length ? b : a;
|
|
528
|
+
var minLen = Math.min(a.weights.length, b.weights.length);
|
|
529
|
+
var maxLen = Math.max(a.weights.length, b.weights.length);
|
|
530
|
+
|
|
531
|
+
(_x$weights = x.weights).push.apply(_x$weights, new Array(maxLen - minLen).fill(0));
|
|
532
|
+
|
|
533
|
+
var w = a.weights.reduce(function (w, _, i) {
|
|
534
|
+
if (w === 0) {
|
|
535
|
+
w = a.weights[i] - b.weights[i];
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return w;
|
|
539
|
+
}, 0);
|
|
540
|
+
return w;
|
|
541
|
+
});
|
|
542
|
+
return list;
|
|
543
|
+
}
|
|
544
|
+
export function isGetLikeMethod(method) {
|
|
545
|
+
return method === Method.GET || method === Method.OPTIONS || method === Method.HEAD;
|
|
546
|
+
}
|
|
547
|
+
export function isPostLikeMethod(method) {
|
|
548
|
+
return !isGetLikeMethod(method);
|
|
549
|
+
}
|
|
550
|
+
export function getPrettier(_x3) {
|
|
551
|
+
return _getPrettier.apply(this, arguments);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function _getPrettier() {
|
|
555
|
+
_getPrettier = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(cwd) {
|
|
556
|
+
var projectPrettierPath;
|
|
557
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
558
|
+
while (1) {
|
|
559
|
+
switch (_context2.prev = _context2.next) {
|
|
560
|
+
case 0:
|
|
561
|
+
projectPrettierPath = path.join(cwd, 'node_modules/prettier');
|
|
562
|
+
_context2.next = 3;
|
|
563
|
+
return fs.pathExists(projectPrettierPath);
|
|
564
|
+
|
|
565
|
+
case 3:
|
|
566
|
+
if (!_context2.sent) {
|
|
567
|
+
_context2.next = 5;
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return _context2.abrupt("return", require(projectPrettierPath));
|
|
572
|
+
|
|
573
|
+
case 5:
|
|
574
|
+
return _context2.abrupt("return", require('prettier'));
|
|
575
|
+
|
|
576
|
+
case 6:
|
|
577
|
+
case "end":
|
|
578
|
+
return _context2.stop();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}, _callee2);
|
|
582
|
+
}));
|
|
583
|
+
return _getPrettier.apply(this, arguments);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
export function getPrettierOptions() {
|
|
587
|
+
return _getPrettierOptions.apply(this, arguments);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function _getPrettierOptions() {
|
|
591
|
+
_getPrettierOptions = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
|
|
592
|
+
var prettierOptions, _yield$run, prettierConfigPathErr, prettierConfigPath, _yield$run2, prettierConfigErr, prettierConfig;
|
|
593
|
+
|
|
594
|
+
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
595
|
+
while (1) {
|
|
596
|
+
switch (_context3.prev = _context3.next) {
|
|
597
|
+
case 0:
|
|
598
|
+
prettierOptions = {
|
|
599
|
+
parser: 'typescript',
|
|
600
|
+
printWidth: 120,
|
|
601
|
+
tabWidth: 2,
|
|
602
|
+
singleQuote: true,
|
|
603
|
+
semi: false,
|
|
604
|
+
trailingComma: 'all',
|
|
605
|
+
bracketSpacing: false,
|
|
606
|
+
endOfLine: 'lf'
|
|
607
|
+
}; // 测试时跳过本地配置的解析
|
|
608
|
+
|
|
609
|
+
if (!process.env.JEST_WORKER_ID) {
|
|
610
|
+
_context3.next = 3;
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return _context3.abrupt("return", prettierOptions);
|
|
615
|
+
|
|
616
|
+
case 3:
|
|
617
|
+
_context3.next = 5;
|
|
618
|
+
return run(function () {
|
|
619
|
+
return prettier.resolveConfigFile();
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
case 5:
|
|
623
|
+
_yield$run = _context3.sent;
|
|
624
|
+
prettierConfigPathErr = _yield$run[0];
|
|
625
|
+
prettierConfigPath = _yield$run[1];
|
|
626
|
+
|
|
627
|
+
if (!(prettierConfigPathErr || !prettierConfigPath)) {
|
|
628
|
+
_context3.next = 10;
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
return _context3.abrupt("return", prettierOptions);
|
|
633
|
+
|
|
634
|
+
case 10:
|
|
635
|
+
_context3.next = 12;
|
|
636
|
+
return run(function () {
|
|
637
|
+
return prettier.resolveConfig(prettierConfigPath);
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
case 12:
|
|
641
|
+
_yield$run2 = _context3.sent;
|
|
642
|
+
prettierConfigErr = _yield$run2[0];
|
|
643
|
+
prettierConfig = _yield$run2[1];
|
|
644
|
+
|
|
645
|
+
if (!(prettierConfigErr || !prettierConfig)) {
|
|
646
|
+
_context3.next = 17;
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
return _context3.abrupt("return", prettierOptions);
|
|
651
|
+
|
|
652
|
+
case 17:
|
|
653
|
+
return _context3.abrupt("return", _extends({}, prettierOptions, prettierConfig, {
|
|
654
|
+
parser: 'typescript'
|
|
655
|
+
}));
|
|
656
|
+
|
|
657
|
+
case 18:
|
|
658
|
+
case "end":
|
|
659
|
+
return _context3.stop();
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}, _callee3);
|
|
663
|
+
}));
|
|
664
|
+
return _getPrettierOptions.apply(this, arguments);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
export var getCachedPrettierOptions = memoize(getPrettierOptions);
|
|
668
|
+
export function httpGet(_x4, _x5) {
|
|
669
|
+
return _httpGet.apply(this, arguments);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function _httpGet() {
|
|
673
|
+
_httpGet = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(url, query) {
|
|
674
|
+
var _url, res;
|
|
675
|
+
|
|
676
|
+
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
677
|
+
while (1) {
|
|
678
|
+
switch (_context4.prev = _context4.next) {
|
|
679
|
+
case 0:
|
|
680
|
+
_url = new URL(url);
|
|
681
|
+
|
|
682
|
+
if (query) {
|
|
683
|
+
Object.keys(query).forEach(function (key) {
|
|
684
|
+
_url.searchParams.set(key, query[key]);
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
url = _url.toString();
|
|
689
|
+
_context4.next = 5;
|
|
690
|
+
return nodeFetch(url, {
|
|
691
|
+
method: 'GET',
|
|
692
|
+
agent: new ProxyAgent()
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
case 5:
|
|
696
|
+
res = _context4.sent;
|
|
697
|
+
return _context4.abrupt("return", res.json());
|
|
698
|
+
|
|
699
|
+
case 7:
|
|
700
|
+
case "end":
|
|
701
|
+
return _context4.stop();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}, _callee4);
|
|
705
|
+
}));
|
|
706
|
+
return _httpGet.apply(this, arguments);
|
|
707
|
+
}
|