echoapi-converter 0.0.38 → 0.0.39

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
@@ -503,7 +503,7 @@ const throwResult$1 = (status, message, data = '') => {
503
503
  return {
504
504
  status,
505
505
  message,
506
- data
506
+ data,
507
507
  };
508
508
  };
509
509
  const transExample = (example) => {
@@ -523,21 +523,22 @@ const getBeautifulRaw = (raw) => {
523
523
  }
524
524
  };
525
525
  const isArray = (value) => {
526
- return Object.prototype.toString.call(value) === '[object Array]';
526
+ return Object.prototype.toString.call(value) === "[object Array]";
527
527
  };
528
528
  const isObject = (value) => {
529
- return Object.prototype.toString.call(value) === '[object Object]';
529
+ return Object.prototype.toString.call(value) === "[object Object]";
530
530
  };
531
531
  const isURL = (_url) => {
532
532
  if (!_url) {
533
533
  return false;
534
534
  }
535
535
  _url = lodash.trim(_url);
536
- return (_url.substr(0, 7).toLowerCase() === 'http://' || _url.substr(0, 8).toLowerCase() === 'https://');
536
+ return (_url.substr(0, 7).toLowerCase() === "http://" ||
537
+ _url.substr(0, 8).toLowerCase() === "https://");
537
538
  };
