@drakkar.software/sunglasses-core 0.10.0 → 0.11.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/dist/index.d.mts CHANGED
@@ -1203,6 +1203,71 @@ interface CaptureExceptionOptions {
1203
1203
  */
1204
1204
  declare function captureException(client: ISunglassesClient, error: unknown, options?: CaptureExceptionOptions): void;
1205
1205
 
1206
+ /** Console methods that can be captured. */
1207
+ type ConsoleLevel = 'error' | 'warn';
1208
+ /**
1209
+ * Options for {@link patchConsole}.
1210
+ */
1211
+ interface ConsoleCaptureOptions {
1212
+ /** Console methods to capture. Default: `['error']`. */
1213
+ levels?: ConsoleLevel[];
1214
+ /**
1215
+ * Skip console output whose composed message matches any of these patterns.
1216
+ * Pattern is tested against the raw (pre-truncation) message.
1217
+ */
1218
+ ignorePatterns?: RegExp[];
1219
+ /** Truncate the composed message to this many characters. Default: `200`. */
1220
+ maxMessageLength?: number;
1221
+ /** Include a stack trace in `$error_stack`. Default: `false` (privacy-safe). */
1222
+ includeStack?: boolean;
1223
+ /** Extra properties merged into every captured `$error` event. */
1224
+ properties?: Record<string, unknown>;
1225
+ }
1226
+ /**
1227
+ * Configuration for the providers' `autoCaptureErrors` option. Extends
1228
+ * {@link CaptureExceptionOptions} (applied to unhandled global errors) with
1229
+ * toggles for the global handlers and console capture.
1230
+ */
1231
+ interface AutoCaptureErrorsOptions extends CaptureExceptionOptions {
1232
+ /**
1233
+ * Install the platform global error handlers (web `window` `error` /
1234
+ * `unhandledrejection`, React Native `ErrorUtils`). Default: `true`.
1235
+ */
1236
+ globalHandlers?: boolean;
1237
+ /**
1238
+ * Also capture console output as `$error` events. `true` captures
1239
+ * `console.error`; pass {@link ConsoleCaptureOptions} to configure levels.
1240
+ * Default: off.
1241
+ */
1242
+ console?: boolean | ConsoleCaptureOptions;
1243
+ }
1244
+ /**
1245
+ * Patch the global `console` so that `console.error` / `console.warn` (per
1246
+ * `levels`) are also captured as SunGlasses `$error` events
1247
+ * (`$error_handled: false`, `$error_source: 'console'`).
1248
+ *
1249
+ * Works identically on web and React Native — both expose a global `console`.
1250
+ * The original method is always called first, so logs still appear in the
1251
+ * console. Returns an unpatch function that restores the original methods.
1252
+ *
1253
+ * Guards against infinite recursion: SunGlasses' own logger writes to
1254
+ * `console.error`/`console.warn` (prefixed with `[SunGlasses]`), and
1255
+ * `client.capture()` may log on failure. A re-entrancy flag plus a prefix skip
1256
+ * prevent a capture -> log -> capture loop.
1257
+ *
1258
+ * @param client - SunGlasses client instance.
1259
+ * @param options - Optional capture configuration.
1260
+ * @returns A function that restores the original console methods.
1261
+ *
1262
+ * @example
1263
+ * ```ts
1264
+ * const unpatch = patchConsole(client, { levels: ['error', 'warn'] });
1265
+ * // ... later
1266
+ * unpatch();
1267
+ * ```
1268
+ */
1269
+ declare function patchConsole(client: ISunglassesClient, options?: ConsoleCaptureOptions): () => void;
1270
+
1206
1271
  /**
1207
1272
  * A typed analytics stub that is safe to use before the SDK initialises.
1208
1273
  *
@@ -1275,4 +1340,4 @@ declare function sha256Hex(input: string): Promise<string>;
1275
1340
  */
1276
1341
  declare function nowISO(): string;
1277
1342
 
1278
- export { type AppMetadata, type AppUpdateInfo, type CaptureExceptionOptions, type CleanupConfig, type ConsentHistoryEntry, ConsentManager, type ConsentState, type ConsentStatus, type ErrorEventProperties, type EventContext, type EventCountPeriod, EventCounter, type EventMap, EventQueue, type EventType, FrequencyMiddleware, type FrequencyMiddlewareOptions, type HttpAdapterConfig, type IAnalyticsAdapter, type IEventCounter, type IMiddleware, type IStorageAdapter, type ISunglassesClient, type ISunglassesTypedClient, IdentityManager, type IdentityState, LocalEventArchive, type Logger, type MiddlewareNext, MiddlewarePipeline, PiiSanitizer, SamplingMiddleware, type SamplingMiddlewareOptions, type ScreenTrackingOptions, SessionManager, type SessionState, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, captureException, createLazyClient, createLogger, generateUUID, nowISO, sha256Hex };
1343
+ export { type AppMetadata, type AppUpdateInfo, type AutoCaptureErrorsOptions, type CaptureExceptionOptions, type CleanupConfig, type ConsentHistoryEntry, ConsentManager, type ConsentState, type ConsentStatus, type ConsoleCaptureOptions, type ConsoleLevel, type ErrorEventProperties, type EventContext, type EventCountPeriod, EventCounter, type EventMap, EventQueue, type EventType, FrequencyMiddleware, type FrequencyMiddlewareOptions, type HttpAdapterConfig, type IAnalyticsAdapter, type IEventCounter, type IMiddleware, type IStorageAdapter, type ISunglassesClient, type ISunglassesTypedClient, IdentityManager, type IdentityState, LocalEventArchive, type Logger, type MiddlewareNext, MiddlewarePipeline, PiiSanitizer, SamplingMiddleware, type SamplingMiddlewareOptions, type ScreenTrackingOptions, SessionManager, type SessionState, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, captureException, createLazyClient, createLogger, generateUUID, nowISO, patchConsole, sha256Hex };
package/dist/index.d.ts CHANGED
@@ -1203,6 +1203,71 @@ interface CaptureExceptionOptions {
1203
1203
  */
1204
1204
  declare function captureException(client: ISunglassesClient, error: unknown, options?: CaptureExceptionOptions): void;
1205
1205
 
1206
+ /** Console methods that can be captured. */
1207
+ type ConsoleLevel = 'error' | 'warn';
1208
+ /**
1209
+ * Options for {@link patchConsole}.
1210
+ */
1211
+ interface ConsoleCaptureOptions {
1212
+ /** Console methods to capture. Default: `['error']`. */
1213
+ levels?: ConsoleLevel[];
1214
+ /**
1215
+ * Skip console output whose composed message matches any of these patterns.
1216
+ * Pattern is tested against the raw (pre-truncation) message.
1217
+ */
1218
+ ignorePatterns?: RegExp[];
1219
+ /** Truncate the composed message to this many characters. Default: `200`. */
1220
+ maxMessageLength?: number;
1221
+ /** Include a stack trace in `$error_stack`. Default: `false` (privacy-safe). */
1222
+ includeStack?: boolean;
1223
+ /** Extra properties merged into every captured `$error` event. */
1224
+ properties?: Record<string, unknown>;
1225
+ }
1226
+ /**
1227
+ * Configuration for the providers' `autoCaptureErrors` option. Extends
1228
+ * {@link CaptureExceptionOptions} (applied to unhandled global errors) with
1229
+ * toggles for the global handlers and console capture.
1230
+ */
1231
+ interface AutoCaptureErrorsOptions extends CaptureExceptionOptions {
1232
+ /**
1233
+ * Install the platform global error handlers (web `window` `error` /
1234
+ * `unhandledrejection`, React Native `ErrorUtils`). Default: `true`.
1235
+ */
1236
+ globalHandlers?: boolean;
1237
+ /**
1238
+ * Also capture console output as `$error` events. `true` captures
1239
+ * `console.error`; pass {@link ConsoleCaptureOptions} to configure levels.
1240
+ * Default: off.
1241
+ */
1242
+ console?: boolean | ConsoleCaptureOptions;
1243
+ }
1244
+ /**
1245
+ * Patch the global `console` so that `console.error` / `console.warn` (per
1246
+ * `levels`) are also captured as SunGlasses `$error` events
1247
+ * (`$error_handled: false`, `$error_source: 'console'`).
1248
+ *
1249
+ * Works identically on web and React Native — both expose a global `console`.
1250
+ * The original method is always called first, so logs still appear in the
1251
+ * console. Returns an unpatch function that restores the original methods.
1252
+ *
1253
+ * Guards against infinite recursion: SunGlasses' own logger writes to
1254
+ * `console.error`/`console.warn` (prefixed with `[SunGlasses]`), and
1255
+ * `client.capture()` may log on failure. A re-entrancy flag plus a prefix skip
1256
+ * prevent a capture -> log -> capture loop.
1257
+ *
1258
+ * @param client - SunGlasses client instance.
1259
+ * @param options - Optional capture configuration.
1260
+ * @returns A function that restores the original console methods.
1261
+ *
1262
+ * @example
1263
+ * ```ts
1264
+ * const unpatch = patchConsole(client, { levels: ['error', 'warn'] });
1265
+ * // ... later
1266
+ * unpatch();
1267
+ * ```
1268
+ */
1269
+ declare function patchConsole(client: ISunglassesClient, options?: ConsoleCaptureOptions): () => void;
1270
+
1206
1271
  /**
1207
1272
  * A typed analytics stub that is safe to use before the SDK initialises.
1208
1273
  *
@@ -1275,4 +1340,4 @@ declare function sha256Hex(input: string): Promise<string>;
1275
1340
  */
1276
1341
  declare function nowISO(): string;
1277
1342
 
1278
- export { type AppMetadata, type AppUpdateInfo, type CaptureExceptionOptions, type CleanupConfig, type ConsentHistoryEntry, ConsentManager, type ConsentState, type ConsentStatus, type ErrorEventProperties, type EventContext, type EventCountPeriod, EventCounter, type EventMap, EventQueue, type EventType, FrequencyMiddleware, type FrequencyMiddlewareOptions, type HttpAdapterConfig, type IAnalyticsAdapter, type IEventCounter, type IMiddleware, type IStorageAdapter, type ISunglassesClient, type ISunglassesTypedClient, IdentityManager, type IdentityState, LocalEventArchive, type Logger, type MiddlewareNext, MiddlewarePipeline, PiiSanitizer, SamplingMiddleware, type SamplingMiddlewareOptions, type ScreenTrackingOptions, SessionManager, type SessionState, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, captureException, createLazyClient, createLogger, generateUUID, nowISO, sha256Hex };
1343
+ export { type AppMetadata, type AppUpdateInfo, type AutoCaptureErrorsOptions, type CaptureExceptionOptions, type CleanupConfig, type ConsentHistoryEntry, ConsentManager, type ConsentState, type ConsentStatus, type ConsoleCaptureOptions, type ConsoleLevel, type ErrorEventProperties, type EventContext, type EventCountPeriod, EventCounter, type EventMap, EventQueue, type EventType, FrequencyMiddleware, type FrequencyMiddlewareOptions, type HttpAdapterConfig, type IAnalyticsAdapter, type IEventCounter, type IMiddleware, type IStorageAdapter, type ISunglassesClient, type ISunglassesTypedClient, IdentityManager, type IdentityState, LocalEventArchive, type Logger, type MiddlewareNext, MiddlewarePipeline, PiiSanitizer, SamplingMiddleware, type SamplingMiddlewareOptions, type ScreenTrackingOptions, SessionManager, type SessionState, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, captureException, createLazyClient, createLogger, generateUUID, nowISO, patchConsole, sha256Hex };
package/dist/index.js CHANGED
@@ -38,6 +38,7 @@ __export(index_exports, {
38
38
  createLogger: () => createLogger,
39
39
  generateUUID: () => generateUUID,
40
40
  nowISO: () => nowISO,
41
+ patchConsole: () => patchConsole,
41
42
  sha256Hex: () => sha256Hex
42
43
  });
43
44
  module.exports = __toCommonJS(index_exports);
@@ -1645,6 +1646,65 @@ function captureException(client, error, options = {}) {
1645
1646
  }
1646
1647
  }
1647
1648
 
1649
+ // src/patchConsole.ts
1650
+ var SELF_LOG_PREFIX = "[SunGlasses]";
1651
+ var LEVEL_TO_SEVERITY = {
1652
+ error: "error",
1653
+ warn: "warning"
1654
+ };
1655
+ function composeMessage(args) {
1656
+ return args.map((arg) => {
1657
+ if (typeof arg === "string") return arg;
1658
+ if (arg instanceof Error) return arg.message;
1659
+ try {
1660
+ return typeof arg === "object" && arg !== null ? JSON.stringify(arg) : String(arg);
1661
+ } catch {
1662
+ return "[object]";
1663
+ }
1664
+ }).join(" ");
1665
+ }
1666
+ function patchConsole(client, options = {}) {
1667
+ const {
1668
+ levels = ["error"],
1669
+ ignorePatterns = [],
1670
+ maxMessageLength = 200,
1671
+ includeStack = false,
1672
+ properties
1673
+ } = options;
1674
+ const uniqueLevels = Array.from(new Set(levels));
1675
+ const originals = /* @__PURE__ */ new Map();
1676
+ let isCapturing = false;
1677
+ for (const level of uniqueLevels) {
1678
+ const original = console[level];
1679
+ originals.set(level, original);
1680
+ console[level] = (...args) => {
1681
+ original.apply(console, args);
1682
+ if (isCapturing) return;
1683
+ if (typeof args[0] === "string" && args[0].startsWith(SELF_LOG_PREFIX)) return;
1684
+ const rawMessage = composeMessage(args);
1685
+ if (ignorePatterns.some((p) => p.test(rawMessage))) return;
1686
+ const errorArg = args.find((a) => a instanceof Error);
1687
+ isCapturing = true;
1688
+ try {
1689
+ captureException(client, errorArg ?? rawMessage, {
1690
+ handled: false,
1691
+ level: LEVEL_TO_SEVERITY[level],
1692
+ includeStack,
1693
+ maxMessageLength,
1694
+ properties: { ...properties, $error_source: "console" }
1695
+ });
1696
+ } finally {
1697
+ isCapturing = false;
1698
+ }
1699
+ };
1700
+ }
1701
+ return () => {
1702
+ for (const [level, original] of originals) {
1703
+ console[level] = original;
1704
+ }
1705
+ };
1706
+ }
1707
+
1648
1708
  // src/LazyClient.ts
1649
1709
  function createLazyClient() {
1650
1710
  let _inner = null;
@@ -1782,5 +1842,6 @@ function createLazyClient() {
1782
1842
  createLogger,
1783
1843
  generateUUID,
1784
1844
  nowISO,
1845
+ patchConsole,
1785
1846
  sha256Hex
1786
1847
  });
package/dist/index.mjs CHANGED
@@ -1601,6 +1601,65 @@ function captureException(client, error, options = {}) {
1601
1601
  }
1602
1602
  }
