@watchupltd/svelte 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,29 @@
1
+ {
2
+ "name": "@watchupltd/svelte",
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 --external svelte",
16
+ "dev": "tsup src/index.ts --format cjs,esm --dts --external svelte --watch"
17
+ },
18
+ "dependencies": {
19
+ "@watchupltd/core": "file:../core"
20
+ },
21
+ "peerDependencies": {
22
+ "svelte": ">=3.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "svelte": "^4.0.0",
26
+ "tsup": "^7.0.0",
27
+ "typescript": "^5.0.0"
28
+ }
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,84 @@
1
+ import { writable, get } from 'svelte/store';
2
+ import { IncidentClient } from '@watchupltd/core';
3
+ import type { IncidentConfig } from '@watchupltd/core';
4
+
5
+ let client: IncidentClient | null = null;
6
+
7
+ export const incidentStore = writable<IncidentClient | null>(null);
8
+
9
+ export interface SvelteIncidentConfig extends IncidentConfig {
10
+ captureConsoleErrors?: boolean;
11
+ }
12
+
13
+ export function initIncident(config: SvelteIncidentConfig): IncidentClient {
14
+ if (client) {
15
+ return client;
16
+ }
17
+
18
+ client = new IncidentClient(config);
19
+ incidentStore.set(client);
20
+
21
+ if (typeof window !== 'undefined') {
22
+ window.addEventListener('error', (event) => {
23
+ const error = event.error || new Error(event.message);
24
+ client?.captureError(error);
25
+ });
26
+
27
+ window.addEventListener('unhandledrejection', (event) => {
28
+ const error = event.reason instanceof Error
29
+ ? event.reason
30
+ : new Error(String(event.reason));
31
+ client?.captureError(error);
32
+ });
33
+
34
+ if (config.captureConsoleErrors) {
35
+ const originalError = console.error;
36
+ console.error = (...args: any[]) => {
37
+ originalError.apply(console, args);
38
+ const message = args.map(arg =>
39
+ typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
40
+ ).join(' ');
41
+ client?.captureMessage(message, 'error');
42
+ };
43
+ }
44
+ }
45
+
46
+ return client;
47
+ }
48
+
49
+ export function getIncidentClient(): IncidentClient | null {
50
+ return get(incidentStore);
51
+ }
52
+
53
+ export function captureError(error: Error, context?: any) {
54
+ const client = get(incidentStore);
55
+ if (client) {
56
+ return client.captureError(error, context);
57
+ }
58
+ return null;
59
+ }
60
+
61
+ export function captureMessage(message: string, level?: any, context?: any) {
62
+ const client = get(incidentStore);
63
+ if (client) {
64
+ return client.captureMessage(message, level, context);
65
+ }
66
+ return null;
67
+ }
68
+
69
+ export function addBreadcrumb(breadcrumb: any) {
70
+ const client = get(incidentStore);
71
+ if (client) {
72
+ client.addBreadcrumb(breadcrumb);
73
+ }
74
+ }
75
+
76
+ export function setUser(user: any) {
77
+ const client = get(incidentStore);
78
+ if (client) {
79
+ client.setUser(user);
80
+ }
81
+ }
82
+
83
+ export { IncidentClient } from '@watchupltd/core';
84
+ 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
+ }