@drakkar.software/sunglasses-core 0.4.0 → 0.6.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
@@ -399,6 +399,20 @@ interface StarfishAdapterConfig {
399
399
  * Can be the same adapter as `SunglassesConfig.storage`.
400
400
  */
401
401
  pathStorage?: IStorageAdapter;
402
+ /**
403
+ * When `true`, events are pushed directly without a prior pull.
404
+ * No merge, no optimistic locking, no conflict detection.
405
+ *
406
+ * Use this for Starfish collections configured with `queueOnly: true` —
407
+ * the server ignores `baseHash` and returns no stored data on pull,
408
+ * so a pull round-trip is always wasted.
409
+ *
410
+ * On push failure the adapter **throws**, allowing SunglassesCore to keep
411
+ * events in the local queue and retry on the next flush interval.
412
+ *
413
+ * Cannot be combined with `rotatePathOnSuccess`.
414
+ */
415
+ pushOnly?: boolean;
402
416
  }
403
417
  /**
404
418
  * In-memory + persisted session state.
@@ -1087,6 +1101,58 @@ declare class LocalEventArchive {
1087
1101
  */
1088
1102
  declare function asTyped<T extends EventMap>(client: ISunglassesClient): ISunglassesTypedClient<T>;
1089
1103
 
1104
+ /**
1105
+ * A typed analytics stub that is safe to use before the SDK initialises.
1106
+ *
1107
+ * All calls are silent no-ops (or safe defaults) until `init()` is called
1108
+ * with the real `SunglassesCore` instance. After `init()`, every method
1109
+ * delegates to the real client transparently.
1110
+ *
1111
+ * This is the recommended pattern for module-level analytics singletons:
1112
+ *
1113
+ * ```typescript
1114
+ * // analytics.ts
1115
+ * import { createLazyClient, SunglassesCore } from '@drakkar.software/sunglasses-core';
1116
+ *
1117
+ * type MyEvents = {
1118
+ * button_clicked: { buttonId: string };
1119
+ * page_viewed: undefined;
1120
+ * };
1121
+ *
1122
+ * export const analytics = createLazyClient<MyEvents>();
1123
+ *
1124
+ * export async function initAnalytics() {
1125
+ * const client = await SunglassesCore.create({ ... });
1126
+ * analytics.init(client);
1127
+ * return client;
1128
+ * }
1129
+ * ```
1130
+ *
1131
+ * ```typescript
1132
+ * // anywhere else
1133
+ * import { analytics } from './analytics';
1134
+ *
1135
+ * // Safe at import time — noop if called before init(), real event after:
1136
+ * analytics.capture('button_clicked', { buttonId: 'cta' }); // ✓ typed
1137
+ * analytics.capture('unknown_event', {}); // ✗ TS error
1138
+ * ```
1139
+ *
1140
+ * ### Why not `Object.assign(stub, asTyped(client))`?
1141
+ *
1142
+ * `asTyped` returns the client itself. `Object.assign` only copies own
1143
+ * enumerable properties — class prototype methods like `capture()` are
1144
+ * **not** own properties and are silently skipped. Using `createLazyClient`
1145
+ * avoids this footgun entirely.
1146
+ */
1147
+ declare function createLazyClient<T extends EventMap>(): ISunglassesTypedClient<T> & {
1148
+ /**
1149
+ * Wire up the real SDK client.
1150
+ * Safe to call multiple times — last call wins.
1151
+ * After calling, all subsequent analytics calls delegate to `client`.
1152
+ */
1153
+ init(client: ISunglassesClient): void;
1154
+ };
1155
+
1090
1156
  /**
1091
1157
  * Generate a UUID v4 using the Web Crypto API.
1092
1158
  *
@@ -1107,4 +1173,4 @@ declare function sha256Hex(input: string): Promise<string>;
1107
1173
  */
1108
1174
  declare function nowISO(): string;
1109
1175
 
