open-api-typescript-request-generator 0.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/README.md +26 -0
- package/bin/apits-gener +4 -0
- package/es/Generator.d.ts +43 -0
- package/es/Generator.js +892 -0
- package/es/cli.d.ts +19 -0
- package/es/cli.js +547 -0
- package/es/console.d.ts +48 -0
- package/es/console.js +151 -0
- package/es/constants.d.ts +22 -0
- package/es/constants.js +39 -0
- package/es/dependenciesHandler.d.ts +16 -0
- package/es/dependenciesHandler.js +270 -0
- package/es/genIndex.d.ts +23 -0
- package/es/genIndex.js +354 -0
- package/es/genRequest.d.ts +3 -0
- package/es/genRequest.js +216 -0
- package/es/getOutputPath.d.ts +5 -0
- package/es/getOutputPath.js +28 -0
- package/es/helpers.d.ts +63 -0
- package/es/helpers.js +458 -0
- package/es/index.d.ts +2 -0
- package/es/index.js +34 -0
- package/es/requestYapiData.d.ts +157 -0
- package/es/requestYapiData.js +1010 -0
- package/es/responseDataJsonSchemaHandler.d.ts +11 -0
- package/es/responseDataJsonSchemaHandler.js +193 -0
- package/es/server/mock.d.ts +1 -0
- package/es/server/mock.js +7 -0
- package/es/server/swaggerJsonToYApiData.d.ts +10 -0
- package/es/server/swaggerJsonToYApiData.js +614 -0
- package/es/spinner.d.ts +15 -0
- package/es/spinner.js +58 -0
- package/es/types.d.ts +736 -0
- package/es/types.js +143 -0
- package/es/utils.d.ts +97 -0
- package/es/utils.js +783 -0
- package/lib/Generator.d.ts +43 -0
- package/lib/Generator.js +892 -0
- package/lib/cli.d.ts +19 -0
- package/lib/cli.js +547 -0
- package/lib/console.d.ts +48 -0
- package/lib/console.js +151 -0
- package/lib/constants.d.ts +22 -0
- package/lib/constants.js +39 -0
- package/lib/dependenciesHandler.d.ts +16 -0
- package/lib/dependenciesHandler.js +270 -0
- package/lib/genIndex.d.ts +23 -0
- package/lib/genIndex.js +354 -0
- package/lib/genRequest.d.ts +3 -0
- package/lib/genRequest.js +216 -0
- package/lib/getOutputPath.d.ts +5 -0
- package/lib/getOutputPath.js +28 -0
- package/lib/helpers.d.ts +63 -0
- package/lib/helpers.js +458 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +34 -0
- package/lib/requestYapiData.d.ts +157 -0
- package/lib/requestYapiData.js +1010 -0
- package/lib/responseDataJsonSchemaHandler.d.ts +11 -0
- package/lib/responseDataJsonSchemaHandler.js +193 -0
- package/lib/server/mock.d.ts +1 -0
- package/lib/server/mock.js +7 -0
- package/lib/server/swaggerJsonToYApiData.d.ts +10 -0
- package/lib/server/swaggerJsonToYApiData.js +614 -0
- package/lib/spinner.d.ts +15 -0
- package/lib/spinner.js +58 -0
- package/lib/types.d.ts +736 -0
- package/lib/types.js +143 -0
- package/lib/utils.d.ts +97 -0
- package/lib/utils.js +783 -0
- package/package.json +106 -0
package/es/utils.js
ADDED
@@ -0,0 +1,783 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
4
|
+
|
5
|
+
var __assign = this && this.__assign || function () {
|
6
|
+
__assign = Object.assign || function (t) {
|
7
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
8
|
+
s = arguments[i];
|
9
|
+
|
10
|
+
for (var p in s) {
|
11
|
+
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
return t;
|
16
|
+
};
|
17
|
+
|
18
|
+
return __assign.apply(this, arguments);
|
19
|
+
};
|
20
|
+
|
21
|
+
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
|
22
|
+
function adopt(value) {
|
23
|
+
return value instanceof P ? value : new P(function (resolve) {
|
24
|
+
resolve(value);
|
25
|
+
});
|
26
|
+
}
|
27
|
+
|
28
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
29
|
+
function fulfilled(value) {
|
30
|
+
try {
|
31
|
+
step(generator.next(value));
|
32
|
+
} catch (e) {
|
33
|
+
reject(e);
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
function rejected(value) {
|
38
|
+
try {
|
39
|
+
step(generator["throw"](value));
|
40
|
+
} catch (e) {
|
41
|
+
reject(e);
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
function step(result) {
|
46
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
47
|
+
}
|
48
|
+
|
49
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
50
|
+
});
|
51
|
+
};
|
52
|
+
|
53
|
+
var __generator = this && this.__generator || function (thisArg, body) {
|
54
|
+
var _ = {
|
55
|
+
label: 0,
|
56
|
+
sent: function sent() {
|
57
|
+
if (t[0] & 1) throw t[1];
|
58
|
+
return t[1];
|
59
|
+
},
|
60
|
+
trys: [],
|
61
|
+
ops: []
|
62
|
+
},
|
63
|
+
f,
|
64
|
+
y,
|
65
|
+
t,
|
66
|
+
g;
|
67
|
+
return g = {
|
68
|
+
next: verb(0),
|
69
|
+
"throw": verb(1),
|
70
|
+
"return": verb(2)
|
71
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
|
72
|
+
return this;
|
73
|
+
}), g;
|
74
|
+
|
75
|
+
function verb(n) {
|
76
|
+
return function (v) {
|
77
|
+
return step([n, v]);
|
78
|
+
};
|
79
|
+
}
|
80
|
+
|
81
|
+
function step(op) {
|
82
|
+
if (f) throw new TypeError("Generator is already executing.");
|
83
|
+
|
84
|
+
while (_) {
|
85
|
+
try {
|
86
|
+
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;
|
87
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
88
|
+
|
89
|
+
switch (op[0]) {
|
90
|
+
case 0:
|
91
|
+
case 1:
|
92
|
+
t = op;
|
93
|
+
break;
|
94
|
+
|
95
|
+
case 4:
|
96
|
+
_.label++;
|
97
|
+
return {
|
98
|
+
value: op[1],
|
99
|
+
done: false
|
100
|
+
};
|
101
|
+
|
102
|
+
case 5:
|
103
|
+
_.label++;
|
104
|
+
y = op[1];
|
105
|
+
op = [0];
|
106
|
+
continue;
|
107
|
+
|
108
|
+
case 7:
|
109
|
+
op = _.ops.pop();
|
110
|
+
|
111
|
+
_.trys.pop();
|
112
|
+
|
113
|
+
continue;
|
114
|
+
|
115
|
+
default:
|
116
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
117
|
+
_ = 0;
|
118
|
+
continue;
|
119
|
+
}
|
120
|
+
|
121
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
122
|
+
_.label = op[1];
|
123
|
+
break;
|
124
|
+
}
|
125
|
+
|
126
|
+
if (op[0] === 6 && _.label < t[1]) {
|
127
|
+
_.label = t[1];
|
128
|
+
t = op;
|
129
|
+
break;
|
130
|
+
}
|
131
|
+
|
132
|
+
if (t && _.label < t[2]) {
|
133
|
+
_.label = t[2];
|
134
|
+
|
135
|
+
_.ops.push(op);
|
136
|
+
|
137
|
+
break;
|
138
|
+
}
|
139
|
+
|
140
|
+
if (t[2]) _.ops.pop();
|
141
|
+
|
142
|
+
_.trys.pop();
|
143
|
+
|
144
|
+
continue;
|
145
|
+
}
|
146
|
+
|
147
|
+
op = body.call(thisArg, _);
|
148
|
+
} catch (e) {
|
149
|
+
op = [6, e];
|
150
|
+
y = 0;
|
151
|
+
} finally {
|
152
|
+
f = t = 0;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
if (op[0] & 5) throw op[1];
|
157
|
+
return {
|
158
|
+
value: op[0] ? op[1] : void 0,
|
159
|
+
done: true
|
160
|
+
};
|
161
|
+
}
|
162
|
+
};
|
163
|
+
|
164
|
+
var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
|
165
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
166
|
+
if (ar || !(i in from)) {
|
167
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
168
|
+
ar[i] = from[i];
|
169
|
+
}
|
170
|
+
}
|
171
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
172
|
+
};
|
173
|
+
|
174
|
+
var __importDefault = this && this.__importDefault || function (mod) {
|
175
|
+
return mod && mod.__esModule ? mod : {
|
176
|
+
"default": mod
|
177
|
+
};
|
178
|
+
};
|
179
|
+
|
180
|
+
Object.defineProperty(exports, "__esModule", {
|
181
|
+
value: true
|
182
|
+
});
|
183
|
+
exports.filterHandler = exports.topNotesContent = exports.formatContent = exports.sortByWeights = exports.getResponseDataJsonSchema = exports.getRequestDataJsonSchema = exports.jsonSchemaToTsCode = exports.preprocessSchema = exports.getPrettier = exports.propDefinitionsToJsonSchema = exports.mockjsTemplateToJsonSchema = exports.jsonToJsonSchema = exports.jsonSchemaStringToJsonSchema = exports.processJsonSchema = exports.getNormalizedRelativePath = exports.toUnixPath = exports.throwError = void 0;
|
184
|
+
|
185
|
+
var json5_1 = __importDefault(require("json5"));
|
186
|
+
|
187
|
+
var lodash_1 = require("lodash");
|
188
|
+
|
189
|
+
var mockjs_1 = __importDefault(require("mockjs"));
|
190
|
+
|
191
|
+
var path_1 = __importDefault(require("path"));
|
192
|
+
|
193
|
+
var to_json_schema_1 = __importDefault(require("to-json-schema"));
|
194
|
+
|
195
|
+
var vtils_1 = require("vtils");
|
196
|
+
|
197
|
+
var json_schema_to_typescript_1 = require("json-schema-to-typescript");
|
198
|
+
|
199
|
+
var helpers_1 = require("./helpers");
|
200
|
+
|
201
|
+
var prettier_1 = __importDefault(require("prettier"));
|
202
|
+
|
203
|
+
var types_1 = require("./types");
|
204
|
+
/**
|
205
|
+
* 抛出错误。
|
206
|
+
*
|
207
|
+
* @param msg 错误信息
|
208
|
+
*/
|
209
|
+
|
210
|
+
|
211
|
+
function throwError() {
|
212
|
+
var msg = [];
|
213
|
+
|
214
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
215
|
+
msg[_i] = arguments[_i];
|
216
|
+
}
|
217
|
+
/* istanbul ignore next */
|
218
|
+
|
219
|
+
|
220
|
+
throw new Error(msg.join(''));
|
221
|
+
}
|
222
|
+
|
223
|
+
exports.throwError = throwError;
|
224
|
+
/**
|
225
|
+
* 将路径统一为 unix 风格的路径。
|
226
|
+
*
|
227
|
+
* @param path 路径
|
228
|
+
* @returns unix 风格的路径
|
229
|
+
*/
|
230
|
+
|
231
|
+
function toUnixPath(path) {
|
232
|
+
return path.replace(/[/\\]+/g, '/');
|
233
|
+
}
|
234
|
+
|
235
|
+
exports.toUnixPath = toUnixPath;
|
236
|
+
/**
|
237
|
+
* 获得规范化的相对路径。
|
238
|
+
*
|
239
|
+
* @param from 来源路径
|
240
|
+
* @param to 去向路径
|
241
|
+
* @returns 相对路径
|
242
|
+
*/
|
243
|
+
|
244
|
+
function getNormalizedRelativePath(from, to) {
|
245
|
+
return toUnixPath(path_1.default.relative(path_1.default.dirname(from), to)).replace(/^(?=[^.])/, './').replace(/\.(ts|js)x?$/i, '');
|
246
|
+
}
|
247
|
+
|
248
|
+
exports.getNormalizedRelativePath = getNormalizedRelativePath;
|
249
|
+
/**
|
250
|
+
* 原地处理 JSONSchema。
|
251
|
+
*
|
252
|
+
* @param jsonSchema 待处理的 JSONSchema
|
253
|
+
* @returns 处理后的 JSONSchema
|
254
|
+
*/
|
255
|
+
|
256
|
+
function processJsonSchema(jsonSchema) {
|
257
|
+
return jsonSchema;
|
258
|
+
/* istanbul ignore if */
|
259
|
+
|
260
|
+
if (!(0, vtils_1.isObject)(jsonSchema)) return jsonSchema; // 去除 title 和 id,防止 json-schema-to-typescript 提取它们作为接口名
|
261
|
+
|
262
|
+
delete jsonSchema.title;
|
263
|
+
delete jsonSchema.id; // 忽略数组长度限制
|
264
|
+
|
265
|
+
delete jsonSchema.minItems;
|
266
|
+
delete jsonSchema.maxItems; // 将 additionalProperties 设为 false
|
267
|
+
// jsonSchema.additionalProperties = false;
|
268
|
+
// 删除通过 swagger 导入时未剔除的 ref
|
269
|
+
// delete jsonSchema.$ref;
|
270
|
+
// delete jsonSchema.$$ref;
|
271
|
+
// 删除 default,防止 json-schema-to-typescript 根据它推测类型
|
272
|
+
|
273
|
+
delete jsonSchema.default; // 处理类型名称为标准的 JSONSchema 类型名称
|
274
|
+
|
275
|
+
if (jsonSchema.type) {
|
276
|
+
var isMultiple = Array.isArray(jsonSchema.type);
|
277
|
+
var types = (0, vtils_1.castArray)(jsonSchema.type).map(function (type) {
|
278
|
+
// 所有类型转成小写,如:String -> string
|
279
|
+
type = type.toLowerCase(); // 映射为标准的 JSONSchema 类型
|
280
|
+
|
281
|
+
type = {
|
282
|
+
int: 'integer'
|
283
|
+
}[type] || type;
|
284
|
+
return type;
|
285
|
+
});
|
286
|
+
jsonSchema.type = isMultiple ? types : types[0];
|
287
|
+
} // Mock.toJSONSchema 产生的 properties 为数组,然而 JSONSchema4 的 properties 为对象
|
288
|
+
|
289
|
+
|
290
|
+
if ((0, vtils_1.isArray)(jsonSchema.properties)) {
|
291
|
+
// @ts-ignore
|
292
|
+
jsonSchema.properties = jsonSchema.properties.reduce(function (props, js) {
|
293
|
+
props[js.name] = js;
|
294
|
+
return props;
|
295
|
+
}, {});
|
296
|
+
} // 移除字段名称首尾空格
|
297
|
+
|
298
|
+
|
299
|
+
if (jsonSchema.properties) {
|
300
|
+
(0, vtils_1.forOwn)(jsonSchema.properties, function (_, prop) {
|
301
|
+
var propDef = jsonSchema.properties[prop];
|
302
|
+
delete jsonSchema.properties[prop];
|
303
|
+
jsonSchema.properties[prop.trim()] = propDef;
|
304
|
+
});
|
305
|
+
jsonSchema.required = jsonSchema.required && jsonSchema.required.map(function (prop) {
|
306
|
+
return prop.trim();
|
307
|
+
});
|
308
|
+
} // 继续处理对象的子元素
|
309
|
+
|
310
|
+
|
311
|
+
if (jsonSchema.properties) {
|
312
|
+
(0, vtils_1.forOwn)(jsonSchema.properties, processJsonSchema);
|
313
|
+
} // 继续处理数组的子元素
|
314
|
+
|
315
|
+
|
316
|
+
if (jsonSchema.items) {
|
317
|
+
(0, vtils_1.castArray)(jsonSchema.items).forEach(processJsonSchema);
|
318
|
+
} // 处理 oneOf
|
319
|
+
|
320
|
+
|
321
|
+
if (jsonSchema.oneOf) {
|
322
|
+
jsonSchema.oneOf.forEach(processJsonSchema);
|
323
|
+
} // 处理 anyOf
|
324
|
+
|
325
|
+
|
326
|
+
if (jsonSchema.anyOf) {
|
327
|
+
jsonSchema.anyOf.forEach(processJsonSchema);
|
328
|
+
} // 处理 allOf
|
329
|
+
|
330
|
+
|
331
|
+
if (jsonSchema.allOf) {
|
332
|
+
jsonSchema.allOf.forEach(processJsonSchema);
|
333
|
+
}
|
334
|
+
|
335
|
+
return jsonSchema;
|
336
|
+
}
|
337
|
+
|
338
|
+
exports.processJsonSchema = processJsonSchema;
|
339
|
+
/**
|
340
|
+
* 将 JSONSchema 字符串转为 JSONSchema 对象。
|
341
|
+
*
|
342
|
+
* @param str 要转换的 JSONSchema 字符串
|
343
|
+
* @returns 转换后的 JSONSchema 对象
|
344
|
+
*/
|
345
|
+
|
346
|
+
function jsonSchemaStringToJsonSchema(str) {
|
347
|
+
return processJsonSchema(JSON.parse(str));
|
348
|
+
}
|
349
|
+
|
350
|
+
exports.jsonSchemaStringToJsonSchema = jsonSchemaStringToJsonSchema;
|
351
|
+
/**
|
352
|
+
* 获得 JSON 数据的 JSONSchema 对象。
|
353
|
+
*
|
354
|
+
* @param json JSON 数据
|
355
|
+
* @returns JSONSchema 对象
|
356
|
+
*/
|
357
|
+
|
358
|
+
function jsonToJsonSchema(json) {
|
359
|
+
var schema = (0, to_json_schema_1.default)(json, {
|
360
|
+
required: false,
|
361
|
+
arrays: {
|
362
|
+
mode: 'first'
|
363
|
+
},
|
364
|
+
objects: {
|
365
|
+
additionalProperties: false
|
366
|
+
},
|
367
|
+
strings: {
|
368
|
+
detectFormat: false
|
369
|
+
},
|
370
|
+
postProcessFnc: function postProcessFnc(type, schema, value) {
|
371
|
+
if (!schema.description && !!value && type !== 'object') {
|
372
|
+
schema.description = JSON.stringify(value);
|
373
|
+
}
|
374
|
+
|
375
|
+
return schema;
|
376
|
+
}
|
377
|
+
});
|
378
|
+
delete schema.description;
|
379
|
+
return processJsonSchema(schema);
|
380
|
+
}
|
381
|
+
|
382
|
+
exports.jsonToJsonSchema = jsonToJsonSchema;
|
383
|
+
/**
|
384
|
+
* 获得 mockjs 模板的 JSONSchema 对象。
|
385
|
+
*
|
386
|
+
* @param template mockjs 模板
|
387
|
+
* @returns JSONSchema 对象
|
388
|
+
*/
|
389
|
+
|
390
|
+
function mockjsTemplateToJsonSchema(template) {
|
391
|
+
return processJsonSchema(mockjs_1.default.toJSONSchema(template));
|
392
|
+
}
|
393
|
+
|
394
|
+
exports.mockjsTemplateToJsonSchema = mockjsTemplateToJsonSchema;
|
395
|
+
/**
|
396
|
+
* 获得属性定义列表的 JSONSchema 对象。
|
397
|
+
*
|
398
|
+
* @param propDefinitions 属性定义列表
|
399
|
+
* @returns JSONSchema 对象
|
400
|
+
*/
|
401
|
+
|
402
|
+
function propDefinitionsToJsonSchema(propDefinitions) {
|
403
|
+
return processJsonSchema({
|
404
|
+
type: 'object',
|
405
|
+
required: propDefinitions.reduce(function (res, prop) {
|
406
|
+
if (prop.required) {
|
407
|
+
res.push(prop.name);
|
408
|
+
}
|
409
|
+
|
410
|
+
return res;
|
411
|
+
}, []),
|
412
|
+
properties: propDefinitions.reduce(function (res, prop) {
|
413
|
+
res[prop.name] = __assign({
|
414
|
+
type: prop.type,
|
415
|
+
description: prop.comment
|
416
|
+
}, prop.type === 'file' ? {
|
417
|
+
tsType: helpers_1.FileData.name
|
418
|
+
} : {});
|
419
|
+
return res;
|
420
|
+
}, {})
|
421
|
+
});
|
422
|
+
}
|
423
|
+
|
424
|
+
exports.propDefinitionsToJsonSchema = propDefinitionsToJsonSchema;
|
425
|
+
/**
|
426
|
+
* 获取prettier配置
|
427
|
+
* @returns
|
428
|
+
*/
|
429
|
+
|
430
|
+
function getPrettier(filePath) {
|
431
|
+
// 从项目中获取prettier配置文件
|
432
|
+
var configPath = prettier_1.default.resolveConfigFile.sync(filePath && path_1.default.resolve(filePath) || process.cwd());
|
433
|
+
var config = configPath && prettier_1.default.resolveConfig.sync(configPath) || {};
|
434
|
+
return config ? __assign({
|
435
|
+
parser: 'babel-ts'
|
436
|
+
}, config) : {
|
437
|
+
printWidth: 120,
|
438
|
+
tabWidth: 2,
|
439
|
+
singleQuote: true,
|
440
|
+
semi: true,
|
441
|
+
trailingComma: 'all',
|
442
|
+
bracketSpacing: false,
|
443
|
+
endOfLine: 'lf',
|
444
|
+
parser: 'babel-ts'
|
445
|
+
};
|
446
|
+
}
|
447
|
+
|
448
|
+
exports.getPrettier = getPrettier; // 预处理函数,处理空enum和其他边界情况
|
449
|
+
|
450
|
+
function preprocessSchema(schema) {
|
451
|
+
if (!(0, vtils_1.isObject)(schema)) {
|
452
|
+
return schema;
|
453
|
+
}
|
454
|
+
|
455
|
+
if (Array.isArray(schema)) {
|
456
|
+
return schema.map(preprocessSchema);
|
457
|
+
}
|
458
|
+
|
459
|
+
var processed = __assign({}, schema); // 处理空enum
|
460
|
+
|
461
|
+
|
462
|
+
if (processed.enum && Array.isArray(processed.enum) && processed.enum.length === 0) {
|
463
|
+
delete processed.enum; // console.warn('Removed empty enum array from schema');
|
464
|
+
} // 递归处理所有属性
|
465
|
+
|
466
|
+
|
467
|
+
for (var key in processed) {
|
468
|
+
if (processed.hasOwnProperty(key)) {
|
469
|
+
processed[key] = preprocessSchema(processed[key]);
|
470
|
+
}
|
471
|
+
}
|
472
|
+
|
473
|
+
return processed;
|
474
|
+
}
|
475
|
+
|
476
|
+
exports.preprocessSchema = preprocessSchema;
|
477
|
+
/**
|
478
|
+
* 根据 JSONSchema 对象生产 TypeScript 类型定义。
|
479
|
+
*
|
480
|
+
* @param jsonSchema JSONSchema 对象
|
481
|
+
* @param typeName 类型名称
|
482
|
+
* @returns TypeScript 类型定义
|
483
|
+
*/
|
484
|
+
|
485
|
+
function jsonSchemaToTsCode(jsonSchema, typeName) {
|
486
|
+
return __awaiter(this, void 0, void 0, function () {
|
487
|
+
function rewriteRefs(obj) {
|
488
|
+
var _loop_1 = function _loop_1(key) {
|
489
|
+
if (obj.hasOwnProperty(key)) {
|
490
|
+
if (key === '$ref' && typeof obj[key] === 'string') {
|
491
|
+
var refValue = obj[key]; // '#/components'是标准路径,但是仍然有些不标准的数据源为'#components'
|
492
|
+
|
493
|
+
if (refValue.startsWith('#components')) {
|
494
|
+
refValue = refValue.replace('#components', '#/components');
|
495
|
+
} // 匹配指向 components.schemas 的引用
|
496
|
+
|
497
|
+
|
498
|
+
if (refValue.startsWith('#/components/schemas/')) {
|
499
|
+
var interfaceName = refValue.replace('#/components/schemas/', '');
|
500
|
+
/**
|
501
|
+
* /components/schemas下的文件会被我们生成 文件名 命名的 ts interface,
|
502
|
+
* 这里使用tsType标记后,compiler会根据这个标记来生成对应的ts interface引用
|
503
|
+
* 但是有个问题compile会把tsType,第一个字母变成大写,如果schemas下的文件名为小写开头,那么最终
|
504
|
+
* 生成出来的引用interface的名称对不上,例如
|
505
|
+
* interface user {}
|
506
|
+
* interface ABC {
|
507
|
+
* user: User;
|
508
|
+
* }
|
509
|
+
* 那么统一命名为大写开头,那么就可以避免compile导致的名称不一致
|
510
|
+
*/
|
511
|
+
|
512
|
+
obj['tsType'] = (0, lodash_1.upperFirst)(interfaceName);
|
513
|
+
/**
|
514
|
+
* 如果输入的对象除了$ref存在,并且还存在其他属性,那么需要用这些属性创建一个ts对象。常见的用例是,url中的/{id}路径参数
|
515
|
+
* 输出:UpdateVideoCollectionDto & { id: 'string' }
|
516
|
+
*/
|
517
|
+
|
518
|
+
if (obj.properties) {
|
519
|
+
var propertiesTsObject_1 = {};
|
520
|
+
Object.keys(obj.properties).forEach(function (key) {
|
521
|
+
propertiesTsObject_1["".concat(key).concat(obj.required.includes(key) ? '' : '?')] = obj.properties[key].tsType || obj.properties[key].type;
|
522
|
+
});
|
523
|
+
obj['tsType'] += " & ".concat(JSON.stringify(propertiesTsObject_1));
|
524
|
+
}
|
525
|
+
|
526
|
+
delete obj['$ref'];
|
527
|
+
}
|
528
|
+
} else if (_typeof(obj[key]) === 'object' && obj[key] !== null) {
|
529
|
+
// 递归遍历对象
|
530
|
+
rewriteRefs(obj[key]);
|
531
|
+
}
|
532
|
+
}
|
533
|
+
};
|
534
|
+
|
535
|
+
for (var key in obj) {
|
536
|
+
_loop_1(key);
|
537
|
+
}
|
538
|
+
}
|
539
|
+
|
540
|
+
var fakeTypeName, code;
|
541
|
+
return __generator(this, function (_a) {
|
542
|
+
switch (_a.label) {
|
543
|
+
case 0:
|
544
|
+
jsonSchema = preprocessSchema(jsonSchema); // 那么统一命名为大写开头,那么就可以避免compile导致的名称不一致
|
545
|
+
|
546
|
+
typeName = (0, lodash_1.upperFirst)(typeName);
|
547
|
+
|
548
|
+
if ((0, vtils_1.isEmpty)(jsonSchema)) {
|
549
|
+
return [2
|
550
|
+
/*return*/
|
551
|
+
, "export interface ".concat(typeName, " {}")];
|
552
|
+
}
|
553
|
+
|
554
|
+
if (jsonSchema.__is_any__) {
|
555
|
+
delete jsonSchema.__is_any__;
|
556
|
+
return [2
|
557
|
+
/*return*/
|
558
|
+
, "export type ".concat(typeName, " = any")];
|
559
|
+
}
|
560
|
+
|
561
|
+
rewriteRefs(jsonSchema);
|
562
|
+
fakeTypeName = 'THISISAFAKETYPENAME';
|
563
|
+
return [4
|
564
|
+
/*yield*/
|
565
|
+
, (0, json_schema_to_typescript_1.compile)(jsonSchema, fakeTypeName, {
|
566
|
+
bannerComment: '',
|
567
|
+
additionalProperties: false,
|
568
|
+
declareExternallyReferenced: false // style: getPrettier(),
|
569
|
+
// customName:(...rest) => {
|
570
|
+
// console.log(rest)
|
571
|
+
// if(rest[0].tsType==='WorkComment'){
|
572
|
+
// return 'WorkComment'+Math.random().toString()
|
573
|
+
// }
|
574
|
+
// return undefined
|
575
|
+
// },
|
576
|
+
|
577
|
+
})];
|
578
|
+
|
579
|
+
case 1:
|
580
|
+
code = _a.sent();
|
581
|
+
|
582
|
+
if (typeName === 'ListFilePathsResDto') {// console.log(jsonSchema);
|
583
|
+
}
|
584
|
+
|
585
|
+
if (typeName === 'GetAwsS3FilesFileIdPathResponse') {// console.log(jsonSchema);
|
586
|
+
}
|
587
|
+
|
588
|
+
if (typeName === 'PatchVideoCollectionsIdRequest') {// console.log(jsonSchema);
|
589
|
+
}
|
590
|
+
|
591
|
+
if (typeName === 'PatchPermissionsPermissionIdRequest') {
|
592
|
+
/**
|
593
|
+
* export type PatchPermissionsPermissionIdRequest = {
|
594
|
+
permissionId: number;
|
595
|
+
} & string;这个& string的问题和req_body_other有关,todo 后续再看
|
596
|
+
*/
|
597
|
+
// console.log(jsonSchema);
|
598
|
+
}
|
599
|
+
|
600
|
+
delete jsonSchema.id;
|
601
|
+
return [2
|
602
|
+
/*return*/
|
603
|
+
, code.replace(fakeTypeName, typeName).trim()];
|
604
|
+
}
|
605
|
+
});
|
606
|
+
});
|
607
|
+
}
|
608
|
+
|
609
|
+
exports.jsonSchemaToTsCode = jsonSchemaToTsCode;
|
610
|
+
|
611
|
+
function getRequestDataJsonSchema(interfaceInfo) {
|
612
|
+
var jsonSchema;
|
613
|
+
|
614
|
+
switch (interfaceInfo.req_body_type) {
|
615
|
+
case types_1.RequestBodyType.form:
|
616
|
+
jsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_body_form.map(function (item) {
|
617
|
+
return {
|
618
|
+
name: item.name,
|
619
|
+
required: item.required === types_1.Required.true,
|
620
|
+
type: item.type === types_1.RequestFormItemType.file ? 'file' : 'string',
|
621
|
+
comment: item.desc
|
622
|
+
};
|
623
|
+
}));
|
624
|
+
break;
|
625
|
+
|
626
|
+
case types_1.RequestBodyType.json:
|
627
|
+
if (interfaceInfo.req_body_other) {
|
628
|
+
jsonSchema = interfaceInfo.req_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.req_body_other) : jsonToJsonSchema(json5_1.default.parse(interfaceInfo.req_body_other));
|
629
|
+
}
|
630
|
+
|
631
|
+
break;
|
632
|
+
|
633
|
+
default:
|
634
|
+
/* istanbul ignore next */
|
635
|
+
break;
|
636
|
+
}
|
637
|
+
|
638
|
+
if ((0, vtils_1.isArray)(interfaceInfo.req_query) && interfaceInfo.req_query.length) {
|
639
|
+
var queryJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_query.map(function (item) {
|
640
|
+
return {
|
641
|
+
name: item.name,
|
642
|
+
required: item.required === types_1.Required.true,
|
643
|
+
type: item.type || 'any',
|
644
|
+
comment: item.desc
|
645
|
+
};
|
646
|
+
}));
|
647
|
+
/* istanbul ignore else */
|
648
|
+
|
649
|
+
if (jsonSchema) {
|
650
|
+
jsonSchema.properties = __assign(__assign({}, jsonSchema.properties), queryJsonSchema.properties);
|
651
|
+
jsonSchema.required = __spreadArray(__spreadArray([], jsonSchema.required || [], true), queryJsonSchema.required || [], true);
|
652
|
+
} else {
|
653
|
+
jsonSchema = queryJsonSchema;
|
654
|
+
}
|
655
|
+
}
|
656
|
+
|
657
|
+
if ((0, vtils_1.isArray)(interfaceInfo.req_params) && interfaceInfo.req_params.length) {
|
658
|
+
var paramsJsonSchema = propDefinitionsToJsonSchema(interfaceInfo.req_params.map(function (item) {
|
659
|
+
return {
|
660
|
+
name: item.name,
|
661
|
+
required: true,
|
662
|
+
type: item.type || 'string',
|
663
|
+
comment: item.desc
|
664
|
+
};
|
665
|
+
}));
|
666
|
+
/* istanbul ignore else */
|
667
|
+
|
668
|
+
if (jsonSchema) {
|
669
|
+
jsonSchema.properties = __assign(__assign({}, jsonSchema.properties), paramsJsonSchema.properties);
|
670
|
+
jsonSchema.required = __spreadArray(__spreadArray([], jsonSchema.required || [], true), paramsJsonSchema.required || [], true);
|
671
|
+
} else {
|
672
|
+
jsonSchema = paramsJsonSchema;
|
673
|
+
}
|
674
|
+
}
|
675
|
+
|
676
|
+
return jsonSchema;
|
677
|
+
}
|
678
|
+
|
679
|
+
exports.getRequestDataJsonSchema = getRequestDataJsonSchema;
|
680
|
+
|
681
|
+
function getResponseDataJsonSchema(interfaceInfo, dataKey) {
|
682
|
+
var jsonSchema = {};
|
683
|
+
|
684
|
+
switch (interfaceInfo.res_body_type) {
|
685
|
+
case types_1.ResponseBodyType.json:
|
686
|
+
if (interfaceInfo.res_body) {
|
687
|
+
jsonSchema = interfaceInfo.res_body_is_json_schema ? jsonSchemaStringToJsonSchema(interfaceInfo.res_body) : mockjsTemplateToJsonSchema(json5_1.default.parse(interfaceInfo.res_body));
|
688
|
+
}
|
689
|
+
|
690
|
+
break;
|
691
|
+
|
692
|
+
default:
|
693
|
+
jsonSchema = {
|
694
|
+
__is_any__: true
|
695
|
+
};
|
696
|
+
break;
|
697
|
+
}
|
698
|
+
/* istanbul ignore if */
|
699
|
+
|
700
|
+
|
701
|
+
if (dataKey && jsonSchema && jsonSchema.properties && jsonSchema.properties[dataKey]) {
|
702
|
+
jsonSchema = jsonSchema.properties[dataKey];
|
703
|
+
}
|
704
|
+
|
705
|
+
return jsonSchema;
|
706
|
+
}
|
707
|
+
|
708
|
+
exports.getResponseDataJsonSchema = getResponseDataJsonSchema;
|
709
|
+
|
710
|
+
function sortByWeights(list) {
|
711
|
+
list.sort(function (a, b) {
|
712
|
+
var _a;
|
713
|
+
|
714
|
+
var x = a.weights.length > b.weights.length ? b : a;
|
715
|
+
var minLen = Math.min(a.weights.length, b.weights.length);
|
716
|
+
var maxLen = Math.max(a.weights.length, b.weights.length);
|
717
|
+
|
718
|
+
(_a = x.weights).push.apply(_a, new Array(maxLen - minLen).fill(0));
|
719
|
+
|
720
|
+
var w = a.weights.reduce(function (w, _, i) {
|
721
|
+
if (w === 0) {
|
722
|
+
w = a.weights[i] - b.weights[i];
|
723
|
+
}
|
724
|
+
|
725
|
+
return w;
|
726
|
+
}, 0);
|
727
|
+
return w;
|
728
|
+
});
|
729
|
+
return list;
|
730
|
+
}
|
731
|
+
|
732
|
+
exports.sortByWeights = sortByWeights;
|
733
|
+
/**
|
734
|
+
* 格式化代码字符串
|
735
|
+
* @param content
|
736
|
+
* @param config
|
737
|
+
* @returns
|
738
|
+
* https://prettier.io/docs/en/options.html
|
739
|
+
*/
|
740
|
+
|
741
|
+
function formatContent(content, prettierConfigPath) {
|
742
|
+
// 从项目中获取prettier配置文件
|
743
|
+
var config = getPrettier(prettierConfigPath);
|
744
|
+
var prettyOutputContent = prettier_1.default.format(content, config);
|
745
|
+
return prettyOutputContent;
|
746
|
+
}
|
747
|
+
|
748
|
+
exports.formatContent = formatContent;
|
749
|
+
/**
|
750
|
+
* 通用生成文件顶部注释
|
751
|
+
* @returns
|
752
|
+
*/
|
753
|
+
|
754
|
+
function topNotesContent() {
|
755
|
+
return "\n /**\n * Created By api-ts-generator\n */\n\n ";
|
756
|
+
}
|
757
|
+
|
758
|
+
exports.topNotesContent = topNotesContent;
|
759
|
+
/**
|
760
|
+
* filter处理函数
|
761
|
+
* @param interfaceFilter
|
762
|
+
* @returns
|
763
|
+
*/
|
764
|
+
|
765
|
+
var filterHandler = function filterHandler(interfaceFilter) {
|
766
|
+
return function (path, id) {
|
767
|
+
if (interfaceFilter instanceof RegExp && !interfaceFilter.test(path)) {
|
768
|
+
return false;
|
769
|
+
}
|
770
|
+
|
771
|
+
if (interfaceFilter instanceof Array && !interfaceFilter.includes(path)) {
|
772
|
+
return false;
|
773
|
+
}
|
774
|
+
|
775
|
+
if (interfaceFilter instanceof Function && !interfaceFilter(path, id)) {
|
776
|
+
return false;
|
777
|
+
}
|
778
|
+
|
779
|
+
return true;
|
780
|
+
};
|
781
|
+
};
|
782
|
+
|
783
|
+
exports.filterHandler = filterHandler;
|