react-native-edgee 1.0.4 → 1.0.6

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.
@@ -3,20 +3,40 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.EdgeeStore = void 0;
6
+ exports.edgeeStore = exports.EdgeeStore = void 0;
7
7
  const async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
8
+ const utils_1 = require("./utils");
9
+ const STORAGE_KEYS = {
10
+ edgeeId: "_edgee",
11
+ userId: "_edgee_u",
12
+ pendingEvents: "_edgee_events",
13
+ };
8
14
  class EdgeeStore {
9
15
  constructor() {
10
- this.store = async_storage_1.default;
11
16
  this.pendingEvents = [];
17
+ this.initialized = false;
12
18
  this.edgeeId = null;
13
19
  this.userId = null;
14
20
  }
15
21
  async init() {
16
- var _a, _b, _c;
17
- this.edgeeId = (_a = (await this.store.getItem("_edgee"))) !== null && _a !== void 0 ? _a : "";
18
- this.userId = (_b = (await this.store.getItem("_edgee_u"))) !== null && _b !== void 0 ? _b : "";
19
- this.pendingEvents = JSON.parse((_c = (await this.store.getItem("_edgee_events"))) !== null && _c !== void 0 ? _c : "[]");
22
+ // Prevent multiple initializations
23
+ if (this.initialized) {
24
+ return;
25
+ }
26
+ try {
27
+ const storedEdgeeId = await async_storage_1.default.getItem(STORAGE_KEYS.edgeeId);
28
+ const storedUserId = await async_storage_1.default.getItem(STORAGE_KEYS.userId);
29
+ const pendingEvents = await async_storage_1.default.getItem(STORAGE_KEYS.pendingEvents);
30
+ // Only set if values exist (not null/empty)
31
+ this.edgeeId = storedEdgeeId && storedEdgeeId !== "" ? storedEdgeeId : null;
32
+ this.userId = storedUserId && storedUserId !== "" ? storedUserId : null;
33
+ this.pendingEvents = pendingEvents ? JSON.parse(pendingEvents) : [];
34
+ this.initialized = true;
35
+ }
36
+ catch (error) {
37
+ (0, utils_1.logError)("Failed to initialize Edgee store", error);
38
+ this.initialized = true;
39
+ }
20
40
  }
21
41
  async getEdgeeId() {
22
42
  return this.edgeeId;
@@ -25,12 +45,16 @@ class EdgeeStore {
25
45
  return this.userId;
26
46
  }
27
47
  async setEdgeeId(edgeeId) {
28
- this.edgeeId = edgeeId;
29
- await this.store.setItem("_edgee", edgeeId);
48
+ if (edgeeId && edgeeId !== "") {
49
+ this.edgeeId = edgeeId;
50
+ await async_storage_1.default.setItem(STORAGE_KEYS.edgeeId, edgeeId);
51
+ }
30
52
  }
31
53
  async setUserId(userId) {
32
- this.userId = userId;
33
- await this.store.setItem("_edgee_u", userId);
54
+ if (userId && userId !== "") {
55
+ this.userId = userId;
56
+ await async_storage_1.default.setItem(STORAGE_KEYS.userId, userId);
57
+ }
34
58
  }
35
59
  async getContext() {
36
60
  return {
@@ -40,14 +64,19 @@ class EdgeeStore {
40
64
  }
41
65
  async addEvent(event) {
42
66
  this.pendingEvents.push(event);
43
- await this.store.setItem("_edgee_events", JSON.stringify(this.pendingEvents));
67
+ await async_storage_1.default.setItem(STORAGE_KEYS.pendingEvents, JSON.stringify(this.pendingEvents));
44
68
  }
45
69
  async getPendingEvents() {
46
70
  return this.pendingEvents;
47
71
  }
48
72
  async clearEvents() {
49
73
  this.pendingEvents = [];
50
- await this.store.removeItem("_edgee_events");
74
+ await async_storage_1.default.removeItem(STORAGE_KEYS.pendingEvents);
51
75
  }
52
76
  }
53
77
  exports.EdgeeStore = EdgeeStore;
78
+ /**
79
+ * Global edgee store instance
80
+ * This allows you to manage the edgee store across your app without passing it around
81
+ */
82
+ exports.edgeeStore = new EdgeeStore();
@@ -1,17 +1,13 @@
1
1
  import { EdgeeConfig } from "..";
2
2
  import { EdgeeEvent } from "../api";
3
- import { EdgeeStore } from "./edgee-store";
4
- import { QueryBuilder } from "./query-builder";
5
3
  export declare class EventQueue {
6
- private store;
7
- private queryBuilder;
8
4
  private config;
9
5
  private q;
10
6
  private ready;
11
7
  private online;
12
8
  private flushing;
13
9
  private unsubscribe?;
14
- constructor(store: EdgeeStore, config: EdgeeConfig, queryBuilder: QueryBuilder);
10
+ constructor(config: EdgeeConfig);
15
11
  destroy(): void;
16
12
  setReady(val: boolean): void;
17
13
  enqueue(item: EdgeeEvent): void;
@@ -6,15 +6,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.EventQueue = void 0;
7
7
  const netinfo_1 = __importDefault(require("@react-native-community/netinfo"));
8
8
  const api_1 = require("../api");
9
+ const edgee_store_1 = require("./edgee-store");
10
+ const utils_1 = require("./utils");
9
11
  class EventQueue {
10
- constructor(store, config, queryBuilder) {
12
+ constructor(config) {
11
13
  this.q = [];
12
14
  this.ready = false;
13
15
  this.online = false;
14
16
  this.flushing = false;
15
- this.store = store;
16
17
  this.config = config;
17
- this.queryBuilder = queryBuilder;
18
18
  // Track connectivity
19
19
  this.unsubscribe = netinfo_1.default.addEventListener((state) => {
20
20
  this.online = !!state.isConnected && !!state.isInternetReachable;
@@ -24,7 +24,7 @@ class EventQueue {
24
24
  void netinfo_1.default.fetch().then((state) => {
25
25
  this.online = !!state.isConnected && !!state.isInternetReachable;
26
26
  });
27
- void this.store.getPendingEvents().then((events) => {
27
+ void edgee_store_1.edgeeStore.getPendingEvents().then((events) => {
28
28
  this.q = events;
29
29
  this.ready = true;
30
30
  });
@@ -40,11 +40,7 @@ class EventQueue {
40
40
  }
41
41
  enqueue(item) {
42
42
  this.q.push(item);
43
- if (this.config.debug) {
44
- // eslint-disable-next-line no-console
45
- console.log("[Edgee RN] enqueue", item);
46
- }
47
- void this.store.addEvent(item);
43
+ void edgee_store_1.edgeeStore.addEvent(item);
48
44
  if (this.shouldFlush())
49
45
  void this.flush();
50
46
  }
@@ -54,51 +50,15 @@ class EventQueue {
54
50
  */
55
51
  async sendEvent(payload) {
56
52
  if (!this.online) {
57
- if (this.config.debug) {
58
- console.log("[Edgee RN] Offline - queuing direct payload");
59
- }
60
53
  // For direct payloads, we can't easily queue them in the current structure
61
54
  // So we'll send them when we come back online
62
55
  return;
63
56
  }
64
57
  try {
65
- const url = await this.queryBuilder.getUrl();
66
- if (this.config.debug) {
67
- console.log("[Edgee RN] sending direct payload", { url, payload });
68
- }
69
- const response = await fetch(url, {
70
- method: "POST",
71
- headers: {
72
- "Content-Type": "application/json",
73
- ...(this.config.debug ? { "Edgee-Debug": "true" } : {}),
74
- },
75
- body: JSON.stringify(payload),
76
- });
77
- const text = await response.text().catch(() => "");
78
- if (this.config.debug) {
79
- console.log("[Edgee RN] direct payload response", {
80
- status: response.status,
81
- ok: response.ok,
82
- body: text,
83
- });
84
- }
85
- // Handle ID updates from response
86
- try {
87
- const data = text ? JSON.parse(text) : null;
88
- if (data && (data.e || data.u)) {
89
- if (data.e)
90
- await this.store.setEdgeeId(data.e);
91
- if (data.u)
92
- await this.store.setUserId(data.u);
93
- }
94
- }
95
- catch (e) {
96
- if (this.config.debug)
97
- console.log("persist ids error", e);
98
- }
58
+ await (0, api_1.sendPayload)(this.config, payload);
99
59
  }
100
60
  catch (error) {
101
- console.warn("[Edgee RN] Failed to send direct payload:", error);
61
+ (0, utils_1.logError)("[Queue] Error sending event.", error);
102
62
  throw error;
103
63
  }
104
64
  }
@@ -112,38 +72,15 @@ class EventQueue {
112
72
  const items = [...this.q];
113
73
  try {
114
74
  for (const item of items) {
115
- const url = await this.queryBuilder.getUrl();
116
- const resp = await (0, api_1.uploadEvent)(url, this.config, item);
117
- const text = await resp.text().catch(() => "");
118
- if (this.config.debug) {
119
- // eslint-disable-next-line no-console
120
- console.log("[Edgee RN] response", {
121
- status: resp.status,
122
- ok: resp.ok,
123
- body: text,
124
- });
125
- }
126
- try {
127
- const data = text ? JSON.parse(text) : null;
128
- if (data && (data.e || data.u)) {
129
- if (data.e)
130
- await this.store.setEdgeeId(data.e);
131
- if (data.u)
132
- await this.store.setUserId(data.u);
133
- }
134
- }
135
- catch (e) {
136
- if (this.config.debug)
137
- console.log("persist ids error", e);
138
- }
75
+ await (0, api_1.uploadEvent)(this.config, item);
139
76
  }
140
77
  this.q = [];
141
- await this.store.clearEvents();
78
+ await edgee_store_1.edgeeStore.clearEvents();
142
79
  }
143
80
  catch (error) {
144
- console.log("flush error", error);
81
+ (0, utils_1.logError)("Error processing event batch.", error);
145
82
  this.q = [];
146
- await this.store.clearEvents();
83
+ await edgee_store_1.edgeeStore.clearEvents();
147
84
  }
148
85
  finally {
149
86
  this.flushing = false;
@@ -0,0 +1,17 @@
1
+ import { EdgeeConfig } from "..";
2
+ /**
3
+ * Check if debug mode is enabled
4
+ */
5
+ export declare const hasDebug: (config?: EdgeeConfig | null) => boolean;
6
+ /**
7
+ * Log function matching JS SDK style
8
+ * Uses styled console.log with EDGEE prefix
9
+ * In React Native, we use a consistent prefix format since CSS styling isn't available
10
+ * Formats all arguments into a single string for Metro CLI compatibility
11
+ */
12
+ export declare const log: (config: EdgeeConfig | null | undefined, ...data: any[]) => void;
13
+ /**
14
+ * Log error function matching JS SDK style
15
+ * Uses styled console.error with EDGEE prefix
16
+ */
17
+ export declare const logError: (...data: any[]) => void;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logError = exports.log = exports.hasDebug = void 0;
4
+ /**
5
+ * Check if debug mode is enabled
6
+ */
7
+ const hasDebug = (config) => {
8
+ return (config === null || config === void 0 ? void 0 : config.debug) === true;
9
+ };
10
+ exports.hasDebug = hasDebug;
11
+ /**
12
+ * Format data array into a single string for Metro CLI compatibility
13
+ */
14
+ const formatLogData = (...data) => {
15
+ return data
16
+ .map((item) => {
17
+ if (item === null)
18
+ return "null";
19
+ if (item === undefined)
20
+ return "undefined";
21
+ if (typeof item === "string")
22
+ return item;
23
+ if (typeof item === "object") {
24
+ try {
25
+ return JSON.stringify(item, null, 2);
26
+ }
27
+ catch {
28
+ return String(item);
29
+ }
30
+ }
31
+ return String(item);
32
+ })
33
+ .join(" ");
34
+ };
35
+ /**
36
+ * Log function matching JS SDK style
37
+ * Uses styled console.log with EDGEE prefix
38
+ * In React Native, we use a consistent prefix format since CSS styling isn't available
39
+ * Formats all arguments into a single string for Metro CLI compatibility
40
+ */
41
+ const log = (config, ...data) => {
42
+ if (console && typeof console.log === "function" && (0, exports.hasDebug)(config)) {
43
+ console.log(`[EDGEE] ${formatLogData(...data)}`);
44
+ }
45
+ };
46
+ exports.log = log;
47
+ /**
48
+ * Log error function matching JS SDK style
49
+ * Uses styled console.error with EDGEE prefix
50
+ */
51
+ const logError = (...data) => {
52
+ console.error("[EDGEE]", ...data);
53
+ };
54
+ exports.logError = logError;
package/dist/index.d.ts CHANGED
@@ -1,36 +1,42 @@
1
- export type ConsentStatus = "granted" | "denied" | "pending";
1
+ import { ConsentStatus } from "./consent";
2
2
  export type EdgeeConfig = {
3
3
  host: string;
4
4
  debug?: boolean;
5
5
  collectDeviceId?: boolean;
6
+ cookieName?: string;
6
7
  };
7
8
  type Components = Record<string, boolean> | undefined;
8
9
  export declare class EdgeeClient {
9
10
  private appState;
10
- private store;
11
11
  private queue;
12
- private queryBuilder;
13
12
  private appStateSubscription;
14
13
  private config;
15
14
  constructor(config: EdgeeConfig);
16
15
  private handleAppStateChange;
16
+ /**
17
+ * Track a screen view event
18
+ */
19
+ screen(screenName: string, data: object, components?: Components): Promise<void>;
20
+ /**
21
+ * Track a custom event
22
+ */
17
23
  track(name: string, data: object, components?: Components): Promise<void>;
24
+ /**
25
+ * Track a user event
26
+ */
18
27
  user(data: object, components?: Components): Promise<void>;
19
- screen(screenName: string, data: object, components?: Components): Promise<void>;
28
+ /**
29
+ * Send consent event directly to Edgee (matches web SDK format)
30
+ */
31
+ consent(status: ConsentStatus): Promise<void>;
20
32
  setConsent(status: ConsentStatus): Promise<void>;
21
33
  getConsent(): ConsentStatus | null;
22
34
  hasConsent(): boolean;
23
- canTrack(): boolean;
24
35
  resetConsent(): Promise<void>;
25
36
  onConsentChange(callback: (status: ConsentStatus | null) => void): () => void;
26
- /**
27
- * Send consent event directly to Edgee (matches web SDK format)
28
- */
29
- consent(status: ConsentStatus): Promise<void>;
30
37
  }
31
- export * as navigation from "./navigation";
32
- export { EdgeeAutoTracker, EdgeeProvider, useEdgee, useEdgeeConsent, } from "./react";
38
+ export { EdgeeProvider, useEdgee, } from "./react";
33
39
  export type { EdgeeClientContext, EdgeeFullContext, EdgeePageContext, } from "./core/context";
34
40
  export { clearContextCache, getNativeContext, isNativeModuleAvailable, } from "./native-context";
35
41
  export type { EdgeeNativeContext, EdgeeNativeContextConfig, EdgeeReactNativeModule, } from "./types";
36
- export { ConsentUtils, EdgeeConsent, edgeeConsent } from "./consent";
42
+ export type { ConsentStatus, } from "./consent";