@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.umd.js CHANGED
@@ -1467,7 +1467,7 @@
1467
1467
  /**
1468
1468
  * 添加请求前的回调处理配置
1469
1469
  */
1470
- addBeforeRequestCallBack(fn) {
1470
+ add(fn) {
1471
1471
  if (typeof fn === "function") {
1472
1472
  let uuid = GenerateUUID();
1473
1473
  this.$config.configList.push({
@@ -1484,7 +1484,7 @@
1484
1484
  * 删除请求前的回调处理配置
1485
1485
  * @param id
1486
1486
  */
1487
- deleteBeforeRequestCallBack(id) {
1487
+ delete(id) {
1488
1488
  if (typeof id === "string") {
1489
1489
  let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1490
1490
  if (findIndex !== -1) {
@@ -1497,7 +1497,7 @@
1497
1497
  /**
1498
1498
  * 清空设置的请求前的回调处理配置
1499
1499
  */
1500
- clearBeforeRequestCallBack() {
1500
+ clearAll() {
1501
1501
  this.$config.configList = [];
1502
1502
  },
1503
1503
  };
@@ -1527,21 +1527,21 @@
1527
1527
  * 失败的回调
1528
1528
  * @param response
1529
1529
  */
1530
- errorResponseCallBack(response) {
1530
+ errorResponseCallBack(data) {
1531
1531
  for (let index = 0; index < this.$config.configList.length; index++) {
1532
1532
  let item = this.$config.configList[index];
1533
1533
  if (typeof item.errorFn === "function") {
1534
- if (item.errorFn(response) == null) {
1534
+ if (item.errorFn(data) == null) {
1535
1535
  return;
1536
1536
  }
1537
1537
  }
1538
1538
  }
1539
- return response;
1539
+ return data;
1540
1540
  },
1541
1541
  /**
1542
1542
  * 添加请求前的回调处理配置
1543
1543
  */
1544
- addAfterResponseCallBack(successFn, errorFn) {
1544
+ add(successFn, errorFn) {
1545
1545
  let id = GenerateUUID();
1546
1546
  this.$config.configList.push({
1547
1547
  id: id,
@@ -1554,7 +1554,7 @@
1554
1554
  * 删除请求前的回调处理配置
1555
1555
  * @param id
1556
1556
  */
1557
- deleteAfterResponseCallBack(id) {
1557
+ delete(id) {
1558
1558
  if (typeof id === "string") {
1559
1559
  let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1560
1560
  if (findIndex !== -1) {
@@ -1567,7 +1567,7 @@
1567
1567
  /**
1568
1568
  * 清空设置的请求前的回调处理配置
1569
1569
  */
1570
- clearAfterResponseCallBack() {
1570
+ clearAll() {
1571
1571
  this.$config.configList = [];
1572
1572
  },
1573
1573
  };
@@ -1784,6 +1784,13 @@
1784
1784
  else if ("onabort" in this.context.#defaultDetails) {
1785
1785
  this.context.#defaultDetails.onabort.apply(this, argumentsList);
1786
1786
  }
1787
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1788
+ type: "onabort",
1789
+ error: new TypeError("request canceled"),
1790
+ response: null,
1791
+ }) == null) {
1792
+ return;
1793
+ }
1787
1794
  resolve({
1788
1795
  status: false,
1789
1796
  data: [...argumentsList],
@@ -1808,6 +1815,13 @@
1808
1815
  if (response.length) {
1809
1816
  response = response[0];
1810
1817
  }
1818
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1819
+ type: "onerror",
1820
+ error: new TypeError("request error"),
1821
+ response: response,
1822
+ }) == null) {
1823
+ return;
1824
+ }
1811
1825
  resolve({
1812
1826
  status: false,
1813
1827
  data: response,
@@ -1829,6 +1843,13 @@
1829
1843
  else if ("ontimeout" in this.context.#defaultDetails) {
1830
1844
  this.context.#defaultDetails.ontimeout.apply(this, argumentsList);
1831
1845
  }
1846
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1847
+ type: "ontimeout",
1848
+ error: new TypeError("request timeout"),
1849
+ response: (argumentsList || [null])[0],
1850
+ }) == null) {
1851
+ return;
1852
+ }
1832
1853
  resolve({
1833
1854
  status: false,
1834
1855
  data: [...argumentsList],
@@ -1909,6 +1930,10 @@
1909
1930
  }
1910
1931
  /* 状态码2xx都是成功的 */
1911
1932
  if (Math.floor(Response.status / 100) === 2) {
1933
+ if (this.context.HttpxResponseHook.successResponseCallBack(Response) ==
1934
+ null) {
1935
+ return;
1936
+ }
1912
1937
  resolve({
1913
1938
  status: true,
1914
1939
  data: Response,
@@ -2157,14 +2182,24 @@
2157
2182
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2158
2183
  */
2159
2184
  use(fn) {
2160
- return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
2185
+ if (typeof fn !== "function") {
2186
+ console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
2187
+ return;
2188
+ }
2189
+ return this.context.HttpxRequestHook.add(fn);
2161
2190
  },
2162
2191
  /**
2163
2192
  * 移除拦截器
2164
2193
  * @param id 通过use返回的id
2165
2194
  */
2166
2195
  eject(id) {
2167
- return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
2196
+ return this.context.HttpxRequestHook.delete(id);
2197
+ },
2198
+ /**
2199
+ * 移除所有拦截器
2200
+ */
2201
+ ejectAll() {
2202
+ this.context.HttpxRequestHook.clearAll();
2168
2203
  },
2169
2204
  },
2170
2205
  /**
@@ -2181,17 +2216,23 @@
2181
2216
  */
2182
2217
  use(successFn, errorFn) {
2183
2218
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2184
- console.warn("[Httpx] 请传入拦截器函数");
2219
+ console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
2185
2220
  return;
2186
2221
  }
2187
- return this.context.HttpxResponseHook.addAfterResponseCallBack(successFn, errorFn);
2222
+ return this.context.HttpxResponseHook.add(successFn, errorFn);
2188
2223
  },
2189
2224
  /**
2190
2225
  * 移除拦截器
2191
2226
  * @param id 通过use返回的id
2192
2227
  */
2193
2228
  eject(id) {
2194
- return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
2229
+ return this.context.HttpxResponseHook.delete(id);
2230
+ },
2231
+ /**
2232
+ * 移除所有拦截器
2233
+ */
2234
+ ejectAll() {
2235
+ this.context.HttpxResponseHook.clearAll();
2195
2236
  },
2196
2237
  },
2197
2238
  };