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