@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.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
  };
@@ -1808,6 +1808,13 @@
1808
1808
  if (response.length) {
1809
1809
  response = response[0];
1810
1810
  }
1811
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1812
+ type: "onerror",
1813
+ error: new TypeError("request error"),
1814
+ response: response,
1815
+ }) == null) {
1816
+ return;
1817
+ }
1811
1818
  resolve({
1812
1819
  status: false,
1813
1820
  data: response,
@@ -1829,6 +1836,13 @@
1829
1836
  else if ("ontimeout" in this.context.#defaultDetails) {
1830
1837
  this.context.#defaultDetails.ontimeout.apply(this, argumentsList);
1831
1838
  }
1839
+ if (this.context.HttpxResponseHook.errorResponseCallBack({
1840
+ type: "ontimeout",
1841
+ error: new TypeError("request timeout"),
1842
+ response: (argumentsList || [null])[0],
1843
+ }) == null) {
1844
+ return;
1845
+ }
1832
1846
  resolve({
1833
1847
  status: false,
1834
1848
  data: [...argumentsList],
@@ -1909,6 +1923,10 @@
1909
1923
  }
1910
1924
  /* 状态码2xx都是成功的 */
1911
1925
  if (Math.floor(Response.status / 100) === 2) {
1926
+ if (this.context.HttpxResponseHook.successResponseCallBack(Response) ==
1927
+ null) {
1928
+ return;
1929
+ }
1912
1930
  resolve({
1913
1931
  status: true,
1914
1932
  data: Response,
@@ -2129,8 +2147,8 @@
2129
2147
  if (typeof __xmlHttpRequest__ !== "function") {
2130
2148
  console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
2131
2149
  }
2132
- this.interceptors.request = this;
2133
- this.interceptors.response = this;
2150
+ this.interceptors.request.context = this;
2151
+ this.interceptors.response.context = this;
2134
2152
  this.GM_Api.xmlHttpRequest = __xmlHttpRequest__;
2135
2153
  }
2136
2154
  /**
@@ -2157,14 +2175,24 @@
2157
2175
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2158
2176
  */
2159
2177
  use(fn) {
2160
- return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
2178
+ if (typeof fn !== "function") {
2179
+ console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
2180
+ return;
2181
+ }
2182
+ return this.context.HttpxRequestHook.add(fn);
2161
2183
  },
2162
2184
  /**
2163
2185
  * 移除拦截器
2164
2186
  * @param id 通过use返回的id
2165
2187
  */
2166
2188
  eject(id) {
2167
- return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
2189
+ return this.context.HttpxRequestHook.delete(id);
2190
+ },
2191
+ /**
2192
+ * 移除所有拦截器
2193
+ */
2194
+ ejectAll() {
2195
+ this.context.HttpxRequestHook.clearAll();
2168
2196
  },
2169
2197
  },
2170
2198
  /**
@@ -2181,17 +2209,23 @@
2181
2209
  */
2182
2210
  use(successFn, errorFn) {
2183
2211
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2184
- console.warn("[Httpx] 请传入拦截器函数");
2212
+ console.warn("[Httpx-interceptors-response] 请传入拦截器函数");
2185
2213
  return;
2186
2214
  }
2187
- return this.context.HttpxResponseHook.addAfterResponseCallBack(successFn, errorFn);
2215
+ return this.context.HttpxResponseHook.add(successFn, errorFn);
2188
2216
  },
2189
2217
  /**
2190
2218
  * 移除拦截器
2191
2219
  * @param id 通过use返回的id
2192
2220
  */
2193
2221
  eject(id) {
2194
- return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
2222
+ return this.context.HttpxResponseHook.delete(id);
2223
+ },
2224
+ /**
2225
+ * 移除所有拦截器
2226
+ */
2227
+ ejectAll() {
2228
+ this.context.HttpxResponseHook.clearAll();
2195
2229
  },
2196
2230
  },
2197
2231
  };