@error-explorer/react 1.1.1

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,458 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React$1, { ReactNode, Component, ComponentType } from 'react';
3
+ import { InitOptions, CaptureContext, Breadcrumb, UserContext } from '@error-explorer/browser';
4
+ export { Breadcrumb, CaptureContext, ErrorExplorer, InitOptions, UserContext } from '@error-explorer/browser';
5
+
6
+ /**
7
+ * Types for @error-explorer/react
8
+ */
9
+
10
+ /**
11
+ * React-specific initialization options
12
+ */
13
+ interface ReactErrorExplorerOptions extends InitOptions {
14
+ /**
15
+ * Whether to capture React component stack traces
16
+ * @default true
17
+ */
18
+ captureComponentStack?: boolean;
19
+ /**
20
+ * Hook called before capturing a React error
21
+ * Return false to skip capturing
22
+ */
23
+ beforeReactCapture?: (error: Error, errorInfo: React.ErrorInfo) => boolean;
24
+ }
25
+ /**
26
+ * React component context captured with errors
27
+ */
28
+ interface ReactComponentContext {
29
+ /**
30
+ * Component name
31
+ */
32
+ name?: string;
33
+ /**
34
+ * React component stack
35
+ */
36
+ componentStack?: string;
37
+ /**
38
+ * Additional component info
39
+ */
40
+ info?: string;
41
+ }
42
+ /**
43
+ * ErrorBoundary component props
44
+ */
45
+ interface ErrorBoundaryProps {
46
+ /**
47
+ * Child components to wrap
48
+ */
49
+ children: ReactNode;
50
+ /**
51
+ * Fallback UI to render when an error occurs
52
+ */
53
+ fallback?: ReactNode | ((props: FallbackProps) => ReactNode);
54
+ /**
55
+ * Callback when an error is caught
56
+ */
57
+ onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
58
+ /**
59
+ * Callback when the error is reset
60
+ */
61
+ onReset?: () => void;
62
+ /**
63
+ * Whether to capture the error to Error Explorer
64
+ * @default true
65
+ */
66
+ capture?: boolean;
67
+ /**
68
+ * Additional tags to add when capturing
69
+ */
70
+ tags?: Record<string, string>;
71
+ /**
72
+ * Additional context to add when capturing
73
+ */
74
+ context?: Record<string, unknown>;
75
+ /**
76
+ * Reset keys - when these change, the error boundary resets
77
+ */
78
+ resetKeys?: unknown[];
79
+ }
80
+ /**
81
+ * Props passed to the fallback component
82
+ */
83
+ interface FallbackProps {
84
+ /**
85
+ * The error that was caught
86
+ */
87
+ error: Error;
88
+ /**
89
+ * Error info including component stack
90
+ */
91
+ errorInfo: React.ErrorInfo | null;
92
+ /**
93
+ * Function to reset the error boundary
94
+ */
95
+ resetErrorBoundary: () => void;
96
+ }
97
+ /**
98
+ * ErrorBoundary state
99
+ */
100
+ interface ErrorBoundaryState {
101
+ hasError: boolean;
102
+ error: Error | null;
103
+ errorInfo: React.ErrorInfo | null;
104
+ }
105
+ /**
106
+ * Provider props for ErrorExplorerProvider
107
+ */
108
+ interface ErrorExplorerProviderProps {
109
+ /**
110
+ * SDK initialization options
111
+ */
112
+ options: ReactErrorExplorerOptions;
113
+ /**
114
+ * Child components
115
+ */
116
+ children: ReactNode;
117
+ }
118
+ /**
119
+ * Context value exposed by ErrorExplorerProvider
120
+ */
121
+ interface ErrorExplorerContextValue {
122
+ /**
123
+ * Check if SDK is initialized
124
+ */
125
+ isInitialized: boolean;
126
+ /**
127
+ * Capture an exception
128
+ */
129
+ captureException: (error: Error, context?: CaptureContext) => string;
130
+ /**
131
+ * Capture a message
132
+ */
133
+ captureMessage: (message: string, level?: 'debug' | 'info' | 'warning' | 'error' | 'critical') => string;
134
+ /**
135
+ * Add a breadcrumb
136
+ */
137
+ addBreadcrumb: (breadcrumb: Breadcrumb) => void;
138
+ /**
139
+ * Set user context
140
+ */
141
+ setUser: (user: UserContext) => void;
142
+ /**
143
+ * Clear user context
144
+ */
145
+ clearUser: () => void;
146
+ /**
147
+ * Set a tag
148
+ */
149
+ setTag: (key: string, value: string) => void;
150
+ /**
151
+ * Set multiple tags
152
+ */
153
+ setTags: (tags: Record<string, string>) => void;
154
+ /**
155
+ * Set extra context
156
+ */
157
+ setExtra: (extra: Record<string, unknown>) => void;
158
+ /**
159
+ * Set named context
160
+ */
161
+ setContext: (name: string, context: Record<string, unknown>) => void;
162
+ /**
163
+ * Flush pending events
164
+ */
165
+ flush: (timeout?: number) => Promise<boolean>;
166
+ /**
167
+ * Close the SDK
168
+ */
169
+ close: (timeout?: number) => Promise<boolean>;
170
+ }
171
+ /**
172
+ * HOC options for withErrorBoundary
173
+ */
174
+ interface WithErrorBoundaryOptions {
175
+ /**
176
+ * Fallback UI to render when an error occurs
177
+ */
178
+ fallback?: ReactNode | ((props: FallbackProps) => ReactNode);
179
+ /**
180
+ * Callback when an error is caught
181
+ */
182
+ onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
183
+ /**
184
+ * Whether to capture the error to Error Explorer
185
+ * @default true
186
+ */
187
+ capture?: boolean;
188
+ /**
189
+ * Additional tags to add when capturing
190
+ */
191
+ tags?: Record<string, string>;
192
+ /**
193
+ * Additional context to add when capturing
194
+ */
195
+ context?: Record<string, unknown>;
196
+ }
197
+
198
+ /**
199
+ * React Context for Error Explorer
200
+ */
201
+ declare const ErrorExplorerContext: React$1.Context<ErrorExplorerContextValue | null>;
202
+ /**
203
+ * Error Explorer Provider Component
204
+ *
205
+ * Initializes the Error Explorer SDK and provides context to child components.
206
+ *
207
+ * @example
208
+ * ```tsx
209
+ * function App() {
210
+ * return (
211
+ * <ErrorExplorerProvider
212
+ * options={{
213
+ * token: 'ee_your_token',
214
+ * project: 'my-react-app',
215
+ * environment: 'production',
216
+ * }}
217
+ * >
218
+ * <MainContent />
219
+ * </ErrorExplorerProvider>
220
+ * );
221
+ * }
222
+ * ```
223
+ */
224
+ declare function ErrorExplorerProvider({ options, children, }: {
225
+ options: ReactErrorExplorerOptions;
226
+ children: ReactNode;
227
+ }): react_jsx_runtime.JSX.Element;
228
+ /**
229
+ * Initialize Error Explorer directly (without provider)
230
+ *
231
+ * Use this if you don't need the React Context and just want to initialize
232
+ * the SDK globally.
233
+ *
234
+ * @example
235
+ * ```tsx
236
+ * // In your entry file (main.tsx)
237
+ * import { initErrorExplorer } from '@error-explorer/react';
238
+ *
239
+ * initErrorExplorer({
240
+ * token: 'ee_your_token',
241
+ * project: 'my-react-app',
242
+ * environment: 'production',
243
+ * });
244
+ *
245
+ * ReactDOM.createRoot(root).render(<App />);
246
+ * ```
247
+ */
248
+ declare function initErrorExplorer(options: ReactErrorExplorerOptions): void;
249
+
250
+ /**
251
+ * ErrorBoundary component for React
252
+ * Catches errors in child components and reports them to Error Explorer
253
+ */
254
+
255
+ /**
256
+ * ErrorBoundary class component
257
+ *
258
+ * React Error Boundaries must be class components - there's no hook equivalent.
259
+ *
260
+ * @example
261
+ * ```tsx
262
+ * <ErrorBoundary fallback={<ErrorFallback />}>
263
+ * <MyComponent />
264
+ * </ErrorBoundary>
265
+ * ```
266
+ *
267
+ * @example
268
+ * With render prop fallback:
269
+ * ```tsx
270
+ * <ErrorBoundary
271
+ * fallback={({ error, resetErrorBoundary }) => (
272
+ * <div>
273
+ * <p>Error: {error.message}</p>
274
+ * <button onClick={resetErrorBoundary}>Retry</button>
275
+ * </div>
276
+ * )}
277
+ * >
278
+ * <MyComponent />
279
+ * </ErrorBoundary>
280
+ * ```
281
+ */
282
+ declare class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
283
+ static defaultProps: {
284
+ capture: boolean;
285
+ tags: {};
286
+ context: {};
287
+ };
288
+ constructor(props: ErrorBoundaryProps);
289
+ static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState>;
290
+ componentDidCatch(error: Error, errorInfo: React$1.ErrorInfo): void;
291
+ componentDidUpdate(prevProps: ErrorBoundaryProps): void;
292
+ reset: () => void;
293
+ render(): ReactNode;
294
+ }
295
+ /**
296
+ * Higher-order component to wrap a component with ErrorBoundary
297
+ *
298
+ * @example
299
+ * ```tsx
300
+ * const SafeComponent = withErrorBoundary(MyComponent, {
301
+ * fallback: <ErrorFallback />,
302
+ * onError: (error) => console.error(error),
303
+ * });
304
+ * ```
305
+ */
306
+ declare function withErrorBoundary<P extends object>(Component: ComponentType<P>, options?: WithErrorBoundaryOptions): ComponentType<P>;
307
+ /**
308
+ * useErrorBoundary hook for functional components
309
+ *
310
+ * Note: This doesn't create an error boundary - those must be class components.
311
+ * Instead, this provides a way to show/trigger the nearest error boundary.
312
+ *
313
+ * @example
314
+ * ```tsx
315
+ * function MyComponent() {
316
+ * const { showBoundary } = useErrorBoundary();
317
+ *
318
+ * const handleClick = async () => {
319
+ * try {
320
+ * await riskyOperation();
321
+ * } catch (error) {
322
+ * showBoundary(error);
323
+ * }
324
+ * };
325
+ * }
326
+ * ```
327
+ */
328
+ declare function useErrorBoundary(): {
329
+ /**
330
+ * Trigger the nearest error boundary with the given error
331
+ */
332
+ showBoundary: (err: Error) => void;
333
+ /**
334
+ * Reset the error state
335
+ */
336
+ resetBoundary: () => void;
337
+ };
338
+
339
+ /**
340
+ * Use Error Explorer instance
341
+ *
342
+ * Returns the Error Explorer SDK methods for capturing errors and managing context.
343
+ *
344
+ * @example
345
+ * ```tsx
346
+ * const { captureException, addBreadcrumb, setUser } = useErrorExplorer();
347
+ *
348
+ * try {
349
+ * await riskyOperation();
350
+ * } catch (error) {
351
+ * captureException(error);
352
+ * }
353
+ * ```
354
+ */
355
+ declare function useErrorExplorer(): ErrorExplorerContextValue;
356
+ /**
357
+ * Error handler hook for async operations
358
+ *
359
+ * @example
360
+ * ```tsx
361
+ * function MyComponent() {
362
+ * const { handleError, wrapAsync } = useErrorHandler();
363
+ *
364
+ * // Option 1: Wrap async function
365
+ * const safeSubmit = wrapAsync(async () => {
366
+ * await api.submit(data);
367
+ * });
368
+ *
369
+ * // Option 2: Manual handling
370
+ * const handleClick = async () => {
371
+ * try {
372
+ * await riskyOperation();
373
+ * } catch (error) {
374
+ * handleError(error, { tags: { operation: 'risky' } });
375
+ * }
376
+ * };
377
+ * }
378
+ * ```
379
+ */
380
+ declare function useErrorHandler(defaultContext?: CaptureContext): {
381
+ handleError: (error: unknown, context?: CaptureContext) => Error;
382
+ wrapAsync: <T extends (...args: any[]) => Promise<any>>(fn: T, context?: CaptureContext) => ((...args: Parameters<T>) => Promise<Awaited<ReturnType<T>> | undefined>);
383
+ tryCatch: <T>(fn: () => T, context?: CaptureContext) => T | undefined;
384
+ };
385
+ /**
386
+ * User context hook
387
+ *
388
+ * Sets the user context and cleans up on unmount.
389
+ *
390
+ * @example
391
+ * ```tsx
392
+ * function App() {
393
+ * const user = useCurrentUser();
394
+ *
395
+ * useUserContext(user ? {
396
+ * id: user.id,
397
+ * email: user.email,
398
+ * name: user.name,
399
+ * } : null);
400
+ *
401
+ * return <MainContent />;
402
+ * }
403
+ * ```
404
+ */
405
+ declare function useUserContext(user: UserContext | null): {
406
+ setUser: (user: UserContext) => void;
407
+ clearUser: () => void;
408
+ };
409
+ /**
410
+ * Action tracker hook for user interactions
411
+ *
412
+ * @example
413
+ * ```tsx
414
+ * function MyComponent() {
415
+ * const { trackAction, trackInteraction } = useActionTracker();
416
+ *
417
+ * const handleSubmit = () => {
418
+ * trackAction('form_submitted', { formId: 'contact' });
419
+ * // ... actual submit logic
420
+ * };
421
+ *
422
+ * return (
423
+ * <button
424
+ * onClick={() => {
425
+ * trackInteraction('submit-button', 'click');
426
+ * handleSubmit();
427
+ * }}
428
+ * >
429
+ * Submit
430
+ * </button>
431
+ * );
432
+ * }
433
+ * ```
434
+ */
435
+ declare function useActionTracker(componentName?: string): {
436
+ trackAction: (action: string, data?: Record<string, unknown>) => void;
437
+ trackInteraction: (element: string, action: "click" | "input" | "focus" | "blur" | "submit", data?: Record<string, unknown>) => void;
438
+ };
439
+ /**
440
+ * Component lifecycle tracking hook
441
+ *
442
+ * @example
443
+ * ```tsx
444
+ * function MyComponent() {
445
+ * useComponentBreadcrumbs('MyComponent');
446
+ *
447
+ * return <div>...</div>;
448
+ * }
449
+ * ```
450
+ */
451
+ declare function useComponentBreadcrumbs(componentName: string): void;
452
+
453
+ /**
454
+ * @error-explorer/react
455
+ * Error Explorer SDK for React - Automatic error tracking with React integration
456
+ */
457
+
458
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorBoundaryState, ErrorExplorerContext, type ErrorExplorerContextValue, ErrorExplorerProvider, type ErrorExplorerProviderProps, type FallbackProps, type ReactComponentContext, type ReactErrorExplorerOptions, type WithErrorBoundaryOptions, ErrorExplorerProvider as default, initErrorExplorer, useActionTracker, useComponentBreadcrumbs, useErrorBoundary, useErrorExplorer, useErrorHandler, useUserContext, withErrorBoundary };