@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.umd.js CHANGED
@@ -1569,14 +1569,23 @@
1569
1569
  * @private
1570
1570
  */
1571
1571
  beforeRequestCallBack(details) {
1572
- if (details.allowInterceptConfig) {
1573
- // 首先配置得存在,不然默认允许拦截
1574
- if (typeof details.allowInterceptConfig.beforeRequest === "boolean" &&
1575
- !details.allowInterceptConfig.beforeRequest) {
1576
- // 设置了禁止拦截
1572
+ if (typeof details.allowInterceptConfig === "boolean") {
1573
+ if (!details.allowInterceptConfig) {
1574
+ // 不允许拦截
1577
1575
  return details;
1578
1576
  }
1579
1577
  }
1578
+ else {
1579
+ if (details.allowInterceptConfig != null) {
1580
+ // 配置存在
1581
+ // 细分处理是否拦截
1582
+ if (typeof details.allowInterceptConfig.beforeRequest === "boolean" &&
1583
+ !details.allowInterceptConfig.beforeRequest) {
1584
+ // 设置了禁止拦截
1585
+ return details;
1586
+ }
1587
+ }
1588
+ }
1580
1589
  for (let index = 0; index < this.$config.configList.length; index++) {
1581
1590
  let item = this.$config.configList[index];
1582
1591
  if (typeof item.fn === "function") {
@@ -1638,15 +1647,24 @@
1638
1647
  * @param details 请求的配置
1639
1648
  */
1640
1649
  successResponseCallBack(response, details) {
1641
- if (details.allowInterceptConfig) {
1642
- // 首先配置得存在,不然默认允许拦截
1643
- if (typeof details.allowInterceptConfig.afterResponseSuccess ===
1644
- "boolean" &&
1645
- !details.allowInterceptConfig.afterResponseSuccess) {
1646
- // 设置了禁止拦截
1650
+ if (typeof details.allowInterceptConfig === "boolean") {
1651
+ if (!details.allowInterceptConfig) {
1652
+ // 不允许拦截
1647
1653
  return details;
1648
1654
  }
1649
1655
  }
1656
+ else {
1657
+ if (details.allowInterceptConfig != null) {
1658
+ // 配置存在
1659
+ // 细分处理是否拦截
1660
+ if (typeof details.allowInterceptConfig.afterResponseSuccess ===
1661
+ "boolean" &&
1662
+ !details.allowInterceptConfig.afterResponseSuccess) {
1663
+ // 设置了禁止拦截
1664
+ return details;
1665
+ }
1666
+ }
1667
+ }
1650
1668
  for (let index = 0; index < this.$config.configList.length; index++) {
1651
1669
  let item = this.$config.configList[index];
1652
1670
  if (typeof item.successFn === "function") {
@@ -1660,17 +1678,28 @@
1660
1678
  /**
1661
1679
  * 失败的回调
1662
1680
  * @param data 配置
1681
+ * @returns
1682
+ * 返回null|undefined就是拦截掉了
1663
1683
  */
1664
1684
  errorResponseCallBack(data) {
1665
- if (data.details.allowInterceptConfig) {
1666
- // 首先配置得存在,不然默认允许拦截
1667
- if (typeof data.details.allowInterceptConfig.afterResponseError ===
1668
- "boolean" &&
1669
- !data.details.allowInterceptConfig.afterResponseError) {
1670
- // 设置了禁止拦截
1685
+ if (typeof data.details.allowInterceptConfig === "boolean") {
1686
+ if (!data.details.allowInterceptConfig) {
1687
+ // 不允许拦截
1671
1688
  return data;
1672
1689
  }
1673
1690
  }
1691
+ else {
1692
+ if (data.details.allowInterceptConfig != null) {
1693
+ // 配置存在
1694
+ // 细分处理是否拦截
1695
+ if (typeof data.details.allowInterceptConfig.afterResponseError ===
1696
+ "boolean" &&
1697
+ !data.details.allowInterceptConfig.afterResponseError) {
1698
+ // 设置了禁止拦截
1699
+ return data;
1700
+ }
1701
+ }
1702
+ }
1674
1703
  for (let index = 0; index < this.$config.configList.length; index++) {
1675
1704
  let item = this.$config.configList[index];
1676
1705
  if (typeof item.errorFn === "function") {
@@ -1741,10 +1770,11 @@
1741
1770
  /**
1742
1771
  * 获取请求配置
1743
1772
  * @param method 当前请求方法,默认get
1744
- * @param resolve promise回调
1745
1773
  * @param details 请求配置
1774
+ * @param resolve promise回调
1775
+ * @param reject 抛出错误回调
1746
1776
  */
1747
- getDetails(method, resolve, details) {
1777
+ getDetails(method, details, resolve, reject) {
1748
1778
  let that = this;
1749
1779
  let result = {
1750
1780
  url: details.url || this.context.#defaultDetails.url,
@@ -1767,13 +1797,21 @@
1767
1797
  fetch: details.fetch || this.context.#defaultDetails.fetch,
1768
1798
  /* 对象使用深拷贝 */
1769
1799
  fetchInit: utils.deepClone(this.context.#defaultDetails.fetchInit),
1800
+ allowInterceptConfig: {
1801
+ beforeRequest: this.context.#defaultDetails
1802
+ .allowInterceptConfig.beforeRequest,
1803
+ afterResponseSuccess: this.context.#defaultDetails
1804
+ .allowInterceptConfig.afterResponseSuccess,
1805
+ afterResponseError: this.context.#defaultDetails
1806
+ .allowInterceptConfig.afterResponseError,
1807
+ },
1770
1808
  user: details.user || this.context.#defaultDetails.user,
1771
1809
  password: details.password || this.context.#defaultDetails.password,
1772
1810
  onabort(...args) {
1773
- that.context.HttpxCallBack.onAbort(details, resolve, args);
1811
+ that.context.HttpxCallBack.onAbort(details, resolve, reject, args);
1774
1812
  },
1775
1813
  onerror(...args) {
1776
- that.context.HttpxCallBack.onError(details, resolve, args);
1814
+ that.context.HttpxCallBack.onError(details, resolve, reject, args);
1777
1815
  },
1778
1816
  onloadstart(...args) {
1779
1817
  that.context.HttpxCallBack.onLoadStart(details, args);
@@ -1785,12 +1823,32 @@
1785
1823
  that.context.HttpxCallBack.onReadyStateChange(details, args);
1786
1824
  },
1787
1825
  ontimeout(...args) {
1788
- that.context.HttpxCallBack.onTimeout(details, resolve, args);
1826
+ that.context.HttpxCallBack.onTimeout(details, resolve, reject, args);
1789
1827
  },
1790
1828
  onload(...args) {
1791
- that.context.HttpxCallBack.onLoad(details, resolve, args);
1829
+ that.context.HttpxCallBack.onLoad(details, resolve, reject, args);
1792
1830
  },
1793
1831
  };
1832
+ // 补全allowInterceptConfig参数
1833
+ if (typeof details.allowInterceptConfig === "boolean") {
1834
+ Object.keys(result.allowInterceptConfig).forEach((keyName) => {
1835
+ result.allowInterceptConfig[keyName] =
1836
+ details.allowInterceptConfig;
1837
+ });
1838
+ }
1839
+ else {
1840
+ if (typeof details.allowInterceptConfig === "object" &&
1841
+ details.allowInterceptConfig != null) {
1842
+ Object.keys(details.allowInterceptConfig).forEach((keyName) => {
1843
+ let value = details.allowInterceptConfig[keyName];
1844
+ if (keyName in
1845
+ result.allowInterceptConfig &&
1846
+ typeof value === "boolean") {
1847
+ result.allowInterceptConfig[keyName] = value;
1848
+ }
1849
+ });
1850
+ }
1851
+ }
1794
1852
  if (typeof this.context.GM_Api.xmlHttpRequest !== "function") {
1795
1853
  result.fetch = true;
1796
1854
  }
@@ -1918,9 +1976,10 @@
1918
1976
  * onabort请求被取消-触发
1919
1977
  * @param details 配置
1920
1978
  * @param resolve 回调
1979
+ * @param reject 抛出错误
1921
1980
  * @param argumentsList 参数列表
1922
1981
  */
1923
- onAbort(details, resolve, argumentsList) {
1982
+ onAbort(details, resolve, reject, argumentsList) {
1924
1983
  if ("onabort" in details) {
1925
1984
  details.onabort.apply(this, argumentsList);
1926
1985
  }
@@ -1933,6 +1992,7 @@
1933
1992
  response: null,
1934
1993
  details: details,
1935
1994
  }) == null) {
1995
+ // reject(new TypeError("response is intercept with onabort"));
1936
1996
  return;
1937
1997
  }
1938
1998
  resolve({
@@ -1946,9 +2006,10 @@
1946
2006
  * onerror请求异常-触发
1947
2007
  * @param details 配置
1948
2008
  * @param resolve 回调
2009
+ * @param reject 抛出错误
1949
2010
  * @param argumentsList 响应的参数列表
1950
2011
  */
1951
- onError(details, resolve, argumentsList) {
2012
+ onError(details, resolve, reject, argumentsList) {
1952
2013
  if ("onerror" in details) {
1953
2014
  details.onerror.apply(this, argumentsList);
1954
2015
  }
@@ -1965,6 +2026,7 @@
1965
2026
  response: response,
1966
2027
  details: details,
1967
2028
  }) == null) {
2029
+ // reject(new TypeError("response is intercept with onerror"));
1968
2030
  return;
1969
2031
  }
1970
2032
  resolve({
@@ -1979,9 +2041,10 @@
1979
2041
  * ontimeout请求超时-触发
1980
2042
  * @param details 配置
1981
2043
  * @param resolve 回调
2044
+ * @param reject 抛出错误
1982
2045
  * @param argumentsList 参数列表
1983
2046
  */
1984
- onTimeout(details, resolve, argumentsList) {
2047
+ onTimeout(details, resolve, reject, argumentsList) {
1985
2048
  if ("ontimeout" in details) {
1986
2049
  details.ontimeout.apply(this, argumentsList);
1987
2050
  }
@@ -1994,6 +2057,7 @@
1994
2057
  response: (argumentsList || [null])[0],
1995
2058
  details: details,
1996
2059
  }) == null) {
2060
+ // reject(new TypeError("response is intercept with ontimeout"));
1997
2061
  return;
1998
2062
  }
1999
2063
  resolve({
@@ -2020,9 +2084,10 @@
2020
2084
  * onload加载完毕-触发
2021
2085
  * @param details 请求的配置
2022
2086
  * @param resolve 回调
2087
+ * @param reject 抛出错误
2023
2088
  * @param argumentsList 参数列表
2024
2089
  */
2025
- onLoad(details, resolve, argumentsList) {
2090
+ onLoad(details, resolve, reject, argumentsList) {
2026
2091
  /* X浏览器会因为设置了responseType导致不返回responseText */
2027
2092
  let Response = argumentsList[0];
2028
2093
  /* responseText为空,response不为空的情况 */
@@ -2077,6 +2142,7 @@
2077
2142
  /* 状态码2xx都是成功的 */
2078
2143
  if (Math.floor(Response.status / 100) === 2) {
2079
2144
  if (this.context.HttpxResponseHook.successResponseCallBack(Response, details) == null) {
2145
+ // reject(new TypeError("response is intercept with onloada"));
2080
2146
  return;
2081
2147
  }
2082
2148
  resolve({
@@ -2088,7 +2154,7 @@
2088
2154
  });
2089
2155
  }
2090
2156
  else {
2091
- this.context.HttpxCallBack.onError(details, resolve, argumentsList);
2157
+ this.context.HttpxCallBack.onError(details, resolve, reject, argumentsList);
2092
2158
  }
2093
2159
  },
2094
2160
  /**
@@ -2399,8 +2465,8 @@
2399
2465
  async get(...args) {
2400
2466
  let that = this;
2401
2467
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2402
- return new Promise((resolve) => {
2403
- let requestDetails = that.HttpxRequestDetails.getDetails("get", resolve, details);
2468
+ return new Promise((resolve, reject) => {
2469
+ let requestDetails = that.HttpxRequestDetails.getDetails("GET", details, resolve, reject);
2404
2470
  Reflect.deleteProperty(requestDetails, "onprogress");
2405
2471
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2406
2472
  that.HttpxRequest.request(requestDetails);
@@ -2412,8 +2478,8 @@
2412
2478
  async post(...args) {
2413
2479
  let that = this;
2414
2480
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2415
- return new Promise((resolve) => {
2416
- let requestDetails = that.HttpxRequestDetails.getDetails("post", resolve, details);
2481
+ return new Promise((resolve, reject) => {
2482
+ let requestDetails = that.HttpxRequestDetails.getDetails("POST", details, resolve, reject);
2417
2483
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2418
2484
  that.HttpxRequest.request(requestDetails);
2419
2485
  });
@@ -2424,8 +2490,8 @@
2424
2490
  async head(...args) {
2425
2491
  let that = this;
2426
2492
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2427
- return new Promise((resolve) => {
2428
- let requestDetails = that.HttpxRequestDetails.getDetails("head", resolve, details);
2493
+ return new Promise((resolve, reject) => {
2494
+ let requestDetails = that.HttpxRequestDetails.getDetails("HEAD", details, resolve, reject);
2429
2495
  Reflect.deleteProperty(requestDetails, "onprogress");
2430
2496
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2431
2497
  that.HttpxRequest.request(requestDetails);
@@ -2437,8 +2503,8 @@
2437
2503
  async options(...args) {
2438
2504
  let that = this;
2439
2505
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2440
- return new Promise((resolve) => {
2441
- let requestDetails = that.HttpxRequestDetails.getDetails("options", resolve, details);
2506
+ return new Promise((resolve, reject) => {
2507
+ let requestDetails = that.HttpxRequestDetails.getDetails("OPTIONS", details, resolve, reject);
2442
2508
  Reflect.deleteProperty(requestDetails, "onprogress");
2443
2509
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2444
2510
  that.HttpxRequest.request(requestDetails);
@@ -2450,8 +2516,8 @@
2450
2516
  async delete(...args) {
2451
2517
  let that = this;
2452
2518
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2453
- return new Promise((resolve) => {
2454
- let requestDetails = that.HttpxRequestDetails.getDetails("delete", resolve, details);
2519
+ return new Promise((resolve, reject) => {
2520
+ let requestDetails = that.HttpxRequestDetails.getDetails("DELETE", details, resolve, reject);
2455
2521
  Reflect.deleteProperty(requestDetails, "onprogress");
2456
2522
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2457
2523
  that.HttpxRequest.request(requestDetails);
@@ -2463,8 +2529,8 @@
2463
2529
  async put(...args) {
2464
2530
  let that = this;
2465
2531
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2466
- return new Promise((resolve) => {
2467
- let requestDetails = that.HttpxRequestDetails.getDetails("put", resolve, details);
2532
+ return new Promise((resolve, reject) => {
2533
+ let requestDetails = that.HttpxRequestDetails.getDetails("PUT", details, resolve, reject);
2468
2534
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2469
2535
  that.HttpxRequest.request(requestDetails);
2470
2536
  });