@whitesev/utils 1.4.7 → 1.4.9

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
  };
@@ -1802,6 +1802,13 @@ class Httpx {
1802
1802
  if (response.length) {
1803
1803
  response = response[0];
1804
1804
  }
1805
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1806
+ type: "onerror",
1807
+ error: new TypeError("request error"),
1808
+ response: response,
1809
+ }) == null) {
1810
+ return;
1811
+ }
1805
1812
  resolve({
1806
1813
  status: false,
1807
1814
  data: response,
@@ -1823,6 +1830,13 @@ class Httpx {
1823
1830
  else if ("ontimeout" in this.context.#defaultDetails) {
1824
1831
  this.context.#defaultDetails.ontimeout.apply(this, argumentsList);
1825
1832
  }
1833
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1834
+ type: "ontimeout",
1835
+ error: new TypeError("request timeout"),
1836
+ response: (argumentsList || [null])[0],
1837
+ }) == null) {
1838
+ return;
1839
+ }
1826
1840
  resolve({
1827
1841
  status: false,
1828
1842
  data: [...argumentsList],
@@ -1903,6 +1917,10 @@ class Httpx {
1903
1917
  }
1904
1918
  /* 状态码2xx都是成功的 */
1905
1919
  if (Math.floor(Response.status / 100) === 2) {
1920
+ if (this.context.HttpxResponseHook.successResponseCallBack(Response) ==
1921
+ null) {
1922
+ return;
1923
+ }
1906
1924
  resolve({
1907
1925
  status: true,
1908
1926
  data: Response,
@@ -2123,8 +2141,8 @@ class Httpx {
2123
2141
  if (typeof __xmlHttpRequest__ !== "function") {
2124
2142
  console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
2125
2143
  }
2126
- this.interceptors.request = this;
2127
- this.interceptors.response = this;
2144
+ this.interceptors.request.context = this;
2145
+ this.interceptors.response.context = this;
2128
2146
  this.GM_Api.xmlHttpRequest = __xmlHttpRequest__;
2129
2147
  }
2130
2148
  /**
@@ -2151,14 +2169,24 @@ class Httpx {
2151
2169
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2152
2170
  */
2153
2171
  use(fn) {
2154
- return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
2172
+ if (typeof fn !== "function") {
2173
+ console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
2174
+ return;
2175
+ }
2176
+ return this.context.HttpxRequestHook.add(fn);
2155
2177
  },
2156
2178
  /**
2157
2179
  * 移除拦截器
2158
2180
  * @param id 通过use返回的id
2159
2181
  */
2160
2182
  eject(id) {
2161
- return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
2183
+ return this.context.HttpxRequestHook.delete(id);
2184
+ },
2185
+ /**
2186
+ * 移除所有拦截器
2187
+ */
2188
+ ejectAll() {
2189
+ this.context.HttpxRequestHook.clearAll();
2162
2190
  },
2163
2191
  },
2164
2192
  /**
@@ -2175,17 +2203,23 @@ class Httpx {
2175
2203
  */
2176
2204
  use(successFn, errorFn) {
2177
2205
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2178
- console.warn("[Httpx] 请传入拦截器函数");
2206
+ console.warn("[Httpx-interceptors-response] 请传入拦截器函数");
2179
2207
  return;
2180
2208
  }
2181
- return this.context.HttpxResponseHook.addAfterResponseCallBack(successFn, errorFn);
2209
+ return this.context.HttpxResponseHook.add(successFn, errorFn);
2182
2210
  },
2183
2211
  /**
2184
2212
  * 移除拦截器
2185
2213
  * @param id 通过use返回的id
2186
2214
  */
2187
2215
  eject(id) {
2188
- return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
2216
+ return this.context.HttpxResponseHook.delete(id);
2217
+ },
2218
+ /**
2219
+ * 移除所有拦截器
2220
+ */
2221
+ ejectAll() {
2222
+ this.context.HttpxResponseHook.clearAll();
2189
2223
  },
2190
2224
  },
2191
2225
  };