@callforge/tracking-client 0.4.2 → 0.5.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.js +32 -23
- package/dist/index.mjs +32 -23
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -58,24 +58,36 @@ var TrackingCache = class {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
|
-
* Get cached session if valid
|
|
62
|
-
*
|
|
61
|
+
* Get cached session if valid.
|
|
62
|
+
*
|
|
63
|
+
* Cache is valid when:
|
|
64
|
+
* - Not expired (with 30s buffer)
|
|
65
|
+
* - AND one of:
|
|
66
|
+
* - No locationId provided (page refresh without loc_physical_ms) → use any cache
|
|
67
|
+
* - locationId matches cached locId (same location)
|
|
68
|
+
*
|
|
69
|
+
* Cache is invalid when:
|
|
70
|
+
* - locationId provided but differs from cached locId (new location overwrites)
|
|
63
71
|
*/
|
|
64
72
|
get(locationId) {
|
|
65
73
|
const cached = this.read();
|
|
66
74
|
if (!cached) return null;
|
|
67
|
-
if (cached.locId !== locationId) return null;
|
|
68
75
|
if (cached.expiresAt - EXPIRY_BUFFER_MS <= Date.now()) return null;
|
|
76
|
+
if (!locationId) return cached;
|
|
77
|
+
if (cached.locId !== locationId) return null;
|
|
69
78
|
return cached;
|
|
70
79
|
}
|
|
71
80
|
/**
|
|
72
81
|
* Get sessionId for refresh if location matches (regardless of expiry).
|
|
73
82
|
* Used when cache is expired but location is same - send sessionId to server.
|
|
83
|
+
*
|
|
84
|
+
* Only returns sessionId if locationId is provided AND matches cached locId.
|
|
85
|
+
* (IP-based sessions don't send sessionId for refresh since location may differ)
|
|
74
86
|
*/
|
|
75
87
|
getSessionId(locationId) {
|
|
76
88
|
const cached = this.read();
|
|
77
89
|
if (!cached) return null;
|
|
78
|
-
if (cached.locId !== locationId) return null;
|
|
90
|
+
if (!locationId || cached.locId !== locationId) return null;
|
|
79
91
|
return cached.sessionId;
|
|
80
92
|
}
|
|
81
93
|
/**
|
|
@@ -223,18 +235,16 @@ var CallForge = class _CallForge {
|
|
|
223
235
|
}
|
|
224
236
|
async fetchSession() {
|
|
225
237
|
const locationId = this.getLocationId();
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return this.formatSession(cached);
|
|
232
|
-
}
|
|
238
|
+
const cached = this.cache.get(locationId);
|
|
239
|
+
if (cached) {
|
|
240
|
+
this.sessionId = cached.sessionId;
|
|
241
|
+
this.sessionCreated = true;
|
|
242
|
+
return this.formatSession(cached);
|
|
233
243
|
}
|
|
234
244
|
const autoParams = this.getAutoParams();
|
|
235
245
|
const cachedParams = this.cache.getParams();
|
|
236
246
|
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
237
|
-
if (
|
|
247
|
+
if (typeof window !== "undefined" && window.__cfTracking) {
|
|
238
248
|
try {
|
|
239
249
|
const data2 = await window.__cfTracking;
|
|
240
250
|
this.saveToCache(locationId, data2, params);
|
|
@@ -244,11 +254,9 @@ var CallForge = class _CallForge {
|
|
|
244
254
|
} catch (e) {
|
|
245
255
|
}
|
|
246
256
|
}
|
|
247
|
-
const cachedSessionId =
|
|
257
|
+
const cachedSessionId = this.cache.getSessionId(locationId);
|
|
248
258
|
const data = await this.fetchFromApi(locationId, cachedSessionId, params);
|
|
249
|
-
|
|
250
|
-
this.saveToCache(locationId, data, params);
|
|
251
|
-
}
|
|
259
|
+
this.saveToCache(locationId, data, params);
|
|
252
260
|
this.sessionId = data.sessionId;
|
|
253
261
|
this.sessionCreated = true;
|
|
254
262
|
return this.formatApiResponse(data);
|
|
@@ -360,19 +368,20 @@ var p={};
|
|
|
360
368
|
for(var i=0;i<ap.length;i++){var v=u.get(ap[i]);if(v)p[ap[i]]=v}
|
|
361
369
|
var key='${cacheKey}';
|
|
362
370
|
var sid=null;
|
|
363
|
-
|
|
371
|
+
try{
|
|
364
372
|
var c=JSON.parse(localStorage.getItem(key));
|
|
365
|
-
if(c&&c.
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
373
|
+
if(c&&c.expiresAt>Date.now()+30000){
|
|
374
|
+
if(!loc||(loc&&c.locId===loc)){c.params=Object.assign({},c.params,p);window.__cfTracking=Promise.resolve(c);return}
|
|
375
|
+
sid=(c.locId===loc)?c.sessionId:null;
|
|
376
|
+
var cp=c.params||{};
|
|
377
|
+
p=Object.assign({},cp,p);
|
|
378
|
+
}}catch(e){}
|
|
370
379
|
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}';
|
|
371
380
|
if(loc)url+='&loc_physical_ms='+loc;
|
|
372
381
|
if(sid)url+='&sessionId='+sid;
|
|
373
382
|
var ks=Object.keys(p).sort();
|
|
374
383
|
for(var j=0;j<ks.length;j++)url+='&'+ks[j]+'='+encodeURIComponent(p[ks[j]]);
|
|
375
|
-
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;
|
|
384
|
+
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;try{localStorage.setItem(key,JSON.stringify({locId:loc,sessionId:d.sessionId,phoneNumber:d.phoneNumber,location:d.location,expiresAt:d.expiresAt,params:p}))}catch(e){}return d});
|
|
376
385
|
})();`.replace(/\n/g, "");
|
|
377
386
|
return `<link rel="preconnect" href="${endpoint}">
|
|
378
387
|
<script>${script}</script>`;
|
package/dist/index.mjs
CHANGED
|
@@ -34,24 +34,36 @@ var TrackingCache = class {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
* Get cached session if valid
|
|
38
|
-
*
|
|
37
|
+
* Get cached session if valid.
|
|
38
|
+
*
|
|
39
|
+
* Cache is valid when:
|
|
40
|
+
* - Not expired (with 30s buffer)
|
|
41
|
+
* - AND one of:
|
|
42
|
+
* - No locationId provided (page refresh without loc_physical_ms) → use any cache
|
|
43
|
+
* - locationId matches cached locId (same location)
|
|
44
|
+
*
|
|
45
|
+
* Cache is invalid when:
|
|
46
|
+
* - locationId provided but differs from cached locId (new location overwrites)
|
|
39
47
|
*/
|
|
40
48
|
get(locationId) {
|
|
41
49
|
const cached = this.read();
|
|
42
50
|
if (!cached) return null;
|
|
43
|
-
if (cached.locId !== locationId) return null;
|
|
44
51
|
if (cached.expiresAt - EXPIRY_BUFFER_MS <= Date.now()) return null;
|
|
52
|
+
if (!locationId) return cached;
|
|
53
|
+
if (cached.locId !== locationId) return null;
|
|
45
54
|
return cached;
|
|
46
55
|
}
|
|
47
56
|
/**
|
|
48
57
|
* Get sessionId for refresh if location matches (regardless of expiry).
|
|
49
58
|
* Used when cache is expired but location is same - send sessionId to server.
|
|
59
|
+
*
|
|
60
|
+
* Only returns sessionId if locationId is provided AND matches cached locId.
|
|
61
|
+
* (IP-based sessions don't send sessionId for refresh since location may differ)
|
|
50
62
|
*/
|
|
51
63
|
getSessionId(locationId) {
|
|
52
64
|
const cached = this.read();
|
|
53
65
|
if (!cached) return null;
|
|
54
|
-
if (cached.locId !== locationId) return null;
|
|
66
|
+
if (!locationId || cached.locId !== locationId) return null;
|
|
55
67
|
return cached.sessionId;
|
|
56
68
|
}
|
|
57
69
|
/**
|
|
@@ -199,18 +211,16 @@ var CallForge = class _CallForge {
|
|
|
199
211
|
}
|
|
200
212
|
async fetchSession() {
|
|
201
213
|
const locationId = this.getLocationId();
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
return this.formatSession(cached);
|
|
208
|
-
}
|
|
214
|
+
const cached = this.cache.get(locationId);
|
|
215
|
+
if (cached) {
|
|
216
|
+
this.sessionId = cached.sessionId;
|
|
217
|
+
this.sessionCreated = true;
|
|
218
|
+
return this.formatSession(cached);
|
|
209
219
|
}
|
|
210
220
|
const autoParams = this.getAutoParams();
|
|
211
221
|
const cachedParams = this.cache.getParams();
|
|
212
222
|
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
213
|
-
if (
|
|
223
|
+
if (typeof window !== "undefined" && window.__cfTracking) {
|
|
214
224
|
try {
|
|
215
225
|
const data2 = await window.__cfTracking;
|
|
216
226
|
this.saveToCache(locationId, data2, params);
|
|
@@ -220,11 +230,9 @@ var CallForge = class _CallForge {
|
|
|
220
230
|
} catch (e) {
|
|
221
231
|
}
|
|
222
232
|
}
|
|
223
|
-
const cachedSessionId =
|
|
233
|
+
const cachedSessionId = this.cache.getSessionId(locationId);
|
|
224
234
|
const data = await this.fetchFromApi(locationId, cachedSessionId, params);
|
|
225
|
-
|
|
226
|
-
this.saveToCache(locationId, data, params);
|
|
227
|
-
}
|
|
235
|
+
this.saveToCache(locationId, data, params);
|
|
228
236
|
this.sessionId = data.sessionId;
|
|
229
237
|
this.sessionCreated = true;
|
|
230
238
|
return this.formatApiResponse(data);
|
|
@@ -336,19 +344,20 @@ var p={};
|
|
|
336
344
|
for(var i=0;i<ap.length;i++){var v=u.get(ap[i]);if(v)p[ap[i]]=v}
|
|
337
345
|
var key='${cacheKey}';
|
|
338
346
|
var sid=null;
|
|
339
|
-
|
|
347
|
+
try{
|
|
340
348
|
var c=JSON.parse(localStorage.getItem(key));
|
|
341
|
-
if(c&&c.
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
349
|
+
if(c&&c.expiresAt>Date.now()+30000){
|
|
350
|
+
if(!loc||(loc&&c.locId===loc)){c.params=Object.assign({},c.params,p);window.__cfTracking=Promise.resolve(c);return}
|
|
351
|
+
sid=(c.locId===loc)?c.sessionId:null;
|
|
352
|
+
var cp=c.params||{};
|
|
353
|
+
p=Object.assign({},cp,p);
|
|
354
|
+
}}catch(e){}
|
|
346
355
|
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}';
|
|
347
356
|
if(loc)url+='&loc_physical_ms='+loc;
|
|
348
357
|
if(sid)url+='&sessionId='+sid;
|
|
349
358
|
var ks=Object.keys(p).sort();
|
|
350
359
|
for(var j=0;j<ks.length;j++)url+='&'+ks[j]+'='+encodeURIComponent(p[ks[j]]);
|
|
351
|
-
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;
|
|
360
|
+
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;try{localStorage.setItem(key,JSON.stringify({locId:loc,sessionId:d.sessionId,phoneNumber:d.phoneNumber,location:d.location,expiresAt:d.expiresAt,params:p}))}catch(e){}return d});
|
|
352
361
|
})();`.replace(/\n/g, "");
|
|
353
362
|
return `<link rel="preconnect" href="${endpoint}">
|
|
354
363
|
<script>${script}</script>`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@callforge/tracking-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,17 +14,17 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"jsdom": "^27.4.0",
|
|
19
|
+
"tsup": "^8.0.0",
|
|
20
|
+
"typescript": "^5.3.0",
|
|
21
|
+
"vitest": "^1.6.0",
|
|
22
|
+
"@callforge/tsconfig": "0.0.0"
|
|
23
|
+
},
|
|
17
24
|
"scripts": {
|
|
18
25
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
19
26
|
"clean": "rm -rf dist",
|
|
20
27
|
"test": "vitest run",
|
|
21
28
|
"test:watch": "vitest"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@callforge/tsconfig": "workspace:*",
|
|
25
|
-
"jsdom": "^27.4.0",
|
|
26
|
-
"tsup": "^8.0.0",
|
|
27
|
-
"typescript": "^5.3.0",
|
|
28
|
-
"vitest": "^1.6.0"
|
|
29
29
|
}
|
|
30
|
-
}
|
|
30
|
+
}
|