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