@whitesev/utils 1.8.0 → 1.9.2

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.esm.js CHANGED
@@ -1563,14 +1563,23 @@ class Httpx {
1563
1563
  * @private
1564
1564
  */
1565
1565
  beforeRequestCallBack(details) {
1566
- if (details.allowInterceptConfig) {
1567
- // 首先配置得存在,不然默认允许拦截
1568
- if (typeof details.allowInterceptConfig.beforeRequest === "boolean" &&
1569
- !details.allowInterceptConfig.beforeRequest) {
1570
- // 设置了禁止拦截
1566
+ if (typeof details.allowInterceptConfig === "boolean") {
1567
+ if (!details.allowInterceptConfig) {
1568
+ // 不允许拦截
1571
1569
  return details;
1572
1570
  }
1573
1571
  }
1572
+ else {
1573
+ if (details.allowInterceptConfig != null) {
1574
+ // 配置存在
1575
+ // 细分处理是否拦截
1576
+ if (typeof details.allowInterceptConfig.beforeRequest === "boolean" &&
1577
+ !details.allowInterceptConfig.beforeRequest) {
1578
+ // 设置了禁止拦截
1579
+ return details;
1580
+ }
1581
+ }
1582
+ }
1574
1583
  for (let index = 0; index < this.$config.configList.length; index++) {
1575
1584
  let item = this.$config.configList[index];
1576
1585
  if (typeof item.fn === "function") {
@@ -1632,15 +1641,24 @@ class Httpx {
1632
1641
  * @param details 请求的配置
1633
1642
  */
1634
1643
  successResponseCallBack(response, details) {
1635
- if (details.allowInterceptConfig) {
1636
- // 首先配置得存在,不然默认允许拦截
1637
- if (typeof details.allowInterceptConfig.afterResponseSuccess ===
1638
- "boolean" &&
1639
- !details.allowInterceptConfig.afterResponseSuccess) {
1640
- // 设置了禁止拦截
1644
+ if (typeof details.allowInterceptConfig === "boolean") {
1645
+ if (!details.allowInterceptConfig) {
1646
+ // 不允许拦截
1641
1647
  return details;
1642
1648
  }
1643
1649
  }
1650
+ else {
1651
+ if (details.allowInterceptConfig != null) {
1652
+ // 配置存在
1653
+ // 细分处理是否拦截
1654
+ if (typeof details.allowInterceptConfig.afterResponseSuccess ===
1655
+ "boolean" &&
1656
+ !details.allowInterceptConfig.afterResponseSuccess) {
1657
+ // 设置了禁止拦截
1658
+ return details;
1659
+ }
1660
+ }
1661
+ }
1644
1662
  for (let index = 0; index < this.$config.configList.length; index++) {
1645
1663
  let item = this.$config.configList[index];
1646
1664
  if (typeof item.successFn === "function") {
@@ -1654,17 +1672,28 @@ class Httpx {
1654
1672
  /**
1655
1673
  * 失败的回调
1656
1674
  * @param data 配置
1675
+ * @returns
1676
+ * 返回null|undefined就是拦截掉了
1657
1677
  */
1658
1678
  errorResponseCallBack(data) {
1659
- if (data.details.allowInterceptConfig) {
1660
- // 首先配置得存在,不然默认允许拦截
1661
- if (typeof data.details.allowInterceptConfig.afterResponseError ===
1662
- "boolean" &&
1663
- !data.details.allowInterceptConfig.afterResponseError) {
1664
- // 设置了禁止拦截
1679
+ if (typeof data.details.allowInterceptConfig === "boolean") {
1680
+ if (!data.details.allowInterceptConfig) {
1681
+ // 不允许拦截
1665
1682
  return data;
1666
1683
  }
1667
1684
  }
1685
+ else {
1686
+ if (data.details.allowInterceptConfig != null) {
1687
+ // 配置存在
1688
+ // 细分处理是否拦截
1689
+ if (typeof data.details.allowInterceptConfig.afterResponseError ===
1690
+ "boolean" &&
1691
+ !data.details.allowInterceptConfig.afterResponseError) {
1692
+ // 设置了禁止拦截
1693
+ return data;
1694
+ }
1695
+ }
1696
+ }
1668
1697
  for (let index = 0; index < this.$config.configList.length; index++) {
1669
1698
  let item = this.$config.configList[index];
1670
1699
  if (typeof item.errorFn === "function") {
@@ -1735,10 +1764,11 @@ class Httpx {
1735
1764
  /**
1736
1765
  * 获取请求配置
1737
1766
  * @param method 当前请求方法,默认get
1738
- * @param resolve promise回调
1739
1767
  * @param details 请求配置
1768
+ * @param resolve promise回调
1769
+ * @param reject 抛出错误回调
1740
1770
  */
1741
- getDetails(method, resolve, details) {
1771
+ getDetails(method, details, resolve, reject) {
1742
1772
  let that = this;
1743
1773
  let result = {
1744
1774
  url: details.url || this.context.#defaultDetails.url,
@@ -1761,13 +1791,21 @@ class Httpx {
1761
1791
  fetch: details.fetch || this.context.#defaultDetails.fetch,
1762
1792
  /* 对象使用深拷贝 */
1763
1793
  fetchInit: utils.deepClone(this.context.#defaultDetails.fetchInit),
1794
+ allowInterceptConfig: {
1795
+ beforeRequest: this.context.#defaultDetails
1796
+ .allowInterceptConfig.beforeRequest,
1797
+ afterResponseSuccess: this.context.#defaultDetails
1798
+ .allowInterceptConfig.afterResponseSuccess,
1799
+ afterResponseError: this.context.#defaultDetails
1800
+ .allowInterceptConfig.afterResponseError,
1801
+ },
1764
1802
  user: details.user || this.context.#defaultDetails.user,
1765
1803
  password: details.password || this.context.#defaultDetails.password,
1766
1804
  onabort(...args) {
1767
- that.context.HttpxCallBack.onAbort(details, resolve, args);
1805
+ that.context.HttpxCallBack.onAbort(details, resolve, reject, args);
1768
1806
  },
1769
1807
  onerror(...args) {
1770
- that.context.HttpxCallBack.onError(details, resolve, args);
1808
+ that.context.HttpxCallBack.onError(details, resolve, reject, args);
1771
1809
  },
1772
1810
  onloadstart(...args) {
1773
1811
  that.context.HttpxCallBack.onLoadStart(details, args);
@@ -1779,12 +1817,32 @@ class Httpx {
1779
1817
  that.context.HttpxCallBack.onReadyStateChange(details, args);
1780
1818
  },
1781
1819
  ontimeout(...args) {
1782
- that.context.HttpxCallBack.onTimeout(details, resolve, args);
1820
+ that.context.HttpxCallBack.onTimeout(details, resolve, reject, args);
1783
1821
  },
1784
1822
  onload(...args) {
1785
- that.context.HttpxCallBack.onLoad(details, resolve, args);
1823
+ that.context.HttpxCallBack.onLoad(details, resolve, reject, args);
1786
1824
  },
1787
1825
  };
1826
+ // 补全allowInterceptConfig参数
1827
+ if (typeof details.allowInterceptConfig === "boolean") {
1828
+ Object.keys(result.allowInterceptConfig).forEach((keyName) => {
1829
+ result.allowInterceptConfig[keyName] =
1830
+ details.allowInterceptConfig;
1831
+ });
1832
+ }
1833
+ else {
1834
+ if (typeof details.allowInterceptConfig === "object" &&
1835
+ details.allowInterceptConfig != null) {
1836
+ Object.keys(details.allowInterceptConfig).forEach((keyName) => {
1837
+ let value = details.allowInterceptConfig[keyName];
1838
+ if (keyName in
1839
+ result.allowInterceptConfig &&
1840
+ typeof value === "boolean") {
1841
+ result.allowInterceptConfig[keyName] = value;
1842
+ }
1843
+ });
1844
+ }
1845
+ }
1788
1846
  if (typeof this.context.GM_Api.xmlHttpRequest !== "function") {
1789
1847
  result.fetch = true;
1790
1848
  }
@@ -1912,9 +1970,10 @@ class Httpx {
1912
1970
  * onabort请求被取消-触发
1913
1971
  * @param details 配置
1914
1972
  * @param resolve 回调
1973
+ * @param reject 抛出错误
1915
1974
  * @param argumentsList 参数列表
1916
1975
  */
1917
- onAbort(details, resolve, argumentsList) {
1976
+ onAbort(details, resolve, reject, argumentsList) {
1918
1977
  if ("onabort" in details) {
1919
1978
  details.onabort.apply(this, argumentsList);
1920
1979
  }
@@ -1927,6 +1986,7 @@ class Httpx {
1927
1986
  response: null,
1928
1987
  details: details,
1929
1988
  }) == null) {
1989
+ // reject(new TypeError("response is intercept with onabort"));
1930
1990
  return;
1931
1991
  }
1932
1992
  resolve({
@@ -1940,9 +2000,10 @@ class Httpx {
1940
2000
  * onerror请求异常-触发
1941
2001
  * @param details 配置
1942
2002
  * @param resolve 回调
2003
+ * @param reject 抛出错误
1943
2004
  * @param argumentsList 响应的参数列表
1944
2005
  */
1945
- onError(details, resolve, argumentsList) {
2006
+ onError(details, resolve, reject, argumentsList) {
1946
2007
  if ("onerror" in details) {
1947
2008
  details.onerror.apply(this, argumentsList);
1948
2009
  }
@@ -1959,6 +2020,7 @@ class Httpx {
1959
2020
  response: response,
1960
2021
  details: details,
1961
2022
  }) == null) {
2023
+ // reject(new TypeError("response is intercept with onerror"));
1962
2024
  return;
1963
2025
  }
1964
2026
  resolve({
@@ -1973,9 +2035,10 @@ class Httpx {
1973
2035
  * ontimeout请求超时-触发
1974
2036
  * @param details 配置
1975
2037
  * @param resolve 回调
2038
+ * @param reject 抛出错误
1976
2039
  * @param argumentsList 参数列表
1977
2040
  */
1978
- onTimeout(details, resolve, argumentsList) {
2041
+ onTimeout(details, resolve, reject, argumentsList) {
1979
2042
  if ("ontimeout" in details) {
1980
2043
  details.ontimeout.apply(this, argumentsList);
1981
2044
  }
@@ -1988,6 +2051,7 @@ class Httpx {
1988
2051
  response: (argumentsList || [null])[0],
1989
2052
  details: details,
1990
2053
  }) == null) {
2054
+ // reject(new TypeError("response is intercept with ontimeout"));
1991
2055
  return;
1992
2056
  }
1993
2057
  resolve({
@@ -2014,9 +2078,10 @@ class Httpx {
2014
2078
  * onload加载完毕-触发
2015
2079
  * @param details 请求的配置
2016
2080
  * @param resolve 回调
2081
+ * @param reject 抛出错误
2017
2082
  * @param argumentsList 参数列表
2018
2083
  */
2019
- onLoad(details, resolve, argumentsList) {
2084
+ onLoad(details, resolve, reject, argumentsList) {
2020
2085
  /* X浏览器会因为设置了responseType导致不返回responseText */
2021
2086
  let Response = argumentsList[0];
2022
2087
  /* responseText为空,response不为空的情况 */
@@ -2071,6 +2136,7 @@ class Httpx {
2071
2136
  /* 状态码2xx都是成功的 */
2072
2137
  if (Math.floor(Response.status / 100) === 2) {
2073
2138
  if (this.context.HttpxResponseHook.successResponseCallBack(Response, details) == null) {
2139
+ // reject(new TypeError("response is intercept with onloada"));
2074
2140
  return;
2075
2141
  }
2076
2142
  resolve({
@@ -2082,7 +2148,7 @@ class Httpx {
2082
2148
  });
2083
2149
  }
2084
2150
  else {
2085
- this.context.HttpxCallBack.onError(details, resolve, argumentsList);
2151
+ this.context.HttpxCallBack.onError(details, resolve, reject, argumentsList);
2086
2152
  }
2087
2153
  },
2088
2154
  /**
@@ -2393,8 +2459,8 @@ class Httpx {
2393
2459
  async get(...args) {
2394
2460
  let that = this;
2395
2461
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2396
- return new Promise((resolve) => {
2397
- let requestDetails = that.HttpxRequestDetails.getDetails("get", resolve, details);
2462
+ return new Promise((resolve, reject) => {
2463
+ let requestDetails = that.HttpxRequestDetails.getDetails("GET", details, resolve, reject);
2398
2464
  Reflect.deleteProperty(requestDetails, "onprogress");
2399
2465
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2400
2466
  that.HttpxRequest.request(requestDetails);
@@ -2406,8 +2472,8 @@ class Httpx {
2406
2472
  async post(...args) {
2407
2473
  let that = this;
2408
2474
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2409
- return new Promise((resolve) => {
2410
- let requestDetails = that.HttpxRequestDetails.getDetails("post", resolve, details);
2475
+ return new Promise((resolve, reject) => {
2476
+ let requestDetails = that.HttpxRequestDetails.getDetails("POST", details, resolve, reject);
2411
2477
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2412
2478
  that.HttpxRequest.request(requestDetails);
2413
2479
  });
@@ -2418,8 +2484,8 @@ class Httpx {
2418
2484
  async head(...args) {
2419
2485
  let that = this;
2420
2486
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2421
- return new Promise((resolve) => {
2422
- let requestDetails = that.HttpxRequestDetails.getDetails("head", resolve, details);
2487
+ return new Promise((resolve, reject) => {
2488
+ let requestDetails = that.HttpxRequestDetails.getDetails("HEAD", details, resolve, reject);
2423
2489
  Reflect.deleteProperty(requestDetails, "onprogress");
2424
2490
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2425
2491
  that.HttpxRequest.request(requestDetails);
@@ -2431,8 +2497,8 @@ class Httpx {
2431
2497
  async options(...args) {
2432
2498
  let that = this;
2433
2499
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2434
- return new Promise((resolve) => {
2435
- let requestDetails = that.HttpxRequestDetails.getDetails("options", resolve, details);
2500
+ return new Promise((resolve, reject) => {
2501
+ let requestDetails = that.HttpxRequestDetails.getDetails("OPTIONS", details, resolve, reject);
2436
2502
  Reflect.deleteProperty(requestDetails, "onprogress");
2437
2503
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2438
2504
  that.HttpxRequest.request(requestDetails);
@@ -2444,8 +2510,8 @@ class Httpx {
2444
2510
  async delete(...args) {
2445
2511
  let that = this;
2446
2512
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2447
- return new Promise((resolve) => {
2448
- let requestDetails = that.HttpxRequestDetails.getDetails("delete", resolve, details);
2513
+ return new Promise((resolve, reject) => {
2514
+ let requestDetails = that.HttpxRequestDetails.getDetails("DELETE", details, resolve, reject);
2449
2515
  Reflect.deleteProperty(requestDetails, "onprogress");
2450
2516
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2451
2517
  that.HttpxRequest.request(requestDetails);
@@ -2457,8 +2523,8 @@ class Httpx {
2457
2523
  async put(...args) {
2458
2524
  let that = this;
2459
2525
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2460
- return new Promise((resolve) => {
2461
- let requestDetails = that.HttpxRequestDetails.getDetails("put", resolve, details);
2526
+ return new Promise((resolve, reject) => {
2527
+ let requestDetails = that.HttpxRequestDetails.getDetails("PUT", details, resolve, reject);
2462
2528
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2463
2529
  that.HttpxRequest.request(requestDetails);
2464
2530
  });