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