@bugmail-js/core 0.1.0 → 0.1.4

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/README.md CHANGED
@@ -1,3 +1,89 @@
1
+ # @bugmail-js/core — Core utilities for BugMail SDKs
2
+
3
+ This package contains framework-agnostic building blocks used by the BugMail JavaScript SDKs (browser, Node, and server integrations).
4
+
5
+ Use this package if you are building a custom integration or need low-level control. It exports small, well-scoped classes for capturing errors, tracking breadcrumbs, and sending reports.
6
+
7
+ Highlights
8
+ - `BugMailCoreClient` — lightweight client that prepares and sends error payloads to the BugMail backend.
9
+ - `BreadcrumbTracker` — record user events leading up to an error.
10
+ - `CoreNetworkManager`, `CoreErrorProcessor` — helpers for networking and normalization.
11
+
12
+ Installation
13
+
14
+ Typically you do not need to install this directly; other SDK packages depend on it. If you need it directly:
15
+
16
+ ```bash
17
+ npm install @bugmail-js/core
18
+ ```
19
+
20
+ Quick example (capture an exception)
21
+
22
+ ```javascript
23
+ import { BugMailCoreClient } from '@bugmail-js/core';
24
+
25
+ const client = new BugMailCoreClient({
26
+ baseUrl: 'http://localhost:8000',
27
+ apiPath: '/api/sdk/v1/errors',
28
+ onError: (info) => console.warn('Reporting failed', info),
29
+ });
30
+
31
+ await client.captureException(new Error('Test error'), {
32
+ headers: { 'X-Bugmail-Api-Key': 'YOUR_PROJECT_API_KEY' },
33
+ payload: {
34
+ error: { name: 'Error', message: 'Test error', stack: '...' },
35
+ context: { url: '/test', environment: 'development' }
36
+ },
37
+ });
38
+ ```
39
+
40
+ API reference (important parts)
41
+
42
+ - `new BugMailCoreClient(config)`
43
+ - `config.baseUrl` — backend base URL (default: env `BUGMAIL_API_BASE_URL` or `http://localhost:8000`)
44
+ - `config.apiPath` — ingestion path (default: `/api/sdk/v1/errors`)
45
+ - `config.onError` — optional callback invoked when report fails
46
+
47
+ - `captureException(error, context)`
48
+ - `error` — Error object or any serializable value
49
+ - `context` — optional object: `{ headers, payload, user, breadcrumbs }`
50
+ - Include `headers: { 'X-Bugmail-Api-Key': '<YOUR_PROJECT_API_KEY>' }`
51
+
52
+ - `BreadcrumbTracker`
53
+ - `new BreadcrumbTracker({ maxBreadcrumbs })`
54
+ - `record(breadcrumb)` — add a breadcrumb object
55
+ - `recordCustom(message, category, data, level)`
56
+ - `recordRequest(requestData)` — convenience for HTTP breadcrumbs
57
+ - `getBreadcrumbs()`, `clear()`
58
+
59
+ Advanced
60
+
61
+ - `CoreNetworkManager` and `CoreErrorProcessor` are exported for advanced integration points (custom retries, queueing, or server-side normalization).
62
+
63
+ Integration notes
64
+
65
+ - The core package is intentionally minimal and has no browser-only dependencies. Browser and Node SDKs re-export and extend it with environment-specific behavior (automatic capture, global handlers, middleware, etc.).
66
+
67
+ Backend integration & headers
68
+
69
+ - Ingestion endpoint: `POST {baseUrl}/api/sdk/v1/errors`
70
+ - Auth header: `x-bugmail-api-key: <YOUR_PROJECT_API_KEY>` (case-insensitive)
71
+ - Typical payload shape:
72
+
73
+ ```
74
+ {
75
+ "error": { "name": "TypeError", "message": "...", "stack": "..." },
76
+ "context": { "breadcrumbs": [...], "request": { ... } },
77
+ "timestamp": "2025-09-06T15:40:00.000Z",
78
+ "environment": "production"
79
+ }
80
+ ```
81
+
82
+ The backend validates the API key, groups the error, stores breadcrumbs, enforces rate/plan limits, may run AI analysis, and returns `201` with `{ status: "success", error_id: "..." }`.
83
+
84
+ Contributing
85
+
86
+ Please follow the repository contributing guidelines. Tests and simple usage examples are appreciated.
1
87
  # Shared core logic for BugMail SDKs
2
88
 
3
89
  This package will contain shared logic for Node.js and Django SDKs.
@@ -26,11 +26,18 @@ export class BugMailCoreClient {
26
26
 
27
27
  try {
28
28
  // Use fetch if available (browser or Node >=18), else fallback
29
+ // Prefer global fetch (browsers, Node 18+). Avoid importing node-fetch in bundlers
29
30
  let fetchFn = (typeof fetch !== 'undefined') ? fetch : undefined;
30
31
  if (!fetchFn) {
32
+ // In Node environments where fetch is not global, try dynamic import at runtime only
31
33
  try {
32
- // Dynamically import node-fetch if not in browser
33
- fetchFn = (await import('node-fetch')).default;
34
+ // Use require-like dynamic import only in Node; bundlers should not include node-fetch for browser builds
35
+ if (typeof process !== 'undefined' && process.versions && process.versions.node) {
36
+ fetchFn = (await import('node-fetch')).default;
37
+ } else {
38
+ console.error('[BugMail] fetch is not available in this environment.');
39
+ return;
40
+ }
34
41
  } catch (e) {
35
42
  console.error('[BugMail] fetch is not available in this environment.');
36
43
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bugmail-js/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.4",
4
4
  "main": "index.js",
5
5
  "module": "index.js",
6
6
  "exports": {
@@ -35,4 +35,4 @@
35
35
  "url": "https://github.com/MarcorpAI/Bugmail-SDKs/issues"
36
36
  },
37
37
  "sideEffects": false
38
- }
38
+ }
package/plugin-manager.js CHANGED
@@ -1,7 +1,45 @@
1
1
  // Plugin manager (core, generic)
2
2
  export class CorePluginManager {
3
3
  constructor() {
4
- // ...core plugin system
4
+ // Maintain a set of plugins and a simple hook map
5
+ this.plugins = new Set();
6
+ this.hooks = {
7
+ setup: new Set(),
8
+ disconnect: new Set(),
9
+ onErrorCaptured: new Set(),
10
+ };
11
+ }
12
+
13
+ register(plugin) {
14
+ this.plugins.add(plugin);
15
+ }
16
+
17
+ unregister(plugin) {
18
+ this.plugins.delete(plugin);
19
+ }
20
+
21
+ runHooks(hookName, data, client) {
22
+ const hooks = this.hooks[hookName];
23
+ if (!hooks) return data;
24
+ let result = data;
25
+ for (const fn of hooks) {
26
+ try {
27
+ const r = fn(result, client);
28
+ if (r !== undefined) result = r;
29
+ } catch (e) {
30
+ // swallow plugin errors to avoid breaking host app
31
+ console.error('[BugMail] Plugin hook error:', e);
32
+ }
33
+ }
34
+ return result;
35
+ }
36
+
37
+ addHook(hookName, fn) {
38
+ if (!this.hooks[hookName]) this.hooks[hookName] = new Set();
39
+ this.hooks[hookName].add(fn);
40
+ }
41
+
42
+ removeHook(hookName, fn) {
43
+ this.hooks[hookName]?.delete(fn);
5
44
  }
6
- // ...core methods
7
45
  }