@whitesev/utils 1.4.6 → 1.4.7

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
@@ -1423,17 +1423,147 @@ class Hooks {
1423
1423
  }
1424
1424
  }
1425
1425
 
1426
+ const GenerateUUID = () => {
1427
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
1428
+ var r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8;
1429
+ return v.toString(16);
1430
+ });
1431
+ };
1426
1432
  class Httpx {
1427
1433
  GM_Api = {
1428
1434
  xmlHttpRequest: null,
1429
1435
  };
1430
1436
  HttpxRequestHook = {
1437
+ /**
1438
+ * @private
1439
+ */
1440
+ $config: {
1441
+ configList: [],
1442
+ },
1431
1443
  /**
1432
1444
  * 发送请求前的回调
1433
1445
  * 如果返回false则阻止本次返回
1434
1446
  * @param details 当前的请求配置
1447
+ * @private
1448
+ */
1449
+ beforeRequestCallBack(details) {
1450
+ for (let index = 0; index < this.$config.configList.length; index++) {
1451
+ let item = this.$config.configList[index];
1452
+ if (typeof item.fn === "function") {
1453
+ let result = item.fn(details);
1454
+ if (result == null) {
1455
+ return;
1456
+ }
1457
+ }
1458
+ }
1459
+ return details;
1460
+ },
1461
+ /**
1462
+ * 添加请求前的回调处理配置
1463
+ */
1464
+ addBeforeRequestCallBack(fn) {
1465
+ if (typeof fn === "function") {
1466
+ let uuid = GenerateUUID();
1467
+ this.$config.configList.push({
1468
+ id: uuid,
1469
+ fn: fn,
1470
+ });
1471
+ return uuid;
1472
+ }
1473
+ else {
1474
+ console.warn("HttpxRequestHook.addBeforeRequestCallBack: fn is not a function");
1475
+ }
1476
+ },
1477
+ /**
1478
+ * 删除请求前的回调处理配置
1479
+ * @param id
1480
+ */
1481
+ deleteBeforeRequestCallBack(id) {
1482
+ if (typeof id === "string") {
1483
+ let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1484
+ if (findIndex !== -1) {
1485
+ this.$config.configList.splice(findIndex, 1);
1486
+ return true;
1487
+ }
1488
+ }
1489
+ return false;
1490
+ },
1491
+ /**
1492
+ * 清空设置的请求前的回调处理配置
1493
+ */
1494
+ clearBeforeRequestCallBack() {
1495
+ this.$config.configList = [];
1496
+ },
1497
+ };
1498
+ HttpxResponseHook = {
1499
+ /**
1500
+ * @private
1501
+ */
1502
+ $config: {
1503
+ configList: [],
1504
+ },
1505
+ /**
1506
+ * 成功的回调
1507
+ * @param response
1508
+ */
1509
+ successResponseCallBack(response) {
1510
+ for (let index = 0; index < this.$config.configList.length; index++) {
1511
+ let item = this.$config.configList[index];
1512
+ if (typeof item.successFn === "function") {
1513
+ if (item.successFn(response) == null) {
1514
+ return;
1515
+ }
1516
+ }
1517
+ }
1518
+ return response;
1519
+ },
1520
+ /**
1521
+ * 失败的回调
1522
+ * @param response
1523
+ */
1524
+ errorResponseCallBack(response) {
1525
+ for (let index = 0; index < this.$config.configList.length; index++) {
1526
+ let item = this.$config.configList[index];
1527
+ if (typeof item.errorFn === "function") {
1528
+ if (item.errorFn(response) == null) {
1529
+ return;
1530
+ }
1531
+ }
1532
+ }
1533
+ return response;
1534
+ },
1535
+ /**
1536
+ * 添加请求前的回调处理配置
1435
1537
  */
1436
- beforeRequestCallBack(details) { },
1538
+ addAfterResponseCallBack(successFn, errorFn) {
1539
+ let id = GenerateUUID();
1540
+ this.$config.configList.push({
1541
+ id: id,
1542
+ successFn: successFn,
1543
+ errorFn: errorFn,
1544
+ });
1545
+ return id;
1546
+ },
1547
+ /**
1548
+ * 删除请求前的回调处理配置
1549
+ * @param id
1550
+ */
1551
+ deleteAfterResponseCallBack(id) {
1552
+ if (typeof id === "string") {
1553
+ let findIndex = this.$config.configList.findIndex((item) => item.id === id);
1554
+ if (findIndex !== -1) {
1555
+ this.$config.configList.splice(findIndex, 1);
1556
+ return true;
1557
+ }
1558
+ }
1559
+ return false;
1560
+ },
1561
+ /**
1562
+ * 清空设置的请求前的回调处理配置
1563
+ */
1564
+ clearAfterResponseCallBack() {
1565
+ this.$config.configList = [];
1566
+ },
1437
1567
  };
1438
1568
  HttpxRequestDetails = {
1439
1569
  context: this,
@@ -1825,7 +1955,7 @@ class Httpx {
1825
1955
  if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
1826
1956
  "function") {
1827
1957
  let hookResult = this.context.HttpxRequestHook.beforeRequestCallBack(details);
1828
- if (typeof hookResult === "boolean" && !hookResult) {
1958
+ if (hookResult == null) {
1829
1959
  return;
1830
1960
  }
1831
1961
  }
@@ -1993,6 +2123,8 @@ class Httpx {
1993
2123
  if (typeof __xmlHttpRequest__ !== "function") {
1994
2124
  console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
1995
2125
  }
2126
+ this.interceptors.request = this;
2127
+ this.interceptors.response = this;
1996
2128
  this.GM_Api.xmlHttpRequest = __xmlHttpRequest__;
1997
2129
  }
1998
2130
  /**
@@ -2005,6 +2137,58 @@ class Httpx {
2005
2137
  }
2006
2138
  this.#defaultDetails = utils.assign(this.#defaultDetails, details);
2007
2139
  }
2140
+ /**
2141
+ * 拦截器
2142
+ */
2143
+ interceptors = {
2144
+ /**
2145
+ * 请求拦截器
2146
+ */
2147
+ request: {
2148
+ context: null,
2149
+ /**
2150
+ * 添加拦截器
2151
+ * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2152
+ */
2153
+ use(fn) {
2154
+ return this.context.HttpxRequestHook.addBeforeRequestCallBack(fn);
2155
+ },
2156
+ /**
2157
+ * 移除拦截器
2158
+ * @param id 通过use返回的id
2159
+ */
2160
+ eject(id) {
2161
+ return this.context.HttpxRequestHook.deleteBeforeRequestCallBack(id);
2162
+ },
2163
+ },
2164
+ /**
2165
+ * 响应拦截器
2166
+ */
2167
+ response: {
2168
+ context: null,
2169
+ /**
2170
+ * 添加拦截器
2171
+ * @param successFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
2172
+ * + 2xx 范围内的状态码都会触发该函数
2173
+ * @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
2174
+ * + 超出 2xx 范围的状态码都会触发该函数
2175
+ */
2176
+ use(successFn, errorFn) {
2177
+ if (typeof successFn !== "function" && typeof errorFn !== "function") {
2178
+ console.warn("[Httpx] 请传入拦截器函数");
2179
+ return;
2180
+ }
2181
+ return this.context.HttpxResponseHook.addAfterResponseCallBack(successFn, errorFn);
2182
+ },
2183
+ /**
2184
+ * 移除拦截器
2185
+ * @param id 通过use返回的id
2186
+ */
2187
+ eject(id) {
2188
+ return this.context.HttpxResponseHook.deleteAfterResponseCallBack(id);
2189
+ },
2190
+ },
2191
+ };
2008
2192
  /**
2009
2193
  * 修改xmlHttpRequest
2010
2194
  * @param httpRequest 网络请求函数