@ywfe/fe-tools 1.2.1-beta.4 → 1.2.1-beta.40

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