@whitesev/utils 1.8.0 → 1.9.0

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,
@@ -1766,10 +1796,10 @@ define((function () { 'use strict';
1766
1796
  user: details.user || this.context.#defaultDetails.user,
1767
1797
  password: details.password || this.context.#defaultDetails.password,
1768
1798
  onabort(...args) {
1769
- that.context.HttpxCallBack.onAbort(details, resolve, args);
1799
+ that.context.HttpxCallBack.onAbort(details, resolve, reject, args);
1770
1800
  },
1771
1801
  onerror(...args) {
1772
- that.context.HttpxCallBack.onError(details, resolve, args);
1802
+ that.context.HttpxCallBack.onError(details, resolve, reject, args);
1773
1803
  },
1774
1804
  onloadstart(...args) {
1775
1805
  that.context.HttpxCallBack.onLoadStart(details, args);
@@ -1781,10 +1811,10 @@ define((function () { 'use strict';
1781
1811
  that.context.HttpxCallBack.onReadyStateChange(details, args);
1782
1812
  },
1783
1813
  ontimeout(...args) {
1784
- that.context.HttpxCallBack.onTimeout(details, resolve, args);
1814
+ that.context.HttpxCallBack.onTimeout(details, resolve, reject, args);
1785
1815
  },
1786
1816
  onload(...args) {
1787
- that.context.HttpxCallBack.onLoad(details, resolve, args);
1817
+ that.context.HttpxCallBack.onLoad(details, resolve, reject, args);
1788
1818
  },
1789
1819
  };
1790
1820
  if (typeof this.context.GM_Api.xmlHttpRequest !== "function") {
@@ -1914,9 +1944,10 @@ define((function () { 'use strict';
1914
1944
  * onabort请求被取消-触发
1915
1945
  * @param details 配置
1916
1946
  * @param resolve 回调
1947
+ * @param reject 抛出错误
1917
1948
  * @param argumentsList 参数列表
1918
1949
  */
1919
- onAbort(details, resolve, argumentsList) {
1950
+ onAbort(details, resolve, reject, argumentsList) {
1920
1951
  if ("onabort" in details) {
1921
1952
  details.onabort.apply(this, argumentsList);
1922
1953
  }
@@ -1929,6 +1960,7 @@ define((function () { 'use strict';
1929
1960
  response: null,
1930
1961
  details: details,
1931
1962
  }) == null) {
1963
+ // reject(new TypeError("response is intercept with onabort"));
1932
1964
  return;
1933
1965
  }
1934
1966
  resolve({
@@ -1942,9 +1974,10 @@ define((function () { 'use strict';
1942
1974
  * onerror请求异常-触发
1943
1975
  * @param details 配置
1944
1976
  * @param resolve 回调
1977
+ * @param reject 抛出错误
1945
1978
  * @param argumentsList 响应的参数列表
1946
1979
  */
1947
- onError(details, resolve, argumentsList) {
1980
+ onError(details, resolve, reject, argumentsList) {
1948
1981
  if ("onerror" in details) {
1949
1982
  details.onerror.apply(this, argumentsList);
1950
1983
  }
@@ -1961,6 +1994,7 @@ define((function () { 'use strict';
1961
1994
  response: response,
1962
1995
  details: details,
1963
1996
  }) == null) {
1997
+ // reject(new TypeError("response is intercept with onerror"));
1964
1998
  return;
1965
1999
  }
1966
2000
  resolve({
@@ -1975,9 +2009,10 @@ define((function () { 'use strict';
1975
2009
  * ontimeout请求超时-触发
1976
2010
  * @param details 配置
1977
2011
  * @param resolve 回调
2012
+ * @param reject 抛出错误
1978
2013
  * @param argumentsList 参数列表
1979
2014
  */
1980
- onTimeout(details, resolve, argumentsList) {
2015
+ onTimeout(details, resolve, reject, argumentsList) {
1981
2016
  if ("ontimeout" in details) {
1982
2017
  details.ontimeout.apply(this, argumentsList);
1983
2018
  }
@@ -1990,6 +2025,7 @@ define((function () { 'use strict';
1990
2025
  response: (argumentsList || [null])[0],
1991
2026
  details: details,
1992
2027
  }) == null) {
2028
+ // reject(new TypeError("response is intercept with ontimeout"));
1993
2029
  return;
1994
2030
  }
1995
2031
  resolve({
@@ -2016,9 +2052,10 @@ define((function () { 'use strict';
2016
2052
  * onload加载完毕-触发
2017
2053
  * @param details 请求的配置
2018
2054
  * @param resolve 回调
2055
+ * @param reject 抛出错误
2019
2056
  * @param argumentsList 参数列表
2020
2057
  */
2021
- onLoad(details, resolve, argumentsList) {
2058
+ onLoad(details, resolve, reject, argumentsList) {
2022
2059
  /* X浏览器会因为设置了responseType导致不返回responseText */
2023
2060
  let Response = argumentsList[0];
2024
2061
  /* responseText为空,response不为空的情况 */
@@ -2073,6 +2110,7 @@ define((function () { 'use strict';
2073
2110
  /* 状态码2xx都是成功的 */
2074
2111
  if (Math.floor(Response.status / 100) === 2) {
2075
2112
  if (this.context.HttpxResponseHook.successResponseCallBack(Response, details) == null) {
2113
+ // reject(new TypeError("response is intercept with onloada"));
2076
2114
  return;
2077
2115
  }
2078
2116
  resolve({
@@ -2084,7 +2122,7 @@ define((function () { 'use strict';
2084
2122
  });
2085
2123
  }
2086
2124
  else {
2087
- this.context.HttpxCallBack.onError(details, resolve, argumentsList);
2125
+ this.context.HttpxCallBack.onError(details, resolve, reject, argumentsList);
2088
2126
  }
2089
2127
  },
2090
2128
  /**
@@ -2395,8 +2433,8 @@ define((function () { 'use strict';
2395
2433
  async get(...args) {
2396
2434
  let that = this;
2397
2435
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2398
- return new Promise((resolve) => {
2399
- let requestDetails = that.HttpxRequestDetails.getDetails("get", resolve, details);
2436
+ return new Promise((resolve, reject) => {
2437
+ let requestDetails = that.HttpxRequestDetails.getDetails("GET", details, resolve, reject);
2400
2438
  Reflect.deleteProperty(requestDetails, "onprogress");
2401
2439
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2402
2440
  that.HttpxRequest.request(requestDetails);
@@ -2408,8 +2446,8 @@ define((function () { 'use strict';
2408
2446
  async post(...args) {
2409
2447
  let that = this;
2410
2448
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2411
- return new Promise((resolve) => {
2412
- let requestDetails = that.HttpxRequestDetails.getDetails("post", resolve, details);
2449
+ return new Promise((resolve, reject) => {
2450
+ let requestDetails = that.HttpxRequestDetails.getDetails("POST", details, resolve, reject);
2413
2451
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2414
2452
  that.HttpxRequest.request(requestDetails);
2415
2453
  });
@@ -2420,8 +2458,8 @@ define((function () { 'use strict';
2420
2458
  async head(...args) {
2421
2459
  let that = this;
2422
2460
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2423
- return new Promise((resolve) => {
2424
- let requestDetails = that.HttpxRequestDetails.getDetails("head", resolve, details);
2461
+ return new Promise((resolve, reject) => {
2462
+ let requestDetails = that.HttpxRequestDetails.getDetails("HEAD", details, resolve, reject);
2425
2463
  Reflect.deleteProperty(requestDetails, "onprogress");
2426
2464
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2427
2465
  that.HttpxRequest.request(requestDetails);
@@ -2433,8 +2471,8 @@ define((function () { 'use strict';
2433
2471
  async options(...args) {
2434
2472
  let that = this;
2435
2473
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2436
- return new Promise((resolve) => {
2437
- let requestDetails = that.HttpxRequestDetails.getDetails("options", resolve, details);
2474
+ return new Promise((resolve, reject) => {
2475
+ let requestDetails = that.HttpxRequestDetails.getDetails("OPTIONS", details, resolve, reject);
2438
2476
  Reflect.deleteProperty(requestDetails, "onprogress");
2439
2477
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2440
2478
  that.HttpxRequest.request(requestDetails);
@@ -2446,8 +2484,8 @@ define((function () { 'use strict';
2446
2484
  async delete(...args) {
2447
2485
  let that = this;
2448
2486
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2449
- return new Promise((resolve) => {
2450
- let requestDetails = that.HttpxRequestDetails.getDetails("delete", resolve, details);
2487
+ return new Promise((resolve, reject) => {
2488
+ let requestDetails = that.HttpxRequestDetails.getDetails("DELETE", details, resolve, reject);
2451
2489
  Reflect.deleteProperty(requestDetails, "onprogress");
2452
2490
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2453
2491
  that.HttpxRequest.request(requestDetails);
@@ -2459,8 +2497,8 @@ define((function () { 'use strict';
2459
2497
  async put(...args) {
2460
2498
  let that = this;
2461
2499
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2462
- return new Promise((resolve) => {
2463
- let requestDetails = that.HttpxRequestDetails.getDetails("put", resolve, details);
2500
+ return new Promise((resolve, reject) => {
2501
+ let requestDetails = that.HttpxRequestDetails.getDetails("PUT", details, resolve, reject);
2464
2502
  requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2465
2503
  that.HttpxRequest.request(requestDetails);
2466
2504
  });