@usethrottle/cart 3.18.0 → 3.19.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.cjs +63 -2
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +63 -2
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -328,6 +328,67 @@ function cartErrorFromResponse(args) {
|
|
|
328
328
|
return new ThrottleApiError(args);
|
|
329
329
|
}
|
|
330
330
|
|
|
331
|
+
// src/analytics-context.ts
|
|
332
|
+
var LONG_KEYS = ["landingPage", "referrer"];
|
|
333
|
+
var SHORT_KEYS = [
|
|
334
|
+
"utmSource",
|
|
335
|
+
"utmMedium",
|
|
336
|
+
"utmCampaign",
|
|
337
|
+
"utmTerm",
|
|
338
|
+
"utmContent",
|
|
339
|
+
"gclid",
|
|
340
|
+
"fbclid",
|
|
341
|
+
"msclkid",
|
|
342
|
+
"ttclid",
|
|
343
|
+
"gaClientId",
|
|
344
|
+
"gaSessionId",
|
|
345
|
+
"fbp",
|
|
346
|
+
"fbc"
|
|
347
|
+
];
|
|
348
|
+
var CONSENT_KEYS = ["analytics", "marketing", "adUserData", "adPersonalization"];
|
|
349
|
+
var MAX_SHORT = 512;
|
|
350
|
+
var MAX_LONG = 2048;
|
|
351
|
+
var MAX_BYTES = 8192;
|
|
352
|
+
function sanitize(raw) {
|
|
353
|
+
const out = {};
|
|
354
|
+
const take = (keys, max) => {
|
|
355
|
+
for (const key of keys) {
|
|
356
|
+
const v = raw[key];
|
|
357
|
+
if (typeof v !== "string") continue;
|
|
358
|
+
const t = v.trim().slice(0, max).trim();
|
|
359
|
+
if (t) out[key] = t;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
take(LONG_KEYS, MAX_LONG);
|
|
363
|
+
take(SHORT_KEYS, MAX_SHORT);
|
|
364
|
+
const consent = raw.consent;
|
|
365
|
+
if (consent && typeof consent === "object") {
|
|
366
|
+
const c = {};
|
|
367
|
+
for (const key of CONSENT_KEYS) {
|
|
368
|
+
const v = consent[key];
|
|
369
|
+
if (typeof v === "boolean") c[key] = v;
|
|
370
|
+
}
|
|
371
|
+
if (Object.keys(c).length > 0) out.consent = c;
|
|
372
|
+
}
|
|
373
|
+
if (Object.keys(out).length === 0) return void 0;
|
|
374
|
+
if (new TextEncoder().encode(JSON.stringify(out)).length > MAX_BYTES) return void 0;
|
|
375
|
+
return out;
|
|
376
|
+
}
|
|
377
|
+
function withAnalyticsContext(input) {
|
|
378
|
+
if (input.clientContext !== void 0) return input;
|
|
379
|
+
try {
|
|
380
|
+
const throttle = globalThis.Throttle;
|
|
381
|
+
const fn = throttle?.analyticsContext;
|
|
382
|
+
if (typeof fn !== "function") return input;
|
|
383
|
+
const raw = fn.call(throttle);
|
|
384
|
+
if (!raw || typeof raw !== "object") return input;
|
|
385
|
+
const clientContext = sanitize(raw);
|
|
386
|
+
return clientContext ? { ...input, clientContext } : input;
|
|
387
|
+
} catch {
|
|
388
|
+
return input;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
331
392
|
// src/client.ts
|
|
332
393
|
function segment(value) {
|
|
333
394
|
return encodeURIComponent(value);
|
|
@@ -375,7 +436,7 @@ var CartClient = class {
|
|
|
375
436
|
create: (input) => this.request("POST", "/api/v1/carts", input),
|
|
376
437
|
get: (id) => this.request("GET", `/api/v1/carts/${segment(id)}`),
|
|
377
438
|
update: (id, input) => this.request("PATCH", `/api/v1/carts/${segment(id)}`, input),
|
|
378
|
-
checkout: (id, input) => this.request("POST", `/api/v1/carts/${segment(id)}/checkout`, input ?? {}),
|
|
439
|
+
checkout: (id, input) => this.request("POST", `/api/v1/carts/${segment(id)}/checkout`, withAnalyticsContext(input ?? {})),
|
|
379
440
|
merge: (id, customerId) => this.request("POST", `/api/v1/carts/${segment(id)}/merge`, { customerId })
|
|
380
441
|
};
|
|
381
442
|
}
|
|
@@ -658,7 +719,7 @@ var CartSessionClient = class _CartSessionClient {
|
|
|
658
719
|
}
|
|
659
720
|
/** Hand off to hosted/embedded checkout. Marks the session converted (no more edits). */
|
|
660
721
|
checkout(input) {
|
|
661
|
-
return this.request("POST", `${this.basePath()}/checkout-session`, input);
|
|
722
|
+
return this.request("POST", `${this.basePath()}/checkout-session`, withAnalyticsContext(input));
|
|
662
723
|
}
|
|
663
724
|
basePath() {
|
|
664
725
|
if (!this.sessionId) {
|
package/dist/index.d.cts
CHANGED
|
@@ -382,6 +382,8 @@ interface CheckoutInput {
|
|
|
382
382
|
termsSnapshot?: string;
|
|
383
383
|
invoiceNumber?: string;
|
|
384
384
|
metadata?: Record<string, unknown>;
|
|
385
|
+
/** The buyer's GA4 ids, UTMs and consent. Defaults to `Throttle.analyticsContext()` when that exists. */
|
|
386
|
+
clientContext?: Record<string, unknown>;
|
|
385
387
|
}
|
|
386
388
|
interface Order {
|
|
387
389
|
id: string;
|
|
@@ -731,6 +733,8 @@ interface CheckoutHandoffInput {
|
|
|
731
733
|
* checkout returns to a live cart. Throttle's own hosted cart page sets this.
|
|
732
734
|
*/
|
|
733
735
|
keepOpen?: boolean;
|
|
736
|
+
/** The buyer's GA4 ids, UTMs and consent. Defaults to `Throttle.analyticsContext()` when that exists. */
|
|
737
|
+
clientContext?: Record<string, unknown>;
|
|
734
738
|
}
|
|
735
739
|
interface CheckoutHandoff {
|
|
736
740
|
cartSessionId: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -382,6 +382,8 @@ interface CheckoutInput {
|
|
|
382
382
|
termsSnapshot?: string;
|
|
383
383
|
invoiceNumber?: string;
|
|
384
384
|
metadata?: Record<string, unknown>;
|
|
385
|
+
/** The buyer's GA4 ids, UTMs and consent. Defaults to `Throttle.analyticsContext()` when that exists. */
|
|
386
|
+
clientContext?: Record<string, unknown>;
|
|
385
387
|
}
|
|
386
388
|
interface Order {
|
|
387
389
|
id: string;
|
|
@@ -731,6 +733,8 @@ interface CheckoutHandoffInput {
|
|
|
731
733
|
* checkout returns to a live cart. Throttle's own hosted cart page sets this.
|
|
732
734
|
*/
|
|
733
735
|
keepOpen?: boolean;
|
|
736
|
+
/** The buyer's GA4 ids, UTMs and consent. Defaults to `Throttle.analyticsContext()` when that exists. */
|
|
737
|
+
clientContext?: Record<string, unknown>;
|
|
734
738
|
}
|
|
735
739
|
interface CheckoutHandoff {
|
|
736
740
|
cartSessionId: string;
|
package/dist/index.js
CHANGED
|
@@ -252,6 +252,67 @@ function cartErrorFromResponse(args) {
|
|
|
252
252
|
return new ThrottleApiError(args);
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
+
// src/analytics-context.ts
|
|
256
|
+
var LONG_KEYS = ["landingPage", "referrer"];
|
|
257
|
+
var SHORT_KEYS = [
|
|
258
|
+
"utmSource",
|
|
259
|
+
"utmMedium",
|
|
260
|
+
"utmCampaign",
|
|
261
|
+
"utmTerm",
|
|
262
|
+
"utmContent",
|
|
263
|
+
"gclid",
|
|
264
|
+
"fbclid",
|
|
265
|
+
"msclkid",
|
|
266
|
+
"ttclid",
|
|
267
|
+
"gaClientId",
|
|
268
|
+
"gaSessionId",
|
|
269
|
+
"fbp",
|
|
270
|
+
"fbc"
|
|
271
|
+
];
|
|
272
|
+
var CONSENT_KEYS = ["analytics", "marketing", "adUserData", "adPersonalization"];
|
|
273
|
+
var MAX_SHORT = 512;
|
|
274
|
+
var MAX_LONG = 2048;
|
|
275
|
+
var MAX_BYTES = 8192;
|
|
276
|
+
function sanitize(raw) {
|
|
277
|
+
const out = {};
|
|
278
|
+
const take = (keys, max) => {
|
|
279
|
+
for (const key of keys) {
|
|
280
|
+
const v = raw[key];
|
|
281
|
+
if (typeof v !== "string") continue;
|
|
282
|
+
const t = v.trim().slice(0, max).trim();
|
|
283
|
+
if (t) out[key] = t;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
take(LONG_KEYS, MAX_LONG);
|
|
287
|
+
take(SHORT_KEYS, MAX_SHORT);
|
|
288
|
+
const consent = raw.consent;
|
|
289
|
+
if (consent && typeof consent === "object") {
|
|
290
|
+
const c = {};
|
|
291
|
+
for (const key of CONSENT_KEYS) {
|
|
292
|
+
const v = consent[key];
|
|
293
|
+
if (typeof v === "boolean") c[key] = v;
|
|
294
|
+
}
|
|
295
|
+
if (Object.keys(c).length > 0) out.consent = c;
|
|
296
|
+
}
|
|
297
|
+
if (Object.keys(out).length === 0) return void 0;
|
|
298
|
+
if (new TextEncoder().encode(JSON.stringify(out)).length > MAX_BYTES) return void 0;
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
function withAnalyticsContext(input) {
|
|
302
|
+
if (input.clientContext !== void 0) return input;
|
|
303
|
+
try {
|
|
304
|
+
const throttle = globalThis.Throttle;
|
|
305
|
+
const fn = throttle?.analyticsContext;
|
|
306
|
+
if (typeof fn !== "function") return input;
|
|
307
|
+
const raw = fn.call(throttle);
|
|
308
|
+
if (!raw || typeof raw !== "object") return input;
|
|
309
|
+
const clientContext = sanitize(raw);
|
|
310
|
+
return clientContext ? { ...input, clientContext } : input;
|
|
311
|
+
} catch {
|
|
312
|
+
return input;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
255
316
|
// src/client.ts
|
|
256
317
|
function segment(value) {
|
|
257
318
|
return encodeURIComponent(value);
|
|
@@ -299,7 +360,7 @@ var CartClient = class {
|
|
|
299
360
|
create: (input) => this.request("POST", "/api/v1/carts", input),
|
|
300
361
|
get: (id) => this.request("GET", `/api/v1/carts/${segment(id)}`),
|
|
301
362
|
update: (id, input) => this.request("PATCH", `/api/v1/carts/${segment(id)}`, input),
|
|
302
|
-
checkout: (id, input) => this.request("POST", `/api/v1/carts/${segment(id)}/checkout`, input ?? {}),
|
|
363
|
+
checkout: (id, input) => this.request("POST", `/api/v1/carts/${segment(id)}/checkout`, withAnalyticsContext(input ?? {})),
|
|
303
364
|
merge: (id, customerId) => this.request("POST", `/api/v1/carts/${segment(id)}/merge`, { customerId })
|
|
304
365
|
};
|
|
305
366
|
}
|
|
@@ -582,7 +643,7 @@ var CartSessionClient = class _CartSessionClient {
|
|
|
582
643
|
}
|
|
583
644
|
/** Hand off to hosted/embedded checkout. Marks the session converted (no more edits). */
|
|
584
645
|
checkout(input) {
|
|
585
|
-
return this.request("POST", `${this.basePath()}/checkout-session`, input);
|
|
646
|
+
return this.request("POST", `${this.basePath()}/checkout-session`, withAnalyticsContext(input));
|
|
586
647
|
}
|
|
587
648
|
basePath() {
|
|
588
649
|
if (!this.sessionId) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usethrottle/cart",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.19.0",
|
|
4
4
|
"description": "Typed REST client for the Throttle Cart API.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@usethrottle/
|
|
22
|
-
"@usethrottle/
|
|
21
|
+
"@usethrottle/money": "^1.1.0",
|
|
22
|
+
"@usethrottle/errors": "^1.1.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"tsup": "^8.0.0",
|