echoapi-converter 0.0.82 → 0.0.84
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/dist/index.d.ts +15 -1
- package/dist/index.js +315 -20
- package/package.json +4 -1
package/dist/index.d.ts
CHANGED
|
@@ -280,4 +280,18 @@ declare class ThunderToApipost {
|
|
|
280
280
|
};
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
|
|
283
|
+
type ImportConfig = {
|
|
284
|
+
mode: string;
|
|
285
|
+
folder_id: string;
|
|
286
|
+
host?: string;
|
|
287
|
+
base_path?: string;
|
|
288
|
+
api_cover_modal: string;
|
|
289
|
+
model_cover_modal: string;
|
|
290
|
+
env_cover_modal: string;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
declare const swaggerConverFormat: (json: any, baseConfig: ImportConfig, project_id: string) => {
|
|
294
|
+
global: {};
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
export { ApipostToSwagger as ApiToSwagger, ApidocToApipost, ApifoxToApipost, ApipostToPostman, HarToApipost, InsomniaToApipost, PostmanToApipost, swaggerConverFormat as SwaggerConverFormat, SwaggerToApipost, ThunderToApipost, YapiToApipost };
|
package/dist/index.js
CHANGED
|
@@ -9,8 +9,9 @@ var i18next = require('i18next');
|
|
|
9
9
|
var MockSchema = require('apipost-mock-schema');
|
|
10
10
|
var axios = require('axios');
|
|
11
11
|
var SwaggerClient = require('swagger-client');
|
|
12
|
-
var
|
|
12
|
+
var generate = require('json-schema-generator');
|
|
13
13
|
var JSON5 = require('json5');
|
|
14
|
+
var toJsonSchemasecond = require('to-json-schema');
|
|
14
15
|
|
|
15
16
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
17
|
|
|
@@ -19,8 +20,9 @@ var i18next__default = /*#__PURE__*/_interopDefaultLegacy(i18next);
|
|
|
19
20
|
var MockSchema__default = /*#__PURE__*/_interopDefaultLegacy(MockSchema);
|
|
20
21
|
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
|
|
21
22
|
var SwaggerClient__default = /*#__PURE__*/_interopDefaultLegacy(SwaggerClient);
|
|
22
|
-
var
|
|
23
|
+
var generate__default = /*#__PURE__*/_interopDefaultLegacy(generate);
|
|
23
24
|
var JSON5__default = /*#__PURE__*/_interopDefaultLegacy(JSON5);
|
|
25
|
+
var toJsonSchemasecond__default = /*#__PURE__*/_interopDefaultLegacy(toJsonSchemasecond);
|
|
24
26
|
|
|
25
27
|
/******************************************************************************
|
|
26
28
|
Copyright (c) Microsoft Corporation.
|
|
@@ -631,6 +633,32 @@ const getSubstring = (str, length) => {
|
|
|
631
633
|
return str;
|
|
632
634
|
};
|
|
633
635
|
|
|
636
|
+
const DEFAULT_RESPONSE = {
|
|
637
|
+
example_id: '1',
|
|
638
|
+
raw: '',
|
|
639
|
+
raw_parameter: [],
|
|
640
|
+
headers: [], // 新headers
|
|
641
|
+
expect: {
|
|
642
|
+
name: '成功',
|
|
643
|
+
is_default: -1,
|
|
644
|
+
code: '200',
|
|
645
|
+
sleep: 0, // 新sleep
|
|
646
|
+
content_type: 'json',
|
|
647
|
+
verify_type: 'schema',
|
|
648
|
+
mock: '',
|
|
649
|
+
schema: {
|
|
650
|
+
type: 'object',
|
|
651
|
+
properties: {},
|
|
652
|
+
},
|
|
653
|
+
},
|
|
654
|
+
};
|
|
655
|
+
const SUCCESS_RESPONSE = Object.assign(Object.assign({}, DEFAULT_RESPONSE), { example_id: '1', expect: Object.assign(Object.assign({}, DEFAULT_RESPONSE.expect), { name: '成功', is_default: 1, code: '200' }) });
|
|
656
|
+
const ERROR_RESPONSE = Object.assign(Object.assign({}, DEFAULT_RESPONSE), { example_id: '2', expect: Object.assign(Object.assign({}, DEFAULT_RESPONSE.expect), { name: '失败', is_default: -1, code: '404' }) });
|
|
657
|
+
const DEFAULT_API_RESPONSE = {
|
|
658
|
+
is_check_result: 1,
|
|
659
|
+
example: [SUCCESS_RESPONSE, ERROR_RESPONSE],
|
|
660
|
+
};
|
|
661
|
+
|
|
634
662
|
const getApipostMode = (mode) => {
|
|
635
663
|
let apipostMode = 'none';
|
|
636
664
|
if (mode == 'multipart/form-data') {
|
|
@@ -715,7 +743,6 @@ const handleBodyJsonSchema = (schema, raw_para, pre = '', requiredArr = [], prop
|
|
|
715
743
|
}
|
|
716
744
|
else if (itemsType === "integer") {
|
|
717
745
|
let value = ((_t = (_s = (_r = schema === null || schema === void 0 ? void 0 : schema.items) === null || _r === void 0 ? void 0 : _r.oneOf) === null || _s === void 0 ? void 0 : _s[0]) === null || _t === void 0 ? void 0 : _t.example) || ((_u = schema === null || schema === void 0 ? void 0 : schema.items) === null || _u === void 0 ? void 0 : _u.example) || 0;
|
|
718
|
-
// console.log(value,"valuevalue");
|
|
719
746
|
if (Object.prototype.toString.call(raw_para) === '[object Array]') {
|
|
720
747
|
raw_para.push({
|
|
721
748
|
key: `${pre}.0`,
|
|
@@ -1069,7 +1096,7 @@ const completionResponse = (responses) => {
|
|
|
1069
1096
|
const example = [];
|
|
1070
1097
|
if (lodash.isPlainObject(responses)) {
|
|
1071
1098
|
Object.keys(responses).forEach((status) => {
|
|
1072
|
-
var _a, _b, _c, _d;
|
|
1099
|
+
var _a, _b, _c, _d, _e;
|
|
1073
1100
|
const element = responses[status];
|
|
1074
1101
|
if ((lodash.has(element, "schema") &&
|
|
1075
1102
|
lodash.has(element.schema, "properties") &&
|
|
@@ -1153,29 +1180,30 @@ const completionResponse = (responses) => {
|
|
|
1153
1180
|
if (lodash.has(schema, "properties")) ;
|
|
1154
1181
|
if (lodash.has(schema, "items")) ;
|
|
1155
1182
|
}
|
|
1156
|
-
if (apipostMode == "json") {
|
|
1183
|
+
if (apipostMode == "json" || apipostMode === "*/*") {
|
|
1157
1184
|
let jsonSchema = {};
|
|
1158
|
-
if (Object.prototype.toString.call((_d = bodyData === null || bodyData === void 0 ? void 0 : bodyData.schema) === null || _d === void 0 ? void 0 : _d["$$ref"]) === "[object String]"
|
|
1185
|
+
if (Object.prototype.toString.call((_d = bodyData === null || bodyData === void 0 ? void 0 : bodyData.schema) === null || _d === void 0 ? void 0 : _d["$$ref"]) === "[object String]" ||
|
|
1186
|
+
Object.prototype.toString.call((_e = bodyData === null || bodyData === void 0 ? void 0 : bodyData.schema) === null || _e === void 0 ? void 0 : _e["$ref"]) === "[object String]") {
|
|
1159
1187
|
jsonSchema = swaggerSchema2apipostSchema(bodyData.schema);
|
|
1160
1188
|
}
|
|
1161
1189
|
else if (lodash.isPlainObject(bodyData === null || bodyData === void 0 ? void 0 : bodyData.schema)) {
|
|
1162
1190
|
jsonSchema = bodyData.schema;
|
|
1163
1191
|
}
|
|
1164
1192
|
if (lodash.has(bodyData, "example")) {
|
|
1165
|
-
const
|
|
1193
|
+
const exampleRaw = bodyData.example;
|
|
1166
1194
|
if (status == 200 || status == 404) {
|
|
1167
1195
|
let originRaw;
|
|
1168
1196
|
try {
|
|
1169
|
-
if (Object.prototype.toString.call(
|
|
1197
|
+
if (Object.prototype.toString.call(exampleRaw) ===
|
|
1170
1198
|
"[object Object]") {
|
|
1171
|
-
originRaw = JSON.stringify(Object.assign({},
|
|
1199
|
+
originRaw = JSON.stringify(Object.assign({}, exampleRaw));
|
|
1172
1200
|
}
|
|
1173
1201
|
else {
|
|
1174
|
-
originRaw = String(
|
|
1202
|
+
originRaw = String(exampleRaw);
|
|
1175
1203
|
}
|
|
1176
1204
|
}
|
|
1177
1205
|
catch (error) {
|
|
1178
|
-
originRaw = String(
|
|
1206
|
+
originRaw = String(exampleRaw);
|
|
1179
1207
|
}
|
|
1180
1208
|
const Raw_text = getBeautifulRaw(originRaw);
|
|
1181
1209
|
example.push({
|
|
@@ -1198,16 +1226,16 @@ const completionResponse = (responses) => {
|
|
|
1198
1226
|
const newUUID = apipostTools.snowflakeId();
|
|
1199
1227
|
let originRaw;
|
|
1200
1228
|
try {
|
|
1201
|
-
if (Object.prototype.toString.call(
|
|
1229
|
+
if (Object.prototype.toString.call(exampleRaw) ===
|
|
1202
1230
|
"[object Object]") {
|
|
1203
|
-
originRaw = JSON.stringify(Object.assign({},
|
|
1231
|
+
originRaw = JSON.stringify(Object.assign({}, exampleRaw));
|
|
1204
1232
|
}
|
|
1205
1233
|
else {
|
|
1206
|
-
originRaw =
|
|
1234
|
+
originRaw = exampleRaw;
|
|
1207
1235
|
}
|
|
1208
1236
|
}
|
|
1209
1237
|
catch (error) {
|
|
1210
|
-
originRaw = String(
|
|
1238
|
+
originRaw = String(exampleRaw);
|
|
1211
1239
|
}
|
|
1212
1240
|
const Raw_text = getBeautifulRaw(originRaw);
|
|
1213
1241
|
example.push({
|
|
@@ -1463,6 +1491,254 @@ const completionSocketMethodRequest = (swaggerApi) => {
|
|
|
1463
1491
|
},
|
|
1464
1492
|
};
|
|
1465
1493
|
};
|
|
1494
|
+
const completionConfig = (res, config, baseConfig) => {
|
|
1495
|
+
var _a;
|
|
1496
|
+
res['config'] = Object.assign(Object.assign({}, baseConfig), { host: config.host, base_path: config.base_path });
|
|
1497
|
+
(_a = res === null || res === void 0 ? void 0 : res.config) === null || _a === void 0 ? true : delete _a.mode;
|
|
1498
|
+
};
|
|
1499
|
+
const completionProject = (res, project, project_id) => {
|
|
1500
|
+
res['name'] = project.name;
|
|
1501
|
+
res['project_id'] = project_id;
|
|
1502
|
+
res['intro'] = project.description;
|
|
1503
|
+
};
|
|
1504
|
+
const completionSwaggerEnv = (res, env) => {
|
|
1505
|
+
res['global']['envs'] = env === null || env === void 0 ? void 0 : env.map(({ name, env_var_list, url }) => ({
|
|
1506
|
+
env_id: apipostTools.snowflakeId(),
|
|
1507
|
+
name,
|
|
1508
|
+
is_private: -1,
|
|
1509
|
+
env_var_list: env_var_list || {},
|
|
1510
|
+
server_list: url
|
|
1511
|
+
? [{ name: '默认服务', server_id: '1', uri: url }]
|
|
1512
|
+
: undefined,
|
|
1513
|
+
}));
|
|
1514
|
+
};
|
|
1515
|
+
const ApiItemInit = (res, apis, parent_id = '0', project_id) => {
|
|
1516
|
+
if (lodash.isArray(apis) && apis.length) {
|
|
1517
|
+
apis.forEach(({ name, target_type, tags, url, method, description, response, request, children, mark_id, }) => {
|
|
1518
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
1519
|
+
const targetItem = {
|
|
1520
|
+
target_type,
|
|
1521
|
+
description,
|
|
1522
|
+
mark_id: mark_id || '1',
|
|
1523
|
+
request: {
|
|
1524
|
+
auth: DEFAULT_AUTH,
|
|
1525
|
+
pre_tasks: [],
|
|
1526
|
+
post_tasks: [],
|
|
1527
|
+
header: {
|
|
1528
|
+
parameter: ((_a = request === null || request === void 0 ? void 0 : request.header) === null || _a === void 0 ? void 0 : _a.parameter) || [],
|
|
1529
|
+
},
|
|
1530
|
+
query: {
|
|
1531
|
+
parameter: ((_b = request === null || request === void 0 ? void 0 : request.query) === null || _b === void 0 ? void 0 : _b.parameter) || [],
|
|
1532
|
+
},
|
|
1533
|
+
cookie: {
|
|
1534
|
+
parameter: ((_c = request === null || request === void 0 ? void 0 : request.cookie) === null || _c === void 0 ? void 0 : _c.parameter) || [],
|
|
1535
|
+
},
|
|
1536
|
+
restful: {
|
|
1537
|
+
parameter: ((_d = request === null || request === void 0 ? void 0 : request.restful) === null || _d === void 0 ? void 0 : _d.parameter) || [],
|
|
1538
|
+
},
|
|
1539
|
+
},
|
|
1540
|
+
ai_expect_enable: -1,
|
|
1541
|
+
is_check_result: 1,
|
|
1542
|
+
is_socket: 1,
|
|
1543
|
+
is_locked: -1,
|
|
1544
|
+
is_force: -1,
|
|
1545
|
+
attribute_info: {},
|
|
1546
|
+
protocol: 'http/1.1',
|
|
1547
|
+
ai_expect: {
|
|
1548
|
+
list: [],
|
|
1549
|
+
none_math_expect_id: '1',
|
|
1550
|
+
},
|
|
1551
|
+
project_id,
|
|
1552
|
+
target_id: apipostTools.snowflakeId(),
|
|
1553
|
+
parent_id,
|
|
1554
|
+
sort: 0,
|
|
1555
|
+
version: 0,
|
|
1556
|
+
server_id: '0',
|
|
1557
|
+
status: 1,
|
|
1558
|
+
is_changed: -1,
|
|
1559
|
+
};
|
|
1560
|
+
if (target_type === 'api') {
|
|
1561
|
+
targetItem['name'] = getSubstring(name, 255) || '新建接口';
|
|
1562
|
+
targetItem['tags'] = tags || [];
|
|
1563
|
+
targetItem['url'] = url || '';
|
|
1564
|
+
targetItem['method'] = method || '';
|
|
1565
|
+
targetItem['request'] = Object.assign(Object.assign({}, targetItem['request']), { body: {
|
|
1566
|
+
mode: ((_e = request === null || request === void 0 ? void 0 : request.body) === null || _e === void 0 ? void 0 : _e.mode) || 'none',
|
|
1567
|
+
parameter: ((_f = request === null || request === void 0 ? void 0 : request.body) === null || _f === void 0 ? void 0 : _f.parameter) || [],
|
|
1568
|
+
raw: ((_g = request === null || request === void 0 ? void 0 : request.body) === null || _g === void 0 ? void 0 : _g.raw) || '',
|
|
1569
|
+
raw_parameter: ((_h = request === null || request === void 0 ? void 0 : request.body) === null || _h === void 0 ? void 0 : _h.raw_parameter) || [],
|
|
1570
|
+
raw_schema: ((_j = request === null || request === void 0 ? void 0 : request.body) === null || _j === void 0 ? void 0 : _j.raw_schema) || { type: 'object' },
|
|
1571
|
+
binary: null,
|
|
1572
|
+
} });
|
|
1573
|
+
targetItem['response'] = Object.assign(Object.assign({}, DEFAULT_API_RESPONSE), response);
|
|
1574
|
+
if (!((_k = response === null || response === void 0 ? void 0 : response.example) === null || _k === void 0 ? void 0 : _k.find((e) => (e === null || e === void 0 ? void 0 : e.example_id) === '2'))) {
|
|
1575
|
+
(_l = targetItem['response']) === null || _l === void 0 ? void 0 : _l.example.unshift(ERROR_RESPONSE);
|
|
1576
|
+
}
|
|
1577
|
+
if (!((_m = response === null || response === void 0 ? void 0 : response.example) === null || _m === void 0 ? void 0 : _m.find((e) => (e === null || e === void 0 ? void 0 : e.example_id) === '1'))) {
|
|
1578
|
+
(_o = targetItem['response']) === null || _o === void 0 ? void 0 : _o.example.unshift(SUCCESS_RESPONSE);
|
|
1579
|
+
}
|
|
1580
|
+
// 通过query重新生成url
|
|
1581
|
+
if (targetItem.url && lodash.isArray((_q = (_p = targetItem === null || targetItem === void 0 ? void 0 : targetItem.request) === null || _p === void 0 ? void 0 : _p.query) === null || _q === void 0 ? void 0 : _q.parameter)) {
|
|
1582
|
+
const newUrl = genUrlByQuery(targetItem.url, (_s = (_r = targetItem === null || targetItem === void 0 ? void 0 : targetItem.request) === null || _r === void 0 ? void 0 : _r.query) === null || _s === void 0 ? void 0 : _s.parameter);
|
|
1583
|
+
targetItem.url = newUrl;
|
|
1584
|
+
}
|
|
1585
|
+
res.apis.push(targetItem);
|
|
1586
|
+
if (lodash.isArray(children) && children.length) {
|
|
1587
|
+
ApiItemInit(res, children, targetItem.target_id, project_id);
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
else if (target_type === 'folder') {
|
|
1591
|
+
targetItem['name'] = getSubstring(name, 255) || '新建目录';
|
|
1592
|
+
targetItem['request'] = Object.assign(Object.assign({}, targetItem['request']), { body: {
|
|
1593
|
+
parameter: ((_t = request === null || request === void 0 ? void 0 : request.body) === null || _t === void 0 ? void 0 : _t.parameter) || [],
|
|
1594
|
+
} });
|
|
1595
|
+
res.apis.push(targetItem);
|
|
1596
|
+
if (lodash.isArray(children) && children.length) {
|
|
1597
|
+
ApiItemInit(res, children, targetItem.target_id, project_id);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1602
|
+
};
|
|
1603
|
+
const ModelItemInit = (res, dataModel, parent_id = '0', project_id) => {
|
|
1604
|
+
if (lodash.isArray(dataModel) && dataModel.length) {
|
|
1605
|
+
// createModel(dataModel, newJson, '0');
|
|
1606
|
+
dataModel.forEach((model) => {
|
|
1607
|
+
let model_type = 'model';
|
|
1608
|
+
if (lodash.has(model, 'model_type')) {
|
|
1609
|
+
if (model.model_type == 'folder' ||
|
|
1610
|
+
(lodash.has(model, 'children') && model.children instanceof Array)) {
|
|
1611
|
+
model_type = 'folder';
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
try {
|
|
1615
|
+
const hashIndex = model === null || model === void 0 ? void 0 : model.model_id.indexOf('#');
|
|
1616
|
+
if (hashIndex !== -1) {
|
|
1617
|
+
model.model_id = model.model_id.substring(hashIndex);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
catch (error) { }
|
|
1621
|
+
const new_model_id = apipostTools.snowflakeId();
|
|
1622
|
+
const target = {
|
|
1623
|
+
model_id: new_model_id,
|
|
1624
|
+
old_model_id: (model === null || model === void 0 ? void 0 : model.model_id) || '',
|
|
1625
|
+
model_type,
|
|
1626
|
+
description: (model === null || model === void 0 ? void 0 : model.description) || '',
|
|
1627
|
+
parent_id,
|
|
1628
|
+
project_id,
|
|
1629
|
+
sort: (model === null || model === void 0 ? void 0 : model.sort) || 0,
|
|
1630
|
+
schema: {
|
|
1631
|
+
type: 'object',
|
|
1632
|
+
properties: {},
|
|
1633
|
+
},
|
|
1634
|
+
name: '新建数据模型',
|
|
1635
|
+
display_name: '',
|
|
1636
|
+
created_at: 0,
|
|
1637
|
+
updated_at: 0,
|
|
1638
|
+
// created_id: '',
|
|
1639
|
+
// updated_id: '',
|
|
1640
|
+
version: 0,
|
|
1641
|
+
is_changed: -1,
|
|
1642
|
+
status: 1,
|
|
1643
|
+
created_user_info: {
|
|
1644
|
+
nick_name: '',
|
|
1645
|
+
portrait: '',
|
|
1646
|
+
},
|
|
1647
|
+
updated_user_info: {
|
|
1648
|
+
nick_name: '',
|
|
1649
|
+
portrait: '',
|
|
1650
|
+
},
|
|
1651
|
+
};
|
|
1652
|
+
if (model_type == 'folder') {
|
|
1653
|
+
target['name'] = (model === null || model === void 0 ? void 0 : model.name) || '新建目录';
|
|
1654
|
+
res.models.push(target);
|
|
1655
|
+
ModelItemInit(res, model === null || model === void 0 ? void 0 : model.children, target.model_id, project_id);
|
|
1656
|
+
}
|
|
1657
|
+
else if (model_type == 'model') {
|
|
1658
|
+
target['name'] = (model === null || model === void 0 ? void 0 : model.name) || '新建接口';
|
|
1659
|
+
target['display_name'] = (model === null || model === void 0 ? void 0 : model.displayName) || '';
|
|
1660
|
+
target['schema'] = (model === null || model === void 0 ? void 0 : model.schema) || {};
|
|
1661
|
+
res.models.push(target);
|
|
1662
|
+
}
|
|
1663
|
+
});
|
|
1664
|
+
}
|
|
1665
|
+
};
|
|
1666
|
+
const completionSwaggerApis = (res, apis, project_id) => {
|
|
1667
|
+
var _a;
|
|
1668
|
+
res['apis'] = [];
|
|
1669
|
+
ApiItemInit(res, apis, (_a = res === null || res === void 0 ? void 0 : res.config) === null || _a === void 0 ? void 0 : _a.folder, project_id);
|
|
1670
|
+
};
|
|
1671
|
+
const completionSwaggerModel = (res, dataModel, version, project_id) => {
|
|
1672
|
+
var _a, _b;
|
|
1673
|
+
res['models'] = [];
|
|
1674
|
+
// 新增目录schemas
|
|
1675
|
+
const DEFAULT_FOLDER = {
|
|
1676
|
+
description: '',
|
|
1677
|
+
displayName: '',
|
|
1678
|
+
model_type: 'folder',
|
|
1679
|
+
name: 'schemas',
|
|
1680
|
+
children: dataModel,
|
|
1681
|
+
};
|
|
1682
|
+
if (version === '2.0') {
|
|
1683
|
+
ModelItemInit(res, [DEFAULT_FOLDER], (_a = res === null || res === void 0 ? void 0 : res.config) === null || _a === void 0 ? void 0 : _a.folder, project_id);
|
|
1684
|
+
}
|
|
1685
|
+
else {
|
|
1686
|
+
ModelItemInit(res, dataModel, (_b = res === null || res === void 0 ? void 0 : res.config) === null || _b === void 0 ? void 0 : _b.folder, project_id);
|
|
1687
|
+
}
|
|
1688
|
+
if (res.models.length > 0) {
|
|
1689
|
+
try {
|
|
1690
|
+
const idMap = new Map();
|
|
1691
|
+
res.models.forEach((model) => {
|
|
1692
|
+
if (model.old_model_id) {
|
|
1693
|
+
idMap.set(model.old_model_id, model.model_id);
|
|
1694
|
+
}
|
|
1695
|
+
});
|
|
1696
|
+
const replaceIdsIteratively = (root) => {
|
|
1697
|
+
const stack = [root];
|
|
1698
|
+
const visited = new WeakSet(); // 防止循环引用
|
|
1699
|
+
while (stack.length > 0) {
|
|
1700
|
+
const current = stack.pop();
|
|
1701
|
+
// 跳过基本类型和已访问过的对象
|
|
1702
|
+
if (typeof current !== 'object' || current === null || visited.has(current)) {
|
|
1703
|
+
continue;
|
|
1704
|
+
}
|
|
1705
|
+
visited.add(current);
|
|
1706
|
+
// 处理数组
|
|
1707
|
+
if (Array.isArray(current)) {
|
|
1708
|
+
stack.push(...current);
|
|
1709
|
+
continue;
|
|
1710
|
+
}
|
|
1711
|
+
// 处理对象
|
|
1712
|
+
for (const key in current) {
|
|
1713
|
+
const value = current[key];
|
|
1714
|
+
// 1. 处理普通字符串ID的直接替换(包括完整路径)
|
|
1715
|
+
if (typeof value === 'string' && idMap.has(value)) {
|
|
1716
|
+
current[key] = idMap.get(value);
|
|
1717
|
+
}
|
|
1718
|
+
// 2. 处理嵌套对象
|
|
1719
|
+
else if (typeof value === 'object' && value !== null) {
|
|
1720
|
+
stack.push(value);
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
};
|
|
1725
|
+
replaceIdsIteratively(res.models);
|
|
1726
|
+
replaceIdsIteratively(res.apis);
|
|
1727
|
+
// let dataModelStr = JSON.stringify(res.models);
|
|
1728
|
+
// let apisStr = JSON.stringify(res.apis);
|
|
1729
|
+
// for (const model of res.models) {
|
|
1730
|
+
// if (model?.old_model_id) {
|
|
1731
|
+
// const reg = new RegExp(`"${escapeRegExp(model.old_model_id)}"`, 'g');
|
|
1732
|
+
// dataModelStr = dataModelStr.replace(reg, `"${model.model_id}"`);
|
|
1733
|
+
// apisStr = apisStr.replace(reg, `"${model.model_id}"`);
|
|
1734
|
+
// }
|
|
1735
|
+
// }
|
|
1736
|
+
// res.apis = JSON.parse(apisStr);
|
|
1737
|
+
// res.models = JSON.parse(dataModelStr);
|
|
1738
|
+
}
|
|
1739
|
+
catch (error) { }
|
|
1740
|
+
}
|
|
1741
|
+
};
|
|
1466
1742
|
|
|
1467
1743
|
var securityConnectionMessageNoPath$4 = "When connecting via a secure connection, {{enable}} server certificate validation, and use client version {{version}} with no specified handshake path.";
|
|
1468
1744
|
var notNeedEnable$4 = "do not enable";
|
|
@@ -2298,6 +2574,9 @@ const createPathItem = (item, sourceObj, pid, pidName, modelsObj, apiVersion, ta
|
|
|
2298
2574
|
description: (item === null || item === void 0 ? void 0 : item.description) || "",
|
|
2299
2575
|
tags: []
|
|
2300
2576
|
};
|
|
2577
|
+
if (lodash.has(item, 'mock_server_enable')) {
|
|
2578
|
+
pathItem['x-mock-server-enable'] = item === null || item === void 0 ? void 0 : item.mock_server_enable;
|
|
2579
|
+
}
|
|
2301
2580
|
if (lodash.isArray(item === null || item === void 0 ? void 0 : item.tags) && (item === null || item === void 0 ? void 0 : item.tags.length) > 0) {
|
|
2302
2581
|
pathItem['x-tags'] = item === null || item === void 0 ? void 0 : item.tags;
|
|
2303
2582
|
}
|
|
@@ -2530,6 +2809,7 @@ class ApipostToSwagger {
|
|
|
2530
2809
|
const servers = {
|
|
2531
2810
|
url: (env === null || env === void 0 ? void 0 : env.url) || "",
|
|
2532
2811
|
description: (env === null || env === void 0 ? void 0 : env.name) || t('createEnv'),
|
|
2812
|
+
"x-env-id": (env === null || env === void 0 ? void 0 : env.env_id) || ''
|
|
2533
2813
|
};
|
|
2534
2814
|
if (lodash.isPlainObject(env === null || env === void 0 ? void 0 : env.env_var_list)) {
|
|
2535
2815
|
servers.variables = {};
|
|
@@ -3028,7 +3308,7 @@ class SwaggerToApipost {
|
|
|
3028
3308
|
mark_id: (swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.deprecated) ? "4" : "1",
|
|
3029
3309
|
};
|
|
3030
3310
|
if (protocol === 'http' || protocol === 'sse') {
|
|
3031
|
-
baseApiInfo = Object.assign(Object.assign({}, baseApiInfo), { target_type: protocol === 'http' ? 'api' : protocol, method: completionMethod(method), protocol: 'http/1.1', request: Object.assign({}, completionRequestParam(swaggerApi)), response: Object.assign({}, completionResponse((swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.
|
|
3311
|
+
baseApiInfo = Object.assign(Object.assign({}, baseApiInfo), { target_type: protocol === 'http' ? 'api' : protocol, method: completionMethod(method), protocol: 'http/1.1', request: Object.assign({}, completionRequestParam(swaggerApi)), response: Object.assign({}, completionResponse((swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.responses) || {})) });
|
|
3032
3312
|
// 根据query参数 重新生成url
|
|
3033
3313
|
baseApiInfo.url = genUrlByQuery(baseApiInfo.url, ((_b = (_a = baseApiInfo === null || baseApiInfo === void 0 ? void 0 : baseApiInfo.request) === null || _a === void 0 ? void 0 : _a.restful) === null || _b === void 0 ? void 0 : _b.parameter) || []);
|
|
3034
3314
|
}
|
|
@@ -3043,10 +3323,10 @@ class SwaggerToApipost {
|
|
|
3043
3323
|
baseApiInfo.url = genUrlByQuery(baseApiInfo.url, ((_f = (_e = baseApiInfo === null || baseApiInfo === void 0 ? void 0 : baseApiInfo.request) === null || _e === void 0 ? void 0 : _e.restful) === null || _f === void 0 ? void 0 : _f.parameter) || []);
|
|
3044
3324
|
}
|
|
3045
3325
|
if (protocol === 'graphql') {
|
|
3046
|
-
baseApiInfo = Object.assign(Object.assign({}, baseApiInfo), { url, request: completionGraphqlRequest(swaggerApi), response: completionResponse((swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.
|
|
3326
|
+
baseApiInfo = Object.assign(Object.assign({}, baseApiInfo), { url, request: completionGraphqlRequest(swaggerApi), response: completionResponse((swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.responses) || {}) });
|
|
3047
3327
|
}
|
|
3048
3328
|
if (protocol === 'socket_method') {
|
|
3049
|
-
baseApiInfo = Object.assign(Object.assign({}, baseApiInfo), { method: 'TCP', url, request: completionSocketMethodRequest(swaggerApi), response: Object.assign({}, completionResponse((swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.
|
|
3329
|
+
baseApiInfo = Object.assign(Object.assign({}, baseApiInfo), { method: 'TCP', url, request: completionSocketMethodRequest(swaggerApi), response: Object.assign({}, completionResponse((swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.responses) || {})) });
|
|
3050
3330
|
}
|
|
3051
3331
|
if (protocol === 'doc') {
|
|
3052
3332
|
baseApiInfo = Object.assign({}, baseApiInfo);
|
|
@@ -6910,7 +7190,7 @@ class PostmanToApipost {
|
|
|
6910
7190
|
params["raw"] = lodash.isString(body === null || body === void 0 ? void 0 : body.raw) ? getBeautifulRaw$1(body === null || body === void 0 ? void 0 : body.raw) : "";
|
|
6911
7191
|
if (apipostTools.isJson5(params["raw"])) {
|
|
6912
7192
|
try {
|
|
6913
|
-
params.raw_schema =
|
|
7193
|
+
params.raw_schema = generate__default["default"](JSON5__default["default"].parse(body === null || body === void 0 ? void 0 : body.raw));
|
|
6914
7194
|
}
|
|
6915
7195
|
catch (error) { }
|
|
6916
7196
|
}
|
|
@@ -7079,7 +7359,7 @@ class PostmanToApipost {
|
|
|
7079
7359
|
};
|
|
7080
7360
|
if (apipostTools.isJson5(item === null || item === void 0 ? void 0 : item["body"])) {
|
|
7081
7361
|
try {
|
|
7082
|
-
raw_schema =
|
|
7362
|
+
raw_schema = generate__default["default"](JSON5__default["default"].parse(item.body));
|
|
7083
7363
|
}
|
|
7084
7364
|
catch (error) { }
|
|
7085
7365
|
}
|
|
@@ -8325,6 +8605,20 @@ class ThunderToApipost {
|
|
|
8325
8605
|
}
|
|
8326
8606
|
}
|
|
8327
8607
|
|
|
8608
|
+
const swaggerConverFormat = (json, baseConfig, project_id) => {
|
|
8609
|
+
const { project, env, apis, config, dataModel, version } = json || {};
|
|
8610
|
+
const res = {
|
|
8611
|
+
global: {},
|
|
8612
|
+
};
|
|
8613
|
+
// 不要动顺序ß
|
|
8614
|
+
completionConfig(res, config, baseConfig);
|
|
8615
|
+
completionProject(res, project, project_id);
|
|
8616
|
+
completionSwaggerEnv(res, env);
|
|
8617
|
+
completionSwaggerApis(res, apis, project_id);
|
|
8618
|
+
completionSwaggerModel(res, dataModel, version, project_id);
|
|
8619
|
+
return res;
|
|
8620
|
+
};
|
|
8621
|
+
|
|
8328
8622
|
exports.ApiToSwagger = ApipostToSwagger;
|
|
8329
8623
|
exports.ApidocToApipost = ApidocToApipost;
|
|
8330
8624
|
exports.ApifoxToApipost = ApifoxToApipost;
|
|
@@ -8332,6 +8626,7 @@ exports.ApipostToPostman = ApipostToPostman;
|
|
|
8332
8626
|
exports.HarToApipost = HarToApipost;
|
|
8333
8627
|
exports.InsomniaToApipost = InsomniaToApipost;
|
|
8334
8628
|
exports.PostmanToApipost = PostmanToApipost;
|
|
8629
|
+
exports.SwaggerConverFormat = swaggerConverFormat;
|
|
8335
8630
|
exports.SwaggerToApipost = SwaggerToApipost;
|
|
8336
8631
|
exports.ThunderToApipost = ThunderToApipost;
|
|
8337
8632
|
exports.YapiToApipost = YapiToApipost;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "echoapi-converter",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.84",
|
|
4
4
|
"description": "echoapi to swagger",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "jest",
|
|
11
11
|
"build": "rollup -c rollup.config.js",
|
|
12
|
+
"build:java": "rollup -c rollup.java.config.js",
|
|
12
13
|
"publish": "npm publish"
|
|
13
14
|
},
|
|
14
15
|
"author": "",
|
|
@@ -32,6 +33,7 @@
|
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
35
|
"@rollup/plugin-json": "^6.1.0",
|
|
36
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
35
37
|
"@types/lodash": "^4.14.202",
|
|
36
38
|
"apipost-mock-schema": "^0.0.37",
|
|
37
39
|
"apipost-tools": "0.0.41",
|
|
@@ -39,6 +41,7 @@
|
|
|
39
41
|
"fs": "^0.0.1-security",
|
|
40
42
|
"i18next": "^24.2.3",
|
|
41
43
|
"jest": "^29.7.0",
|
|
44
|
+
"json-schema-generator": "^2.0.6",
|
|
42
45
|
"json5": "^2.2.3",
|
|
43
46
|
"lodash": "^4.17.21",
|
|
44
47
|
"path": "^0.12.7",
|