yapi-apifox-to-typescript 4.2.3 → 5.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/lib/cjs/ApifoxToYApiData.js +312 -588
- package/lib/cjs/Generator.js +513 -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 +509 -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 +695 -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/esm/utils.js
CHANGED
|
@@ -1,754 +1,384 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import fs from
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import prettier from
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
import { compile } from 'json-schema-to-typescript';
|
|
15
|
-
import { FileData } from './helpers';
|
|
16
|
-
import { URL } from 'url';
|
|
1
|
+
import { __require } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { Method, RequestBodyType, RequestFormItemType, Required, ResponseBodyType } from "./types.js";
|
|
3
|
+
import { FileData } from "./helpers.js";
|
|
4
|
+
import { castArray, cloneDeepFast, forOwn, isArray, isEmpty, isObject, mapKeys, memoize, run, traverse, uniq } from "./mini-ut.js";
|
|
5
|
+
import fs from "fs-extra";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import JSON5 from "json5";
|
|
8
|
+
import nodeFetch from "node-fetch";
|
|
9
|
+
import prettier from "prettier";
|
|
10
|
+
import toJsonSchema from "to-json-schema";
|
|
11
|
+
import { compile } from "json-schema-to-typescript";
|
|
12
|
+
import { URL } from "url";
|
|
13
|
+
//#region src/utils.ts
|
|
17
14
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
msg[_key] = arguments[_key];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/* istanbul ignore next */
|
|
29
|
-
throw new Error(msg.join(''));
|
|
15
|
+
* 抛出错误。
|
|
16
|
+
*
|
|
17
|
+
* @param msg 错误信息
|
|
18
|
+
*/
|
|
19
|
+
function throwError(...msg) {
|
|
20
|
+
/* istanbul ignore next */
|
|
21
|
+
throw new Error(msg.join(""));
|
|
30
22
|
}
|
|
31
23
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return path.replace(/[/\\]+/g, '/');
|
|
24
|
+
* 将路径统一为 unix 风格的路径。
|
|
25
|
+
*
|
|
26
|
+
* @param path 路径
|
|
27
|
+
* @returns unix 风格的路径
|
|
28
|
+
*/
|
|
29
|
+
function toUnixPath(path) {
|
|
30
|
+
return path.replace(/[/\\]+/g, "/");
|
|
40
31
|
}
|
|
41
32
|
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return toUnixPath(path.relative(path.dirname(from), to)).replace(/^(?=[^.])/, './').replace(/\.(ts|js)x?$/i, '');
|
|
33
|
+
* 获得规范化的相对路径。
|
|
34
|
+
*
|
|
35
|
+
* @param from 来源路径
|
|
36
|
+
* @param to 去向路径
|
|
37
|
+
* @returns 相对路径
|
|
38
|
+
*/
|
|
39
|
+
function getNormalizedRelativePath(from, to) {
|
|
40
|
+
return toUnixPath(path.relative(path.dirname(from), to)).replace(/^(?=[^.])/, "./").replace(/\.(ts|js)x?$/i, "");
|
|
51
41
|
}
|
|
52
42
|
/**
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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;
|
|
43
|
+
* 原地遍历 JSONSchema。
|
|
44
|
+
*/
|
|
45
|
+
function traverseJsonSchema(jsonSchema, cb, currentPath = []) {
|
|
46
|
+
/* istanbul ignore if */
|
|
47
|
+
if (!isObject(jsonSchema)) return jsonSchema;
|
|
48
|
+
if (isArray(jsonSchema.properties)) jsonSchema.properties = jsonSchema.properties.reduce((props, js) => {
|
|
49
|
+
props[js.name] = js;
|
|
50
|
+
return props;
|
|
51
|
+
}, {});
|
|
52
|
+
cb(jsonSchema, currentPath);
|
|
53
|
+
if (jsonSchema.properties) forOwn(jsonSchema.properties, (item, key) => traverseJsonSchema(item, cb, [...currentPath, key]));
|
|
54
|
+
if (jsonSchema.items) castArray(jsonSchema.items).forEach((item, index) => traverseJsonSchema(item, cb, [...currentPath, index]));
|
|
55
|
+
if (jsonSchema.oneOf) jsonSchema.oneOf.forEach((item) => traverseJsonSchema(item, cb, currentPath));
|
|
56
|
+
if (jsonSchema.anyOf) jsonSchema.anyOf.forEach((item) => traverseJsonSchema(item, cb, currentPath));
|
|
57
|
+
if (jsonSchema.allOf) jsonSchema.allOf.forEach((item) => traverseJsonSchema(item, cb, currentPath));
|
|
58
|
+
return jsonSchema;
|
|
109
59
|
}
|
|
110
60
|
/**
|
|
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
|
-
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
|
-
});
|
|
61
|
+
* 原地处理 JSONSchema。
|
|
62
|
+
*
|
|
63
|
+
* @param jsonSchema 待处理的 JSONSchema
|
|
64
|
+
* @returns 处理后的 JSONSchema
|
|
65
|
+
*/
|
|
66
|
+
function processJsonSchema(jsonSchema, customTypeMapping) {
|
|
67
|
+
return traverseJsonSchema(jsonSchema, (jsonSchema) => {
|
|
68
|
+
delete jsonSchema.$ref;
|
|
69
|
+
delete jsonSchema.$$ref;
|
|
70
|
+
if (jsonSchema.type === "array" && Array.isArray(jsonSchema.items) && jsonSchema.items.length) jsonSchema.items = jsonSchema.items[0];
|
|
71
|
+
if (jsonSchema.type) {
|
|
72
|
+
const typeMapping = {
|
|
73
|
+
byte: "integer",
|
|
74
|
+
short: "integer",
|
|
75
|
+
int: "integer",
|
|
76
|
+
long: "integer",
|
|
77
|
+
float: "number",
|
|
78
|
+
double: "number",
|
|
79
|
+
bigdecimal: "number",
|
|
80
|
+
char: "string",
|
|
81
|
+
void: "null",
|
|
82
|
+
...mapKeys(customTypeMapping, (_, key) => key.toLowerCase())
|
|
83
|
+
};
|
|
84
|
+
const isMultiple = Array.isArray(jsonSchema.type);
|
|
85
|
+
const types = castArray(jsonSchema.type).map((type) => {
|
|
86
|
+
type = type.toLowerCase();
|
|
87
|
+
type = typeMapping[type] || type;
|
|
88
|
+
return type;
|
|
89
|
+
});
|
|
90
|
+
jsonSchema.type = isMultiple ? types : types[0];
|
|
91
|
+
}
|
|
92
|
+
if (jsonSchema.properties) {
|
|
93
|
+
forOwn(jsonSchema.properties, (_, prop) => {
|
|
94
|
+
const propDef = jsonSchema.properties[prop];
|
|
95
|
+
delete jsonSchema.properties[prop];
|
|
96
|
+
jsonSchema.properties[prop.trim()] = propDef;
|
|
97
|
+
});
|
|
98
|
+
if (Array.isArray(jsonSchema.required)) jsonSchema.required = jsonSchema.required.map((prop) => prop.trim());
|
|
99
|
+
}
|
|
100
|
+
return jsonSchema;
|
|
101
|
+
});
|
|
172
102
|
}
|
|
173
103
|
/**
|
|
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
|
-
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
|
-
});
|
|
104
|
+
* 获取适用于 JSTT 的 JSONSchema。
|
|
105
|
+
*
|
|
106
|
+
* @param jsonSchema 待处理的 JSONSchema
|
|
107
|
+
* @returns 适用于 JSTT 的 JSONSchema
|
|
108
|
+
*/
|
|
109
|
+
function jsonSchemaToJSTTJsonSchema(jsonSchema, typeName) {
|
|
110
|
+
if (jsonSchema) delete jsonSchema.description;
|
|
111
|
+
return traverseJsonSchema(jsonSchema, (jsonSchema, currentPath) => {
|
|
112
|
+
const refValue = jsonSchema.title == null ? jsonSchema.description : jsonSchema.title;
|
|
113
|
+
if (refValue?.startsWith("&")) {
|
|
114
|
+
const typeRelativePath = refValue.substring(1);
|
|
115
|
+
const typeAbsolutePathArr = toUnixPath(path.resolve(path.dirname(`/${currentPath.join("/")}`.replace(/\/{2,}/g, "/")), typeRelativePath).replace(/^[a-z]+:/i, "")).split("/").filter(Boolean);
|
|
116
|
+
let tsTypeLeft = "";
|
|
117
|
+
let tsTypeRight = typeName;
|
|
118
|
+
for (const key of typeAbsolutePathArr) {
|
|
119
|
+
tsTypeLeft += "NonNullable<";
|
|
120
|
+
tsTypeRight += `[${JSON.stringify(key)}]>`;
|
|
121
|
+
}
|
|
122
|
+
jsonSchema.tsType = `${tsTypeLeft}${tsTypeRight}`;
|
|
123
|
+
}
|
|
124
|
+
delete jsonSchema.title;
|
|
125
|
+
delete jsonSchema.id;
|
|
126
|
+
delete jsonSchema.minItems;
|
|
127
|
+
delete jsonSchema.maxItems;
|
|
128
|
+
if (jsonSchema.type === "object") jsonSchema.additionalProperties = false;
|
|
129
|
+
delete jsonSchema.default;
|
|
130
|
+
return jsonSchema;
|
|
131
|
+
});
|
|
224
132
|
}
|
|
225
133
|
/**
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return processJsonSchema(JSON.parse(str), customTypeMapping);
|
|
134
|
+
* 将 JSONSchema 字符串转为 JSONSchema 对象。
|
|
135
|
+
*
|
|
136
|
+
* @param str 要转换的 JSONSchema 字符串
|
|
137
|
+
* @returns 转换后的 JSONSchema 对象
|
|
138
|
+
*/
|
|
139
|
+
function jsonSchemaStringToJsonSchema(str, customTypeMapping) {
|
|
140
|
+
return processJsonSchema(JSON.parse(str), customTypeMapping);
|
|
234
141
|
}
|
|
235
142
|
/**
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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);
|
|
143
|
+
* 获得 JSON 数据的 JSONSchema 对象。
|
|
144
|
+
*
|
|
145
|
+
* @param json JSON 数据
|
|
146
|
+
* @returns JSONSchema 对象
|
|
147
|
+
*/
|
|
148
|
+
function jsonToJsonSchema(json, customTypeMapping) {
|
|
149
|
+
const schema = toJsonSchema(json, {
|
|
150
|
+
required: false,
|
|
151
|
+
arrays: { mode: "first" },
|
|
152
|
+
objects: { additionalProperties: false },
|
|
153
|
+
strings: { detectFormat: false },
|
|
154
|
+
postProcessFnc: (type, schema, value) => {
|
|
155
|
+
if (!schema.description && !!value && type !== "object") schema.description = JSON.stringify(value);
|
|
156
|
+
return schema;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
delete schema.description;
|
|
160
|
+
return processJsonSchema(schema, customTypeMapping);
|
|
264
161
|
}
|
|
265
162
|
/**
|
|
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
|
-
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);
|
|
163
|
+
* 获得 mockjs 模板的 JSONSchema 对象。
|
|
164
|
+
*
|
|
165
|
+
* @param template mockjs 模板
|
|
166
|
+
* @returns JSONSchema 对象
|
|
167
|
+
*/
|
|
168
|
+
function mockjsTemplateToJsonSchema(template, customTypeMapping) {
|
|
169
|
+
const actions = [];
|
|
170
|
+
const keyRe = /(.+)\|(?:\+(\d+)|([+-]?\d+-?[+-]?\d*)?(?:\.(\d+-?\d*))?)/;
|
|
171
|
+
const numberPatterns = [
|
|
172
|
+
"natural",
|
|
173
|
+
"integer",
|
|
174
|
+
"float",
|
|
175
|
+
"range",
|
|
176
|
+
"increment"
|
|
177
|
+
];
|
|
178
|
+
const boolPatterns = ["boolean", "bool"];
|
|
179
|
+
const normalizeValue = (value) => {
|
|
180
|
+
if (typeof value === "string" && value.startsWith("@")) {
|
|
181
|
+
const pattern = value.slice(1);
|
|
182
|
+
if (numberPatterns.some((p) => pattern.startsWith(p))) return 1;
|
|
183
|
+
if (boolPatterns.some((p) => pattern.startsWith(p))) return true;
|
|
184
|
+
}
|
|
185
|
+
return value;
|
|
186
|
+
};
|
|
187
|
+
traverse(template, (value, key, parent) => {
|
|
188
|
+
if (typeof key === "string") actions.push(() => {
|
|
189
|
+
delete parent[key];
|
|
190
|
+
parent[key.replace(keyRe, "$1")] = normalizeValue(value);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
actions.forEach((action) => action());
|
|
194
|
+
return jsonToJsonSchema(template, customTypeMapping);
|
|
313
195
|
}
|
|
314
196
|
/**
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
tsType: FileData.name
|
|
337
|
-
} : {});
|
|
338
|
-
return res;
|
|
339
|
-
}, {})
|
|
340
|
-
}, customTypeMapping);
|
|
197
|
+
* 获得属性定义列表的 JSONSchema 对象。
|
|
198
|
+
*
|
|
199
|
+
* @param propDefinitions 属性定义列表
|
|
200
|
+
* @returns JSONSchema 对象
|
|
201
|
+
*/
|
|
202
|
+
function propDefinitionsToJsonSchema(propDefinitions, customTypeMapping) {
|
|
203
|
+
return processJsonSchema({
|
|
204
|
+
type: "object",
|
|
205
|
+
required: propDefinitions.reduce((res, prop) => {
|
|
206
|
+
if (prop.required) res.push(prop.name);
|
|
207
|
+
return res;
|
|
208
|
+
}, []),
|
|
209
|
+
properties: propDefinitions.reduce((res, prop) => {
|
|
210
|
+
res[prop.name] = {
|
|
211
|
+
type: prop.type,
|
|
212
|
+
description: prop.comment,
|
|
213
|
+
...prop.type === "file" ? { tsType: FileData.name } : {}
|
|
214
|
+
};
|
|
215
|
+
return res;
|
|
216
|
+
}, {})
|
|
217
|
+
}, customTypeMapping);
|
|
341
218
|
}
|
|
342
219
|
var JSTTOptions = {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
220
|
+
bannerComment: "",
|
|
221
|
+
style: {
|
|
222
|
+
bracketSpacing: false,
|
|
223
|
+
printWidth: 120,
|
|
224
|
+
semi: true,
|
|
225
|
+
singleQuote: true,
|
|
226
|
+
tabWidth: 2,
|
|
227
|
+
trailingComma: "none",
|
|
228
|
+
useTabs: false
|
|
229
|
+
}
|
|
353
230
|
};
|
|
354
231
|
/**
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
232
|
+
* 根据 JSONSchema 对象生产 TypeScript 类型定义。
|
|
233
|
+
*
|
|
234
|
+
* @param jsonSchema JSONSchema 对象
|
|
235
|
+
* @param typeName 类型名称
|
|
236
|
+
* @returns TypeScript 类型定义
|
|
237
|
+
*/
|
|
238
|
+
async function jsonSchemaToType(jsonSchema, typeName) {
|
|
239
|
+
if (isEmpty(jsonSchema)) return `export interface ${typeName} {}`;
|
|
240
|
+
if (jsonSchema.__is_any__) {
|
|
241
|
+
delete jsonSchema.__is_any__;
|
|
242
|
+
return `export type ${typeName} = any`;
|
|
243
|
+
}
|
|
244
|
+
const fakeTypeName = "THISISAFAKETYPENAME";
|
|
245
|
+
return (await compile(jsonSchemaToJSTTJsonSchema(cloneDeepFast(jsonSchema), typeName), fakeTypeName, JSTTOptions)).replace(fakeTypeName, typeName).trim();
|
|
364
246
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
247
|
+
function getRequestDataJsonSchema(interfaceInfo, customTypeMapping) {
|
|
248
|
+
let jsonSchema;
|
|
249
|
+
if (isPostLikeMethod(interfaceInfo.method)) switch (interfaceInfo.req_body_type) {
|
|
250
|
+
case RequestBodyType.form:
|
|
251
|
+
jsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_body_form.map((item) => ({
|
|
252
|
+
name: item.name,
|
|
253
|
+
required: item.required === Required.true,
|
|
254
|
+
type: item.type === RequestFormItemType.file ? "file" : "string",
|
|
255
|
+
comment: item.desc
|
|
256
|
+
})), customTypeMapping);
|
|
257
|
+
break;
|
|
258
|
+
case RequestBodyType.json: if (interfaceInfo.req_body_other) jsonSchema = interfaceInfo.req_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.req_body_other, customTypeMapping) : jsonToJsonSchema(JSON5.parse(interfaceInfo.req_body_other), customTypeMapping);
|
|
259
|
+
}
|
|
260
|
+
if (isArray(interfaceInfo.req_query) && interfaceInfo.req_query.length) {
|
|
261
|
+
const queryJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_query.map((item) => ({
|
|
262
|
+
name: item.name,
|
|
263
|
+
required: item.required === Required.true,
|
|
264
|
+
type: item.type || "string",
|
|
265
|
+
comment: item.desc
|
|
266
|
+
})), customTypeMapping);
|
|
267
|
+
/* istanbul ignore else */
|
|
268
|
+
if (jsonSchema) {
|
|
269
|
+
jsonSchema.properties = {
|
|
270
|
+
...jsonSchema.properties,
|
|
271
|
+
...queryJsonSchema.properties
|
|
272
|
+
};
|
|
273
|
+
jsonSchema.required = [...Array.isArray(jsonSchema.required) ? jsonSchema.required : [], ...Array.isArray(queryJsonSchema.required) ? queryJsonSchema.required : []];
|
|
274
|
+
} else jsonSchema = queryJsonSchema;
|
|
275
|
+
}
|
|
276
|
+
if (isArray(interfaceInfo.req_params) && interfaceInfo.req_params.length) {
|
|
277
|
+
const paramsJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_params.map((item) => ({
|
|
278
|
+
name: item.name,
|
|
279
|
+
required: true,
|
|
280
|
+
type: item.type || "string",
|
|
281
|
+
comment: item.desc
|
|
282
|
+
})), customTypeMapping);
|
|
283
|
+
/* istanbul ignore else */
|
|
284
|
+
if (jsonSchema) {
|
|
285
|
+
jsonSchema.properties = {
|
|
286
|
+
...jsonSchema.properties,
|
|
287
|
+
...paramsJsonSchema.properties
|
|
288
|
+
};
|
|
289
|
+
jsonSchema.required = [...Array.isArray(jsonSchema.required) ? jsonSchema.required : [], ...Array.isArray(paramsJsonSchema.required) ? paramsJsonSchema.required : []];
|
|
290
|
+
} else jsonSchema = paramsJsonSchema;
|
|
291
|
+
}
|
|
292
|
+
return jsonSchema || {};
|
|
407
293
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
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 || {};
|
|
294
|
+
function getResponseDataJsonSchema(interfaceInfo, customTypeMapping, dataKey) {
|
|
295
|
+
let jsonSchema = {};
|
|
296
|
+
switch (interfaceInfo.res_body_type) {
|
|
297
|
+
case ResponseBodyType.json:
|
|
298
|
+
if (interfaceInfo.res_body) jsonSchema = interfaceInfo.res_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.res_body, customTypeMapping) : mockjsTemplateToJsonSchema(JSON5.parse(interfaceInfo.res_body), customTypeMapping);
|
|
299
|
+
break;
|
|
300
|
+
default: jsonSchema = { __is_any__: true };
|
|
301
|
+
}
|
|
302
|
+
if (dataKey && jsonSchema) jsonSchema = reachJsonSchema(jsonSchema, dataKey);
|
|
303
|
+
return jsonSchema;
|
|
479
304
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
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;
|
|
305
|
+
function reachJsonSchema(jsonSchema, path) {
|
|
306
|
+
let last = jsonSchema;
|
|
307
|
+
for (const segment of castArray(path)) {
|
|
308
|
+
const _last = last.properties?.[segment];
|
|
309
|
+
if (!_last) return jsonSchema;
|
|
310
|
+
last = _last;
|
|
311
|
+
}
|
|
312
|
+
return last;
|
|
503
313
|
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
last = _last;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
return last;
|
|
314
|
+
function sortByWeights(list) {
|
|
315
|
+
list.sort((a, b) => {
|
|
316
|
+
const x = a.weights.length > b.weights.length ? b : a;
|
|
317
|
+
const minLen = Math.min(a.weights.length, b.weights.length);
|
|
318
|
+
const maxLen = Math.max(a.weights.length, b.weights.length);
|
|
319
|
+
x.weights.push(...new Array(maxLen - minLen).fill(0));
|
|
320
|
+
return a.weights.reduce((w, _, i) => {
|
|
321
|
+
if (w === 0) w = a.weights[i] - b.weights[i];
|
|
322
|
+
return w;
|
|
323
|
+
}, 0);
|
|
324
|
+
});
|
|
325
|
+
return list;
|
|
522
326
|
}
|
|
523
|
-
|
|
524
|
-
|
|
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;
|
|
327
|
+
function isGetLikeMethod(method) {
|
|
328
|
+
return method === Method.GET || method === Method.OPTIONS || method === Method.HEAD;
|
|
543
329
|
}
|
|
544
|
-
|
|
545
|
-
|
|
330
|
+
function isPostLikeMethod(method) {
|
|
331
|
+
return !isGetLikeMethod(method);
|
|
546
332
|
}
|
|
547
|
-
|
|
548
|
-
|
|
333
|
+
async function getPrettier(cwd) {
|
|
334
|
+
const projectPrettierPath = path.join(cwd, "node_modules/prettier");
|
|
335
|
+
if (await fs.pathExists(projectPrettierPath)) return __require(projectPrettierPath);
|
|
336
|
+
return __require("prettier");
|
|
549
337
|
}
|
|
550
|
-
|
|
551
|
-
|
|
338
|
+
async function getPrettierOptions() {
|
|
339
|
+
const prettierOptions = {
|
|
340
|
+
parser: "typescript",
|
|
341
|
+
printWidth: 120,
|
|
342
|
+
tabWidth: 2,
|
|
343
|
+
singleQuote: true,
|
|
344
|
+
semi: false,
|
|
345
|
+
trailingComma: "all",
|
|
346
|
+
bracketSpacing: false,
|
|
347
|
+
endOfLine: "lf"
|
|
348
|
+
};
|
|
349
|
+
if (process.env.JEST_WORKER_ID) return prettierOptions;
|
|
350
|
+
const [prettierConfigPathErr, prettierConfigPath] = await run(() => prettier.resolveConfigFile());
|
|
351
|
+
if (prettierConfigPathErr || !prettierConfigPath) return prettierOptions;
|
|
352
|
+
const [prettierConfigErr, prettierConfig] = await run(() => prettier.resolveConfig(prettierConfigPath));
|
|
353
|
+
if (prettierConfigErr || !prettierConfig) return prettierOptions;
|
|
354
|
+
return {
|
|
355
|
+
...prettierOptions,
|
|
356
|
+
...prettierConfig,
|
|
357
|
+
parser: "typescript"
|
|
358
|
+
};
|
|
552
359
|
}
|
|
553
|
-
|
|
554
|
-
function
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
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);
|
|
360
|
+
var getCachedPrettierOptions = memoize(getPrettierOptions);
|
|
361
|
+
async function httpGet(url, query) {
|
|
362
|
+
const _url = new URL(url);
|
|
363
|
+
if (query) Object.keys(query).forEach((key) => {
|
|
364
|
+
_url.searchParams.set(key, query[key]);
|
|
365
|
+
});
|
|
366
|
+
url = _url.toString();
|
|
367
|
+
return await (await nodeFetch(url, { method: "GET" })).json();
|
|
670
368
|
}
|
|
671
369
|
/**
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
_url.searchParams.set(key, query[key]);
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
url = _url.toString();
|
|
692
|
-
_context4.next = 5;
|
|
693
|
-
return nodeFetch(url, {
|
|
694
|
-
method: 'GET',
|
|
695
|
-
agent: new ProxyAgent()
|
|
696
|
-
});
|
|
697
|
-
|
|
698
|
-
case 5:
|
|
699
|
-
res = _context4.sent;
|
|
700
|
-
return _context4.abrupt("return", res.json());
|
|
701
|
-
|
|
702
|
-
case 7:
|
|
703
|
-
case "end":
|
|
704
|
-
return _context4.stop();
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
}, _callee4);
|
|
708
|
-
}));
|
|
709
|
-
return _httpGet.apply(this, arguments);
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
export var getFilteredCat = function getFilteredCat(cat, cats, isApifox) {
|
|
713
|
-
if (isApifox === void 0) {
|
|
714
|
-
isApifox = false;
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
// 分类处理
|
|
718
|
-
// 数组化
|
|
719
|
-
var categoryIds = castArray(cat.id); // 全部分类
|
|
720
|
-
|
|
721
|
-
if (categoryIds.includes(0)) {
|
|
722
|
-
var _categoryIds;
|
|
723
|
-
|
|
724
|
-
(_categoryIds = categoryIds).push.apply(_categoryIds, cats.map(function (c) {
|
|
725
|
-
return c._id;
|
|
726
|
-
}));
|
|
727
|
-
} // 唯一化
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
categoryIds = uniq(categoryIds); // 去掉被排除的分类
|
|
731
|
-
|
|
732
|
-
var excludedCategoryIds = categoryIds.filter(function (id) {
|
|
733
|
-
return id < 0;
|
|
734
|
-
}).map(Math.abs);
|
|
735
|
-
categoryIds = categoryIds.filter(function (id) {
|
|
736
|
-
return !excludedCategoryIds.includes(Math.abs(id));
|
|
737
|
-
}); // 删除不存在的分类
|
|
738
|
-
|
|
739
|
-
if (!isApifox) {
|
|
740
|
-
categoryIds = categoryIds.filter(function (id) {
|
|
741
|
-
return !!cats.find(function (c) {
|
|
742
|
-
return c._id === id;
|
|
743
|
-
});
|
|
744
|
-
});
|
|
745
|
-
} else {
|
|
746
|
-
categoryIds = categoryIds.filter(function (id) {
|
|
747
|
-
return id !== 0;
|
|
748
|
-
});
|
|
749
|
-
} // 顺序化
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
categoryIds = categoryIds.sort();
|
|
753
|
-
return categoryIds;
|
|
754
|
-
};
|
|
370
|
+
* 获取过滤后的菜单
|
|
371
|
+
*/
|
|
372
|
+
var getFilteredCat = (cat, cats, isApifox = false) => {
|
|
373
|
+
let categoryIds = castArray(cat.id);
|
|
374
|
+
if (categoryIds.includes(0)) categoryIds.push(...cats.map((c) => c._id));
|
|
375
|
+
categoryIds = uniq(categoryIds);
|
|
376
|
+
const excludedCategoryIds = categoryIds.filter((id) => id < 0).map(Math.abs);
|
|
377
|
+
categoryIds = categoryIds.filter((id) => !excludedCategoryIds.includes(Math.abs(id)));
|
|
378
|
+
if (!isApifox) categoryIds = categoryIds.filter((id) => !!cats.find((c) => c._id === id));
|
|
379
|
+
else categoryIds = categoryIds.filter((id) => id !== 0);
|
|
380
|
+
categoryIds = categoryIds.sort();
|
|
381
|
+
return categoryIds;
|
|
382
|
+
};
|
|
383
|
+
//#endregion
|
|
384
|
+
export { getCachedPrettierOptions, getFilteredCat, getNormalizedRelativePath, getPrettier, getPrettierOptions, getRequestDataJsonSchema, getResponseDataJsonSchema, httpGet, isGetLikeMethod, isPostLikeMethod, jsonSchemaStringToJsonSchema, jsonSchemaToJSTTJsonSchema, jsonSchemaToType, jsonToJsonSchema, mockjsTemplateToJsonSchema, processJsonSchema, propDefinitionsToJsonSchema, reachJsonSchema, sortByWeights, throwError, toUnixPath, traverseJsonSchema };
|