1110
- export { 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 StarfishAdapterConfig, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, createLogger, generateUUID, nowISO, sha256Hex };
1176
+ export { 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 StarfishAdapterConfig, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, createLazyClient, createLogger, generateUUID, nowISO, sha256Hex };
package/dist/index.d.ts CHANGED
@@ -399,6 +399,20 @@ interface StarfishAdapterConfig {
399
399
  * Can be the same adapter as `SunglassesConfig.storage`.
400
400
  */
401
401
  pathStorage?: IStorageAdapter;
402
+ /**
403
+ * When `true`, events are pushed directly without a prior pull.
404
+ * No merge, no optimistic locking, no conflict detection.
405
+ *
406
+ * Use this for Starfish collections configured with `queueOnly: true` —
407
+ * the server ignores `baseHash` and returns no stored data on pull,
408
+ * so a pull round-trip is always wasted.
409
+ *
410
+ * On push failure the adapter **throws**, allowing SunglassesCore to keep
411
+ * events in the local queue and retry on the next flush interval.
412
+ *
413
+ * Cannot be combined with `rotatePathOnSuccess`.
414
+ */
415
+ pushOnly?: boolean;
402
416
  }
403
417
  /**
404
418
  * In-memory + persisted session state.
@@ -1087,6 +1101,58 @@ declare class LocalEventArchive {
1087
1101
  */
1088
1102
  declare function asTyped<T extends EventMap>(client: ISunglassesClient): ISunglassesTypedClient<T>;
1089
1103
 
1104
+ /**
1105
+ * A typed analytics stub that is safe to use before the SDK initialises.
1106
+ *
1107
+ * All calls are silent no-ops (or safe defaults) until `init()` is called
1108
+ * with the real `SunglassesCore` instance. After `init()`, every method
1109
+ * delegates to the real client transparently.
1110
+ *
1111
+ * This is the recommended pattern for module-level analytics singletons:
1112
+ *
1113
+ * ```typescript
1114
+ * // analytics.ts
1115
+ * import { createLazyClient, SunglassesCore } from '@drakkar.software/sunglasses-core';
1116
+ *
1117
+ * type MyEvents = {
1118
+ * button_clicked: { buttonId: string };
1119
+ * page_viewed: undefined;
1120
+ * };
1121
+ *
1122
+ * export const analytics = createLazyClient<MyEvents>();
1123
+ *
1124
+ * export async function initAnalytics() {
1125
+ * const client = await SunglassesCore.create({ ... });
1126
+ * analytics.init(client);
1127
+ * return client;
1128
+ * }
1129
+ * ```
1130
+ *
1131
+ * ```typescript
1132
+ * // anywhere else
1133
+ * import { analytics } from './analytics';
1134
+ *
1135
+ * // Safe at import time — noop if called before init(), real event after:
1136
+ * analytics.capture('button_clicked', { buttonId: 'cta' }); // ✓ typed
1137
+ * analytics.capture('unknown_event', {}); // ✗ TS error
1138
+ * ```
1139
+ *
1140
+ * ### Why not `Object.assign(stub, asTyped(client))`?
1141
+ *
1142
+ * `asTyped` returns the client itself. `Object.assign` only copies own
1143
+ * enumerable properties — class prototype methods like `capture()` are
1144
+ * **not** own properties and are silently skipped. Using `createLazyClient`
1145
+ * avoids this footgun entirely.
1146
+ */
1147
+ declare function createLazyClient<T extends EventMap>(): ISunglassesTypedClient<T> & {
1148
+ /**
1149
+ * Wire up the real SDK client.
1150
+ * Safe to call multiple times — last call wins.
1151
+ * After calling, all subsequent analytics calls delegate to `client`.
1152
+ */
1153
+ init(client: ISunglassesClient): void;
1154
+ };
1155
+
1090
1156
  /**
1091
1157
  * Generate a UUID v4 using the Web Crypto API.
1092
1158
  *
@@ -1107,4 +1173,4 @@ declare function sha256Hex(input: string): Promise<string>;
1107
1173
  */
1108
1174
  declare function nowISO(): string;
1109
1175
 
1110
- export { 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 StarfishAdapterConfig, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, createLogger, generateUUID, nowISO, sha256Hex };
1176
+ export { 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 StarfishAdapterConfig, type SunglassesConfig, SunglassesCore, type SunglassesEvent, TraitManager, type UserDataExport, asTyped, createLazyClient, createLogger, generateUUID, nowISO, sha256Hex };
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  SunglassesCore: () => SunglassesCore,
34
34
  TraitManager: () => TraitManager,
35
35
  asTyped: () => asTyped,
36
+ createLazyClient: () => createLazyClient,
36
37
  createLogger: () => createLogger,
37
38
  generateUUID: () => generateUUID,
38
39
  nowISO: () => nowISO,
