@whitesev/utils 1.5.8 → 1.6.0

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.cjs.js CHANGED
@@ -1532,12 +1532,21 @@ class Hooks {
1532
1532
  }
1533
1533
  }
1534
1534
 
1535
- const GenerateUUID = () => {
1536
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
1537
- var r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8;
1538
- return v.toString(16);
1539
- });
1535
+ /**
1536
+ * 生成uuid
1537
+ */
1538
+ const GenerateUUID = function () {
1539
+ if (typeof UtilsCore.globalThis?.crypto?.randomUUID === "function") {
1540
+ return UtilsCore.globalThis.crypto.randomUUID();
1541
+ }
1542
+ else {
1543
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
1544
+ var randomValue = (Math.random() * 16) | 0, randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
1545
+ return randomCharValue.toString(16);
1546
+ });
1547
+ }
1540
1548
  };
1549
+
1541
1550
  class Httpx {
1542
1551
  GM_Api = {
1543
1552
  xmlHttpRequest: null,
@@ -1556,6 +1565,12 @@ class Httpx {
1556
1565
  * @private
1557
1566
  */
1558
1567
  beforeRequestCallBack(details) {
1568
+ if (!details.allowInterceptConfig) {
1569
+ return details;
1570
+ }
1571
+ if (!details.allowInterceptConfig.beforeRequest) {
1572
+ return details;
1573
+ }
1559
1574
  for (let index = 0; index < this.$config.configList.length; index++) {
1560
1575
  let item = this.$config.configList[index];
1561
1576
  if (typeof item.fn === "function") {
@@ -1617,6 +1632,12 @@ class Httpx {
1617
1632
  * @param details 请求的配置
1618
1633
  */
1619
1634
  successResponseCallBack(response, details) {
1635
+ if (!details.allowInterceptConfig) {
1636
+ return details;
1637
+ }
1638
+ if (!details.allowInterceptConfig.afterResponseSuccess) {
1639
+ return details;
1640
+ }
1620
1641
  for (let index = 0; index < this.$config.configList.length; index++) {
1621
1642
  let item = this.$config.configList[index];
1622
1643
  if (typeof item.successFn === "function") {
@@ -1632,6 +1653,12 @@ class Httpx {
1632
1653
  * @param data 配置
1633
1654
  */
1634
1655
  errorResponseCallBack(data) {
1656
+ if (!data.details.allowInterceptConfig) {
1657
+ return data;
1658
+ }
1659
+ if (!data.details.allowInterceptConfig.afterResponseError) {
1660
+ return data;
1661
+ }
1635
1662
  for (let index = 0; index < this.$config.configList.length; index++) {
1636
1663
  let item = this.$config.configList[index];
1637
1664
  if (typeof item.errorFn === "function") {
@@ -2243,6 +2270,11 @@ class Httpx {
2243
2270
  anonymous: void 0,
2244
2271
  fetch: void 0,
2245
2272
  fetchInit: void 0,
2273
+ allowInterceptConfig: {
2274
+ beforeRequest: true,
2275
+ afterResponseSuccess: true,
2276
+ afterResponseError: true,
2277
+ },
2246
2278
  user: void 0,
2247
2279
  password: void 0,
2248
2280
  onabort() { },
@@ -3431,13 +3463,20 @@ class Utils {
3431
3463
  return source;
3432
3464
  }
3433
3465
  }
3466
+ if (source == null) {
3467
+ return target;
3468
+ }
3469
+ if (target == null) {
3470
+ target = {};
3471
+ }
3434
3472
  if (isAdd) {
3435
3473
  for (const sourceKeyName in source) {
3436
3474
  const targetKeyName = sourceKeyName;
3437
3475
  let targetValue = target[targetKeyName];
3438
3476
  let sourceValue = source[sourceKeyName];
3439
- if (sourceKeyName in target &&
3440
- typeof sourceValue === "object" &&
3477
+ if (typeof sourceValue === "object" &&
3478
+ sourceValue != null &&
3479
+ sourceKeyName in target &&
3441
3480
  !UtilsContext.isDOM(sourceValue)) {
3442
3481
  /* 源端的值是object类型,且不是元素节点 */
3443
3482
  target[sourceKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
@@ -3452,6 +3491,7 @@ class Utils {
3452
3491
  let targetValue = target[targetKeyName];
3453
3492
  let sourceValue = source[targetKeyName];
3454
3493
  if (typeof sourceValue === "object" &&
3494
+ sourceValue != null &&
3455
3495
  !UtilsContext.isDOM(sourceValue) &&
3456
3496
  Object.keys(sourceValue).length) {
3457
3497
  /* 源端的值是object类型,且不是元素节点 */
@@ -6356,17 +6396,7 @@ class Utils {
6356
6396
  * @example
6357
6397
  * Utils.generateUUID()
6358
6398
  */
6359
- generateUUID() {
6360
- if (typeof globalThis?.crypto?.randomUUID === "function") {
6361
- return globalThis.crypto.randomUUID();
6362
- }
6363
- else {
6364
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
6365
- var randomValue = (Math.random() * 16) | 0, randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
6366
- return randomCharValue.toString(16);
6367
- });
6368
- }
6369
- }
6399
+ generateUUID = GenerateUUID;
6370
6400
  }
6371
6401
  let utils = new Utils();
6372
6402