@thumbmarkjs/thumbmarkjs 1.1.3-rc.2 → 1.1.3-rc.3
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/package.json +1 -1
- package/src/functions/index.ts +18 -3
- package/src/utils/visitorId.ts +27 -0
package/package.json
CHANGED
package/src/functions/index.ts
CHANGED
|
@@ -28,6 +28,7 @@ import { getVersion } from "../utils/version";
|
|
|
28
28
|
import { filterThumbmarkData } from './filterComponents'
|
|
29
29
|
import { logThumbmarkData } from '../utils/log';
|
|
30
30
|
import { API_ENDPOINT } from "../options";
|
|
31
|
+
import { getVisitorId, setVisitorId } from "../utils/visitorId";
|
|
31
32
|
|
|
32
33
|
// ===================== Types & Interfaces =====================
|
|
33
34
|
|
|
@@ -58,10 +59,10 @@ interface infoInterface {
|
|
|
58
59
|
* API response structure
|
|
59
60
|
*/
|
|
60
61
|
interface apiResponse {
|
|
61
|
-
thumbmark?: string;
|
|
62
62
|
info?: infoInterface;
|
|
63
63
|
version?: string;
|
|
64
64
|
components?: componentInterface;
|
|
65
|
+
visitorId?: string;
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
/**
|
|
@@ -105,6 +106,17 @@ export const getApiPromise = (
|
|
|
105
106
|
|
|
106
107
|
// 3. Otherwise, initiate a new API call with timeout.
|
|
107
108
|
const endpoint = `${API_ENDPOINT}/thumbmark`;
|
|
109
|
+
const visitorId = getVisitorId();
|
|
110
|
+
const requestBody: any = {
|
|
111
|
+
components,
|
|
112
|
+
options,
|
|
113
|
+
clientHash: hash(JSON.stringify(components)),
|
|
114
|
+
version: getVersion()
|
|
115
|
+
};
|
|
116
|
+
if (visitorId) {
|
|
117
|
+
requestBody.visitorId = visitorId;
|
|
118
|
+
}
|
|
119
|
+
|
|
108
120
|
const fetchPromise = fetch(endpoint, {
|
|
109
121
|
method: 'POST',
|
|
110
122
|
headers: {
|
|
@@ -112,7 +124,7 @@ export const getApiPromise = (
|
|
|
112
124
|
'Authorization': 'custom-authorized',
|
|
113
125
|
'Content-Type': 'application/json',
|
|
114
126
|
},
|
|
115
|
-
body: JSON.stringify(
|
|
127
|
+
body: JSON.stringify(requestBody),
|
|
116
128
|
})
|
|
117
129
|
.then(response => {
|
|
118
130
|
// Handle HTTP errors that aren't network errors
|
|
@@ -122,6 +134,10 @@ export const getApiPromise = (
|
|
|
122
134
|
return response.json();
|
|
123
135
|
})
|
|
124
136
|
.then(data => {
|
|
137
|
+
// Handle visitor ID from server response
|
|
138
|
+
if (data.visitorId && data.visitorId !== visitorId) {
|
|
139
|
+
setVisitorId(data.visitorId);
|
|
140
|
+
}
|
|
125
141
|
apiPromiseResult = data; // Cache the successful result
|
|
126
142
|
currentApiPromise = null; // Clear the in-flight promise
|
|
127
143
|
return data;
|
|
@@ -138,7 +154,6 @@ export const getApiPromise = (
|
|
|
138
154
|
const timeoutPromise = new Promise<apiResponse>((resolve) => {
|
|
139
155
|
setTimeout(() => {
|
|
140
156
|
resolve({
|
|
141
|
-
thumbmark: hash(JSON.stringify(components)),
|
|
142
157
|
info: { timed_out: true },
|
|
143
158
|
version: getVersion(),
|
|
144
159
|
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visitor ID storage utilities - localStorage only, server generates IDs
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const VISITOR_ID_KEY = 'thumbmark_visitor_id';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Gets visitor ID from localStorage, returns null if unavailable
|
|
9
|
+
*/
|
|
10
|
+
export function getVisitorId(): string | null {
|
|
11
|
+
try {
|
|
12
|
+
return localStorage.getItem(VISITOR_ID_KEY);
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Sets visitor ID in localStorage
|
|
20
|
+
*/
|
|
21
|
+
export function setVisitorId(visitorId: string): void {
|
|
22
|
+
try {
|
|
23
|
+
localStorage.setItem(VISITOR_ID_KEY, visitorId);
|
|
24
|
+
} catch {
|
|
25
|
+
// Ignore storage errors
|
|
26
|
+
}
|
|
27
|
+
}
|