@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.
@@ -1464,7 +1464,7 @@ var Utils = (function () {
1464
1464
  /**
1465
1465
  * 添加请求前的回调处理配置
1466
1466
  */
1467
- addBeforeRequestCallBack(fn) {
1467
+ add(fn) {
1468
1468
  if (typeof fn === "function") {
1469
1469
  let uuid = GenerateUUID();
1470
1470
  this.$config.configList.push({
@@ -1481,7 +1481,7 @@ var Utils = (function () {
1481
1481
  * 删除请求前的回调处理配置
1482
1482
  * @param id
1483
1483
  */
1484
- deleteBeforeRequestCallBack(id) {
1484
+ delete(id) {
1485
1485
  if (typeof id === "string") {
1486
1486
  let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1487
1487
  if (findIndex !== -1) {
@@ -1494,7 +1494,7 @@ var Utils = (function () {
1494
1494
  /**
1495
1495
  * 清空设置的请求前的回调处理配置
1496
1496
  */
1497
- clearBeforeRequestCallBack() {
1497
+ clearAll() {
1498
1498
  this.$config.configList = [];
1499
1499
  },
1500
1500
  };
@@ -1524,21 +1524,21 @@ var Utils = (function () {
1524
1524
  * 失败的回调
1525
1525
  * @param response
1526
1526
  */
1527
- errorResponseCallBack(response) {
1527
+ errorResponseCallBack(data) {
1528
1528
  for (let index = 0; index < this.$config.configList.length; index++) {
1529
1529
  let item = this.$config.configList[index];
1530
1530
  if (typeof item.errorFn === "function") {
1531
- if (item.errorFn(response) == null) {
1531
+ if (item.errorFn(data) == null) {
1532
1532
  return;
1533
1533
  }
1534
1534
  }
1535
1535
  }
1536
- return response;
1536
+ return data;
1537
1537
  },
1538
1538
  /**
1539
1539
  * 添加请求前的回调处理配置
1540
1540
  */
1541
- addAfterResponseCallBack(successFn, errorFn) {
1541
+ add(successFn, errorFn) {
1542
1542
  let id = GenerateUUID();
1543
1543
  this.$config.configList.push({
1544
1544
  id: id,
@@ -1551,7 +1551,7 @@ var Utils = (function () {
1551
1551
  * 删除请求前的回调处理配置
1552
1552
  * @param id
1553
1553
  */
1554
- deleteAfterResponseCallBack(id) {
1554
+ delete(id) {
1555
1555
  if (typeof id === "string") {
1556
1556
  let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1557
1557
  if (findIndex !== -1) {
@@ -1564,7 +1564,7 @@ var Utils = (function () {
1564
1564
  /**
1565
1565
  * 清空设置的请求前的回调处理配置
1566
1566
  */
1567
- clearAfterResponseCallBack() {
1567
+ clearAll() {
1568
1568
  this.$config.configList = [];
1569
1569
  },
1570
1570
  };
@@ -1781,6 +1781,13 @@ var Utils = (function () {
1781
1781
  else if ("onabort" in this.context.#defaultDetails) {
1782
1782
  this.context.#defaultDetails.onabort.apply(this, argumentsList);
1783
1783
  }
1784
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1785
+ type: "onabort",
1786
+ error: new TypeError("request canceled"),
1787
+ response: null,
1788
+ }) == null) {
1789
+ return;
1790
+ }
1784
1791
  resolve({
1785
1792
  status: false,
1786
1793
  data: [...argumentsList],
@@ -1805,6 +1812,13 @@ var Utils = (function () {
1805
1812
  if (response.length) {
1806
1813
  response = response[0];
1807
1814
  }
1815
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1816
+ type: "onerror",
1817
+ error: new TypeError("request error"),
1818
+ response: response,
1819
+ }) == null) {
1820
+ return;
1821
+ }
1808
1822
  resolve({
1809
1823
  status: false,
1810
1824
  data: response,
@@ -1826,6 +1840,13 @@ var Utils = (function () {
1826
1840
  else if ("ontimeout" in this.context.#defaultDetails) {
1827
1841
  this.context.#defaultDetails.ontimeout.apply(this, argumentsList);
1828
1842
  }
1843
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1844
+ type: "ontimeout",
1845
+ error: new TypeError("request timeout"),
1846
+ response: (argumentsList || [null])[0],
1847
+ }) == null) {
1848
+ return;
1849
+ }
1829
1850
  resolve({
1830
1851
  status: false,
1831
1852
  data: [...argumentsList],
@@ -1906,6 +1927,10 @@ var Utils = (function () {
1906
1927
  }
1907
1928
  /* 状态码2xx都是成功的 */
1908
1929
  if (Math.floor(Response.status / 100) === 2) {
1930
+ if (this.context.HttpxResponseHook.successResponseCallBack(Response) ==
1931
+ null) {
1932
+ return;
1933
+ }
1909
1934
  resolve({
1910
1935
  status: true,
1911
1936
  data: Response,
@@ -2154,14 +2179,24 @@ var Utils = (function () {
2154
2179
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2155
2180
  */
2156
2181
  use(fn) {
2157
- return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
2182
+ if (typeof fn !== "function") {
2183
+ console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
2184
+ return;
2185
+ }
2186
+ return this.context.HttpxRequestHook.add(fn);
2158
2187
  },
2159
2188
  /**
2160
2189
  * 移除拦截器
2161
2190
  * @param id 通过use返回的id
2162
2191
  */
2163
2192
  eject(id) {
2164
- return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
2193
+ return this.context.HttpxRequestHook.delete(id);
2194
+ },
2195
+ /**
2196
+ * 移除所有拦截器
2197
+ */
2198
+ ejectAll() {
2199
+ this.context.HttpxRequestHook.clearAll();
2165
2200
  },
2166
2201
  },
2167
2202
  /**
@@ -2178,17 +2213,23 @@ var Utils = (function () {
2178
2213
  */
2179
2214
  use(successFn, errorFn) {
2180
2215
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2181
- console.warn("[Httpx] 请传入拦截器函数");
2216
+ console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
2182
2217
  return;
2183
2218
  }
2184
- return this.context.HttpxResponseHook.addAfterResponseCallBack(successFn, errorFn);
2219
+ return this.context.HttpxResponseHook.add(successFn, errorFn);
2185
2220
  },
2186
2221
  /**
2187
2222
  * 移除拦截器
2188
2223
  * @param id 通过use返回的id
2189
2224
  */
2190
2225
  eject(id) {
2191
- return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
2226
+ return this.context.HttpxResponseHook.delete(id);
2227
+ },
2228
+ /**
2229
+ * 移除所有拦截器
2230
+ */
2231
+ ejectAll() {
2232
+ this.context.HttpxResponseHook.clearAll();
2192
2233
  },
2193
2234
  },
2194
2235
  };