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