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