@etsoo/shared 1.2.35 → 1.2.36

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/README.md CHANGED
@@ -227,6 +227,7 @@ DOM/window related utilities
227
227
  |mergeFormData|Merge form data to primary one|
228
228
  |mergeURLSearchParams|Merge URL search parameters|
229
229
  |setFocus|Set HTML element focus by name|
230
+ |setupLogging|Setup frontend logging|
230
231
  |verifyPermission|Verify file system permission|
231
232
  |zhHans|Get simplified Chinese resources definition|
232
233
  |zhHant|Get traditional Chinese resources definition|
@@ -333,7 +333,7 @@ test('Tests for setupLogging', async () => {
333
333
  const action = jest.fn((data: ErrorData) => {
334
334
  expect(data.message).toBe('Test');
335
335
  });
336
- DomUtils.setupLogging(action, true);
336
+ DomUtils.setupLogging(action, true, globalThis.self);
337
337
 
338
338
  // Act
339
339
  console.error('Test');
@@ -151,11 +151,12 @@ export declare namespace DomUtils {
151
151
  */
152
152
  function setFocus(name: string | object, container?: HTMLElement): void;
153
153
  /**
154
- * Setup logging
154
+ * Setup frontend logging
155
155
  * @param action Logging action
156
156
  * @param preventDefault Is prevent default action
157
+ * @param window Window object
157
158
  */
158
- function setupLogging(action: (data: ErrorData) => void | Promise<void>, preventDefault?: ((type: ErrorType) => boolean) | boolean): void;
159
+ function setupLogging(action: (data: ErrorData) => void | Promise<void>, preventDefault?: ((type: ErrorType) => boolean) | boolean, window?: Window & typeof globalThis): void;
159
160
  /**
160
161
  * Verify file system permission
161
162
  * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
@@ -497,22 +497,24 @@ var DomUtils;
497
497
  }
498
498
  DomUtils.setFocus = setFocus;
499
499
  /**
500
- * Setup logging
500
+ * Setup frontend logging
501
501
  * @param action Logging action
502
502
  * @param preventDefault Is prevent default action
503
+ * @param window Window object
503
504
  */
504
- function setupLogging(action, preventDefault) {
505
- // Avoid multiple setup, if there is already a handler, please set "globalThis.onunhandledrejection = null" first
506
- if (globalThis.onunhandledrejection)
505
+ function setupLogging(action, preventDefault, window = globalThis.window) {
506
+ // Avoid multiple setup, if there is already a handler, please set "window.onunhandledrejection = null" first
507
+ if (window.onunhandledrejection)
507
508
  return;
508
- const errorPD = Utils_1.Utils.getResult(preventDefault, 'error') ?? true;
509
- globalThis.onerror = (message, source, lineNo, colNo, error) => {
509
+ const errorType = 'error';
510
+ const errorPD = Utils_1.Utils.getResult(preventDefault, errorType) ?? true;
511
+ window.onerror = (message, source, lineNo, colNo, error) => {
510
512
  // Default source
511
- source || (source = globalThis.location.href);
513
+ source || (source = window.location.href);
512
514
  let data;
513
515
  if (typeof message === 'string') {
514
516
  data = {
515
- type: 'error',
517
+ type: errorType,
516
518
  message, // Share the same message with error
517
519
  source,
518
520
  lineNo,
@@ -522,7 +524,7 @@ var DomUtils;
522
524
  }
523
525
  else {
524
526
  data = {
525
- type: 'error',
527
+ type: errorType,
526
528
  subType: message.type,
527
529
  message: error?.message ??
528
530
  `${message.currentTarget} event error`,
@@ -536,28 +538,31 @@ var DomUtils;
536
538
  // Return true to suppress error alert
537
539
  return errorPD;
538
540
  };
539
- const rejectionPD = Utils_1.Utils.getResult(preventDefault, 'error') ?? true;
540
- globalThis.onunhandledrejection = (event) => {
541
+ const rejectionType = 'unhandledrejection';
542
+ const rejectionPD = Utils_1.Utils.getResult(preventDefault, rejectionType) ?? true;
543
+ window.onunhandledrejection = (event) => {
541
544
  if (rejectionPD)
542
545
  event.preventDefault();
543
546
  const reason = event.reason;
547
+ const source = window.location.href;
544
548
  let data;
545
549
  if (reason instanceof Error) {
550
+ const { name: subType, message, stack } = reason;
546
551
  data = {
547
- type: 'unhandledrejection',
548
- subType: reason.name,
549
- message: reason.message,
550
- stack: reason.stack,
551
- source: globalThis.location.href
552
+ type: rejectionType,
553
+ subType,
554
+ message,
555
+ stack,
556
+ source
552
557
  };
553
558
  }
554
559
  else {
555
560
  data = {
556
- type: 'unhandledrejection',
561
+ type: rejectionType,
557
562
  message: typeof reason === 'string'
558
563
  ? reason
559
564
  : JSON.stringify(reason),
560
- source: globalThis.location.href
565
+ source
561
566
  };
562
567
  }
563
568
  action(data);
@@ -582,14 +587,14 @@ var DomUtils;
582
587
  const data = {
583
588
  type,
584
589
  message,
585
- source: globalThis.location.href,
590
+ source: window.location.href,
586
591
  stack
587
592
  };
588
593
  action(data);
589
594
  };
590
595
  };
591
- globalThis.console.warn = localConsole('consoleWarn', globalThis.console.warn);
592
- globalThis.console.error = localConsole('consoleError', globalThis.console.error);
596
+ window.console.warn = localConsole('consoleWarn', window.console.warn);
597
+ window.console.error = localConsole('consoleError', window.console.error);
593
598
  }
594
599
  DomUtils.setupLogging = setupLogging;
595
600
  /**
@@ -151,11 +151,12 @@ export declare namespace DomUtils {
151
151
  */
152
152
  function setFocus(name: string | object, container?: HTMLElement): void;
153
153
  /**
154
- * Setup logging
154
+ * Setup frontend logging
155
155
  * @param action Logging action
156
156
  * @param preventDefault Is prevent default action
157
+ * @param window Window object
157
158
  */
158
- function setupLogging(action: (data: ErrorData) => void | Promise<void>, preventDefault?: ((type: ErrorType) => boolean) | boolean): void;
159
+ function setupLogging(action: (data: ErrorData) => void | Promise<void>, preventDefault?: ((type: ErrorType) => boolean) | boolean, window?: Window & typeof globalThis): void;
159
160
  /**
160
161
  * Verify file system permission
161
162
  * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
@@ -494,22 +494,24 @@ export var DomUtils;
494
494
  }
495
495
  DomUtils.setFocus = setFocus;
496
496
  /**
497
- * Setup logging
497
+ * Setup frontend logging
498
498
  * @param action Logging action
499
499
  * @param preventDefault Is prevent default action
500
+ * @param window Window object
500
501
  */
501
- function setupLogging(action, preventDefault) {
502
- // Avoid multiple setup, if there is already a handler, please set "globalThis.onunhandledrejection = null" first
503
- if (globalThis.onunhandledrejection)
502
+ function setupLogging(action, preventDefault, window = globalThis.window) {
503
+ // Avoid multiple setup, if there is already a handler, please set "window.onunhandledrejection = null" first
504
+ if (window.onunhandledrejection)
504
505
  return;
505
- const errorPD = Utils.getResult(preventDefault, 'error') ?? true;
506
- globalThis.onerror = (message, source, lineNo, colNo, error) => {
506
+ const errorType = 'error';
507
+ const errorPD = Utils.getResult(preventDefault, errorType) ?? true;
508
+ window.onerror = (message, source, lineNo, colNo, error) => {
507
509
  // Default source
508
- source || (source = globalThis.location.href);
510
+ source || (source = window.location.href);
509
511
  let data;
510
512
  if (typeof message === 'string') {
511
513
  data = {
512
- type: 'error',
514
+ type: errorType,
513
515
  message, // Share the same message with error
514
516
  source,
515
517
  lineNo,
@@ -519,7 +521,7 @@ export var DomUtils;
519
521
  }
520
522
  else {
521
523
  data = {
522
- type: 'error',
524
+ type: errorType,
523
525
  subType: message.type,
524
526
  message: error?.message ??
525
527
  `${message.currentTarget} event error`,
@@ -533,28 +535,31 @@ export var DomUtils;
533
535
  // Return true to suppress error alert
534
536
  return errorPD;
535
537
  };
536
- const rejectionPD = Utils.getResult(preventDefault, 'error') ?? true;
537
- globalThis.onunhandledrejection = (event) => {
538
+ const rejectionType = 'unhandledrejection';
539
+ const rejectionPD = Utils.getResult(preventDefault, rejectionType) ?? true;
540
+ window.onunhandledrejection = (event) => {
538
541
  if (rejectionPD)
539
542
  event.preventDefault();
540
543
  const reason = event.reason;
544
+ const source = window.location.href;
541
545
  let data;
542
546
  if (reason instanceof Error) {
547
+ const { name: subType, message, stack } = reason;
543
548
  data = {
544
- type: 'unhandledrejection',
545
- subType: reason.name,
546
- message: reason.message,
547
- stack: reason.stack,
548
- source: globalThis.location.href
549
+ type: rejectionType,
550
+ subType,
551
+ message,
552
+ stack,
553
+ source
549
554
  };
550
555
  }
551
556
  else {
552
557
  data = {
553
- type: 'unhandledrejection',
558
+ type: rejectionType,
554
559
  message: typeof reason === 'string'
555
560
  ? reason
556
561
  : JSON.stringify(reason),
557
- source: globalThis.location.href
562
+ source
558
563
  };
559
564
  }
560
565
  action(data);
@@ -579,14 +584,14 @@ export var DomUtils;
579
584
  const data = {
580
585
  type,
581
586
  message,
582
- source: globalThis.location.href,
587
+ source: window.location.href,
583
588
  stack
584
589
  };
585
590
  action(data);
586
591
  };
587
592
  };
588
- globalThis.console.warn = localConsole('consoleWarn', globalThis.console.warn);
589
- globalThis.console.error = localConsole('consoleError', globalThis.console.error);
593
+ window.console.warn = localConsole('consoleWarn', window.console.warn);
594
+ window.console.error = localConsole('consoleError', window.console.error);
590
595
  }
591
596
  DomUtils.setupLogging = setupLogging;
592
597
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.35",
3
+ "version": "1.2.36",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -59,7 +59,7 @@
59
59
  "jest": "^29.7.0",
60
60
  "jest-environment-jsdom": "^29.7.0",
61
61
  "ts-jest": "^29.1.2",
62
- "typescript": "^5.3.3"
62
+ "typescript": "^5.4.2"
63
63
  },
64
64
  "dependencies": {
65
65
  "lodash.isequal": "^4.5.0"
package/src/DomUtils.ts CHANGED
@@ -602,25 +602,28 @@ export namespace DomUtils {
602
602
  }
603
603
 
604
604
  /**
605
- * Setup logging
605
+ * Setup frontend logging
606
606
  * @param action Logging action
607
607
  * @param preventDefault Is prevent default action
608
+ * @param window Window object
608
609
  */
609
610
  export function setupLogging(
610
611
  action: (data: ErrorData) => void | Promise<void>,
611
- preventDefault?: ((type: ErrorType) => boolean) | boolean
612
+ preventDefault?: ((type: ErrorType) => boolean) | boolean,
613
+ window: Window & typeof globalThis = globalThis.window
612
614
  ) {
613
- // Avoid multiple setup, if there is already a handler, please set "globalThis.onunhandledrejection = null" first
614
- if (globalThis.onunhandledrejection) return;
615
+ // Avoid multiple setup, if there is already a handler, please set "window.onunhandledrejection = null" first
616
+ if (window.onunhandledrejection) return;
615
617
 
616
- const errorPD = Utils.getResult(preventDefault, 'error') ?? true;
617
- globalThis.onerror = (message, source, lineNo, colNo, error) => {
618
+ const errorType: ErrorType = 'error';
619
+ const errorPD = Utils.getResult(preventDefault, errorType) ?? true;
620
+ window.onerror = (message, source, lineNo, colNo, error) => {
618
621
  // Default source
619
- source ||= globalThis.location.href;
622
+ source ||= window.location.href;
620
623
  let data: ErrorData;
621
624
  if (typeof message === 'string') {
622
625
  data = {
623
- type: 'error',
626
+ type: errorType,
624
627
  message, // Share the same message with error
625
628
  source,
626
629
  lineNo,
@@ -629,7 +632,7 @@ export namespace DomUtils {
629
632
  };
630
633
  } else {
631
634
  data = {
632
- type: 'error',
635
+ type: errorType,
633
636
  subType: message.type,
634
637
  message:
635
638
  error?.message ??
@@ -647,29 +650,33 @@ export namespace DomUtils {
647
650
  return errorPD;
648
651
  };
649
652
 
650
- const rejectionPD = Utils.getResult(preventDefault, 'error') ?? true;
651
- globalThis.onunhandledrejection = (event) => {
653
+ const rejectionType: ErrorType = 'unhandledrejection';
654
+ const rejectionPD =
655
+ Utils.getResult(preventDefault, rejectionType) ?? true;
656
+ window.onunhandledrejection = (event) => {
652
657
  if (rejectionPD) event.preventDefault();
653
658
 
654
659
  const reason = event.reason;
660
+ const source = window.location.href;
655
661
  let data: ErrorData;
656
662
 
657
663
  if (reason instanceof Error) {
664
+ const { name: subType, message, stack } = reason;
658
665
  data = {
659
- type: 'unhandledrejection',
660
- subType: reason.name,
661
- message: reason.message,
662
- stack: reason.stack,
663
- source: globalThis.location.href
666
+ type: rejectionType,
667
+ subType,
668
+ message,
669
+ stack,
670
+ source
664
671
  };
665
672
  } else {
666
673
  data = {
667
- type: 'unhandledrejection',
674
+ type: rejectionType,
668
675
  message:
669
676
  typeof reason === 'string'
670
677
  ? reason
671
678
  : JSON.stringify(reason),
672
- source: globalThis.location.href
679
+ source
673
680
  };
674
681
  }
675
682
 
@@ -701,7 +708,7 @@ export namespace DomUtils {
701
708
  const data: ErrorData = {
702
709
  type,
703
710
  message,
704
- source: globalThis.location.href,
711
+ source: window.location.href,
705
712
  stack
706
713
  };
707
714
 
@@ -709,14 +716,11 @@ export namespace DomUtils {
709
716
  };
710
717
  };
711
718
 
712
- globalThis.console.warn = localConsole(
713
- 'consoleWarn',
714
- globalThis.console.warn
715
- );
719
+ window.console.warn = localConsole('consoleWarn', window.console.warn);
716
720
 
717
- globalThis.console.error = localConsole(
721
+ window.console.error = localConsole(
718
722
  'consoleError',
719
- globalThis.console.error
723
+ window.console.error
720
724
  );
721
725
  }
722
726