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