538
539
  const createUrl = (_url) => {
539
- if (typeof _url !== 'string') {
540
- _url += '';
540
+ if (typeof _url !== "string") {
541
+ _url += "";
541
542
  }
542
543
  if (!isURL(_url)) {
543
544
  _url = `http://${_url}`;
@@ -557,7 +558,7 @@ const createUrl = (_url) => {
557
558
  }
558
559
  catch (_a) {
559
560
  const http_url = `https://www.echoapi.cn${_url.substring(origin.length)}`;
560
- const a = document.createElement('a');
561
+ const a = document.createElement("a");
561
562
  a.href = http_url;
562
563
  urls = {
563
564
  source: _url,
@@ -570,23 +571,23 @@ const createUrl = (_url) => {
570
571
  search: a.search,
571
572
  pathname: a.pathname,
572
573
  // eslint-disable-next-line no-useless-escape
573
- file: (a.pathname.match(/\/([^\/?#]+)$/i) || ['', ''])[1],
574
+ file: (a.pathname.match(/\/([^\/?#]+)$/i) || ["", ""])[1],
574
575
  hash: a.hash,
575
576
  };
576
577
  }
577
578
  return urls;
578
579
  };
579
580
  // 根据url生成新的restful数组
580
- const genRestfulByUrl = (url, oldParameter, key = 'key') => {
581
+ const genRestfulByUrl = (url, oldParameter, key = "key") => {
581
582
  const restfulList = [];
582
583
  // 自动拼接url http://
583
584
  if (!isURL(url)) {
584
585
  url = `http://${url}`;
585
586
  }
586
587
  const urlObj = createUrl(url);
587
- const paths = urlObj.pathname.split('/');
588
+ const paths = urlObj.pathname.split("/");
588
589
  lodash.forEach(paths, (pathsItem) => {
589
- if (pathsItem.substring(0, 1) === ':' && pathsItem.length > 1) {
590
+ if (pathsItem.substring(0, 1) === ":" && pathsItem.length > 1) {
590
591
  let obj = null;
591
592
  const oldIndex = lodash.findIndex(oldParameter, {
592
593
  [key]: pathsItem.substring(1, pathsItem.length),
@@ -596,12 +597,12 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
596
597
  }
597
598
  restfulList.push({
598
599
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || apipostTools.snowflakeId(),
599
- field_type: 'String',
600
+ field_type: "String",
600
601
  is_checked: 1,
601
602
  key: pathsItem.substring(1, pathsItem.length),
602
603
  not_null: 1,
603
- value: (obj === null || obj === void 0 ? void 0 : obj.value) || '',
604
- description: (obj === null || obj === void 0 ? void 0 : obj.description) || '',
604
+ value: (obj === null || obj === void 0 ? void 0 : obj.value) || "",
605
+ description: (obj === null || obj === void 0 ? void 0 : obj.description) || "",
605
606
  });
606
607
  }
607
608
  });
@@ -611,7 +612,7 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
611
612
  if (isArray(matches) && matches.length > 0) {
612
613
  lodash.forEach(matches, (matchesItem) => {
613
614
  if (matchesItem.length > 0) {
614
- matchesItem = matchesItem.replace(/^{|}$/g, '');
615
+ matchesItem = matchesItem.replace(/^{|}$/g, "");
615
616
  let obj = null;
616
617
  const oldIndex = lodash.findIndex(oldParameter, {
617
618
  [key]: matchesItem,
@@ -621,12 +622,12 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
621
622
  }
622
623
  restfulList.push({
623
624
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || apipostTools.snowflakeId(),
624
- field_type: 'String',
625
+ field_type: "String",
625
626
  is_checked: 1,
626
627
  key: matchesItem,
627
628
  not_null: 1,
628
- value: (obj === null || obj === void 0 ? void 0 : obj.value) || '',
629
- description: (obj === null || obj === void 0 ? void 0 : obj.description) || '',
629
+ value: (obj === null || obj === void 0 ? void 0 : obj.value) || "",
630
+ description: (obj === null || obj === void 0 ? void 0 : obj.description) || "",
630
631
  });
631
632
  }
632
633
  });
@@ -637,7 +638,7 @@ const genRestfulByUrl = (url, oldParameter, key = 'key') => {
637
638
  };
638
639
  const removePTags = (str) => {
639
640
  // 使用正则表达式替换<p>和</p>标签为空字符串
640
- return str.replace(/<\/?[^>]+(>|$)/g, '');
641
+ return str.replace(/<\/?[^>]+(>|$)/g, "");
641
642
  };
642
643
  const yapiGenRestfulByUrl = (url, oldParameter) => {
643
644
  const restfulList = [];
@@ -646,9 +647,9 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
646
647
  url = `http://${url}`;
647
648
  }
648
649
  const urlObj = createUrl(url);
649
- const paths = urlObj.pathname.split('/');
650
+ const paths = urlObj.pathname.split("/");
650
651
  lodash.forEach(paths, (pathsItem) => {
651
- if (pathsItem.substring(0, 1) === ':' && pathsItem.length > 1) {
652
+ if (pathsItem.substring(0, 1) === ":" && pathsItem.length > 1) {
652
653
  let obj = null;
653
654
  const oldIndex = lodash.findIndex(oldParameter, {
654
655
  name: pathsItem.substring(1, pathsItem.length),
@@ -658,12 +659,12 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
658
659
  }
659
660
  restfulList.push({
660
661
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || apipostTools.snowflakeId(),
661
- field_type: 'String',
662
+ field_type: "String",
662
663
  is_checked: 1,
663
664
  key: pathsItem.substring(1, pathsItem.length),
664
665
  not_null: 1,
665
- value: (obj === null || obj === void 0 ? void 0 : obj.example) || '',
666
- description: (obj === null || obj === void 0 ? void 0 : obj.desc) || '',
666
+ value: (obj === null || obj === void 0 ? void 0 : obj.example) || "",
667
+ description: (obj === null || obj === void 0 ? void 0 : obj.desc) || "",
667
668
  });
668
669
  }
669
670
  });
@@ -673,7 +674,7 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
673
674
  if (isArray(matches) && matches.length > 0) {
674
675
  lodash.forEach(matches, (matchesItem) => {
675
676
  if (matchesItem.length > 0) {
676
- matchesItem = matchesItem.replace(/^{|}$/g, '');
677
+ matchesItem = matchesItem.replace(/^{|}$/g, "");
677
678
  let obj = null;
678
679
  const oldIndex = lodash.findIndex(oldParameter, {
679
680
  name: matchesItem,
@@ -683,12 +684,12 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
683
684
  }
684
685
  restfulList.push({
685
686
  param_id: (obj === null || obj === void 0 ? void 0 : obj.param_id) || apipostTools.snowflakeId(),
686
- field_type: 'String',
687
+ field_type: "String",
687
688
  is_checked: 1,
688
689
  key: matchesItem,
689
690
  not_null: 1,
690
- value: (obj === null || obj === void 0 ? void 0 : obj.example) || '',
691
- description: (obj === null || obj === void 0 ? void 0 : obj.desc) || '',
691
+ value: (obj === null || obj === void 0 ? void 0 : obj.example) || "",
692
+ description: (obj === null || obj === void 0 ? void 0 : obj.desc) || "",
692
693
  });
693
694
  }
694
695
  });
@@ -699,9 +700,9 @@ const yapiGenRestfulByUrl = (url, oldParameter) => {
699
700
  };
700
701
  const fixUrlPath = (url) => {
701
702
  try {
702
- const index = url.indexOf('://');
703
+ const index = url.indexOf("://");
703
704
  let path;
704
- let protocolAndDomain = '';
705
+ let protocolAndDomain = "";
705
706
  if (index === -1) {
706
707
  path = url;
707
708
  }
@@ -711,7 +712,7 @@ const fixUrlPath = (url) => {
711
712
  path = url.slice(index + 3);
712
713
  }
713
714
  // 修正路径中的 '//'
714
- const fixedPath = path.replace(/\/\/+/g, '/');
715
+ const fixedPath = path.replace(/\/\/+/g, "/");
715
716
  // 拼接返回
716
717
  return `${protocolAndDomain}${fixedPath}`;
717
718
  }
@@ -733,7 +734,7 @@ const anyToString = (desc) => {
733
734
  };
734
735
  const completionMethod = (method) => {
735
736
  if (!method || !lodash.isString(method))
736
- return 'GET';
737
+ return "GET";
737
738
  if (API_METHODS.includes(method === null || method === void 0 ? void 0 : method.toUpperCase())) {
738
739
  return method === null || method === void 0 ? void 0 : method.toUpperCase();
739
740
  }
@@ -742,26 +743,26 @@ const completionMethod = (method) => {
742
743
  // 根据query数组生成新的url
743
744
  const genUrlByQuery = (url, parameter, query_add_equal = 1) => {
744
745
  var _a;
745
- let paramsStr = '';
746
- let newUrl = url || '';
747
- const handleEncode = (value = '') => {
746
+ let paramsStr = "";
747
+ let newUrl = url || "";
748
+ const handleEncode = (value = "") => {
748
749
  let _value = lodash.cloneDeep(value);
749
750
  try {
750
751
  // 将#符号encode
751
- _value = _value.replace(/#/g, encodeURIComponent('#'));
752
+ _value = _value.replace(/#/g, encodeURIComponent("#"));
752
753
  // 将&符号encode
753
- _value = _value.replace(/&/g, encodeURIComponent('&'));
754
+ _value = _value.replace(/&/g, encodeURIComponent("&"));
754
755
  }
755
756
  catch (e) { }
756
757
  return _value;
757
758
  };
758
759
  lodash.forEach(parameter, (item) => {
759
760
  if (item.key && lodash.isEqual(item.is_checked, 1)) {
760
- paramsStr += `${paramsStr ? '&' : ''}${handleEncode(item.key)}${lodash.isEqual(query_add_equal, -1) && !item.value ? '' : '='}${handleEncode(item.value)}`;
761
+ paramsStr += `${paramsStr ? "&" : ""}${handleEncode(item.key)}${lodash.isEqual(query_add_equal, -1) && !item.value ? "" : "="}${handleEncode(item.value)}`;
761
762
  }
762
763
  });
763
764
  // NOTE 原地址有hash的话,需拼接回去
764
- newUrl = `${lodash.head(newUrl.split('?'))}${paramsStr ? '?' : ''}${paramsStr}${((_a = urlParse__default["default"](newUrl)) === null || _a === void 0 ? void 0 : _a.hash) || ''}`;
765
+ newUrl = `${lodash.head(newUrl.split("?"))}${paramsStr ? "?" : ""}${paramsStr}${((_a = urlParse__default["default"](newUrl)) === null || _a === void 0 ? void 0 : _a.hash) || ""}`;
765
766
  return newUrl;
766
767
  };
767
768
 
@@ -6536,7 +6537,7 @@ class ThunderToApipost {
6536
6537
  }
6537
6538
  // auth
6538
6539
  completionApiRequestAuth(auth) {
6539
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
6540
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
6540
6541
  const customAuth = {};
6541
6542
  if ((auth === null || auth === void 0 ? void 0 : auth.type) === "bearer") {
6542
6543
  customAuth["type"] = "bearer";
@@ -6572,6 +6573,31 @@ class ThunderToApipost {
6572
6573
  sessionToken: ((_l = auth === null || auth === void 0 ? void 0 : auth.aws) === null || _l === void 0 ? void 0 : _l.sessionToken) || "",
6573
6574
  };
6574
6575
  }
6576
+ else if ((auth === null || auth === void 0 ? void 0 : auth.type) === "oauth2") {
6577
+ customAuth["type"] = "oauth2";
6578
+ customAuth["oauth2"] = {
6579
+ addTokenTo: "header",
6580
+ headerPrefix: "Bearer",
6581
+ access_token: "",
6582
+ grant_type: ((_m = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _m === void 0 ? void 0 : _m.grantType) || "authorization_code",
6583
+ redirect_uri: ((_o = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _o === void 0 ? void 0 : _o.callbackUrl) || "",
6584
+ authUrl: ((_p = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _p === void 0 ? void 0 : _p.authUrl) || "",
6585
+ accessTokenUrl: ((_q = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _q === void 0 ? void 0 : _q.tokenUrl) || "",
6586
+ clientId: ((_r = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _r === void 0 ? void 0 : _r.clientId) || "",
6587
+ clientSecret: ((_s = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _s === void 0 ? void 0 : _s.clientSecret) || "",
6588
+ username: ((_t = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _t === void 0 ? void 0 : _t.username) || "",
6589
+ password: ((_u = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _u === void 0 ? void 0 : _u.password) || "",
6590
+ challengeAlgorithm: "S256",
6591
+ code_verifier: "Bearer",
6592
+ scope: ((_v = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _v === void 0 ? void 0 : _v.scope) || "",
6593
+ state: ((_w = auth === null || auth === void 0 ? void 0 : auth.oauth2) === null || _w === void 0 ? void 0 : _w.state) || "",
6594
+ client_authentication: "header",
6595
+ refreshTokenUrl: "",
6596
+ authRequestParams: [],
6597
+ tokenRequestParams: [],
6598
+ refreshRequestParams: [],
6599
+ };
6600
+ }
6575
6601
  return Object.assign(Object.assign({}, DEFAULT_AUTH), customAuth);
6576
6602
  }
6577
6603
  // script
@@ -6878,13 +6904,88 @@ class ThunderToApipost {
6878
6904
  is_force: -1,
6879
6905
  };
6880
6906
  }
6907
+ completionWs2Request(item) {
6908
+ const headerParameter = this.completionApiRequestParameter(item === null || item === void 0 ? void 0 : item.headers);
6909
+ const queryParameter = this.completionApiRequestParameter(item === null || item === void 0 ? void 0 : item.params);
6910
+ return {
6911
+ header: {
6912
+ parameter: headerParameter,
6913
+ },
6914
+ query: {
6915
+ parameter: queryParameter,
6916
+ },
6917
+ cookie: {
6918
+ parameter: [],
6919
+ },
6920
+ };
6921
+ }
6922
+ completionWs2Message(item) {
6923
+ var _a, _b;
6924
+ return [
6925
+ {
6926
+ name: "Message",
6927
+ param_id: apipostTools.snowflakeId(),
6928
+ request: {
6929
+ mode: (_a = item === null || item === void 0 ? void 0 : item.body) === null || _a === void 0 ? void 0 : _a.type,
6930
+ raw: ((_b = item === null || item === void 0 ? void 0 : item.body) === null || _b === void 0 ? void 0 : _b.raw) || "",
6931
+ raw_parameter: [],
6932
+ raw_schema: {
6933
+ type: "object",
6934
+ },
6935
+ },
6936
+ response: {
6937
+ mode: "text",
6938
+ raw: "",
6939
+ raw_parameter: [],
6940
+ raw_schema: {
6941
+ type: "object",
6942
+ },
6943
+ },
6944
+ },
6945
+ ];
6946
+ }
6947
+ // websocket2
6948
+ completionWs2Target(item, parentInfo) {
6949
+ return {
6950
+ target_id: apipostTools.snowflakeId(),
6951
+ project_id: this.project.project_id || "",
6952
+ parent_id: (parentInfo === null || parentInfo === void 0 ? void 0 : parentInfo.target_id) || "0",
6953
+ target_type: "websocket2",
6954
+ name: (item === null || item === void 0 ? void 0 : item.name) || "New Websocket",
6955
+ version: 0,
6956
+ sort: 0,
6957
+ description: (item === null || item === void 0 ? void 0 : item.docs) || "",
6958
+ method: "",
6959
+ url: (item === null || item === void 0 ? void 0 : item.url) || "",
6960
+ mark_id: "1",
6961
+ request: this.completionWs2Request(item),
6962
+ message: this.completionWs2Message(item),
6963
+ attribute_info: {},
6964
+ tags: [],
6965
+ is_force: -1,
6966
+ config: {
6967
+ certificate_verification: -1,
6968
+ information_size: 5,
6969
+ reconnect_num: 5,
6970
+ reconnect_time: 5000,
6971
+ shake_hands_timeout: 0,
6972
+ },
6973
+ };
6974
+ }
6881
6975
  completionApis(arr, parentInfo) {
6882
6976
  if (arr === null || arr === void 0 ? void 0 : arr.length) {
6883
6977
  return arr === null || arr === void 0 ? void 0 : arr.reduce((pre, targetItem) => {
6978
+ console.log(targetItem, "targetItem.type");
6884
6979
  if (!lodash.isEqual(targetItem.type, "folder")) {
6980
+ let target;
6885
6981
  // 接口
6886
- const target = this.completionApiTarget(targetItem, parentInfo);
6887
- pre.push(target);
6982
+ if (lodash.isEqual(targetItem === null || targetItem === void 0 ? void 0 : targetItem.reqType, "ws")) {
6983
+ target = this.completionWs2Target(targetItem, parentInfo);
6984
+ }
6985
+ else {
6986
+ target = this.completionApiTarget(targetItem, parentInfo);
6987
+ }
6988
+ target && pre.push(target);
6888
6989
  }
6889
6990
  else {
6890
6991
  // 目录
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "echoapi-converter",
3
- "version": "0.0.38",
3
+ "version": "0.0.39",
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",