yapi-apifox-to-typescript 4.2.3 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/ApifoxToYApiData.js +312 -588
- package/lib/cjs/Generator.js +507 -1171
- package/lib/cjs/SwaggerToYApiServer.js +125 -336
- package/lib/cjs/_virtual/_rolldown/runtime.js +23 -0
- package/lib/cjs/cli.js +171 -280
- package/lib/cjs/helpers.js +125 -209
- package/lib/cjs/index.js +15 -19
- package/lib/cjs/mini-ut.js +220 -0
- package/lib/cjs/swaggerJsonToYApiData.js +268 -482
- package/lib/cjs/types.js +93 -93
- package/lib/cjs/utils.js +364 -769
- package/lib/esm/ApifoxToYApiData.d.ts +49 -0
- package/lib/esm/ApifoxToYApiData.js +310 -572
- package/lib/esm/Generator.d.ts +53 -0
- package/lib/esm/Generator.js +503 -1146
- package/lib/esm/SwaggerToYApiServer.d.ts +23 -0
- package/lib/esm/SwaggerToYApiServer.js +119 -317
- package/lib/esm/_virtual/_rolldown/runtime.js +7 -0
- package/lib/esm/cli.d.ts +4 -0
- package/lib/esm/cli.js +162 -251
- package/lib/esm/helpers.d.ts +49 -0
- package/lib/esm/helpers.js +125 -198
- package/lib/esm/index.d.ts +2 -897
- package/lib/esm/index.js +3 -2
- package/lib/esm/mini-ut.d.ts +105 -0
- package/lib/esm/mini-ut.js +198 -0
- package/lib/esm/swaggerJsonToYApiData.d.ts +6 -0
- package/lib/esm/swaggerJsonToYApiData.js +265 -469
- package/lib/esm/types.d.ts +698 -0
- package/lib/esm/types.js +87 -73
- package/lib/esm/utils.d.ts +95 -0
- package/lib/esm/utils.js +350 -720
- package/package.json +49 -59
package/lib/cjs/utils.js
CHANGED
|
@@ -1,816 +1,411 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
exports.processJsonSchema = processJsonSchema;
|
|
21
|
-
exports.propDefinitionsToJsonSchema = propDefinitionsToJsonSchema;
|
|
22
|
-
exports.reachJsonSchema = reachJsonSchema;
|
|
23
|
-
exports.sortByWeights = sortByWeights;
|
|
24
|
-
exports.throwError = throwError;
|
|
25
|
-
exports.toUnixPath = toUnixPath;
|
|
26
|
-
exports.traverseJsonSchema = traverseJsonSchema;
|
|
27
|
-
|
|
28
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
29
|
-
|
|
30
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
31
|
-
|
|
32
|
-
var _createForOfIteratorHelperLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelperLoose"));
|
|
33
|
-
|
|
34
|
-
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
35
|
-
|
|
36
|
-
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
37
|
-
|
|
38
|
-
var _json = _interopRequireDefault(require("json5"));
|
|
39
|
-
|
|
40
|
-
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
|
41
|
-
|
|
42
|
-
var _path = _interopRequireDefault(require("path"));
|
|
43
|
-
|
|
44
|
-
var _prettier = _interopRequireDefault(require("prettier"));
|
|
45
|
-
|
|
46
|
-
var _proxyAgent = _interopRequireDefault(require("proxy-agent"));
|
|
47
|
-
|
|
48
|
-
var _toJsonSchema = _interopRequireDefault(require("to-json-schema"));
|
|
49
|
-
|
|
50
|
-
var _vtils = require("vtils");
|
|
51
|
-
|
|
52
|
-
var _types = require("./types");
|
|
53
|
-
|
|
54
|
-
var _jsonSchemaToTypescript = require("json-schema-to-typescript");
|
|
55
|
-
|
|
56
|
-
var _helpers = require("./helpers");
|
|
57
|
-
|
|
58
|
-
var _url2 = require("url");
|
|
59
|
-
|
|
1
|
+
const require_runtime = require("./_virtual/_rolldown/runtime.js");
|
|
2
|
+
const require_types = require("./types.js");
|
|
3
|
+
const require_helpers = require("./helpers.js");
|
|
4
|
+
const require_mini_ut = require("./mini-ut.js");
|
|
5
|
+
let fs_extra = require("fs-extra");
|
|
6
|
+
fs_extra = require_runtime.__toESM(fs_extra);
|
|
7
|
+
let path = require("path");
|
|
8
|
+
path = require_runtime.__toESM(path);
|
|
9
|
+
let json5 = require("json5");
|
|
10
|
+
json5 = require_runtime.__toESM(json5);
|
|
11
|
+
let node_fetch = require("node-fetch");
|
|
12
|
+
node_fetch = require_runtime.__toESM(node_fetch);
|
|
13
|
+
let prettier = require("prettier");
|
|
14
|
+
prettier = require_runtime.__toESM(prettier);
|
|
15
|
+
let to_json_schema = require("to-json-schema");
|
|
16
|
+
to_json_schema = require_runtime.__toESM(to_json_schema);
|
|
17
|
+
let json_schema_to_typescript = require("json-schema-to-typescript");
|
|
18
|
+
let url = require("url");
|
|
19
|
+
//#region src/utils.ts
|
|
60
20
|
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
function throwError() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/* istanbul ignore next */
|
|
71
|
-
throw new Error(msg.join(''));
|
|
21
|
+
* 抛出错误。
|
|
22
|
+
*
|
|
23
|
+
* @param msg 错误信息
|
|
24
|
+
*/
|
|
25
|
+
function throwError(...msg) {
|
|
26
|
+
/* istanbul ignore next */
|
|
27
|
+
throw new Error(msg.join(""));
|
|
72
28
|
}
|
|
73
29
|
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
function toUnixPath(path) {
|
|
82
|
-
return path.replace(/[/\\]+/g, '/');
|
|
30
|
+
* 将路径统一为 unix 风格的路径。
|
|
31
|
+
*
|
|
32
|
+
* @param path 路径
|
|
33
|
+
* @returns unix 风格的路径
|
|
34
|
+
*/
|
|
35
|
+
function toUnixPath(path$2) {
|
|
36
|
+
return path$2.replace(/[/\\]+/g, "/");
|
|
83
37
|
}
|
|
84
38
|
/**
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
39
|
+
* 获得规范化的相对路径。
|
|
40
|
+
*
|
|
41
|
+
* @param from 来源路径
|
|
42
|
+
* @param to 去向路径
|
|
43
|
+
* @returns 相对路径
|
|
44
|
+
*/
|
|
93
45
|
function getNormalizedRelativePath(from, to) {
|
|
94
|
-
|
|
46
|
+
return toUnixPath(path.default.relative(path.default.dirname(from), to)).replace(/^(?=[^.])/, "./").replace(/\.(ts|js)x?$/i, "");
|
|
95
47
|
}
|
|
96
48
|
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}, {});
|
|
114
|
-
} // 处理传入的 JSONSchema
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
cb(jsonSchema, currentPath); // 继续处理对象的子元素
|
|
118
|
-
|
|
119
|
-
if (jsonSchema.properties) {
|
|
120
|
-
(0, _vtils.forOwn)(jsonSchema.properties, function (item, key) {
|
|
121
|
-
return traverseJsonSchema(item, cb, [].concat(currentPath, [key]));
|
|
122
|
-
});
|
|
123
|
-
} // 继续处理数组的子元素
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (jsonSchema.items) {
|
|
127
|
-
(0, _vtils.castArray)(jsonSchema.items).forEach(function (item, index) {
|
|
128
|
-
return traverseJsonSchema(item, cb, [].concat(currentPath, [index]));
|
|
129
|
-
});
|
|
130
|
-
} // 处理 oneOf
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (jsonSchema.oneOf) {
|
|
134
|
-
jsonSchema.oneOf.forEach(function (item) {
|
|
135
|
-
return traverseJsonSchema(item, cb, currentPath);
|
|
136
|
-
});
|
|
137
|
-
} // 处理 anyOf
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (jsonSchema.anyOf) {
|
|
141
|
-
jsonSchema.anyOf.forEach(function (item) {
|
|
142
|
-
return traverseJsonSchema(item, cb, currentPath);
|
|
143
|
-
});
|
|
144
|
-
} // 处理 allOf
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (jsonSchema.allOf) {
|
|
148
|
-
jsonSchema.allOf.forEach(function (item) {
|
|
149
|
-
return traverseJsonSchema(item, cb, currentPath);
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return jsonSchema;
|
|
49
|
+
* 原地遍历 JSONSchema。
|
|
50
|
+
*/
|
|
51
|
+
function traverseJsonSchema(jsonSchema, cb, currentPath = []) {
|
|
52
|
+
/* istanbul ignore if */
|
|
53
|
+
if (!require_mini_ut.isObject(jsonSchema)) return jsonSchema;
|
|
54
|
+
if (require_mini_ut.isArray(jsonSchema.properties)) jsonSchema.properties = jsonSchema.properties.reduce((props, js) => {
|
|
55
|
+
props[js.name] = js;
|
|
56
|
+
return props;
|
|
57
|
+
}, {});
|
|
58
|
+
cb(jsonSchema, currentPath);
|
|
59
|
+
if (jsonSchema.properties) require_mini_ut.forOwn(jsonSchema.properties, (item, key) => traverseJsonSchema(item, cb, [...currentPath, key]));
|
|
60
|
+
if (jsonSchema.items) require_mini_ut.castArray(jsonSchema.items).forEach((item, index) => traverseJsonSchema(item, cb, [...currentPath, index]));
|
|
61
|
+
if (jsonSchema.oneOf) jsonSchema.oneOf.forEach((item) => traverseJsonSchema(item, cb, currentPath));
|
|
62
|
+
if (jsonSchema.anyOf) jsonSchema.anyOf.forEach((item) => traverseJsonSchema(item, cb, currentPath));
|
|
63
|
+
if (jsonSchema.allOf) jsonSchema.allOf.forEach((item) => traverseJsonSchema(item, cb, currentPath));
|
|
64
|
+
return jsonSchema;
|
|
154
65
|
}
|
|
155
66
|
/**
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
67
|
+
* 原地处理 JSONSchema。
|
|
68
|
+
*
|
|
69
|
+
* @param jsonSchema 待处理的 JSONSchema
|
|
70
|
+
* @returns 处理后的 JSONSchema
|
|
71
|
+
*/
|
|
163
72
|
function processJsonSchema(jsonSchema, customTypeMapping) {
|
|
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
|
-
if (jsonSchema.properties) {
|
|
202
|
-
(0, _vtils.forOwn)(jsonSchema.properties, function (_, prop) {
|
|
203
|
-
var propDef = jsonSchema.properties[prop];
|
|
204
|
-
delete jsonSchema.properties[prop];
|
|
205
|
-
jsonSchema.properties[prop.trim()] = propDef;
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
if (Array.isArray(jsonSchema.required)) {
|
|
209
|
-
jsonSchema.required = jsonSchema.required.map(function (prop) {
|
|
210
|
-
return prop.trim();
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return jsonSchema;
|
|
216
|
-
});
|
|
73
|
+
return traverseJsonSchema(jsonSchema, (jsonSchema) => {
|
|
74
|
+
delete jsonSchema.$ref;
|
|
75
|
+
delete jsonSchema.$$ref;
|
|
76
|
+
if (jsonSchema.type === "array" && Array.isArray(jsonSchema.items) && jsonSchema.items.length) jsonSchema.items = jsonSchema.items[0];
|
|
77
|
+
if (jsonSchema.type) {
|
|
78
|
+
const typeMapping = {
|
|
79
|
+
byte: "integer",
|
|
80
|
+
short: "integer",
|
|
81
|
+
int: "integer",
|
|
82
|
+
long: "integer",
|
|
83
|
+
float: "number",
|
|
84
|
+
double: "number",
|
|
85
|
+
bigdecimal: "number",
|
|
86
|
+
char: "string",
|
|
87
|
+
void: "null",
|
|
88
|
+
...require_mini_ut.mapKeys(customTypeMapping, (_, key) => key.toLowerCase())
|
|
89
|
+
};
|
|
90
|
+
const isMultiple = Array.isArray(jsonSchema.type);
|
|
91
|
+
const types = require_mini_ut.castArray(jsonSchema.type).map((type) => {
|
|
92
|
+
type = type.toLowerCase();
|
|
93
|
+
type = typeMapping[type] || type;
|
|
94
|
+
return type;
|
|
95
|
+
});
|
|
96
|
+
jsonSchema.type = isMultiple ? types : types[0];
|
|
97
|
+
}
|
|
98
|
+
if (jsonSchema.properties) {
|
|
99
|
+
require_mini_ut.forOwn(jsonSchema.properties, (_, prop) => {
|
|
100
|
+
const propDef = jsonSchema.properties[prop];
|
|
101
|
+
delete jsonSchema.properties[prop];
|
|
102
|
+
jsonSchema.properties[prop.trim()] = propDef;
|
|
103
|
+
});
|
|
104
|
+
if (Array.isArray(jsonSchema.required)) jsonSchema.required = jsonSchema.required.map((prop) => prop.trim());
|
|
105
|
+
}
|
|
106
|
+
return jsonSchema;
|
|
107
|
+
});
|
|
217
108
|
}
|
|
218
109
|
/**
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
110
|
+
* 获取适用于 JSTT 的 JSONSchema。
|
|
111
|
+
*
|
|
112
|
+
* @param jsonSchema 待处理的 JSONSchema
|
|
113
|
+
* @returns 适用于 JSTT 的 JSONSchema
|
|
114
|
+
*/
|
|
226
115
|
function jsonSchemaToJSTTJsonSchema(jsonSchema, typeName) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
var tsType = "" + tsTypeLeft + tsTypeRight;
|
|
251
|
-
jsonSchema.tsType = tsType;
|
|
252
|
-
} // 去除 title 和 id,防止 json-schema-to-typescript 提取它们作为接口名
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
delete jsonSchema.title;
|
|
256
|
-
delete jsonSchema.id; // 忽略数组长度限制
|
|
257
|
-
|
|
258
|
-
delete jsonSchema.minItems;
|
|
259
|
-
delete jsonSchema.maxItems;
|
|
260
|
-
|
|
261
|
-
if (jsonSchema.type === 'object') {
|
|
262
|
-
// 将 additionalProperties 设为 false
|
|
263
|
-
jsonSchema.additionalProperties = false;
|
|
264
|
-
} // 删除 default,防止 json-schema-to-typescript 根据它推测类型
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
delete jsonSchema.default;
|
|
268
|
-
return jsonSchema;
|
|
269
|
-
});
|
|
116
|
+
if (jsonSchema) delete jsonSchema.description;
|
|
117
|
+
return traverseJsonSchema(jsonSchema, (jsonSchema, currentPath) => {
|
|
118
|
+
const refValue = jsonSchema.title == null ? jsonSchema.description : jsonSchema.title;
|
|
119
|
+
if (refValue?.startsWith("&")) {
|
|
120
|
+
const typeRelativePath = refValue.substring(1);
|
|
121
|
+
const typeAbsolutePathArr = toUnixPath(path.default.resolve(path.default.dirname(`/${currentPath.join("/")}`.replace(/\/{2,}/g, "/")), typeRelativePath).replace(/^[a-z]+:/i, "")).split("/").filter(Boolean);
|
|
122
|
+
let tsTypeLeft = "";
|
|
123
|
+
let tsTypeRight = typeName;
|
|
124
|
+
for (const key of typeAbsolutePathArr) {
|
|
125
|
+
tsTypeLeft += "NonNullable<";
|
|
126
|
+
tsTypeRight += `[${JSON.stringify(key)}]>`;
|
|
127
|
+
}
|
|
128
|
+
jsonSchema.tsType = `${tsTypeLeft}${tsTypeRight}`;
|
|
129
|
+
}
|
|
130
|
+
delete jsonSchema.title;
|
|
131
|
+
delete jsonSchema.id;
|
|
132
|
+
delete jsonSchema.minItems;
|
|
133
|
+
delete jsonSchema.maxItems;
|
|
134
|
+
if (jsonSchema.type === "object") jsonSchema.additionalProperties = false;
|
|
135
|
+
delete jsonSchema.default;
|
|
136
|
+
return jsonSchema;
|
|
137
|
+
});
|
|
270
138
|
}
|
|
271
139
|
/**
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
140
|
+
* 将 JSONSchema 字符串转为 JSONSchema 对象。
|
|
141
|
+
*
|
|
142
|
+
* @param str 要转换的 JSONSchema 字符串
|
|
143
|
+
* @returns 转换后的 JSONSchema 对象
|
|
144
|
+
*/
|
|
279
145
|
function jsonSchemaStringToJsonSchema(str, customTypeMapping) {
|
|
280
|
-
|
|
146
|
+
return processJsonSchema(JSON.parse(str), customTypeMapping);
|
|
281
147
|
}
|
|
282
148
|
/**
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
149
|
+
* 获得 JSON 数据的 JSONSchema 对象。
|
|
150
|
+
*
|
|
151
|
+
* @param json JSON 数据
|
|
152
|
+
* @returns JSONSchema 对象
|
|
153
|
+
*/
|
|
290
154
|
function jsonToJsonSchema(json, customTypeMapping) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (!schema.description && !!value && type !== 'object') {
|
|
304
|
-
schema.description = JSON.stringify(value);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return schema;
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
delete schema.description;
|
|
311
|
-
return processJsonSchema(schema, customTypeMapping);
|
|
155
|
+
const schema = (0, to_json_schema.default)(json, {
|
|
156
|
+
required: false,
|
|
157
|
+
arrays: { mode: "first" },
|
|
158
|
+
objects: { additionalProperties: false },
|
|
159
|
+
strings: { detectFormat: false },
|
|
160
|
+
postProcessFnc: (type, schema, value) => {
|
|
161
|
+
if (!schema.description && !!value && type !== "object") schema.description = JSON.stringify(value);
|
|
162
|
+
return schema;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
delete schema.description;
|
|
166
|
+
return processJsonSchema(schema, customTypeMapping);
|
|
312
167
|
}
|
|
313
168
|
/**
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
169
|
+
* 获得 mockjs 模板的 JSONSchema 对象。
|
|
170
|
+
*
|
|
171
|
+
* @param template mockjs 模板
|
|
172
|
+
* @returns JSONSchema 对象
|
|
173
|
+
*/
|
|
321
174
|
function mockjsTemplateToJsonSchema(template, customTypeMapping) {
|
|
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
|
-
(0, _vtils.traverse)(template, function (value, key, parent) {
|
|
350
|
-
if (typeof key === 'string') {
|
|
351
|
-
actions.push(function () {
|
|
352
|
-
delete parent[key];
|
|
353
|
-
parent[// https://github.com/nuysoft/Mock/blob/refactoring/src/mock/schema/schema.js#L16
|
|
354
|
-
key.replace(keyRe, '$1')] = normalizeValue(value);
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
actions.forEach(function (action) {
|
|
359
|
-
return action();
|
|
360
|
-
});
|
|
361
|
-
return jsonToJsonSchema(template, customTypeMapping);
|
|
175
|
+
const actions = [];
|
|
176
|
+
const keyRe = /(.+)\|(?:\+(\d+)|([+-]?\d+-?[+-]?\d*)?(?:\.(\d+-?\d*))?)/;
|
|
177
|
+
const numberPatterns = [
|
|
178
|
+
"natural",
|
|
179
|
+
"integer",
|
|
180
|
+
"float",
|
|
181
|
+
"range",
|
|
182
|
+
"increment"
|
|
183
|
+
];
|
|
184
|
+
const boolPatterns = ["boolean", "bool"];
|
|
185
|
+
const normalizeValue = (value) => {
|
|
186
|
+
if (typeof value === "string" && value.startsWith("@")) {
|
|
187
|
+
const pattern = value.slice(1);
|
|
188
|
+
if (numberPatterns.some((p) => pattern.startsWith(p))) return 1;
|
|
189
|
+
if (boolPatterns.some((p) => pattern.startsWith(p))) return true;
|
|
190
|
+
}
|
|
191
|
+
return value;
|
|
192
|
+
};
|
|
193
|
+
require_mini_ut.traverse(template, (value, key, parent) => {
|
|
194
|
+
if (typeof key === "string") actions.push(() => {
|
|
195
|
+
delete parent[key];
|
|
196
|
+
parent[key.replace(keyRe, "$1")] = normalizeValue(value);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
actions.forEach((action) => action());
|
|
200
|
+
return jsonToJsonSchema(template, customTypeMapping);
|
|
362
201
|
}
|
|
363
202
|
/**
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
203
|
+
* 获得属性定义列表的 JSONSchema 对象。
|
|
204
|
+
*
|
|
205
|
+
* @param propDefinitions 属性定义列表
|
|
206
|
+
* @returns JSONSchema 对象
|
|
207
|
+
*/
|
|
371
208
|
function propDefinitionsToJsonSchema(propDefinitions, customTypeMapping) {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
} : {});
|
|
388
|
-
return res;
|
|
389
|
-
}, {})
|
|
390
|
-
}, customTypeMapping);
|
|
209
|
+
return processJsonSchema({
|
|
210
|
+
type: "object",
|
|
211
|
+
required: propDefinitions.reduce((res, prop) => {
|
|
212
|
+
if (prop.required) res.push(prop.name);
|
|
213
|
+
return res;
|
|
214
|
+
}, []),
|
|
215
|
+
properties: propDefinitions.reduce((res, prop) => {
|
|
216
|
+
res[prop.name] = {
|
|
217
|
+
type: prop.type,
|
|
218
|
+
description: prop.comment,
|
|
219
|
+
...prop.type === "file" ? { tsType: require_helpers.FileData.name } : {}
|
|
220
|
+
};
|
|
221
|
+
return res;
|
|
222
|
+
}, {})
|
|
223
|
+
}, customTypeMapping);
|
|
391
224
|
}
|
|
392
|
-
|
|
393
225
|
var JSTTOptions = {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
226
|
+
bannerComment: "",
|
|
227
|
+
style: {
|
|
228
|
+
bracketSpacing: false,
|
|
229
|
+
printWidth: 120,
|
|
230
|
+
semi: true,
|
|
231
|
+
singleQuote: true,
|
|
232
|
+
tabWidth: 2,
|
|
233
|
+
trailingComma: "none",
|
|
234
|
+
useTabs: false
|
|
235
|
+
}
|
|
404
236
|
};
|
|
405
237
|
/**
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
return _regenerator.default.wrap(function _callee$(_context) {
|
|
421
|
-
while (1) {
|
|
422
|
-
switch (_context.prev = _context.next) {
|
|
423
|
-
case 0:
|
|
424
|
-
if (!(0, _vtils.isEmpty)(jsonSchema)) {
|
|
425
|
-
_context.next = 2;
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
return _context.abrupt("return", "export interface " + typeName + " {}");
|
|
430
|
-
|
|
431
|
-
case 2:
|
|
432
|
-
if (!jsonSchema.__is_any__) {
|
|
433
|
-
_context.next = 5;
|
|
434
|
-
break;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
delete jsonSchema.__is_any__;
|
|
438
|
-
return _context.abrupt("return", "export type " + typeName + " = any");
|
|
439
|
-
|
|
440
|
-
case 5:
|
|
441
|
-
// JSTT 会转换 typeName,因此传入一个全大写的假 typeName,生成代码后再替换回真正的 typeName
|
|
442
|
-
fakeTypeName = 'THISISAFAKETYPENAME';
|
|
443
|
-
_context.next = 8;
|
|
444
|
-
return (0, _jsonSchemaToTypescript.compile)(jsonSchemaToJSTTJsonSchema((0, _vtils.cloneDeepFast)(jsonSchema), typeName), fakeTypeName, JSTTOptions);
|
|
445
|
-
|
|
446
|
-
case 8:
|
|
447
|
-
code = _context.sent;
|
|
448
|
-
return _context.abrupt("return", code.replace(fakeTypeName, typeName).trim());
|
|
449
|
-
|
|
450
|
-
case 10:
|
|
451
|
-
case "end":
|
|
452
|
-
return _context.stop();
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
}, _callee);
|
|
456
|
-
}));
|
|
457
|
-
return _jsonSchemaToType.apply(this, arguments);
|
|
238
|
+
* 根据 JSONSchema 对象生产 TypeScript 类型定义。
|
|
239
|
+
*
|
|
240
|
+
* @param jsonSchema JSONSchema 对象
|
|
241
|
+
* @param typeName 类型名称
|
|
242
|
+
* @returns TypeScript 类型定义
|
|
243
|
+
*/
|
|
244
|
+
async function jsonSchemaToType(jsonSchema, typeName) {
|
|
245
|
+
if (require_mini_ut.isEmpty(jsonSchema)) return `export interface ${typeName} {}`;
|
|
246
|
+
if (jsonSchema.__is_any__) {
|
|
247
|
+
delete jsonSchema.__is_any__;
|
|
248
|
+
return `export type ${typeName} = any`;
|
|
249
|
+
}
|
|
250
|
+
const fakeTypeName = "THISISAFAKETYPENAME";
|
|
251
|
+
return (await (0, json_schema_to_typescript.compile)(jsonSchemaToJSTTJsonSchema(require_mini_ut.cloneDeepFast(jsonSchema), typeName), fakeTypeName, JSTTOptions)).replace(fakeTypeName, typeName).trim();
|
|
458
252
|
}
|
|
459
|
-
|
|
460
253
|
function getRequestDataJsonSchema(interfaceInfo, customTypeMapping) {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
}
|
|
507
|
-
} // 处理路径参数
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
if ((0, _vtils.isArray)(interfaceInfo.req_params) && interfaceInfo.req_params.length) {
|
|
511
|
-
var paramsJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_params.map(function (item) {
|
|
512
|
-
return {
|
|
513
|
-
name: item.name,
|
|
514
|
-
required: true,
|
|
515
|
-
type: item.type || 'string',
|
|
516
|
-
comment: item.desc
|
|
517
|
-
};
|
|
518
|
-
}), customTypeMapping);
|
|
519
|
-
/* istanbul ignore else */
|
|
520
|
-
|
|
521
|
-
if (jsonSchema) {
|
|
522
|
-
jsonSchema.properties = (0, _extends2.default)({}, jsonSchema.properties, paramsJsonSchema.properties);
|
|
523
|
-
jsonSchema.required = [].concat(Array.isArray(jsonSchema.required) ? jsonSchema.required : [], Array.isArray(paramsJsonSchema.required) ? paramsJsonSchema.required : []);
|
|
524
|
-
} else {
|
|
525
|
-
jsonSchema = paramsJsonSchema;
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
return jsonSchema || {};
|
|
254
|
+
let jsonSchema;
|
|
255
|
+
if (isPostLikeMethod(interfaceInfo.method)) switch (interfaceInfo.req_body_type) {
|
|
256
|
+
case require_types.RequestBodyType.form:
|
|
257
|
+
jsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_body_form.map((item) => ({
|
|
258
|
+
name: item.name,
|
|
259
|
+
required: item.required === require_types.Required.true,
|
|
260
|
+
type: item.type === require_types.RequestFormItemType.file ? "file" : "string",
|
|
261
|
+
comment: item.desc
|
|
262
|
+
})), customTypeMapping);
|
|
263
|
+
break;
|
|
264
|
+
case require_types.RequestBodyType.json: if (interfaceInfo.req_body_other) jsonSchema = interfaceInfo.req_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.req_body_other, customTypeMapping) : jsonToJsonSchema(json5.default.parse(interfaceInfo.req_body_other), customTypeMapping);
|
|
265
|
+
}
|
|
266
|
+
if (require_mini_ut.isArray(interfaceInfo.req_query) && interfaceInfo.req_query.length) {
|
|
267
|
+
const queryJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_query.map((item) => ({
|
|
268
|
+
name: item.name,
|
|
269
|
+
required: item.required === require_types.Required.true,
|
|
270
|
+
type: item.type || "string",
|
|
271
|
+
comment: item.desc
|
|
272
|
+
})), customTypeMapping);
|
|
273
|
+
/* istanbul ignore else */
|
|
274
|
+
if (jsonSchema) {
|
|
275
|
+
jsonSchema.properties = {
|
|
276
|
+
...jsonSchema.properties,
|
|
277
|
+
...queryJsonSchema.properties
|
|
278
|
+
};
|
|
279
|
+
jsonSchema.required = [...Array.isArray(jsonSchema.required) ? jsonSchema.required : [], ...Array.isArray(queryJsonSchema.required) ? queryJsonSchema.required : []];
|
|
280
|
+
} else jsonSchema = queryJsonSchema;
|
|
281
|
+
}
|
|
282
|
+
if (require_mini_ut.isArray(interfaceInfo.req_params) && interfaceInfo.req_params.length) {
|
|
283
|
+
const paramsJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_params.map((item) => ({
|
|
284
|
+
name: item.name,
|
|
285
|
+
required: true,
|
|
286
|
+
type: item.type || "string",
|
|
287
|
+
comment: item.desc
|
|
288
|
+
})), customTypeMapping);
|
|
289
|
+
/* istanbul ignore else */
|
|
290
|
+
if (jsonSchema) {
|
|
291
|
+
jsonSchema.properties = {
|
|
292
|
+
...jsonSchema.properties,
|
|
293
|
+
...paramsJsonSchema.properties
|
|
294
|
+
};
|
|
295
|
+
jsonSchema.required = [...Array.isArray(jsonSchema.required) ? jsonSchema.required : [], ...Array.isArray(paramsJsonSchema.required) ? paramsJsonSchema.required : []];
|
|
296
|
+
} else jsonSchema = paramsJsonSchema;
|
|
297
|
+
}
|
|
298
|
+
return jsonSchema || {};
|
|
530
299
|
}
|
|
531
|
-
|
|
532
300
|
function getResponseDataJsonSchema(interfaceInfo, customTypeMapping, dataKey) {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
default:
|
|
544
|
-
jsonSchema = {
|
|
545
|
-
__is_any__: true
|
|
546
|
-
};
|
|
547
|
-
break;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
if (dataKey && jsonSchema) {
|
|
551
|
-
jsonSchema = reachJsonSchema(jsonSchema, dataKey);
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
return jsonSchema;
|
|
301
|
+
let jsonSchema = {};
|
|
302
|
+
switch (interfaceInfo.res_body_type) {
|
|
303
|
+
case require_types.ResponseBodyType.json:
|
|
304
|
+
if (interfaceInfo.res_body) jsonSchema = interfaceInfo.res_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.res_body, customTypeMapping) : mockjsTemplateToJsonSchema(json5.default.parse(interfaceInfo.res_body), customTypeMapping);
|
|
305
|
+
break;
|
|
306
|
+
default: jsonSchema = { __is_any__: true };
|
|
307
|
+
}
|
|
308
|
+
if (dataKey && jsonSchema) jsonSchema = reachJsonSchema(jsonSchema, dataKey);
|
|
309
|
+
return jsonSchema;
|
|
555
310
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
var _last = (_last$properties = last.properties) == null ? void 0 : _last$properties[segment];
|
|
566
|
-
|
|
567
|
-
if (!_last) {
|
|
568
|
-
return jsonSchema;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
last = _last;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
return last;
|
|
311
|
+
function reachJsonSchema(jsonSchema, path$3) {
|
|
312
|
+
let last = jsonSchema;
|
|
313
|
+
for (const segment of require_mini_ut.castArray(path$3)) {
|
|
314
|
+
const _last = last.properties?.[segment];
|
|
315
|
+
if (!_last) return jsonSchema;
|
|
316
|
+
last = _last;
|
|
317
|
+
}
|
|
318
|
+
return last;
|
|
575
319
|
}
|
|
576
|
-
|
|
577
320
|
function sortByWeights(list) {
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
w = a.weights[i] - b.weights[i];
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
return w;
|
|
593
|
-
}, 0);
|
|
594
|
-
return w;
|
|
595
|
-
});
|
|
596
|
-
return list;
|
|
321
|
+
list.sort((a, b) => {
|
|
322
|
+
const x = a.weights.length > b.weights.length ? b : a;
|
|
323
|
+
const minLen = Math.min(a.weights.length, b.weights.length);
|
|
324
|
+
const maxLen = Math.max(a.weights.length, b.weights.length);
|
|
325
|
+
x.weights.push(...new Array(maxLen - minLen).fill(0));
|
|
326
|
+
return a.weights.reduce((w, _, i) => {
|
|
327
|
+
if (w === 0) w = a.weights[i] - b.weights[i];
|
|
328
|
+
return w;
|
|
329
|
+
}, 0);
|
|
330
|
+
});
|
|
331
|
+
return list;
|
|
597
332
|
}
|
|
598
|
-
|
|
599
333
|
function isGetLikeMethod(method) {
|
|
600
|
-
|
|
334
|
+
return method === require_types.Method.GET || method === require_types.Method.OPTIONS || method === require_types.Method.HEAD;
|
|
601
335
|
}
|
|
602
|
-
|
|
603
336
|
function isPostLikeMethod(method) {
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
function getPrettier(_x3) {
|
|
608
|
-
return _getPrettier.apply(this, arguments);
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
function _getPrettier() {
|
|
612
|
-
_getPrettier = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(cwd) {
|
|
613
|
-
var projectPrettierPath;
|
|
614
|
-
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
615
|
-
while (1) {
|
|
616
|
-
switch (_context2.prev = _context2.next) {
|
|
617
|
-
case 0:
|
|
618
|
-
projectPrettierPath = _path.default.join(cwd, 'node_modules/prettier');
|
|
619
|
-
_context2.next = 3;
|
|
620
|
-
return _fsExtra.default.pathExists(projectPrettierPath);
|
|
621
|
-
|
|
622
|
-
case 3:
|
|
623
|
-
if (!_context2.sent) {
|
|
624
|
-
_context2.next = 5;
|
|
625
|
-
break;
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
return _context2.abrupt("return", require(projectPrettierPath));
|
|
629
|
-
|
|
630
|
-
case 5:
|
|
631
|
-
return _context2.abrupt("return", require('prettier'));
|
|
632
|
-
|
|
633
|
-
case 6:
|
|
634
|
-
case "end":
|
|
635
|
-
return _context2.stop();
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}, _callee2);
|
|
639
|
-
}));
|
|
640
|
-
return _getPrettier.apply(this, arguments);
|
|
337
|
+
return !isGetLikeMethod(method);
|
|
641
338
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
339
|
+
async function getPrettier(cwd) {
|
|
340
|
+
const projectPrettierPath = path.default.join(cwd, "node_modules/prettier");
|
|
341
|
+
if (await fs_extra.default.pathExists(projectPrettierPath)) return require(projectPrettierPath);
|
|
342
|
+
return require("prettier");
|
|
645
343
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
_context3.next = 3;
|
|
668
|
-
break;
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
return _context3.abrupt("return", prettierOptions);
|
|
672
|
-
|
|
673
|
-
case 3:
|
|
674
|
-
_context3.next = 5;
|
|
675
|
-
return (0, _vtils.run)(function () {
|
|
676
|
-
return _prettier.default.resolveConfigFile();
|
|
677
|
-
});
|
|
678
|
-
|
|
679
|
-
case 5:
|
|
680
|
-
_yield$run = _context3.sent;
|
|
681
|
-
prettierConfigPathErr = _yield$run[0];
|
|
682
|
-
prettierConfigPath = _yield$run[1];
|
|
683
|
-
|
|
684
|
-
if (!(prettierConfigPathErr || !prettierConfigPath)) {
|
|
685
|
-
_context3.next = 10;
|
|
686
|
-
break;
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
return _context3.abrupt("return", prettierOptions);
|
|
690
|
-
|
|
691
|
-
case 10:
|
|
692
|
-
_context3.next = 12;
|
|
693
|
-
return (0, _vtils.run)(function () {
|
|
694
|
-
return _prettier.default.resolveConfig(prettierConfigPath);
|
|
695
|
-
});
|
|
696
|
-
|
|
697
|
-
case 12:
|
|
698
|
-
_yield$run2 = _context3.sent;
|
|
699
|
-
prettierConfigErr = _yield$run2[0];
|
|
700
|
-
prettierConfig = _yield$run2[1];
|
|
701
|
-
|
|
702
|
-
if (!(prettierConfigErr || !prettierConfig)) {
|
|
703
|
-
_context3.next = 17;
|
|
704
|
-
break;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
return _context3.abrupt("return", prettierOptions);
|
|
708
|
-
|
|
709
|
-
case 17:
|
|
710
|
-
return _context3.abrupt("return", (0, _extends2.default)({}, prettierOptions, prettierConfig, {
|
|
711
|
-
parser: 'typescript'
|
|
712
|
-
}));
|
|
713
|
-
|
|
714
|
-
case 18:
|
|
715
|
-
case "end":
|
|
716
|
-
return _context3.stop();
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
}, _callee3);
|
|
720
|
-
}));
|
|
721
|
-
return _getPrettierOptions.apply(this, arguments);
|
|
344
|
+
async function getPrettierOptions() {
|
|
345
|
+
const prettierOptions = {
|
|
346
|
+
parser: "typescript",
|
|
347
|
+
printWidth: 120,
|
|
348
|
+
tabWidth: 2,
|
|
349
|
+
singleQuote: true,
|
|
350
|
+
semi: false,
|
|
351
|
+
trailingComma: "all",
|
|
352
|
+
bracketSpacing: false,
|
|
353
|
+
endOfLine: "lf"
|
|
354
|
+
};
|
|
355
|
+
if (process.env.JEST_WORKER_ID) return prettierOptions;
|
|
356
|
+
const [prettierConfigPathErr, prettierConfigPath] = await require_mini_ut.run(() => prettier.default.resolveConfigFile());
|
|
357
|
+
if (prettierConfigPathErr || !prettierConfigPath) return prettierOptions;
|
|
358
|
+
const [prettierConfigErr, prettierConfig] = await require_mini_ut.run(() => prettier.default.resolveConfig(prettierConfigPath));
|
|
359
|
+
if (prettierConfigErr || !prettierConfig) return prettierOptions;
|
|
360
|
+
return {
|
|
361
|
+
...prettierOptions,
|
|
362
|
+
...prettierConfig,
|
|
363
|
+
parser: "typescript"
|
|
364
|
+
};
|
|
722
365
|
}
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
366
|
+
var getCachedPrettierOptions = require_mini_ut.memoize(getPrettierOptions);
|
|
367
|
+
async function httpGet(url$1, query) {
|
|
368
|
+
const _url = new url.URL(url$1);
|
|
369
|
+
if (query) Object.keys(query).forEach((key) => {
|
|
370
|
+
_url.searchParams.set(key, query[key]);
|
|
371
|
+
});
|
|
372
|
+
url$1 = _url.toString();
|
|
373
|
+
return await (await (0, node_fetch.default)(url$1, { method: "GET" })).json();
|
|
729
374
|
}
|
|
730
375
|
/**
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
_url = new _url2.URL(url);
|
|
744
|
-
|
|
745
|
-
if (query) {
|
|
746
|
-
Object.keys(query).forEach(function (key) {
|
|
747
|
-
_url.searchParams.set(key, query[key]);
|
|
748
|
-
});
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
url = _url.toString();
|
|
752
|
-
_context4.next = 5;
|
|
753
|
-
return (0, _nodeFetch.default)(url, {
|
|
754
|
-
method: 'GET',
|
|
755
|
-
agent: new _proxyAgent.default()
|
|
756
|
-
});
|
|
757
|
-
|
|
758
|
-
case 5:
|
|
759
|
-
res = _context4.sent;
|
|
760
|
-
return _context4.abrupt("return", res.json());
|
|
761
|
-
|
|
762
|
-
case 7:
|
|
763
|
-
case "end":
|
|
764
|
-
return _context4.stop();
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
}, _callee4);
|
|
768
|
-
}));
|
|
769
|
-
return _httpGet.apply(this, arguments);
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
var getFilteredCat = function getFilteredCat(cat, cats, isApifox) {
|
|
773
|
-
if (isApifox === void 0) {
|
|
774
|
-
isApifox = false;
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
// 分类处理
|
|
778
|
-
// 数组化
|
|
779
|
-
var categoryIds = (0, _vtils.castArray)(cat.id); // 全部分类
|
|
780
|
-
|
|
781
|
-
if (categoryIds.includes(0)) {
|
|
782
|
-
var _categoryIds;
|
|
783
|
-
|
|
784
|
-
(_categoryIds = categoryIds).push.apply(_categoryIds, cats.map(function (c) {
|
|
785
|
-
return c._id;
|
|
786
|
-
}));
|
|
787
|
-
} // 唯一化
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
categoryIds = (0, _vtils.uniq)(categoryIds); // 去掉被排除的分类
|
|
791
|
-
|
|
792
|
-
var excludedCategoryIds = categoryIds.filter(function (id) {
|
|
793
|
-
return id < 0;
|
|
794
|
-
}).map(Math.abs);
|
|
795
|
-
categoryIds = categoryIds.filter(function (id) {
|
|
796
|
-
return !excludedCategoryIds.includes(Math.abs(id));
|
|
797
|
-
}); // 删除不存在的分类
|
|
798
|
-
|
|
799
|
-
if (!isApifox) {
|
|
800
|
-
categoryIds = categoryIds.filter(function (id) {
|
|
801
|
-
return !!cats.find(function (c) {
|
|
802
|
-
return c._id === id;
|
|
803
|
-
});
|
|
804
|
-
});
|
|
805
|
-
} else {
|
|
806
|
-
categoryIds = categoryIds.filter(function (id) {
|
|
807
|
-
return id !== 0;
|
|
808
|
-
});
|
|
809
|
-
} // 顺序化
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
categoryIds = categoryIds.sort();
|
|
813
|
-
return categoryIds;
|
|
376
|
+
* 获取过滤后的菜单
|
|
377
|
+
*/
|
|
378
|
+
var getFilteredCat = (cat, cats, isApifox = false) => {
|
|
379
|
+
let categoryIds = require_mini_ut.castArray(cat.id);
|
|
380
|
+
if (categoryIds.includes(0)) categoryIds.push(...cats.map((c) => c._id));
|
|
381
|
+
categoryIds = require_mini_ut.uniq(categoryIds);
|
|
382
|
+
const excludedCategoryIds = categoryIds.filter((id) => id < 0).map(Math.abs);
|
|
383
|
+
categoryIds = categoryIds.filter((id) => !excludedCategoryIds.includes(Math.abs(id)));
|
|
384
|
+
if (!isApifox) categoryIds = categoryIds.filter((id) => !!cats.find((c) => c._id === id));
|
|
385
|
+
else categoryIds = categoryIds.filter((id) => id !== 0);
|
|
386
|
+
categoryIds = categoryIds.sort();
|
|
387
|
+
return categoryIds;
|
|
814
388
|
};
|
|
815
|
-
|
|
816
|
-
exports.
|
|
389
|
+
//#endregion
|
|
390
|
+
exports.getCachedPrettierOptions = getCachedPrettierOptions;
|
|
391
|
+
exports.getFilteredCat = getFilteredCat;
|
|
392
|
+
exports.getNormalizedRelativePath = getNormalizedRelativePath;
|
|
393
|
+
exports.getPrettier = getPrettier;
|
|
394
|
+
exports.getPrettierOptions = getPrettierOptions;
|
|
395
|
+
exports.getRequestDataJsonSchema = getRequestDataJsonSchema;
|
|
396
|
+
exports.getResponseDataJsonSchema = getResponseDataJsonSchema;
|
|
397
|
+
exports.httpGet = httpGet;
|
|
398
|
+
exports.isGetLikeMethod = isGetLikeMethod;
|
|
399
|
+
exports.isPostLikeMethod = isPostLikeMethod;
|
|
400
|
+
exports.jsonSchemaStringToJsonSchema = jsonSchemaStringToJsonSchema;
|
|
401
|
+
exports.jsonSchemaToJSTTJsonSchema = jsonSchemaToJSTTJsonSchema;
|
|
402
|
+
exports.jsonSchemaToType = jsonSchemaToType;
|
|
403
|
+
exports.jsonToJsonSchema = jsonToJsonSchema;
|
|
404
|
+
exports.mockjsTemplateToJsonSchema = mockjsTemplateToJsonSchema;
|
|
405
|
+
exports.processJsonSchema = processJsonSchema;
|
|
406
|
+
exports.propDefinitionsToJsonSchema = propDefinitionsToJsonSchema;
|
|
407
|
+
exports.reachJsonSchema = reachJsonSchema;
|
|
408
|
+
exports.sortByWeights = sortByWeights;
|
|
409
|
+
exports.throwError = throwError;
|
|
410
|
+
exports.toUnixPath = toUnixPath;
|
|
411
|
+
exports.traverseJsonSchema = traverseJsonSchema;
|