@@ -1590,6 +1591,105 @@ var SamplingMiddleware = class {
1590
1591
  function asTyped(client) {
1591
1592
  return client;
1592
1593
  }
1594
+
1595
+ // src/LazyClient.ts
1596
+ function createLazyClient() {
1597
+ let _inner = null;
1598
+ const lazy = {
1599
+ init(client) {
1600
+ _inner = client;
1601
+ },
1602
+ // ── Event tracking ────────────────────────────────────────────────────────
1603
+ capture(eventName, properties, options) {
1604
+ _inner?.capture(eventName, properties, options);
1605
+ },
1606
+ screen(screenName, properties) {
1607
+ _inner?.screen(screenName, properties);
1608
+ },
1609
+ identify(userId, traits) {
1610
+ _inner?.identify(userId, traits);
1611
+ },
1612
+ alias(newId, existingId) {
1613
+ _inner?.alias(newId, existingId);
1614
+ },
1615
+ group(groupId, groupTraits) {
1616
+ _inner?.group(groupId, groupTraits);
1617
+ },
1618
+ async reset() {
1619
+ await _inner?.reset();
1620
+ },
1621
+ // ── Super properties ──────────────────────────────────────────────────────
1622
+ register(properties) {
1623
+ _inner?.register(properties);
1624
+ },
1625
+ unregister(...keys) {
1626
+ _inner?.unregister(...keys);
1627
+ },
1628
+ getRegisteredProperties() {
1629
+ return _inner?.getRegisteredProperties() ?? {};
1630
+ },
1631
+ // ── Consent ───────────────────────────────────────────────────────────────
1632
+ async optIn() {
1633
+ await _inner?.optIn();
1634
+ },
1635
+ async optOut() {
1636
+ await _inner?.optOut();
1637
+ },
1638
+ hasOptedIn() {
1639
+ return _inner?.hasOptedIn() ?? false;
1640
+ },
1641
+ hasOptedOut() {
1642
+ return _inner?.hasOptedOut() ?? false;
1643
+ },
1644
+ getConsentStatus() {
1645
+ return _inner?.getConsentStatus() ?? "unknown";
1646
+ },
1647
+ getConsentHistory() {
1648
+ return _inner?.getConsentHistory() ?? [];
1649
+ },
1650
+ // ── Lifecycle ─────────────────────────────────────────────────────────────
1651
+ async flush() {
1652
+ await _inner?.flush();
1653
+ },
1654
+ async shutdown() {
1655
+ await _inner?.shutdown();
1656
+ },
1657
+ // ── Event counting ────────────────────────────────────────────────────────
1658
+ async getEventCount(eventName, period, date) {
1659
+ return _inner?.getEventCount(eventName, period, date) ?? Promise.resolve(0);
1660
+ },
1661
+ async resetEventCount(eventName) {
1662
+ await _inner?.resetEventCount(eventName);
1663
+ },
1664
+ get eventCounter() {
1665
+ return _inner?.eventCounter ?? null;
1666
+ },
1667
+ getQueuedEventCount() {
1668
+ return _inner?.getQueuedEventCount() ?? 0;
1669
+ },
1670
+ // ── Local archive / data portability ─────────────────────────────────────
1671
+ async clearLocalArchive(config) {
1672
+ await _inner?.clearLocalArchive(config);
1673
+ },
1674
+ async exportUserData() {
1675
+ return _inner?.exportUserData() ?? {
1676
+ exportedAt: (/* @__PURE__ */ new Date()).toISOString(),
1677
+ anonymousId: "",
1678
+ distinctId: null,
1679
+ traits: {},
1680
+ consentStatus: "unknown",
1681
+ consentHistory: [],
1682
+ queuedEvents: [],
1683
+ archivedEvents: [],
1684
+ eventCountSummary: {}
1685
+ };
1686
+ },
1687
+ async deleteUserData(options) {
1688
+ await _inner?.deleteUserData(options);
1689
+ }
1690
+ };
1691
+ return lazy;
1692
+ }
1593
1693
  // Annotate the CommonJS export names for ESM import in node:
1594
1694
  0 && (module.exports = {
1595
1695
  ConsentManager,
@@ -1605,6 +1705,7 @@ function asTyped(client) {
1605
1705
  SunglassesCore,
1606
1706
  TraitManager,
1607
1707
  asTyped,
1708
+ createLazyClient,
1608
1709
  createLogger,
1609
1710
  generateUUID,
1610
1711
  nowISO,
package/dist/index.mjs CHANGED
@@ -1548,6 +1548,105 @@ var SamplingMiddleware = class {
1548
1548
  function asTyped(client) {
1549
1549
  return client;
1550
1550
  }
1551
+
1552
+ // src/LazyClient.ts
1553
+ function createLazyClient() {
1554
+ let _inner = null;
1555
+ const lazy = {
1556
+ init(client) {
1557
+ _inner = client;
1558
+ },
1559
+ // ── Event tracking ────────────────────────────────────────────────────────
1560
+ capture(eventName, properties, options) {
1561
+ _inner?.capture(eventName, properties, options);
1562
+ },
1563
+ screen(screenName, properties) {
1564
+ _inner?.screen(screenName, properties);
1565
+ },
1566
+ identify(userId, traits) {
1567
+ _inner?.identify(userId, traits);
1568
+ },
1569
+ alias(newId, existingId) {
1570
+ _inner?.alias(newId, existingId);
1571
+ },
1572
+ group(groupId, groupTraits) {
1573
+ _inner?.group(groupId, groupTraits);
1574
+ },
1575
+ async reset() {
1576
+ await _inner?.reset();
1577
+ },
1578
+ // ── Super properties ──────────────────────────────────────────────────────
1579
+ register(properties) {
1580
+ _inner?.register(properties);
1581
+ },
1582
+ unregister(...keys) {
1583
+ _inner?.unregister(...keys);
1584
+ },
1585
+ getRegisteredProperties() {
1586
+ return _inner?.getRegisteredProperties() ?? {};
1587
+ },
1588
+ // ── Consent ───────────────────────────────────────────────────────────────
1589
+ async optIn() {
1590
+ await _inner?.optIn();
1591
+ },
1592
+ async optOut() {
1593
+ await _inner?.optOut();
1594
+ },
1595
+ hasOptedIn() {
1596
+ return _inner?.hasOptedIn() ?? false;
1597
+ },
1598
+ hasOptedOut() {
1599
+ return _inner?.hasOptedOut() ?? false;
1600
+ },
1601
+ getConsentStatus() {
1602
+ return _inner?.getConsentStatus() ?? "unknown";
1603
+ },
1604
+ getConsentHistory() {
1605
+ return _inner?.getConsentHistory() ?? [];
1606
+ },
1607
+ // ── Lifecycle ─────────────────────────────────────────────────────────────
1608
+ async flush() {
1609
+ await _inner?.flush();
1610
+ },
1611
+ async shutdown() {
1612
+ await _inner?.shutdown();
1613
+ },
1614
+ // ── Event counting ────────────────────────────────────────────────────────
1615
+ async getEventCount(eventName, period, date) {
1616
+ return _inner?.getEventCount(eventName, period, date) ?? Promise.resolve(0);
1617
+ },
1618
+ async resetEventCount(eventName) {
1619
+ await _inner?.resetEventCount(eventName);
1620
+ },
1621
+ get eventCounter() {
1622
+ return _inner?.eventCounter ?? null;
1623
+ },
1624
+ getQueuedEventCount() {
1625
+ return _inner?.getQueuedEventCount() ?? 0;
1626
+ },
1627
+ // ── Local archive / data portability ─────────────────────────────────────
1628
+ async clearLocalArchive(config) {
1629
+ await _inner?.clearLocalArchive(config);
1630
+ },
1631
+ async exportUserData() {
1632
+ return _inner?.exportUserData() ?? {
1633
+ exportedAt: (/* @__PURE__ */ new Date()).toISOString(),
1634
+ anonymousId: "",
1635
+ distinctId: null,
1636
+ traits: {},
1637
+ consentStatus: "unknown",
1638
+ consentHistory: [],
1639
+ queuedEvents: [],
1640
+ archivedEvents: [],
1641
+ eventCountSummary: {}
1642
+ };
1643
+ },
1644
+ async deleteUserData(options) {
1645
+ await _inner?.deleteUserData(options);
1646
+ }
1647
+ };
1648
+ return lazy;
1649
+ }
1551
1650
  export {
1552
1651
  ConsentManager,
1553
1652
  EventCounter,
@@ -1562,6 +1661,7 @@ export {
1562
1661
  SunglassesCore,
1563
1662
  TraitManager,
1564
1663
  asTyped,
1664
+ createLazyClient,
1565
1665
  createLogger,
1566
1666
  generateUUID,
1567
1667
  nowISO,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drakkar.software/sunglasses-core",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Platform-agnostic event tracking engine for SunGlasses",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",