@ywfe/fe-tools 1.2.1-beta.3 → 1.2.1-beta.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -8704,50 +8704,53 @@ var require$$1 = /*@__PURE__*/getAugmentedNamespace(empty$1);
8704
8704
 
8705
8705
  function request(_a) {
8706
8706
  return __awaiter(this, arguments, void 0, function (_b) {
8707
- var method, basePathMap, getBasePath, isValidURL, prefix, requestUrl, fetchOption, res;
8707
+ var method, basePathMap, getBasePath, isValidURL, prefix, requestUrl, fetchOption, response, message, result;
8708
8708
  var input = _b.input;
8709
8709
  return __generator(this, function (_c) {
8710
8710
  switch (_c.label) {
8711
8711
  case 0:
8712
- method = input.method || 'GET';
8712
+ method = input.method || "GET";
8713
8713
  basePathMap = new Map([
8714
- ['prod', 'https://gateway.ywwl.com'],
8715
- ['test', 'https://test-gateway.ywwl.com'],
8716
- ['test2', 'https://test2-gateway.ywwl.com'],
8717
- ['dev', 'https://dev-gateway.ywwl.com'],
8714
+ ["prod", "https://gateway.ywwl.com"],
8715
+ ["test", "https://test-gateway.ywwl.com"],
8716
+ ["test2", "https://test2-gateway.ywwl.com"],
8717
+ ["dev", "https://dev-gateway.ywwl.com"],
8718
8718
  ]);
8719
8719
  getBasePath = function () {
8720
- var env = '{{env}}';
8720
+ var env = "{{env}}";
8721
8721
  return basePathMap.get(env);
8722
8722
  };
8723
8723
  isValidURL = function (url) {
8724
- var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
8725
- '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
8726
- '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
8727
- '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
8728
- '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
8729
- '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
8730
- return !!pattern.test(url);
8724
+ var regex = /^https?:\/\//;
8725
+ return regex.test(url);
8731
8726
  };
8732
8727
  prefix = getBasePath();
8733
8728
  requestUrl = isValidURL(input.url) ? input.url : "".concat(prefix).concat(input.url);
8729
+ console.log('requestUrl', requestUrl, isValidURL(input.url));
8734
8730
  fetchOption = {
8735
8731
  method: method,
8736
8732
  headers: {
8737
- 'Content-type': 'application/json',
8738
- 'x-token': '{{token}}',
8733
+ "Content-type": "application/json",
8734
+ "x-token": "{{token}}",
8739
8735
  },
8740
8736
  };
8741
- if (method.toUpperCase() === 'POST') {
8737
+ if (method.toUpperCase() === "POST") {
8742
8738
  fetchOption.body = JSON.stringify(input.params);
8743
8739
  }
8744
- else if (method.toUpperCase() === 'GET') {
8740
+ else if (method.toUpperCase() === "GET") {
8745
8741
  requestUrl = "".concat(requestUrl, "?").concat(ywfeUtils_esm.querystring.stringify(input.params || {}));
8746
8742
  }
8747
8743
  return [4 /*yield*/, fetch(requestUrl, fetchOption)];
8748
8744
  case 1:
8749
- res = _c.sent();
8750
- return [2 /*return*/, res.json()];
8745
+ response = _c.sent();
8746
+ if (!response.ok) {
8747
+ message = "An error has occured: ".concat(response.status);
8748
+ return [2 /*return*/, { success: false, message: message }];
8749
+ }
8750
+ return [4 /*yield*/, response.json()];
8751
+ case 2:
8752
+ result = _c.sent();
8753
+ return [2 /*return*/, result];
8751
8754
  }
8752
8755
  });
8753
8756
  });
@@ -8755,24 +8758,48 @@ function request(_a) {
8755
8758
 
8756
8759
  function userInputToJson(_a) {
8757
8760
  return __awaiter(this, arguments, void 0, function (_b) {
8761
+ var res, result;
8758
8762
  var input = _b.input;
8759
8763
  return __generator(this, function (_c) {
8760
8764
  switch (_c.label) {
8761
8765
  case 0:
8762
- input.userInput, input.rules;
8766
+ console.log("input", input);
8763
8767
  return [4 /*yield*/, request({
8764
8768
  input: {
8765
8769
  url: "https://fc-typechat.ywwl.com/userInputToJson",
8766
- method: "GET"
8767
- }
8770
+ method: "GET",
8771
+ params: input,
8772
+ },
8768
8773
  })];
8769
8774
  case 1:
8770
- _c.sent();
8771
- return [2 /*return*/, "2"];
8775
+ res = _c.sent();
8776
+ if (res.success) {
8777
+ result = res.data;
8778
+ return [2 /*return*/, result.success ? result.data : result];
8779
+ }
8780
+ else {
8781
+ return [2 /*return*/, res.success ? res.data : res];
8782
+ }
8772
8783
  }
8773
8784
  });
8774
8785
  });
8775
8786
  }
8776
8787
 
8777
- export { request, userInputToJson };
8788
+ // Ctrl-S保存代码
8789
+ function JSON2String(_a) {
8790
+ var input = _a.input;
8791
+ var data = input.data;
8792
+ var result = { content: '' };
8793
+ try {
8794
+ var str = JSON.stringify(data);
8795
+ result.content = '```json' + str + 'json```';
8796
+ return result;
8797
+ }
8798
+ catch (error) {
8799
+ result.content = '```json' + '{}' + 'json```';
8800
+ return result;
8801
+ }
8802
+ }
8803
+
8804
+ export { JSON2String, request, userInputToJson };
8778
8805
  //# sourceMappingURL=ywfe-tools.esm.js.map