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