@etsoo/shared 1.2.22 → 1.2.24

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.
@@ -326,3 +326,15 @@ test('Tests for getInputValue', () => {
326
326
  expect(result.getDate()).toBe(21);
327
327
  }
328
328
  });
329
+
330
+ test('Tests for setupLogging', () => {
331
+ // Arrange
332
+ const action = jest.fn();
333
+ DomUtils.setupLogging(action, true);
334
+
335
+ // Act
336
+ console.error('Test');
337
+
338
+ // Assert
339
+ expect(action).toHaveBeenCalledTimes(1);
340
+ });
@@ -1,5 +1,6 @@
1
1
  /// <reference lib="dom" />
2
2
  import { DataTypes } from './DataTypes';
3
+ import { ErrorData, ErrorType } from './types/ErrorData';
3
4
  import { FormDataFieldValue, IFormData } from './types/FormData';
4
5
  /**
5
6
  * Dom Utilities
@@ -149,6 +150,12 @@ export declare namespace DomUtils {
149
150
  * @param container Container, limits the element range
150
151
  */
151
152
  function setFocus(name: string | object, container?: HTMLElement): void;
153
+ /**
154
+ * Setup logging
155
+ * @param action Logging action
156
+ * @param preventDefault Is prevent default action
157
+ */
158
+ function setupLogging(action: (data: ErrorData) => void | Promise<void>, preventDefault?: ((type: ErrorType) => boolean) | boolean): void;
152
159
  /**
153
160
  * Verify file system permission
154
161
  * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
@@ -4,6 +4,7 @@ exports.DomUtils = void 0;
4
4
  /// <reference lib="dom" />
5
5
  const DataTypes_1 = require("./DataTypes");
6
6
  const DateUtils_1 = require("./DateUtils");
7
+ const Utils_1 = require("./Utils");
7
8
  if (typeof navigator === 'undefined') {
8
9
  // Test mock only
9
10
  global.navigator = { language: 'en-US' };
@@ -498,6 +499,93 @@ var DomUtils;
498
499
  element.focus();
499
500
  }
500
501
  DomUtils.setFocus = setFocus;
502
+ /**
503
+ * Setup logging
504
+ * @param action Logging action
505
+ * @param preventDefault Is prevent default action
506
+ */
507
+ function setupLogging(action, preventDefault) {
508
+ var _a, _b;
509
+ // Avoid multiple setup, if there is already a handler, please set "globalThis.onunhandledrejection = null" first
510
+ if (globalThis.onunhandledrejection)
511
+ return;
512
+ const errorPD = (_a = Utils_1.Utils.getResult(preventDefault, 'error')) !== null && _a !== void 0 ? _a : true;
513
+ globalThis.onerror = (message, source, lineNo, colNo, error) => {
514
+ var _a;
515
+ // Default source
516
+ source || (source = globalThis.location.href);
517
+ let data;
518
+ if (typeof message === 'string') {
519
+ data = {
520
+ type: 'error',
521
+ message, // Share the same message with error
522
+ source,
523
+ lineNo,
524
+ colNo,
525
+ stack: error === null || error === void 0 ? void 0 : error.stack
526
+ };
527
+ }
528
+ else {
529
+ data = {
530
+ type: 'error',
531
+ eventType: message.type,
532
+ message: (_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : `${message.currentTarget} event error`,
533
+ source,
534
+ lineNo,
535
+ colNo,
536
+ stack: error === null || error === void 0 ? void 0 : error.stack
537
+ };
538
+ }
539
+ action(data);
540
+ // Return true to suppress error alert
541
+ return errorPD;
542
+ };
543
+ const rejectionPD = (_b = Utils_1.Utils.getResult(preventDefault, 'error')) !== null && _b !== void 0 ? _b : true;
544
+ globalThis.onunhandledrejection = (event) => {
545
+ if (rejectionPD)
546
+ event.preventDefault();
547
+ const reason = event.reason;
548
+ const data = {
549
+ type: 'unhandledrejection',
550
+ eventType: event.type,
551
+ message: typeof reason === 'string'
552
+ ? reason
553
+ : JSON.stringify(reason),
554
+ source: globalThis.location.href
555
+ };
556
+ action(data);
557
+ };
558
+ const localConsole = (type, orgin) => {
559
+ var _a;
560
+ const consolePD = (_a = Utils_1.Utils.getResult(preventDefault, type)) !== null && _a !== void 0 ? _a : false;
561
+ return (...args) => {
562
+ // Keep original action
563
+ if (!consolePD)
564
+ orgin(...args);
565
+ const [first, ...rest] = args;
566
+ let message;
567
+ if (typeof first === 'string') {
568
+ message = first;
569
+ }
570
+ else {
571
+ message = JSON.stringify(first);
572
+ }
573
+ const stack = rest.length > 0
574
+ ? rest.map((item) => JSON.stringify(item)).join(', ')
575
+ : undefined;
576
+ const data = {
577
+ type,
578
+ message,
579
+ source: globalThis.location.href,
580
+ stack
581
+ };
582
+ action(data);
583
+ };
584
+ };
585
+ globalThis.console.warn = localConsole('consoleWarn', globalThis.console.warn);
586
+ globalThis.console.error = localConsole('consoleError', globalThis.console.error);
587
+ }
588
+ DomUtils.setupLogging = setupLogging;
501
589
  /**
502
590
  * Verify file system permission
503
591
  * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
@@ -0,0 +1,10 @@
1
+ export type ErrorType = 'error' | 'unhandledrejection' | 'consoleWarn' | 'consoleError';
2
+ export type ErrorData = {
3
+ type: ErrorType;
4
+ eventType?: string;
5
+ message: string;
6
+ source?: string;
7
+ lineNo?: number;
8
+ colNo?: number;
9
+ stack?: string;
10
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +1,6 @@
1
1
  /// <reference lib="dom" />
2
2
  import { DataTypes } from './DataTypes';
3
+ import { ErrorData, ErrorType } from './types/ErrorData';
3
4
  import { FormDataFieldValue, IFormData } from './types/FormData';
4
5
  /**
5
6
  * Dom Utilities
@@ -149,6 +150,12 @@ export declare namespace DomUtils {
149
150
  * @param container Container, limits the element range
150
151
  */
151
152
  function setFocus(name: string | object, container?: HTMLElement): void;
153
+ /**
154
+ * Setup logging
155
+ * @param action Logging action
156
+ * @param preventDefault Is prevent default action
157
+ */
158
+ function setupLogging(action: (data: ErrorData) => void | Promise<void>, preventDefault?: ((type: ErrorType) => boolean) | boolean): void;
152
159
  /**
153
160
  * Verify file system permission
154
161
  * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
@@ -1,6 +1,7 @@
1
1
  /// <reference lib="dom" />
2
2
  import { DataTypes } from './DataTypes';
3
3
  import { DateUtils } from './DateUtils';
4
+ import { Utils } from './Utils';
4
5
  if (typeof navigator === 'undefined') {
5
6
  // Test mock only
6
7
  global.navigator = { language: 'en-US' };
@@ -495,6 +496,93 @@ export var DomUtils;
495
496
  element.focus();
496
497
  }
497
498
  DomUtils.setFocus = setFocus;
499
+ /**
500
+ * Setup logging
501
+ * @param action Logging action
502
+ * @param preventDefault Is prevent default action
503
+ */
504
+ function setupLogging(action, preventDefault) {
505
+ var _a, _b;
506
+ // Avoid multiple setup, if there is already a handler, please set "globalThis.onunhandledrejection = null" first
507
+ if (globalThis.onunhandledrejection)
508
+ return;
509
+ const errorPD = (_a = Utils.getResult(preventDefault, 'error')) !== null && _a !== void 0 ? _a : true;
510
+ globalThis.onerror = (message, source, lineNo, colNo, error) => {
511
+ var _a;
512
+ // Default source
513
+ source || (source = globalThis.location.href);
514
+ let data;
515
+ if (typeof message === 'string') {
516
+ data = {
517
+ type: 'error',
518
+ message, // Share the same message with error
519
+ source,
520
+ lineNo,
521
+ colNo,
522
+ stack: error === null || error === void 0 ? void 0 : error.stack
523
+ };
524
+ }
525
+ else {
526
+ data = {
527
+ type: 'error',
528
+ eventType: message.type,
529
+ message: (_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : `${message.currentTarget} event error`,
530
+ source,
531
+ lineNo,
532
+ colNo,
533
+ stack: error === null || error === void 0 ? void 0 : error.stack
534
+ };
535
+ }
536
+ action(data);
537
+ // Return true to suppress error alert
538
+ return errorPD;
539
+ };
540
+ const rejectionPD = (_b = Utils.getResult(preventDefault, 'error')) !== null && _b !== void 0 ? _b : true;
541
+ globalThis.onunhandledrejection = (event) => {
542
+ if (rejectionPD)
543
+ event.preventDefault();
544
+ const reason = event.reason;
545
+ const data = {
546
+ type: 'unhandledrejection',
547
+ eventType: event.type,
548
+ message: typeof reason === 'string'
549
+ ? reason
550
+ : JSON.stringify(reason),
551
+ source: globalThis.location.href
552
+ };
553
+ action(data);
554
+ };
555
+ const localConsole = (type, orgin) => {
556
+ var _a;
557
+ const consolePD = (_a = Utils.getResult(preventDefault, type)) !== null && _a !== void 0 ? _a : false;
558
+ return (...args) => {
559
+ // Keep original action
560
+ if (!consolePD)
561
+ orgin(...args);
562
+ const [first, ...rest] = args;
563
+ let message;
564
+ if (typeof first === 'string') {
565
+ message = first;
566
+ }
567
+ else {
568
+ message = JSON.stringify(first);
569
+ }
570
+ const stack = rest.length > 0
571
+ ? rest.map((item) => JSON.stringify(item)).join(', ')
572
+ : undefined;
573
+ const data = {
574
+ type,
575
+ message,
576
+ source: globalThis.location.href,
577
+ stack
578
+ };
579
+ action(data);
580
+ };
581
+ };
582
+ globalThis.console.warn = localConsole('consoleWarn', globalThis.console.warn);
583
+ globalThis.console.error = localConsole('consoleError', globalThis.console.error);
584
+ }
585
+ DomUtils.setupLogging = setupLogging;
498
586
  /**
499
587
  * Verify file system permission
500
588
  * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
@@ -0,0 +1,10 @@
1
+ export type ErrorType = 'error' | 'unhandledrejection' | 'consoleWarn' | 'consoleError';
2
+ export type ErrorData = {
3
+ type: ErrorType;
4
+ eventType?: string;
5
+ message: string;
6
+ source?: string;
7
+ lineNo?: number;
8
+ colNo?: number;
9
+ stack?: string;
10
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.22",
3
+ "version": "1.2.24",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,12 +54,12 @@
54
54
  },
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "devDependencies": {
57
- "@types/jest": "^29.5.8",
57
+ "@types/jest": "^29.5.12",
58
58
  "@types/lodash.isequal": "^4.5.8",
59
59
  "jest": "^29.7.0",
60
60
  "jest-environment-jsdom": "^29.7.0",
61
- "ts-jest": "^29.1.1",
62
- "typescript": "^5.2.2"
61
+ "ts-jest": "^29.1.2",
62
+ "typescript": "^5.3.3"
63
63
  },
64
64
  "dependencies": {
65
65
  "lodash.isequal": "^4.5.0"
package/src/DomUtils.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  /// <reference lib="dom" />
2
2
  import { DataTypes } from './DataTypes';
3
3
  import { DateUtils } from './DateUtils';
4
+ import { Utils } from './Utils';
5
+ import { ErrorData, ErrorType } from './types/ErrorData';
4
6
  import { FormDataFieldValue, IFormData } from './types/FormData';
5
7
 
6
8
  if (typeof navigator === 'undefined') {
@@ -599,6 +601,114 @@ export namespace DomUtils {
599
601
  if (element != null) element.focus();
600
602
  }
601
603
 
604
+ /**
605
+ * Setup logging
606
+ * @param action Logging action
607
+ * @param preventDefault Is prevent default action
608
+ */
609
+ export function setupLogging(
610
+ action: (data: ErrorData) => void | Promise<void>,
611
+ preventDefault?: ((type: ErrorType) => boolean) | boolean
612
+ ) {
613
+ // Avoid multiple setup, if there is already a handler, please set "globalThis.onunhandledrejection = null" first
614
+ if (globalThis.onunhandledrejection) return;
615
+
616
+ const errorPD = Utils.getResult(preventDefault, 'error') ?? true;
617
+ globalThis.onerror = (message, source, lineNo, colNo, error) => {
618
+ // Default source
619
+ source ||= globalThis.location.href;
620
+ let data: ErrorData;
621
+ if (typeof message === 'string') {
622
+ data = {
623
+ type: 'error',
624
+ message, // Share the same message with error
625
+ source,
626
+ lineNo,
627
+ colNo,
628
+ stack: error?.stack
629
+ };
630
+ } else {
631
+ data = {
632
+ type: 'error',
633
+ eventType: message.type,
634
+ message:
635
+ error?.message ??
636
+ `${message.currentTarget} event error`,
637
+ source,
638
+ lineNo,
639
+ colNo,
640
+ stack: error?.stack
641
+ };
642
+ }
643
+
644
+ action(data);
645
+
646
+ // Return true to suppress error alert
647
+ return errorPD;
648
+ };
649
+
650
+ const rejectionPD = Utils.getResult(preventDefault, 'error') ?? true;
651
+ globalThis.onunhandledrejection = (event) => {
652
+ if (rejectionPD) event.preventDefault();
653
+
654
+ const reason = event.reason;
655
+ const data: ErrorData = {
656
+ type: 'unhandledrejection',
657
+ eventType: event.type,
658
+ message:
659
+ typeof reason === 'string'
660
+ ? reason
661
+ : JSON.stringify(reason),
662
+ source: globalThis.location.href
663
+ };
664
+
665
+ action(data);
666
+ };
667
+
668
+ const localConsole = (
669
+ type: 'consoleWarn' | 'consoleError',
670
+ orgin: (...args: any[]) => void
671
+ ) => {
672
+ const consolePD = Utils.getResult(preventDefault, type) ?? false;
673
+ return (...args: any[]) => {
674
+ // Keep original action
675
+ if (!consolePD) orgin(...args);
676
+
677
+ const [first, ...rest] = args;
678
+ let message: string;
679
+ if (typeof first === 'string') {
680
+ message = first;
681
+ } else {
682
+ message = JSON.stringify(first);
683
+ }
684
+
685
+ const stack =
686
+ rest.length > 0
687
+ ? rest.map((item) => JSON.stringify(item)).join(', ')
688
+ : undefined;
689
+
690
+ const data: ErrorData = {
691
+ type,
692
+ message,
693
+ source: globalThis.location.href,
694
+ stack
695
+ };
696
+
697
+ action(data);
698
+ };
699
+ };
700
+
701
+ globalThis.console.warn = localConsole(
702
+ 'consoleWarn',
703
+ globalThis.console.warn
704
+ );
705
+
706
+ globalThis.console.error = localConsole(
707
+ 'consoleError',
708
+ globalThis.console.error
709
+ );
710
+ }
711
+
602
712
  /**
603
713
  * Verify file system permission
604
714
  * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
@@ -0,0 +1,15 @@
1
+ export type ErrorType =
2
+ | 'error'
3
+ | 'unhandledrejection'
4
+ | 'consoleWarn'
5
+ | 'consoleError';
6
+
7
+ export type ErrorData = {
8
+ type: ErrorType;
9
+ eventType?: string;
10
+ message: string;
11
+ source?: string;
12
+ lineNo?: number;
13
+ colNo?: number;
14
+ stack?: string;
15
+ };