@whitesev/utils 1.5.3 → 1.5.5

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.
@@ -1507,13 +1507,14 @@ var Utils = (function () {
1507
1507
  },
1508
1508
  /**
1509
1509
  * 成功的回调
1510
- * @param response
1510
+ * @param response 响应
1511
+ * @param details 请求的配置
1511
1512
  */
1512
- successResponseCallBack(response) {
1513
+ successResponseCallBack(response, details) {
1513
1514
  for (let index = 0; index < this.$config.configList.length; index++) {
1514
1515
  let item = this.$config.configList[index];
1515
1516
  if (typeof item.successFn === "function") {
1516
- if (item.successFn(response) == null) {
1517
+ if (item.successFn(response, details) == null) {
1517
1518
  return;
1518
1519
  }
1519
1520
  }
@@ -1522,7 +1523,7 @@ var Utils = (function () {
1522
1523
  },
1523
1524
  /**
1524
1525
  * 失败的回调
1525
- * @param response
1526
+ * @param data 配置
1526
1527
  */
1527
1528
  errorResponseCallBack(data) {
1528
1529
  for (let index = 0; index < this.$config.configList.length; index++) {
@@ -1785,6 +1786,7 @@ var Utils = (function () {
1785
1786
  type: "onabort",
1786
1787
  error: new TypeError("request canceled"),
1787
1788
  response: null,
1789
+ details: details,
1788
1790
  }) == null) {
1789
1791
  return;
1790
1792
  }
@@ -1816,6 +1818,7 @@ var Utils = (function () {
1816
1818
  type: "onerror",
1817
1819
  error: new TypeError("request error"),
1818
1820
  response: response,
1821
+ details: details,
1819
1822
  }) == null) {
1820
1823
  return;
1821
1824
  }
@@ -1844,6 +1847,7 @@ var Utils = (function () {
1844
1847
  type: "ontimeout",
1845
1848
  error: new TypeError("request timeout"),
1846
1849
  response: (argumentsList || [null])[0],
1850
+ details: details,
1847
1851
  }) == null) {
1848
1852
  return;
1849
1853
  }
@@ -1927,8 +1931,7 @@ var Utils = (function () {
1927
1931
  }
1928
1932
  /* 状态码2xx都是成功的 */
1929
1933
  if (Math.floor(Response.status / 100) === 2) {
1930
- if (this.context.HttpxResponseHook.successResponseCallBack(Response) ==
1931
- null) {
1934
+ if (this.context.HttpxResponseHook.successResponseCallBack(Response, details) == null) {
1932
1935
  return;
1933
1936
  }
1934
1937
  resolve({
@@ -2049,7 +2052,7 @@ var Utils = (function () {
2049
2052
  let responseText = "";
2050
2053
  /** 响应xml文档 */
2051
2054
  let responseXML = "";
2052
- let arrayBuffer = await resp.arrayBuffer;
2055
+ let arrayBuffer = await resp.arrayBuffer();
2053
2056
  let encoding = "utf-8";
2054
2057
  if (resp.headers.has("Content-Type")) {
2055
2058
  let charsetMatched = resp.headers
@@ -2060,7 +2063,7 @@ var Utils = (function () {
2060
2063
  }
2061
2064
  }
2062
2065
  let textDecoder = new TextDecoder(encoding);
2063
- responseText = textDecoder.decode(await resp.arrayBuffer());
2066
+ responseText = textDecoder.decode(arrayBuffer);
2064
2067
  response = responseText;
2065
2068
  if (details.responseType === "arraybuffer") {
2066
2069
  response = arrayBuffer;
@@ -6216,6 +6219,33 @@ var Utils = (function () {
6216
6219
  });
6217
6220
  return formData;
6218
6221
  }
6222
+ /**
6223
+ * 将链接转为URL对象,自动补充URL的protocol或者origin
6224
+ * @param text 需要转换的链接字符串
6225
+ * @example
6226
+ * Utils.toUrl("//www.baidu.com/s?word=666");
6227
+ * Utils.toUrl("/s?word=666");
6228
+ */
6229
+ toUrl(text) {
6230
+ if (typeof text !== "string") {
6231
+ throw new TypeError("toUrl: text must be string");
6232
+ }
6233
+ text = text.trim();
6234
+ if (text === "") {
6235
+ throw new TypeError("toUrl: text must not be empty");
6236
+ }
6237
+ if (text.startsWith("//")) {
6238
+ /* //www.baidu.com/xxxxxxx */
6239
+ /* 没有protocol,加上 */
6240
+ text = UtilsCore.globalThis.location.protocol + text;
6241
+ }
6242
+ else if (text.startsWith("/")) {
6243
+ /* /xxx/info?xxx=xxx */
6244
+ /* 没有Origin,加上 */
6245
+ text = UtilsCore.globalThis.location.origin + text;
6246
+ }
6247
+ return new URL(text);
6248
+ }
6219
6249
  /**
6220
6250
  * 生成uuid
6221
6251
  * @example