1603
1603
 
1604
+ // src/patchConsole.ts
1605
+ var SELF_LOG_PREFIX = "[SunGlasses]";
1606
+ var LEVEL_TO_SEVERITY = {
1607
+ error: "error",
1608
+ warn: "warning"
1609
+ };
1610
+ function composeMessage(args) {
1611
+ return args.map((arg) => {
1612
+ if (typeof arg === "string") return arg;
1613
+ if (arg instanceof Error) return arg.message;
1614
+ try {
1615
+ return typeof arg === "object" && arg !== null ? JSON.stringify(arg) : String(arg);
1616
+ } catch {
1617
+ return "[object]";
1618
+ }
1619
+ }).join(" ");
1620
+ }
1621
+ function patchConsole(client, options = {}) {
1622
+ const {
1623
+ levels = ["error"],
1624
+ ignorePatterns = [],
1625
+ maxMessageLength = 200,
1626
+ includeStack = false,
1627
+ properties
1628
+ } = options;
1629
+ const uniqueLevels = Array.from(new Set(levels));
1630
+ const originals = /* @__PURE__ */ new Map();
1631
+ let isCapturing = false;
1632
+ for (const level of uniqueLevels) {
1633
+ const original = console[level];
1634
+ originals.set(level, original);
1635
+ console[level] = (...args) => {
1636
+ original.apply(console, args);
1637
+ if (isCapturing) return;
1638
+ if (typeof args[0] === "string" && args[0].startsWith(SELF_LOG_PREFIX)) return;
1639
+ const rawMessage = composeMessage(args);
1640
+ if (ignorePatterns.some((p) => p.test(rawMessage))) return;
1641
+ const errorArg = args.find((a) => a instanceof Error);
1642
+ isCapturing = true;
1643
+ try {
1644
+ captureException(client, errorArg ?? rawMessage, {
1645
+ handled: false,
1646
+ level: LEVEL_TO_SEVERITY[level],
1647
+ includeStack,
1648
+ maxMessageLength,
1649
+ properties: { ...properties, $error_source: "console" }
1650
+ });
1651
+ } finally {
1652
+ isCapturing = false;
1653
+ }
1654
+ };
1655
+ }
1656
+ return () => {
1657
+ for (const [level, original] of originals) {
1658
+ console[level] = original;
1659
+ }
1660
+ };
1661
+ }
1662
+
1604
1663
  // src/LazyClient.ts
1605
1664
  function createLazyClient() {
1606
1665
  let _inner = null;
@@ -1737,5 +1796,6 @@ export {
1737
1796
  createLogger,
1738
1797
  generateUUID,
1739
1798
  nowISO,
1799
+ patchConsole,
1740
1800
  sha256Hex
1741
1801
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drakkar.software/sunglasses-core",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Platform-agnostic event tracking engine for SunGlasses",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",