@bugwatch/core 0.1.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.
@@ -0,0 +1,323 @@
1
+ /**
2
+ * Configuration options for the Bugwatch SDK
3
+ */
4
+ interface BugwatchOptions {
5
+ /** Your project's API key */
6
+ apiKey: string;
7
+ /** API endpoint URL (defaults to https://api.bugwatch.dev) */
8
+ endpoint?: string;
9
+ /** Application environment (e.g., 'production', 'staging') */
10
+ environment?: string;
11
+ /** Application release/version */
12
+ release?: string;
13
+ /** Enable debug logging */
14
+ debug?: boolean;
15
+ /** Sample rate for error events (0.0 to 1.0) */
16
+ sampleRate?: number;
17
+ /** Maximum breadcrumbs to capture */
18
+ maxBreadcrumbs?: number;
19
+ /** Tags to attach to all events */
20
+ tags?: Record<string, string>;
21
+ /** User context */
22
+ user?: UserContext;
23
+ /** Before send hook - return null to drop the event */
24
+ beforeSend?: (event: ErrorEvent) => ErrorEvent | null;
25
+ /** Patterns to ignore (matches against error message) */
26
+ ignoreErrors?: (string | RegExp)[];
27
+ }
28
+ /**
29
+ * User context attached to events
30
+ */
31
+ interface UserContext {
32
+ id?: string;
33
+ email?: string;
34
+ username?: string;
35
+ [key: string]: unknown;
36
+ }
37
+ /**
38
+ * Stack frame in an error stack trace
39
+ */
40
+ interface StackFrame {
41
+ filename: string;
42
+ function: string;
43
+ lineno: number;
44
+ colno: number;
45
+ context_line?: string;
46
+ pre_context?: string[];
47
+ post_context?: string[];
48
+ in_app: boolean;
49
+ }
50
+ /**
51
+ * Exception/error information
52
+ */
53
+ interface ExceptionInfo {
54
+ type: string;
55
+ value: string;
56
+ stacktrace: StackFrame[];
57
+ }
58
+ /**
59
+ * Breadcrumb for tracking events leading to an error
60
+ */
61
+ interface Breadcrumb {
62
+ timestamp: string;
63
+ category: string;
64
+ message: string;
65
+ level?: "debug" | "info" | "warning" | "error";
66
+ data?: Record<string, unknown>;
67
+ }
68
+ /**
69
+ * Request context for HTTP errors
70
+ */
71
+ interface RequestContext {
72
+ url?: string;
73
+ method?: string;
74
+ headers?: Record<string, string>;
75
+ query_string?: string;
76
+ data?: unknown;
77
+ }
78
+ /**
79
+ * Runtime information
80
+ */
81
+ interface RuntimeInfo {
82
+ name: string;
83
+ version: string;
84
+ }
85
+ /**
86
+ * SDK information
87
+ */
88
+ interface SdkInfo {
89
+ name: string;
90
+ version: string;
91
+ }
92
+ /**
93
+ * The main error event payload sent to the API
94
+ */
95
+ interface ErrorEvent {
96
+ /** Event ID (generated by SDK) */
97
+ event_id: string;
98
+ /** Timestamp in ISO format */
99
+ timestamp: string;
100
+ /** Platform identifier */
101
+ platform: string;
102
+ /** Error level */
103
+ level: "fatal" | "error" | "warning" | "info" | "debug";
104
+ /** Error message */
105
+ message: string;
106
+ /** Exception information */
107
+ exception?: ExceptionInfo;
108
+ /** Breadcrumbs */
109
+ breadcrumbs?: Breadcrumb[];
110
+ /** Tags */
111
+ tags?: Record<string, string>;
112
+ /** Extra context data */
113
+ extra?: Record<string, unknown>;
114
+ /** User context */
115
+ user?: UserContext;
116
+ /** Request context */
117
+ request?: RequestContext;
118
+ /** Application environment */
119
+ environment?: string;
120
+ /** Application release */
121
+ release?: string;
122
+ /** Server name / hostname */
123
+ server_name?: string;
124
+ /** SDK information */
125
+ sdk?: SdkInfo;
126
+ /** Runtime information */
127
+ runtime?: RuntimeInfo;
128
+ }
129
+ /**
130
+ * Transport interface for sending events
131
+ */
132
+ interface Transport {
133
+ send(event: ErrorEvent): Promise<void>;
134
+ }
135
+ /**
136
+ * Integration interface for platform-specific functionality
137
+ */
138
+ interface Integration {
139
+ name: string;
140
+ setup(client: BugwatchClient): void;
141
+ }
142
+ /**
143
+ * Bugwatch client interface
144
+ */
145
+ interface BugwatchClient {
146
+ captureException(error: Error, context?: Partial<ErrorEvent>): string;
147
+ captureMessage(message: string, level?: ErrorEvent["level"]): string;
148
+ addBreadcrumb(breadcrumb: Omit<Breadcrumb, "timestamp">): void;
149
+ setUser(user: UserContext | null): void;
150
+ setTag(key: string, value: string): void;
151
+ setExtra(key: string, value: unknown): void;
152
+ getOptions(): BugwatchOptions;
153
+ }
154
+
155
+ /**
156
+ * Core Bugwatch client implementation
157
+ */
158
+ declare class Bugwatch implements BugwatchClient {
159
+ private options;
160
+ private transport;
161
+ private breadcrumbs;
162
+ private tags;
163
+ private extra;
164
+ private user;
165
+ private integrations;
166
+ private initialized;
167
+ constructor(options: BugwatchOptions);
168
+ private createTransport;
169
+ /**
170
+ * Register an integration
171
+ */
172
+ use(integration: Integration): this;
173
+ /**
174
+ * Get SDK options
175
+ */
176
+ getOptions(): BugwatchOptions;
177
+ /**
178
+ * Capture an exception
179
+ */
180
+ captureException(error: Error, context?: Partial<ErrorEvent>): string;
181
+ /**
182
+ * Capture a message
183
+ */
184
+ captureMessage(message: string, level?: ErrorEvent["level"]): string;
185
+ /**
186
+ * Add a breadcrumb
187
+ */
188
+ addBreadcrumb(breadcrumb: Omit<Breadcrumb, "timestamp">): void;
189
+ /**
190
+ * Set user context
191
+ */
192
+ setUser(user: UserContext | null): void;
193
+ /**
194
+ * Set a tag
195
+ */
196
+ setTag(key: string, value: string): void;
197
+ /**
198
+ * Set extra context
199
+ */
200
+ setExtra(key: string, value: unknown): void;
201
+ /**
202
+ * Clear breadcrumbs
203
+ */
204
+ clearBreadcrumbs(): void;
205
+ /**
206
+ * Create an event from an Error object
207
+ */
208
+ private createEventFromError;
209
+ /**
210
+ * Create a base event
211
+ */
212
+ private createEvent;
213
+ /**
214
+ * Check if error should be ignored
215
+ */
216
+ private shouldIgnoreError;
217
+ /**
218
+ * Detect the current platform
219
+ */
220
+ private detectPlatform;
221
+ }
222
+
223
+ /**
224
+ * HTTP transport for sending events to the Bugwatch API
225
+ */
226
+ declare class HttpTransport implements Transport {
227
+ private endpoint;
228
+ private apiKey;
229
+ private debug;
230
+ constructor(options: BugwatchOptions);
231
+ send(event: ErrorEvent): Promise<void>;
232
+ /**
233
+ * Abstract fetch to support different environments
234
+ */
235
+ private fetch;
236
+ }
237
+ /**
238
+ * No-op transport for testing or disabled SDK
239
+ */
240
+ declare class NoopTransport implements Transport {
241
+ send(_event: ErrorEvent): Promise<void>;
242
+ }
243
+ /**
244
+ * Console transport for development/debugging
245
+ */
246
+ declare class ConsoleTransport implements Transport {
247
+ send(event: ErrorEvent): Promise<void>;
248
+ }
249
+ /**
250
+ * Batching transport that queues events and sends them in batches
251
+ */
252
+ declare class BatchTransport implements Transport {
253
+ private transport;
254
+ private queue;
255
+ private maxBatchSize;
256
+ private flushInterval;
257
+ private timer;
258
+ constructor(transport: Transport, options?: {
259
+ maxBatchSize?: number;
260
+ flushInterval?: number;
261
+ });
262
+ send(event: ErrorEvent): Promise<void>;
263
+ flush(): Promise<void>;
264
+ private startTimer;
265
+ destroy(): void;
266
+ }
267
+
268
+ /**
269
+ * Parse an Error's stack trace into structured frames
270
+ */
271
+ declare function parseStackTrace(error: Error): StackFrame[];
272
+ /**
273
+ * Extract error name and message from Error object
274
+ */
275
+ declare function extractErrorInfo(error: Error): {
276
+ type: string;
277
+ value: string;
278
+ };
279
+
280
+ /**
281
+ * Generate a fingerprint for an error to group similar errors together.
282
+ * Uses a combination of error type, message patterns, and stack trace.
283
+ */
284
+ declare function generateFingerprint(errorType: string, message: string, stacktrace?: StackFrame[]): string;
285
+ /**
286
+ * Generate fingerprint from exception info
287
+ */
288
+ declare function fingerprintFromException(exception: ExceptionInfo): string;
289
+
290
+ /**
291
+ * Initialize the global Bugwatch client
292
+ */
293
+ declare function init(options: BugwatchOptions): Bugwatch;
294
+ /**
295
+ * Get the global Bugwatch client
296
+ */
297
+ declare function getClient(): Bugwatch | null;
298
+ /**
299
+ * Capture an exception using the global client
300
+ */
301
+ declare function captureException(error: Error, context?: Partial<ErrorEvent>): string;
302
+ /**
303
+ * Capture a message using the global client
304
+ */
305
+ declare function captureMessage(message: string, level?: ErrorEvent["level"]): string;
306
+ /**
307
+ * Add a breadcrumb using the global client
308
+ */
309
+ declare function addBreadcrumb(breadcrumb: Omit<Breadcrumb, "timestamp">): void;
310
+ /**
311
+ * Set user context on the global client
312
+ */
313
+ declare function setUser(user: UserContext | null): void;
314
+ /**
315
+ * Set a tag on the global client
316
+ */
317
+ declare function setTag(key: string, value: string): void;
318
+ /**
319
+ * Set extra context on the global client
320
+ */
321
+ declare function setExtra(key: string, value: unknown): void;
322
+
323
+ export { BatchTransport, type Breadcrumb, Bugwatch, type BugwatchClient, type BugwatchOptions, ConsoleTransport, type ErrorEvent, type ExceptionInfo, HttpTransport, type Integration, NoopTransport, type RequestContext, type RuntimeInfo, type SdkInfo, type StackFrame, type Transport, type UserContext, addBreadcrumb, captureException, captureMessage, extractErrorInfo, fingerprintFromException, generateFingerprint, getClient, init, parseStackTrace, setExtra, setTag, setUser };
@@ -0,0 +1,323 @@
1
+ /**
2
+ * Configuration options for the Bugwatch SDK
3
+ */
4
+ interface BugwatchOptions {
5
+ /** Your project's API key */
6
+ apiKey: string;
7
+ /** API endpoint URL (defaults to https://api.bugwatch.dev) */
8
+ endpoint?: string;
9
+ /** Application environment (e.g., 'production', 'staging') */
10
+ environment?: string;
11
+ /** Application release/version */
12
+ release?: string;
13
+ /** Enable debug logging */
14
+ debug?: boolean;
15
+ /** Sample rate for error events (0.0 to 1.0) */
16
+ sampleRate?: number;
17
+ /** Maximum breadcrumbs to capture */
18
+ maxBreadcrumbs?: number;
19
+ /** Tags to attach to all events */
20
+ tags?: Record<string, string>;
21
+ /** User context */
22
+ user?: UserContext;
23
+ /** Before send hook - return null to drop the event */
24
+ beforeSend?: (event: ErrorEvent) => ErrorEvent | null;
25
+ /** Patterns to ignore (matches against error message) */
26
+ ignoreErrors?: (string | RegExp)[];
27
+ }
28
+ /**
29
+ * User context attached to events
30
+ */
31
+ interface UserContext {
32
+ id?: string;
33
+ email?: string;
34
+ username?: string;
35
+ [key: string]: unknown;
36
+ }
37
+ /**
38
+ * Stack frame in an error stack trace
39
+ */
40
+ interface StackFrame {
41
+ filename: string;
42
+ function: string;
43
+ lineno: number;
44
+ colno: number;
45
+ context_line?: string;
46
+ pre_context?: string[];
47
+ post_context?: string[];
48
+ in_app: boolean;
49
+ }
50
+ /**
51
+ * Exception/error information
52
+ */
53
+ interface ExceptionInfo {
54
+ type: string;
55
+ value: string;
56
+ stacktrace: StackFrame[];
57
+ }
58
+ /**
59
+ * Breadcrumb for tracking events leading to an error
60
+ */
61
+ interface Breadcrumb {
62
+ timestamp: string;
63
+ category: string;
64
+ message: string;
65
+ level?: "debug" | "info" | "warning" | "error";
66
+ data?: Record<string, unknown>;
67
+ }
68
+ /**
69
+ * Request context for HTTP errors
70
+ */
71
+ interface RequestContext {
72
+ url?: string;
73
+ method?: string;
74
+ headers?: Record<string, string>;
75
+ query_string?: string;
76
+ data?: unknown;
77
+ }
78
+ /**
79
+ * Runtime information
80
+ */
81
+ interface RuntimeInfo {
82
+ name: string;
83
+ version: string;
84
+ }
85
+ /**
86
+ * SDK information
87
+ */
88
+ interface SdkInfo {
89
+ name: string;
90
+ version: string;
91
+ }
92
+ /**
93
+ * The main error event payload sent to the API
94
+ */
95
+ interface ErrorEvent {
96
+ /** Event ID (generated by SDK) */
97
+ event_id: string;
98
+ /** Timestamp in ISO format */
99
+ timestamp: string;
100
+ /** Platform identifier */
101
+ platform: string;
102
+ /** Error level */
103
+ level: "fatal" | "error" | "warning" | "info" | "debug";
104
+ /** Error message */
105
+ message: string;
106
+ /** Exception information */
107
+ exception?: ExceptionInfo;
108
+ /** Breadcrumbs */
109
+ breadcrumbs?: Breadcrumb[];
110
+ /** Tags */
111
+ tags?: Record<string, string>;
112
+ /** Extra context data */
113
+ extra?: Record<string, unknown>;
114
+ /** User context */
115
+ user?: UserContext;
116
+ /** Request context */
117
+ request?: RequestContext;
118
+ /** Application environment */
119
+ environment?: string;
120
+ /** Application release */
121
+ release?: string;
122
+ /** Server name / hostname */
123
+ server_name?: string;
124
+ /** SDK information */
125
+ sdk?: SdkInfo;
126
+ /** Runtime information */
127
+ runtime?: RuntimeInfo;
128
+ }
129
+ /**
130
+ * Transport interface for sending events
131
+ */
132
+ interface Transport {
133
+ send(event: ErrorEvent): Promise<void>;
134
+ }
135
+ /**
136
+ * Integration interface for platform-specific functionality
137
+ */
138
+ interface Integration {
139
+ name: string;
140
+ setup(client: BugwatchClient): void;
141
+ }
142
+ /**
143
+ * Bugwatch client interface
144
+ */
145
+ interface BugwatchClient {
146
+ captureException(error: Error, context?: Partial<ErrorEvent>): string;
147
+ captureMessage(message: string, level?: ErrorEvent["level"]): string;
148
+ addBreadcrumb(breadcrumb: Omit<Breadcrumb, "timestamp">): void;
149
+ setUser(user: UserContext | null): void;
150
+ setTag(key: string, value: string): void;
151
+ setExtra(key: string, value: unknown): void;
152
+ getOptions(): BugwatchOptions;
153
+ }
154
+
155
+ /**
156
+ * Core Bugwatch client implementation
157
+ */
158
+ declare class Bugwatch implements BugwatchClient {
159
+ private options;
160
+ private transport;
161
+ private breadcrumbs;
162
+ private tags;
163
+ private extra;
164
+ private user;
165
+ private integrations;
166
+ private initialized;
167
+ constructor(options: BugwatchOptions);
168
+ private createTransport;
169
+ /**
170
+ * Register an integration
171
+ */
172
+ use(integration: Integration): this;
173
+ /**
174
+ * Get SDK options
175
+ */
176
+ getOptions(): BugwatchOptions;
177
+ /**
178
+ * Capture an exception
179
+ */
180
+ captureException(error: Error, context?: Partial<ErrorEvent>): string;
181
+ /**
182
+ * Capture a message
183
+ */
184
+ captureMessage(message: string, level?: ErrorEvent["level"]): string;
185
+ /**
186
+ * Add a breadcrumb
187
+ */
188
+ addBreadcrumb(breadcrumb: Omit<Breadcrumb, "timestamp">): void;
189
+ /**
190
+ * Set user context
191
+ */
192
+ setUser(user: UserContext | null): void;
193
+ /**
194
+ * Set a tag
195
+ */
196
+ setTag(key: string, value: string): void;
197
+ /**
198
+ * Set extra context
199
+ */
200
+ setExtra(key: string, value: unknown): void;
201
+ /**
202
+ * Clear breadcrumbs
203
+ */
204
+ clearBreadcrumbs(): void;
205
+ /**
206
+ * Create an event from an Error object
207
+ */
208
+ private createEventFromError;
209
+ /**
210
+ * Create a base event
211
+ */
212
+ private createEvent;
213
+ /**
214
+ * Check if error should be ignored
215
+ */
216
+ private shouldIgnoreError;
217
+ /**
218
+ * Detect the current platform
219
+ */
220
+ private detectPlatform;
221
+ }
222
+
223
+ /**
224
+ * HTTP transport for sending events to the Bugwatch API
225
+ */
226
+ declare class HttpTransport implements Transport {
227
+ private endpoint;
228
+ private apiKey;
229
+ private debug;
230
+ constructor(options: BugwatchOptions);
231
+ send(event: ErrorEvent): Promise<void>;
232
+ /**
233
+ * Abstract fetch to support different environments
234
+ */
235
+ private fetch;
236
+ }
237
+ /**
238
+ * No-op transport for testing or disabled SDK
239
+ */
240
+ declare class NoopTransport implements Transport {
241
+ send(_event: ErrorEvent): Promise<void>;
242
+ }
243
+ /**
244
+ * Console transport for development/debugging
245
+ */
246
+ declare class ConsoleTransport implements Transport {
247
+ send(event: ErrorEvent): Promise<void>;
248
+ }
249
+ /**
250
+ * Batching transport that queues events and sends them in batches
251
+ */
252
+ declare class BatchTransport implements Transport {
253
+ private transport;
254
+ private queue;
255
+ private maxBatchSize;
256
+ private flushInterval;
257
+ private timer;
258
+ constructor(transport: Transport, options?: {
259
+ maxBatchSize?: number;
260
+ flushInterval?: number;
261
+ });
262
+ send(event: ErrorEvent): Promise<void>;
263
+ flush(): Promise<void>;
264
+ private startTimer;
265
+ destroy(): void;
266
+ }
267
+
268
+ /**
269
+ * Parse an Error's stack trace into structured frames
270
+ */
271
+ declare function parseStackTrace(error: Error): StackFrame[];
272
+ /**
273
+ * Extract error name and message from Error object
274
+ */
275
+ declare function extractErrorInfo(error: Error): {
276
+ type: string;
277
+ value: string;
278
+ };
279
+
280
+ /**
281
+ * Generate a fingerprint for an error to group similar errors together.
282
+ * Uses a combination of error type, message patterns, and stack trace.
283
+ */
284
+ declare function generateFingerprint(errorType: string, message: string, stacktrace?: StackFrame[]): string;
285
+ /**
286
+ * Generate fingerprint from exception info
287
+ */
288
+ declare function fingerprintFromException(exception: ExceptionInfo): string;
289
+
290
+ /**
291
+ * Initialize the global Bugwatch client
292
+ */
293
+ declare function init(options: BugwatchOptions): Bugwatch;
294
+ /**
295
+ * Get the global Bugwatch client
296
+ */
297
+ declare function getClient(): Bugwatch | null;
298
+ /**
299
+ * Capture an exception using the global client
300
+ */
301
+ declare function captureException(error: Error, context?: Partial<ErrorEvent>): string;
302
+ /**
303
+ * Capture a message using the global client
304
+ */
305
+ declare function captureMessage(message: string, level?: ErrorEvent["level"]): string;
306
+ /**
307
+ * Add a breadcrumb using the global client
308
+ */
309
+ declare function addBreadcrumb(breadcrumb: Omit<Breadcrumb, "timestamp">): void;
310
+ /**
311
+ * Set user context on the global client
312
+ */
313
+ declare function setUser(user: UserContext | null): void;
314
+ /**
315
+ * Set a tag on the global client
316
+ */
317
+ declare function setTag(key: string, value: string): void;
318
+ /**
319
+ * Set extra context on the global client
320
+ */
321
+ declare function setExtra(key: string, value: unknown): void;
322
+
323
+ export { BatchTransport, type Breadcrumb, Bugwatch, type BugwatchClient, type BugwatchOptions, ConsoleTransport, type ErrorEvent, type ExceptionInfo, HttpTransport, type Integration, NoopTransport, type RequestContext, type RuntimeInfo, type SdkInfo, type StackFrame, type Transport, type UserContext, addBreadcrumb, captureException, captureMessage, extractErrorInfo, fingerprintFromException, generateFingerprint, getClient, init, parseStackTrace, setExtra, setTag, setUser };