@whitesev/utils 1.4.8 → 1.5.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.esm.js CHANGED
@@ -1461,7 +1461,7 @@ class Httpx {
1461
1461
  /**
1462
1462
  * 添加请求前的回调处理配置
1463
1463
  */
1464
- addBeforeRequestCallBack(fn) {
1464
+ add(fn) {
1465
1465
  if (typeof fn === "function") {
1466
1466
  let uuid = GenerateUUID();
1467
1467
  this.$config.configList.push({
@@ -1478,7 +1478,7 @@ class Httpx {
1478
1478
  * 删除请求前的回调处理配置
1479
1479
  * @param id
1480
1480
  */
1481
- deleteBeforeRequestCallBack(id) {
1481
+ delete(id) {
1482
1482
  if (typeof id === "string") {
1483
1483
  let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1484
1484
  if (findIndex !== -1) {
@@ -1491,7 +1491,7 @@ class Httpx {
1491
1491
  /**
1492
1492
  * 清空设置的请求前的回调处理配置
1493
1493
  */
1494
- clearBeforeRequestCallBack() {
1494
+ clearAll() {
1495
1495
  this.$config.configList = [];
1496
1496
  },
1497
1497
  };
@@ -1521,21 +1521,21 @@ class Httpx {
1521
1521
  * 失败的回调
1522
1522
  * @param response
1523
1523
  */
1524
- errorResponseCallBack(response) {
1524
+ errorResponseCallBack(data) {
1525
1525
  for (let index = 0; index < this.$config.configList.length; index++) {
1526
1526
  let item = this.$config.configList[index];
1527
1527
  if (typeof item.errorFn === "function") {
1528
- if (item.errorFn(response) == null) {
1528
+ if (item.errorFn(data) == null) {
1529
1529
  return;
1530
1530
  }
1531
1531
  }
1532
1532
  }
1533
- return response;
1533
+ return data;
1534
1534
  },
1535
1535
  /**
1536
1536
  * 添加请求前的回调处理配置
1537
1537
  */
1538
- addAfterResponseCallBack(successFn, errorFn) {
1538
+ add(successFn, errorFn) {
1539
1539
  let id = GenerateUUID();
1540
1540
  this.$config.configList.push({
1541
1541
  id: id,
@@ -1548,7 +1548,7 @@ class Httpx {
1548
1548
  * 删除请求前的回调处理配置
1549
1549
  * @param id
1550
1550
  */
1551
- deleteAfterResponseCallBack(id) {
1551
+ delete(id) {
1552
1552
  if (typeof id === "string") {
1553
1553
  let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1554
1554
  if (findIndex !== -1) {
@@ -1561,7 +1561,7 @@ class Httpx {
1561
1561
  /**
1562
1562
  * 清空设置的请求前的回调处理配置
1563
1563
  */
1564
- clearAfterResponseCallBack() {
1564
+ clearAll() {
1565
1565
  this.$config.configList = [];
1566
1566
  },
1567
1567
  };
@@ -1778,6 +1778,13 @@ class Httpx {
1778
1778
  else if ("onabort" in this.context.#defaultDetails) {
1779
1779
  this.context.#defaultDetails.onabort.apply(this, argumentsList);
1780
1780
  }
1781
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1782
+ type: "onabort",
1783
+ error: new TypeError("request canceled"),
1784
+ response: null,
1785
+ }) == null) {
1786
+ return;
1787
+ }
1781
1788
  resolve({
1782
1789
  status: false,
1783
1790
  data: [...argumentsList],
@@ -1802,6 +1809,13 @@ class Httpx {
1802
1809
  if (response.length) {
1803
1810
  response = response[0];
1804
1811
  }
1812
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1813
+ type: "onerror",
1814
+ error: new TypeError("request error"),
1815
+ response: response,
1816
+ }) == null) {
1817
+ return;
1818
+ }
1805
1819
  resolve({
1806
1820
  status: false,
1807
1821
  data: response,
@@ -1823,6 +1837,13 @@ class Httpx {
1823
1837
  else if ("ontimeout" in this.context.#defaultDetails) {
1824
1838
  this.context.#defaultDetails.ontimeout.apply(this, argumentsList);
1825
1839
  }
1840
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1841
+ type: "ontimeout",
1842
+ error: new TypeError("request timeout"),
1843
+ response: (argumentsList || [null])[0],
1844
+ }) == null) {
1845
+ return;
1846
+ }
1826
1847
  resolve({
1827
1848
  status: false,
1828
1849
  data: [...argumentsList],
@@ -1903,6 +1924,10 @@ class Httpx {
1903
1924
  }
1904
1925
  /* 状态码2xx都是成功的 */
1905
1926
  if (Math.floor(Response.status / 100) === 2) {
1927
+ if (this.context.HttpxResponseHook.successResponseCallBack(Response) ==
1928
+ null) {
1929
+ return;
1930
+ }
1906
1931
  resolve({
1907
1932
  status: true,
1908
1933
  data: Response,
@@ -2151,14 +2176,24 @@ class Httpx {
2151
2176
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2152
2177
  */
2153
2178
  use(fn) {
2154
- return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
2179
+ if (typeof fn !== "function") {
2180
+ console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
2181
+ return;
2182
+ }
2183
+ return this.context.HttpxRequestHook.add(fn);
2155
2184
  },
2156
2185
  /**
2157
2186
  * 移除拦截器
2158
2187
  * @param id 通过use返回的id
2159
2188
  */
2160
2189
  eject(id) {
2161
- return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
2190
+ return this.context.HttpxRequestHook.delete(id);
2191
+ },
2192
+ /**
2193
+ * 移除所有拦截器
2194
+ */
2195
+ ejectAll() {
2196
+ this.context.HttpxRequestHook.clearAll();
2162
2197
  },
2163
2198
  },
2164
2199
  /**
@@ -2175,17 +2210,23 @@ class Httpx {
2175
2210
  */
2176
2211
  use(successFn, errorFn) {
2177
2212
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2178
- console.warn("[Httpx] 请传入拦截器函数");
2213
+ console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
2179
2214
  return;
2180
2215
  }
2181
- return this.context.HttpxResponseHook.addAfterResponseCallBack(successFn, errorFn);
2216
+ return this.context.HttpxResponseHook.add(successFn, errorFn);
2182
2217
  },
2183
2218
  /**
2184
2219
  * 移除拦截器
2185
2220
  * @param id 通过use返回的id
2186
2221
  */
2187
2222
  eject(id) {
2188
- return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
2223
+ return this.context.HttpxResponseHook.delete(id);
2224
+ },
2225
+ /**
2226
+ * 移除所有拦截器
2227
+ */
2228
+ ejectAll() {
2229
+ this.context.HttpxResponseHook.clearAll();
2189
2230
  },
2190
2231
  },
2191
2232
  };