echoapi-converter 0.0.35 → 0.0.37

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 CHANGED
@@ -259,6 +259,9 @@ declare class ThunderToApipost {
259
259
  private completionTests;
260
260
  private completionApiRequest;
261
261
  private completionApiTarget;
262
+ private completionWs2Request;
263
+ private completionWs2Message;
264
+ private completionWs2Target;
262
265
  private completionApis;
263
266
  private initApis;
264
267
  private initGlobal;
package/dist/index.js CHANGED
@@ -500,11 +500,11 @@ const handelPathItemResponse = (response) => {
500
500
  return temp_responses;
501
501
  };
502
502
 
503
- const throwResult$1 = (status, message, data = '') => {
503
+ const throwResult$1 = (status, message, data = "") => {
504
504
  return {
505
505
  status,
506
506
  message,
507
- data
507
+ data,
508
508
  };
509
509
  };
510
510
  const transExample = (example) => {
@@ -524,21 +524,22 @@ const getBeautifulRaw = (raw) => {
524
524
  }
525
525
  };
526
526
  const isArray = (value) => {
527
- return Object.prototype.toString.call(value) === '[object Array]';
527
+ return Object.prototype.toString.call(value) === "[object Array]";
528
528
  };
529
529
  const isObject = (value) => {
530
- return Object.prototype.toString.call(value) === '[object Object]';
530
+ return Object.prototype.toString.call(value) === "[object Object]";
531
531
  };
532
532
  const isURL = (_url) => {
533
533
  if (!_url) {
534
534
  return false;
535
535
  }
536
536
  _url = lodash.trim(_url);
537
- return (_url.substr(0, 7).toLowerCase() === 'http://' || _url.substr(0, 8).toLowerCase() === 'https://');
537
+ return (_url.substr(0, 7).toLowerCase() === "http://" ||
538
+ _url.substr(0, 8).toLowerCase() === "https://");
538
539
  };
539
540
  const createUrl = (_url) => {
540
- if (typeof _url !== 'string') {
541
- _url += '';
541
+ if (typeof _url !== "string") {
542
+ _url += "";
542
543
  }
543
544
  if (!isURL(_url)) {
544
545
  _url = `http://${_url}`;
@@ -558,7 +559,7 @@ const createUrl = (_url) => {
558
559
  }
559
560
  catch (_a) {
560
561
  const http_url = `https://www.echoapi.cn${_url.substring(origin.length)}`;
561
- const a = document.createElement('a');
562
+ const a = document.createElement("a");
562
563
  a.href = http_url;
563
564
  urls = {
564
565
  source: _url,
@@ -571,23 +572,23 @@ const createUrl = (_url) => {
571
572
  search: a.search,
572
573
  pathname: a.pathname,
573
574
  // eslint-disable-next-line no-useless-escape
574
- file: (a.pathname.match(/\/([^\/?#]+)$/i) || ['', ''])[1],
575
+ file: (a.pathname.match(/\/([^\/?#]+)$/i) || ["", ""])[1],
575
576
  hash: a.hash,
576
577
  };
577
578
  }
578
579
  return urls;
579
580
  };
580
581
  // 根据url生成新的restful数组
581
- const genRestfulByUrl = (url, oldParameter, key = 'key') => {
582
+ const genRestfulByUrl = (url, oldParameter, key = "key") => {
582
583
  const restfulList = [];
583
584
  // 自动拼接url http://
584
585
  if (!isURL(url)) {
585
586
  url = `http://${url}`;
586
587
  }
587
588
  const urlObj = createUrl(url);
588
- const paths = urlObj.pathname.split('/');
589
+ const paths = urlObj.pathname.split("/");
589
590
  lodash.forEach(paths, (pathsItem) => {
590
- if (pathsItem.substring(0, 1) === ':' && pathsItem.length > 1) {
591
+ if (pathsItem.substring(0, 1) === ":" && pathsItem.length > 1) {
591
592
  let obj = null;
592
593
  const oldIndex = lodash.findIndex(oldParameter, {
593
594
  [key]: pathsItem.substring(1, pathsItem.length),
@@ -597,12 +598,12 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
597
598
  }
598
599
  restfulList.push({
599
600
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || ATools.snowflakeId(),
600
- field_type: 'String',
601
+ field_type: "String",
601
602
  is_checked: 1,
602
603
  key: pathsItem.substring(1, pathsItem.length),
603
604
  not_null: 1,
604
- value: (obj === null || obj === void 0 ? void 0 : obj.value) || '',
605
- description: (obj === null || obj === void 0 ? void 0 : obj.description) || '',
605
+ value: (obj === null || obj === void 0 ? void 0 : obj.value) || "",
606
+ description: (obj === null || obj === void 0 ? void 0 : obj.description) || "",
606
607
  });
607
608
  }
608
609
  });
@@ -612,7 +613,7 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
612
613
  if (isArray(matches) && matches.length > 0) {
613
614
  lodash.forEach(matches, (matchesItem) => {
614
615
  if (matchesItem.length > 0) {
615
- matchesItem = matchesItem.replace(/^{|}$/g, '');
616
+ matchesItem = matchesItem.replace(/^{|}$/g, "");
616
617
  let obj = null;
617
618
  const oldIndex = lodash.findIndex(oldParameter, {
618
619
  [key]: matchesItem,
@@ -622,12 +623,12 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
622
623
  }
623
624
  restfulList.push({
624
625
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || ATools.snowflakeId(),
625
- field_type: 'String',
626
+ field_type: "String",
626
627
  is_checked: 1,
627
628
  key: matchesItem,
628
629
  not_null: 1,
629
- value: (obj === null || obj === void 0 ? void 0 : obj.value) || '',
630
- description: (obj === null || obj === void 0 ? void 0 : obj.description) || '',
630
+ value: (obj === null || obj === void 0 ? void 0 : obj.value) || "",
631
+ description: (obj === null || obj === void 0 ? void 0 : obj.description) || "",
631
632
  });
632
633
  }
633
634
  });
@@ -638,7 +639,7 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
638
639
  };
639
640
  const removePTags = (str) => {
640
641
  // 使用正则表达式替换<p>和</p>标签为空字符串
641
- return str.replace(/<\/?[^>]+(>|$)/g, '');
642
+ return str.replace(/<\/?[^>]+(>|$)/g, "");
642
643
  };
643
644
  const yapiGenRestfulByUrl = (url, oldParameter) => {
644
645
  const restfulList = [];
@@ -647,9 +648,9 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
647
648
  url = `http://${url}`;
648
649
  }
649
650
  const urlObj = createUrl(url);
650
- const paths = urlObj.pathname.split('/');
651
+ const paths = urlObj.pathname.split("/");
651
652
  lodash.forEach(paths, (pathsItem) => {
652
- if (pathsItem.substring(0, 1) === ':' && pathsItem.length > 1) {
653
+ if (pathsItem.substring(0, 1) === ":" && pathsItem.length > 1) {
653
654
  let obj = null;
654
655
  const oldIndex = lodash.findIndex(oldParameter, {
655
656
  name: pathsItem.substring(1, pathsItem.length),
@@ -659,12 +660,12 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
659
660
  }
660
661
  restfulList.push({
661
662
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || ATools.snowflakeId(),
662
- field_type: 'String',
663
+ field_type: "String",
663
664
  is_checked: 1,
664
665
  key: pathsItem.substring(1, pathsItem.length),
665
666
  not_null: 1,
666
- value: (obj === null || obj === void 0 ? void 0 : obj.example) || '',
667
- description: (obj === null || obj === void 0 ? void 0 : obj.desc) || '',
667
+ value: (obj === null || obj === void 0 ? void 0 : obj.example) || "",
668
+ description: (obj === null || obj === void 0 ? void 0 : obj.desc) || "",
668
669
  });
669
670
  }
670
671
  });
@@ -674,7 +675,7 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
674
675
  if (isArray(matches) && matches.length > 0) {
675
676
  lodash.forEach(matches, (matchesItem) => {
676
677
  if (matchesItem.length > 0) {
677
- matchesItem = matchesItem.replace(/^{|}$/g, '');
678
+ matchesItem = matchesItem.replace(/^{|}$/g, "");
678
679
  let obj = null;
679
680
  const oldIndex = lodash.findIndex(oldParameter, {
680
681
  name: matchesItem,
@@ -684,12 +685,12 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
684
685
  }
685
686
  restfulList.push({
686
687
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || ATools.snowflakeId(),
687
- field_type: 'String',
688
+ field_type: "String",
688
689
  is_checked: 1,
689
690
  key: matchesItem,
690
691
  not_null: 1,
691
- value: (obj === null || obj === void 0 ? void 0 : obj.example) || '',
692
- description: (obj === null || obj === void 0 ? void 0 : obj.desc) || '',
692
+ value: (obj === null || obj === void 0 ? void 0 : obj.example) || "",
693
+ description: (obj === null || obj === void 0 ? void 0 : obj.desc) || "",
693
694
  });
694
695
  }
695
696
  });
@@ -700,9 +701,9 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
700
701
  };
701
702
  const fixUrlPath = (url) => {
702
703
  try {
703
- const index = url.indexOf('://');
704
+ const index = url.indexOf("://");
704
705
  let path;
705
- let protocolAndDomain = '';
706
+ let protocolAndDomain = "";
706
707
  if (index === -1) {
707
708
  path = url;
708
709
  }
@@ -712,7 +713,7 @@ const fixUrlPath = (url) => {
712
713
  path = url.slice(index + 3);
713
714
  }
714
715
  // 修正路径中的 '//'
715
- const fixedPath = path.replace(/\/\/+/g, '/');
716
+ const fixedPath = path.replace(/\/\/+/g, "/");
716
717
  // 拼接返回
717
718
  return `${protocolAndDomain}${fixedPath}`;
718
719
  }
@@ -734,7 +735,7 @@ const anyToString = (desc) => {
734
735
  };
735
736
  const completionMethod = (method) => {
736
737
  if (!method || !lodash.isString(method))
737
- return 'GET';
738
+ return "GET";
738
739
  if (API_METHODS.includes(method === null || method === void 0 ? void 0 : method.toUpperCase())) {
739
740
  return method === null || method === void 0 ? void 0 : method.toUpperCase();
740
741
  }
@@ -743,26 +744,26 @@ const completionMethod = (method) => {
743
744
  // 根据query数组生成新的url
744
745
  const genUrlByQuery = (url, parameter, query_add_equal = 1) => {
745
746
  var _a;
746
- let paramsStr = '';
747
- let newUrl = url || '';
748
- const handleEncode = (value = '') => {
747
+ let paramsStr = "";
748
+ let newUrl = url || "";
749
+ const handleEncode = (value = "") => {
749
750
  let _value = lodash.cloneDeep(value);
750
751
  try {
751
752
  // 将#符号encode
752
- _value = _value.replace(/#/g, encodeURIComponent('#'));
753
+ _value = _value.replace(/#/g, encodeURIComponent("#"));
753
754
  // 将&符号encode
754
- _value = _value.replace(/&/g, encodeURIComponent('&'));
755
+ _value = _value.replace(/&/g, encodeURIComponent("&"));
755
756
  }
756
757
  catch (e) { }
757
758
  return _value;
758
759
  };
759
760
  lodash.forEach(parameter, (item) => {
760
761
  if (item.key && lodash.isEqual(item.is_checked, 1)) {
761
- paramsStr += `${paramsStr ? '&' : ''}${handleEncode(item.key)}${lodash.isEqual(query_add_equal, -1) && !item.value ? '' : '='}${handleEncode(item.value)}`;
762
+ paramsStr += `${paramsStr ? "&" : ""}${handleEncode(item.key)}${lodash.isEqual(query_add_equal, -1) && !item.value ? "" : "="}${handleEncode(item.value)}`;
762
763
  }
763
764
  });
764
765
  // NOTE 原地址有hash的话,需拼接回去
765
- newUrl = `${lodash.head(newUrl.split('?'))}${paramsStr ? '?' : ''}${paramsStr}${((_a = urlParse__default["default"](newUrl)) === null || _a === void 0 ? void 0 : _a.hash) || ''}`;
766
+ newUrl = `${lodash.head(newUrl.split("?"))}${paramsStr ? "?" : ""}${paramsStr}${((_a = urlParse__default["default"](newUrl)) === null || _a === void 0 ? void 0 : _a.hash) || ""}`;
766
767
  return newUrl;
767
768
  };
768
769
 
@@ -1562,8 +1563,8 @@ class SwaggerToApipost {
1562
1563
  }
1563
1564
  }
1564
1565
  handlePathV3(path, pathItem, tags) {
1565
- var _a, _b, _c, _d, _e, _f, _g, _h;
1566
1566
  return __awaiter(this, void 0, void 0, function* () {
1567
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1567
1568
  let url = path;
1568
1569
  for (const method in pathItem) {
1569
1570
  const swaggerApi = pathItem[method];
@@ -1980,8 +1981,8 @@ class SwaggerToApipost {
1980
1981
  });
1981
1982
  }
1982
1983
  handlePathsV3(json) {
1983
- var _a, _b;
1984
1984
  return __awaiter(this, void 0, void 0, function* () {
1985
+ var _a, _b;
1985
1986
  try {
1986
1987
  const paths = ((_a = json === null || json === void 0 ? void 0 : json.openapi) === null || _a === void 0 ? void 0 : _a.paths) || json.paths;
1987
1988
  const tags = ((_b = json === null || json === void 0 ? void 0 : json.openapi) === null || _b === void 0 ? void 0 : _b.tags) || json.tags;
@@ -2013,8 +2014,8 @@ class SwaggerToApipost {
2013
2014
  }
2014
2015
  }
2015
2016
  handlePath(path, pathItem, tags) {
2016
- var _a, _b, _c, _d, _e, _f, _g, _h;
2017
2017
  return __awaiter(this, void 0, void 0, function* () {
2018
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2018
2019
  let url = path;
2019
2020
  url = decodeURI(this.basePath + url);
2020
2021
  url = fixUrlPath(url);
@@ -2421,8 +2422,8 @@ class SwaggerToApipost {
2421
2422
  catch (err) { }
2422
2423
  });
2423
2424
  }
2424
- convert(json, options = null) {
2425
- return __awaiter(this, void 0, void 0, function* () {
2425
+ convert(json_1) {
2426
+ return __awaiter(this, arguments, void 0, function* (json, options = null) {
2426
2427
  this.clear();
2427
2428
  try {
2428
2429
  if (options && options instanceof Object) {
@@ -5722,7 +5723,7 @@ class ThunderToApipost {
5722
5723
  }
5723
5724
  // auth
5724
5725
  completionApiRequestAuth(auth) {
5725
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
5726
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
5726
5727
  const customAuth = {};
5727
5728
  if ((auth === null || auth === void 0 ? void 0 : auth.type) === "bearer") {
5728
5729
  customAuth["type"] = "bearer";
@@ -5758,6 +5759,31 @@ class ThunderToApipost {
5758
5759
  sessionToken: ((_l = auth === null || auth === void 0 ? void 0 : auth.aws) === null || _l === void 0 ? void 0 : _l.sessionToken) || "",
5759
5760
  };
5760
5761
  }
5762
+ else if ((auth === null || auth === void 0 ? void 0 : auth.type) === "oauth2") {
5763
+ customAuth["type"] = "oauth2";
5764
+ customAuth["oauth2"] = {
5765
+ addTokenTo: "header",
5766
+ headerPrefix: "Bearer",
5767
+ access_token: "",
5768
+ grant_type: ((_m = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _m === void 0 ? void 0 : _m.grantType) || "authorization_code",
5769
+ redirect_uri: ((_o = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _o === void 0 ? void 0 : _o.callbackUrl) || "",
5770
+ authUrl: ((_p = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _p === void 0 ? void 0 : _p.authUrl) || "",
5771
+ accessTokenUrl: ((_q = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _q === void 0 ? void 0 : _q.tokenUrl) || "",
5772
+ clientId: ((_r = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _r === void 0 ? void 0 : _r.clientId) || "",
5773
+ clientSecret: ((_s = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _s === void 0 ? void 0 : _s.clientSecret) || "",
5774
+ username: ((_t = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _t === void 0 ? void 0 : _t.username) || "",
5775
+ password: ((_u = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _u === void 0 ? void 0 : _u.password) || "",
5776
+ challengeAlgorithm: "S256",
5777
+ code_verifier: "Bearer",
5778
+ scope: ((_v = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _v === void 0 ? void 0 : _v.scope) || "",
5779
+ state: ((_w = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _w === void 0 ? void 0 : _w.state) || "",
5780
+ client_authentication: "header",
5781
+ refreshTokenUrl: "",
5782
+ authRequestParams: [],
5783
+ tokenRequestParams: [],
5784
+ refreshRequestParams: [],
5785
+ };
5786
+ }
5761
5787
  return Object.assign(Object.assign({}, DEFAULT_AUTH), customAuth);
5762
5788
  }
5763
5789
  // script
@@ -6064,13 +6090,88 @@ class ThunderToApipost {
6064
6090
  is_force: -1,
6065
6091
  };
6066
6092
  }
6093
+ completionWs2Request(item) {
6094
+ const headerParameter = this.completionApiRequestParameter(item === null || item === void 0 ? void 0 : item.headers);
6095
+ const queryParameter = this.completionApiRequestParameter(item === null || item === void 0 ? void 0 : item.params);
6096
+ return {
6097
+ header: {
6098
+ parameter: headerParameter,
6099
+ },
6100
+ query: {
6101
+ parameter: queryParameter,
6102
+ },
6103
+ cookie: {
6104
+ parameter: [],
6105
+ },
6106
+ };
6107
+ }
6108
+ completionWs2Message(item) {
6109
+ var _a, _b;
6110
+ return [
6111
+ {
6112
+ name: "Message",
6113
+ param_id: ATools.snowflakeId(),
6114
+ request: {
6115
+ mode: (_a = item === null || item === void 0 ? void 0 : item.body) === null || _a === void 0 ? void 0 : _a.type,
6116
+ raw: ((_b = item === null || item === void 0 ? void 0 : item.body) === null || _b === void 0 ? void 0 : _b.raw) || "",
6117
+ raw_parameter: [],
6118
+ raw_schema: {
6119
+ type: "object",
6120
+ },
6121
+ },
6122
+ response: {
6123
+ mode: "text",
6124
+ raw: "",
6125
+ raw_parameter: [],
6126
+ raw_schema: {
6127
+ type: "object",
6128
+ },
6129
+ },
6130
+ },
6131
+ ];
6132
+ }
6133
+ // websocket2
6134
+ completionWs2Target(item, parentInfo) {
6135
+ return {
6136
+ target_id: ATools.snowflakeId(),
6137
+ project_id: this.project.project_id || "",
6138
+ parent_id: (parentInfo === null || parentInfo === void 0 ? void 0 : parentInfo.target_id) || "0",
6139
+ target_type: "websocket2",
6140
+ name: (item === null || item === void 0 ? void 0 : item.name) || "New Websocket",
6141
+ version: 0,
6142
+ sort: 0,
6143
+ description: (item === null || item === void 0 ? void 0 : item.docs) || "",
6144
+ method: "",
6145
+ url: (item === null || item === void 0 ? void 0 : item.url) || "",
6146
+ mark_id: "1",
6147
+ request: this.completionWs2Request(item),
6148
+ message: this.completionWs2Message(item),
6149
+ attribute_info: {},
6150
+ tags: [],
6151
+ is_force: -1,
6152
+ config: {
6153
+ certificate_verification: -1,
6154
+ information_size: 5,
6155
+ reconnect_num: 5,
6156
+ reconnect_time: 5000,
6157
+ shake_hands_timeout: 0,
6158
+ },
6159
+ };
6160
+ }
6067
6161
  completionApis(arr, parentInfo) {
6068
6162
  if (arr === null || arr === void 0 ? void 0 : arr.length) {
6069
6163
  return arr === null || arr === void 0 ? void 0 : arr.reduce((pre, targetItem) => {
6164
+ console.log(targetItem, "targetItem.type");
6070
6165
  if (!lodash.isEqual(targetItem.type, "folder")) {
6166
+ let target;
6071
6167
  // 接口
6072
- const target = this.completionApiTarget(targetItem, parentInfo);
6073
- pre.push(target);
6168
+ if (lodash.isEqual(targetItem === null || targetItem === void 0 ? void 0 : targetItem.reqType, "ws")) {
6169
+ target = this.completionWs2Target(targetItem, parentInfo);
6170
+ }
6171
+ else {
6172
+ target = this.completionApiTarget(targetItem, parentInfo);
6173
+ }
6174
+ target && pre.push(target);
6074
6175
  }
6075
6176
  else {
6076
6177
  // 目录
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "echoapi-converter",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "description": "echoapi to swagger",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -26,7 +26,8 @@
26
26
  "rollup-plugin-copy": "^3.5.0",
27
27
  "rollup-plugin-dts": "^4.2.2",
28
28
  "rollup-plugin-typescript": "^1.0.1",
29
- "rollup-plugin-typescript2": "^0.36.0"
29
+ "rollup-plugin-typescript2": "^0.36.0",
30
+ "ts-jest": "^29.2.5"
30
31
  },
31
32
  "dependencies": {
32
33
  "@types/lodash": "^4.14.202",