@callforge/tracking-client 0.4.2 → 0.5.1
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 +43 -29
- package/dist/index.mjs +43 -29
- 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
|
/**
|
|
@@ -222,33 +234,34 @@ var CallForge = class _CallForge {
|
|
|
222
234
|
});
|
|
223
235
|
}
|
|
224
236
|
async fetchSession() {
|
|
237
|
+
var _a;
|
|
225
238
|
const locationId = this.getLocationId();
|
|
226
|
-
if (
|
|
227
|
-
const cached = this.cache.get(locationId);
|
|
228
|
-
if (cached) {
|
|
229
|
-
this.sessionId = cached.sessionId;
|
|
230
|
-
this.sessionCreated = true;
|
|
231
|
-
return this.formatSession(cached);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
const autoParams = this.getAutoParams();
|
|
235
|
-
const cachedParams = this.cache.getParams();
|
|
236
|
-
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
237
|
-
if (locationId && typeof window !== "undefined" && window.__cfTracking) {
|
|
239
|
+
if (typeof window !== "undefined" && window.__cfTracking) {
|
|
238
240
|
try {
|
|
239
241
|
const data2 = await window.__cfTracking;
|
|
240
|
-
|
|
242
|
+
const dataWithExtras = data2;
|
|
243
|
+
const effectiveLocId = (_a = dataWithExtras.locId) != null ? _a : locationId;
|
|
244
|
+
const autoParams2 = this.getAutoParams();
|
|
245
|
+
const params2 = __spreadValues(__spreadValues(__spreadValues({}, autoParams2), dataWithExtras.params), this.customParams);
|
|
246
|
+
this.saveToCache(effectiveLocId, data2, params2);
|
|
241
247
|
this.sessionId = data2.sessionId;
|
|
242
248
|
this.sessionCreated = true;
|
|
243
249
|
return this.formatApiResponse(data2);
|
|
244
250
|
} catch (e) {
|
|
245
251
|
}
|
|
246
252
|
}
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
this.
|
|
253
|
+
const cached = this.cache.get(locationId);
|
|
254
|
+
if (cached) {
|
|
255
|
+
this.sessionId = cached.sessionId;
|
|
256
|
+
this.sessionCreated = true;
|
|
257
|
+
return this.formatSession(cached);
|
|
251
258
|
}
|
|
259
|
+
const autoParams = this.getAutoParams();
|
|
260
|
+
const cachedParams = this.cache.getParams();
|
|
261
|
+
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
262
|
+
const cachedSessionId = this.cache.getSessionId(locationId);
|
|
263
|
+
const data = await this.fetchFromApi(locationId, cachedSessionId, params);
|
|
264
|
+
this.saveToCache(locationId, data, params);
|
|
252
265
|
this.sessionId = data.sessionId;
|
|
253
266
|
this.sessionCreated = true;
|
|
254
267
|
return this.formatApiResponse(data);
|
|
@@ -289,7 +302,7 @@ var CallForge = class _CallForge {
|
|
|
289
302
|
}
|
|
290
303
|
saveToCache(locationId, data, params) {
|
|
291
304
|
const cached = {
|
|
292
|
-
locId: locationId,
|
|
305
|
+
locId: locationId != null ? locationId : null,
|
|
293
306
|
sessionId: data.sessionId,
|
|
294
307
|
phoneNumber: data.phoneNumber,
|
|
295
308
|
location: data.location,
|
|
@@ -360,19 +373,20 @@ var p={};
|
|
|
360
373
|
for(var i=0;i<ap.length;i++){var v=u.get(ap[i]);if(v)p[ap[i]]=v}
|
|
361
374
|
var key='${cacheKey}';
|
|
362
375
|
var sid=null;
|
|
363
|
-
|
|
376
|
+
try{
|
|
364
377
|
var c=JSON.parse(localStorage.getItem(key));
|
|
365
|
-
if(c&&c.
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
378
|
+
if(c&&c.expiresAt>Date.now()+30000){
|
|
379
|
+
if(!loc||(loc&&c.locId===loc)){c.params=Object.assign({},c.params,p);window.__cfTracking=Promise.resolve(c);return}
|
|
380
|
+
sid=(c.locId===loc)?c.sessionId:null;
|
|
381
|
+
var cp=c.params||{};
|
|
382
|
+
p=Object.assign({},cp,p);
|
|
383
|
+
}}catch(e){}
|
|
370
384
|
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}';
|
|
371
385
|
if(loc)url+='&loc_physical_ms='+loc;
|
|
372
386
|
if(sid)url+='&sessionId='+sid;
|
|
373
387
|
var ks=Object.keys(p).sort();
|
|
374
388
|
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;
|
|
389
|
+
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
390
|
})();`.replace(/\n/g, "");
|
|
377
391
|
return `<link rel="preconnect" href="${endpoint}">
|
|
378
392
|
<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
|
/**
|
|
@@ -198,33 +210,34 @@ var CallForge = class _CallForge {
|
|
|
198
210
|
});
|
|
199
211
|
}
|
|
200
212
|
async fetchSession() {
|
|
213
|
+
var _a;
|
|
201
214
|
const locationId = this.getLocationId();
|
|
202
|
-
if (
|
|
203
|
-
const cached = this.cache.get(locationId);
|
|
204
|
-
if (cached) {
|
|
205
|
-
this.sessionId = cached.sessionId;
|
|
206
|
-
this.sessionCreated = true;
|
|
207
|
-
return this.formatSession(cached);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
const autoParams = this.getAutoParams();
|
|
211
|
-
const cachedParams = this.cache.getParams();
|
|
212
|
-
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
213
|
-
if (locationId && typeof window !== "undefined" && window.__cfTracking) {
|
|
215
|
+
if (typeof window !== "undefined" && window.__cfTracking) {
|
|
214
216
|
try {
|
|
215
217
|
const data2 = await window.__cfTracking;
|
|
216
|
-
|
|
218
|
+
const dataWithExtras = data2;
|
|
219
|
+
const effectiveLocId = (_a = dataWithExtras.locId) != null ? _a : locationId;
|
|
220
|
+
const autoParams2 = this.getAutoParams();
|
|
221
|
+
const params2 = __spreadValues(__spreadValues(__spreadValues({}, autoParams2), dataWithExtras.params), this.customParams);
|
|
222
|
+
this.saveToCache(effectiveLocId, data2, params2);
|
|
217
223
|
this.sessionId = data2.sessionId;
|
|
218
224
|
this.sessionCreated = true;
|
|
219
225
|
return this.formatApiResponse(data2);
|
|
220
226
|
} catch (e) {
|
|
221
227
|
}
|
|
222
228
|
}
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
this.
|
|
229
|
+
const cached = this.cache.get(locationId);
|
|
230
|
+
if (cached) {
|
|
231
|
+
this.sessionId = cached.sessionId;
|
|
232
|
+
this.sessionCreated = true;
|
|
233
|
+
return this.formatSession(cached);
|
|
227
234
|
}
|
|
235
|
+
const autoParams = this.getAutoParams();
|
|
236
|
+
const cachedParams = this.cache.getParams();
|
|
237
|
+
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
238
|
+
const cachedSessionId = this.cache.getSessionId(locationId);
|
|
239
|
+
const data = await this.fetchFromApi(locationId, cachedSessionId, params);
|
|
240
|
+
this.saveToCache(locationId, data, params);
|
|
228
241
|
this.sessionId = data.sessionId;
|
|
229
242
|
this.sessionCreated = true;
|
|
230
243
|
return this.formatApiResponse(data);
|
|
@@ -265,7 +278,7 @@ var CallForge = class _CallForge {
|
|
|
265
278
|
}
|
|
266
279
|
saveToCache(locationId, data, params) {
|
|
267
280
|
const cached = {
|
|
268
|
-
locId: locationId,
|
|
281
|
+
locId: locationId != null ? locationId : null,
|
|
269
282
|
sessionId: data.sessionId,
|
|
270
283
|
phoneNumber: data.phoneNumber,
|
|
271
284
|
location: data.location,
|
|
@@ -336,19 +349,20 @@ var p={};
|
|
|
336
349
|
for(var i=0;i<ap.length;i++){var v=u.get(ap[i]);if(v)p[ap[i]]=v}
|
|
337
350
|
var key='${cacheKey}';
|
|
338
351
|
var sid=null;
|
|
339
|
-
|
|
352
|
+
try{
|
|
340
353
|
var c=JSON.parse(localStorage.getItem(key));
|
|
341
|
-
if(c&&c.
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
354
|
+
if(c&&c.expiresAt>Date.now()+30000){
|
|
355
|
+
if(!loc||(loc&&c.locId===loc)){c.params=Object.assign({},c.params,p);window.__cfTracking=Promise.resolve(c);return}
|
|
356
|
+
sid=(c.locId===loc)?c.sessionId:null;
|
|
357
|
+
var cp=c.params||{};
|
|
358
|
+
p=Object.assign({},cp,p);
|
|
359
|
+
}}catch(e){}
|
|
346
360
|
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}';
|
|
347
361
|
if(loc)url+='&loc_physical_ms='+loc;
|
|
348
362
|
if(sid)url+='&sessionId='+sid;
|
|
349
363
|
var ks=Object.keys(p).sort();
|
|
350
364
|
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;
|
|
365
|
+
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
366
|
})();`.replace(/\n/g, "");
|
|
353
367
|
return `<link rel="preconnect" href="${endpoint}">
|
|
354
368
|
<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.1",
|
|
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
|
+
}
|