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