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