@watchupltd/browser 1.0.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/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@watchupltd/browser",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.js",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.mjs",
10
+ "require": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "build": "tsup src/index.ts --format cjs,esm --dts",
16
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch"
17
+ },
18
+ "dependencies": {
19
+ "@watchupltd/core": "file:../core"
20
+ },
21
+ "devDependencies": {
22
+ "tsup": "^7.0.0",
23
+ "typescript": "^5.0.0"
24
+ }
25
+ }
package/src/index.ts ADDED
@@ -0,0 +1,70 @@
1
+ import { IncidentClient } from '@watchupltd/core';
2
+ import type { IncidentConfig } from '@watchupltd/core';
3
+
4
+ let globalClient: IncidentClient | null = null;
5
+
6
+ export interface BrowserConfig extends IncidentConfig {
7
+ captureConsoleErrors?: boolean;
8
+ }
9
+
10
+ export function init(config: BrowserConfig): IncidentClient {
11
+ if (globalClient) {
12
+ return globalClient;
13
+ }
14
+
15
+ globalClient = new IncidentClient(config);
16
+
17
+ if (typeof window !== 'undefined') {
18
+ setupGlobalErrorHandlers(globalClient);
19
+
20
+ if (config.captureConsoleErrors) {
21
+ setupConsoleCapture(globalClient);
22
+ }
23
+ }
24
+
25
+ return globalClient;
26
+ }
27
+
28
+ export function getClient(): IncidentClient | null {
29
+ return globalClient;
30
+ }
31
+
32
+ function setupGlobalErrorHandlers(client: IncidentClient): void {
33
+ window.addEventListener('error', (event) => {
34
+ const error = event.error || new Error(event.message);
35
+ client.captureError(error, {
36
+ extra: {
37
+ filename: event.filename,
38
+ lineno: event.lineno,
39
+ colno: event.colno
40
+ }
41
+ });
42
+ });
43
+
44
+ window.addEventListener('unhandledrejection', (event) => {
45
+ const error = event.reason instanceof Error
46
+ ? event.reason
47
+ : new Error(String(event.reason));
48
+
49
+ client.captureError(error, {
50
+ tags: { type: 'unhandledrejection' }
51
+ });
52
+ });
53
+ }
54
+
55
+ function setupConsoleCapture(client: IncidentClient): void {
56
+ const originalError = console.error;
57
+
58
+ console.error = (...args: any[]) => {
59
+ originalError.apply(console, args);
60
+
61
+ const message = args.map(arg =>
62
+ typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
63
+ ).join(' ');
64
+
65
+ client.captureMessage(message, 'error');
66
+ };
67
+ }
68
+
69
+ export { IncidentClient } from '@watchupltd/core';
70
+ export type { IncidentConfig, SeverityLevel, User, Breadcrumb } from '@watchupltd/core';
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "lib": ["ES2020", "DOM"]
7
+ },
8
+ "include": ["src/**/*"]
9
+ }