@konfiqo/storefront-react 0.6.0 → 0.7.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.d.cts CHANGED
@@ -111,7 +111,7 @@ interface ResourceResult<T> {
111
111
  refetch: () => void;
112
112
  }
113
113
 
114
- /** The store (`store.get()`): name, currency, theme, legal texts, checkout challenge. */
114
+ /** The store (`store.get()`): name, currency, theme, company details, checkout challenge. */
115
115
  declare function useStore(): ResourceResult<Store>;
116
116
  /** The active category tree (`categories.list()`). */
117
117
  declare function useCategories(): ResourceResult<CategoryList>;
@@ -192,6 +192,12 @@ declare function useAccount(): UseAccountResult;
192
192
 
193
193
  /** Options of `checkout()`. */
194
194
  interface CheckoutCallOptions {
195
+ /**
196
+ * The `agreementsHash` of the agreements the buyer was shown
197
+ * (`client.carts.agreements(cartId)`). Required. A cart or legal text
198
+ * that changed since makes checkout fail with `agreements_changed`.
199
+ */
200
+ agreementsHash: string;
195
201
  /**
196
202
  * The checkout challenge token (from `<CheckoutChallenge>`), required
197
203
  * when `store.checkoutChallenge` is set. Single-use.
@@ -201,12 +207,12 @@ interface CheckoutCallOptions {
201
207
  /** What {@link useCheckout} returns. */
202
208
  interface UseCheckoutResult extends CheckoutSnapshot {
203
209
  /**
204
- * Checks the cart out: `carts.checkout(cartId, { turnstileToken })`.
210
+ * Checks the cart out: `carts.checkout(cartId, { agreementsHash, turnstileToken })`.
205
211
  * Resolves with the result (present it with `<PaymentForm>`; keep its
206
212
  * `token`, returned only once) or rejects with the error (also in
207
213
  * `error`). A call while one is in flight returns the same promise.
208
214
  */
209
- checkout(options?: CheckoutCallOptions): Promise<CheckoutResult>;
215
+ checkout(options: CheckoutCallOptions): Promise<CheckoutResult>;
210
216
  /** Clears `result` and `error`. */
211
217
  reset(): void;
212
218
  }
@@ -221,6 +227,8 @@ interface UseCheckoutResult extends CheckoutSnapshot {
221
227
  * `cart_changed`, `error.details` has the new `lines`, totals and
222
228
  * `shippingOptions` (the cart is also read again): show them and let the
223
229
  * buyer confirm before calling `checkout()` again — never retry silently.
230
+ * For `agreements_changed`, load the agreements again, show the new texts
231
+ * and call `checkout()` with the new `agreementsHash`.
224
232
  */
225
233
  declare function useCheckout(): UseCheckoutResult;
226
234
 
@@ -302,10 +310,10 @@ interface CheckoutChallengeProps extends Omit<HTMLAttributes<HTMLDivElement>, 'c
302
310
  * Null / empty renders an empty `<div>` (the store has no challenge).
303
311
  */
304
312
  siteKey: string | null | undefined;
305
- /** A token is ready: pass it to `checkout({ turnstileToken })`. */
313
+ /** A token is ready: pass it to `checkout({ agreementsHash, turnstileToken })`. */
306
314
  onToken: (token: string) => void;
307
315
  /**
308
- * The current token can no longer be used — it expired, or a checkout
316
+ * The current token can no longer be used: it expired, or a checkout
309
317
  * consumed it and the widget was reset. Drop it; a new one follows.
310
318
  */
311
319
  onExpired?: () => void;
@@ -482,10 +490,10 @@ interface AnalyticsTrackerProps {
482
490
  * Configures analytics and sends a `page_view` on mount and every route change.
483
491
  *
484
492
  * Pages whose URL carries a buyer secret are protected by path. On an
485
- * order-status page the URL sent is `/orders/:id`, and on both
486
- * `/orders/…` and `/cart/recover` the query is left out. A custom storefront must serve its
487
- * order-status and cart-recovery pages only at those paths, or not render
488
- * this component there.
493
+ * order-status page the URL sent is `/orders/:id`. On both `/orders/…` and
494
+ * `/cart/recover` the query is left out. A custom storefront must serve
495
+ * its order-status and cart-recovery pages only at those paths, or not
496
+ * render this component there.
489
497
  */
490
498
  declare function AnalyticsTracker({ baseUrl, publishableKey, pathname, }: AnalyticsTrackerProps): null;
491
499
 
@@ -543,6 +551,12 @@ declare function resetPixels(): void;
543
551
  * analytics or pixel events from them.
544
552
  */
545
553
  declare function onSecretPage(path?: string): boolean;
554
+ /**
555
+ * The current page URL as the platforms may see it: the `token` query
556
+ * parameter removed always, and the whole query string and fragment removed
557
+ * on a page where {@link onSecretPage} is true.
558
+ */
559
+ declare function scrubbedLocation(): string;
546
560
  /**
547
561
  * Sets the store's pixels and whether the shopper granted `marketing`.
548
562
  * Scripts load once, on the first grant. Platform cookies are deleted only
@@ -556,7 +570,10 @@ declare function configurePixels(next: AdPixels | undefined, isGranted: boolean,
556
570
  hostOnly?: boolean;
557
571
  currency?: string;
558
572
  }): void;
559
- /** Reports `e` to every enabled platform; a no-op without a grant. */
573
+ /**
574
+ * Reports `e` to every enabled platform; a no-op without a grant. Before
575
+ * the page's first `configurePixels`, the newest 20 events are held for it.
576
+ */
560
577
  declare function firePixel(e: PixelEvent): void;
561
578
 
562
579
  /** Props of {@link AdPixelsLoader}. */
@@ -593,4 +610,4 @@ interface AdPixelsLoaderProps {
593
610
  */
594
611
  declare function AdPixelsLoader({ pixels, hostOnly, currency, pathname, }: AdPixelsLoaderProps): null;
595
612
 
596
- export { AdPixelsLoader, type AdPixelsLoaderProps, type AnalyticsEventType, type AnalyticsExtra, AnalyticsTracker, type AnalyticsTrackerProps, type CartSnapshot, type CheckoutCallOptions, CheckoutChallenge, type CheckoutChallengeHandle, type CheckoutChallengeProps, type CheckoutSnapshot, type ClientConfig, ConsentProvider, type ConsentProviderProps, type ConsentRecord, type ConsentState, Content, type ContentComponents, type ContentProps, OPTIONAL_CATEGORIES, ORDER_POLL_MAX_MS, ORDER_POLL_MIN_MS, type OptionalCookieCategory, PaymentForm, type PaymentFormProps, type PixelEvent, type ResourceResult, type StoredConsent, type StorefrontHookError, StorefrontProvider, type StorefrontProviderProps, type UseAccountResult, type UseCartResult, type UseCheckoutResult, type UseOrderOptions, type UseOrderResult, configureAnalytics, configurePixels, defaultCartStorage, firePixel, isDeadCartError, isOrderSettled, onSecretPage, orderPollDelay, recordConsent, renderContent, resetAnalytics, resetPixels, track, useAccount, useCart, useCartStorage, useCategories, useCheckout, useConsent, useOrder, useProduct, useProducts, useStore, useStorefrontClient };
613
+ export { AdPixelsLoader, type AdPixelsLoaderProps, type AnalyticsEventType, type AnalyticsExtra, AnalyticsTracker, type AnalyticsTrackerProps, type CartSnapshot, type CheckoutCallOptions, CheckoutChallenge, type CheckoutChallengeHandle, type CheckoutChallengeProps, type CheckoutSnapshot, type ClientConfig, ConsentProvider, type ConsentProviderProps, type ConsentRecord, type ConsentState, Content, type ContentComponents, type ContentProps, OPTIONAL_CATEGORIES, ORDER_POLL_MAX_MS, ORDER_POLL_MIN_MS, type OptionalCookieCategory, PaymentForm, type PaymentFormProps, type PixelEvent, type ResourceResult, type StoredConsent, type StorefrontHookError, StorefrontProvider, type StorefrontProviderProps, type UseAccountResult, type UseCartResult, type UseCheckoutResult, type UseOrderOptions, type UseOrderResult, configureAnalytics, configurePixels, defaultCartStorage, firePixel, isDeadCartError, isOrderSettled, onSecretPage, orderPollDelay, recordConsent, renderContent, resetAnalytics, resetPixels, scrubbedLocation, track, useAccount, useCart, useCartStorage, useCategories, useCheckout, useConsent, useOrder, useProduct, useProducts, useStore, useStorefrontClient };
package/dist/index.d.ts CHANGED
@@ -111,7 +111,7 @@ interface ResourceResult<T> {
111
111
  refetch: () => void;
112
112
  }
113
113
 
114
- /** The store (`store.get()`): name, currency, theme, legal texts, checkout challenge. */
114
+ /** The store (`store.get()`): name, currency, theme, company details, checkout challenge. */
115
115
  declare function useStore(): ResourceResult<Store>;
116
116
  /** The active category tree (`categories.list()`). */
117
117
  declare function useCategories(): ResourceResult<CategoryList>;
@@ -192,6 +192,12 @@ declare function useAccount(): UseAccountResult;
192
192
 
193
193
  /** Options of `checkout()`. */
194
194
  interface CheckoutCallOptions {
195
+ /**
196
+ * The `agreementsHash` of the agreements the buyer was shown
197
+ * (`client.carts.agreements(cartId)`). Required. A cart or legal text
198
+ * that changed since makes checkout fail with `agreements_changed`.
199
+ */
200
+ agreementsHash: string;
195
201
  /**
196
202
  * The checkout challenge token (from `<CheckoutChallenge>`), required
197
203
  * when `store.checkoutChallenge` is set. Single-use.
@@ -201,12 +207,12 @@ interface CheckoutCallOptions {
201
207
  /** What {@link useCheckout} returns. */
202
208
  interface UseCheckoutResult extends CheckoutSnapshot {
203
209
  /**
204
- * Checks the cart out: `carts.checkout(cartId, { turnstileToken })`.
210
+ * Checks the cart out: `carts.checkout(cartId, { agreementsHash, turnstileToken })`.
205
211
  * Resolves with the result (present it with `<PaymentForm>`; keep its
206
212
  * `token`, returned only once) or rejects with the error (also in
207
213
  * `error`). A call while one is in flight returns the same promise.
208
214
  */
209
- checkout(options?: CheckoutCallOptions): Promise<CheckoutResult>;
215
+ checkout(options: CheckoutCallOptions): Promise<CheckoutResult>;
210
216
  /** Clears `result` and `error`. */
211
217
  reset(): void;
212
218
  }
@@ -221,6 +227,8 @@ interface UseCheckoutResult extends CheckoutSnapshot {
221
227
  * `cart_changed`, `error.details` has the new `lines`, totals and
222
228
  * `shippingOptions` (the cart is also read again): show them and let the
223
229
  * buyer confirm before calling `checkout()` again — never retry silently.
230
+ * For `agreements_changed`, load the agreements again, show the new texts
231
+ * and call `checkout()` with the new `agreementsHash`.
224
232
  */
225
233
  declare function useCheckout(): UseCheckoutResult;
226
234
 
@@ -302,10 +310,10 @@ interface CheckoutChallengeProps extends Omit<HTMLAttributes<HTMLDivElement>, 'c
302
310
  * Null / empty renders an empty `<div>` (the store has no challenge).
303
311
  */
304
312
  siteKey: string | null | undefined;
305
- /** A token is ready: pass it to `checkout({ turnstileToken })`. */
313
+ /** A token is ready: pass it to `checkout({ agreementsHash, turnstileToken })`. */
306
314
  onToken: (token: string) => void;
307
315
  /**
308
- * The current token can no longer be used — it expired, or a checkout
316
+ * The current token can no longer be used: it expired, or a checkout
309
317
  * consumed it and the widget was reset. Drop it; a new one follows.
310
318
  */
311
319
  onExpired?: () => void;
@@ -482,10 +490,10 @@ interface AnalyticsTrackerProps {
482
490
  * Configures analytics and sends a `page_view` on mount and every route change.
483
491
  *
484
492
  * Pages whose URL carries a buyer secret are protected by path. On an
485
- * order-status page the URL sent is `/orders/:id`, and on both
486
- * `/orders/…` and `/cart/recover` the query is left out. A custom storefront must serve its
487
- * order-status and cart-recovery pages only at those paths, or not render
488
- * this component there.
493
+ * order-status page the URL sent is `/orders/:id`. On both `/orders/…` and
494
+ * `/cart/recover` the query is left out. A custom storefront must serve
495
+ * its order-status and cart-recovery pages only at those paths, or not
496
+ * render this component there.
489
497
  */
490
498
  declare function AnalyticsTracker({ baseUrl, publishableKey, pathname, }: AnalyticsTrackerProps): null;
491
499
 
@@ -543,6 +551,12 @@ declare function resetPixels(): void;
543
551
  * analytics or pixel events from them.
544
552
  */
545
553
  declare function onSecretPage(path?: string): boolean;
554
+ /**
555
+ * The current page URL as the platforms may see it: the `token` query
556
+ * parameter removed always, and the whole query string and fragment removed
557
+ * on a page where {@link onSecretPage} is true.
558
+ */
559
+ declare function scrubbedLocation(): string;
546
560
  /**
547
561
  * Sets the store's pixels and whether the shopper granted `marketing`.
548
562
  * Scripts load once, on the first grant. Platform cookies are deleted only
@@ -556,7 +570,10 @@ declare function configurePixels(next: AdPixels | undefined, isGranted: boolean,
556
570
  hostOnly?: boolean;
557
571
  currency?: string;
558
572
  }): void;
559
- /** Reports `e` to every enabled platform; a no-op without a grant. */
573
+ /**
574
+ * Reports `e` to every enabled platform; a no-op without a grant. Before
575
+ * the page's first `configurePixels`, the newest 20 events are held for it.
576
+ */
560
577
  declare function firePixel(e: PixelEvent): void;
561
578
 
562
579
  /** Props of {@link AdPixelsLoader}. */
@@ -593,4 +610,4 @@ interface AdPixelsLoaderProps {
593
610
  */
594
611
  declare function AdPixelsLoader({ pixels, hostOnly, currency, pathname, }: AdPixelsLoaderProps): null;
595
612
 
596
- export { AdPixelsLoader, type AdPixelsLoaderProps, type AnalyticsEventType, type AnalyticsExtra, AnalyticsTracker, type AnalyticsTrackerProps, type CartSnapshot, type CheckoutCallOptions, CheckoutChallenge, type CheckoutChallengeHandle, type CheckoutChallengeProps, type CheckoutSnapshot, type ClientConfig, ConsentProvider, type ConsentProviderProps, type ConsentRecord, type ConsentState, Content, type ContentComponents, type ContentProps, OPTIONAL_CATEGORIES, ORDER_POLL_MAX_MS, ORDER_POLL_MIN_MS, type OptionalCookieCategory, PaymentForm, type PaymentFormProps, type PixelEvent, type ResourceResult, type StoredConsent, type StorefrontHookError, StorefrontProvider, type StorefrontProviderProps, type UseAccountResult, type UseCartResult, type UseCheckoutResult, type UseOrderOptions, type UseOrderResult, configureAnalytics, configurePixels, defaultCartStorage, firePixel, isDeadCartError, isOrderSettled, onSecretPage, orderPollDelay, recordConsent, renderContent, resetAnalytics, resetPixels, track, useAccount, useCart, useCartStorage, useCategories, useCheckout, useConsent, useOrder, useProduct, useProducts, useStore, useStorefrontClient };
613
+ export { AdPixelsLoader, type AdPixelsLoaderProps, type AnalyticsEventType, type AnalyticsExtra, AnalyticsTracker, type AnalyticsTrackerProps, type CartSnapshot, type CheckoutCallOptions, CheckoutChallenge, type CheckoutChallengeHandle, type CheckoutChallengeProps, type CheckoutSnapshot, type ClientConfig, ConsentProvider, type ConsentProviderProps, type ConsentRecord, type ConsentState, Content, type ContentComponents, type ContentProps, OPTIONAL_CATEGORIES, ORDER_POLL_MAX_MS, ORDER_POLL_MIN_MS, type OptionalCookieCategory, PaymentForm, type PaymentFormProps, type PixelEvent, type ResourceResult, type StoredConsent, type StorefrontHookError, StorefrontProvider, type StorefrontProviderProps, type UseAccountResult, type UseCartResult, type UseCheckoutResult, type UseOrderOptions, type UseOrderResult, configureAnalytics, configurePixels, defaultCartStorage, firePixel, isDeadCartError, isOrderSettled, onSecretPage, orderPollDelay, recordConsent, renderContent, resetAnalytics, resetPixels, scrubbedLocation, track, useAccount, useCart, useCartStorage, useCategories, useCheckout, useConsent, useOrder, useProduct, useProducts, useStore, useStorefrontClient };
package/dist/index.js CHANGED
@@ -293,7 +293,7 @@ var CartStore = class {
293
293
  * anyway); a second call while one is in flight returns the same
294
294
  * promise. The cart is read again afterwards.
295
295
  */
296
- checkout = (options = {}) => {
296
+ checkout = (options) => {
297
297
  if (this.pendingCheckout) return this.pendingCheckout;
298
298
  this.checkoutStarted = true;
299
299
  this.setCheckout({ isPending: true, error: null });
@@ -310,6 +310,7 @@ var CartStore = class {
310
310
  );
311
311
  }
312
312
  return this.client.carts.checkout(id, {
313
+ agreementsHash: options.agreementsHash,
313
314
  turnstileToken: options.turnstileToken || void 0
314
315
  });
315
316
  });
@@ -673,7 +674,10 @@ function useCheckout() {
673
674
  if (store.checkoutStarted) void store.refresh();
674
675
  }, [store]);
675
676
  const checkout = useCallback3(
676
- (options = {}) => store.checkout({ turnstileToken: options.turnstileToken ?? void 0 }),
677
+ (options) => store.checkout({
678
+ agreementsHash: options.agreementsHash,
679
+ turnstileToken: options.turnstileToken ?? void 0
680
+ }),
677
681
  [store]
678
682
  );
679
683
  return { ...snapshot, checkout, reset: store.clearCheckout };
@@ -1101,6 +1105,7 @@ import {
1101
1105
  useCallback as useCallback5,
1102
1106
  useContext as useContext2,
1103
1107
  useEffect as useEffect10,
1108
+ useInsertionEffect,
1104
1109
  useMemo as useMemo3,
1105
1110
  useRef as useRef6,
1106
1111
  useState as useState4,
@@ -1205,7 +1210,9 @@ function ConsentProvider({
1205
1210
  const [decidedVersion, setDecidedVersion] = useState4(null);
1206
1211
  const opener = useRef6(null);
1207
1212
  const onDecisionRef = useRef6(onDecision);
1208
- onDecisionRef.current = onDecision;
1213
+ useInsertionEffect(() => {
1214
+ onDecisionRef.current = onDecision;
1215
+ });
1209
1216
  const categories = useMemo3(
1210
1217
  () => OPTIONAL_CATEGORIES.filter((c) => cookies.some((k) => k.category === c)),
1211
1218
  [cookies]
@@ -1438,12 +1445,17 @@ var pixels;
1438
1445
  var currency = DEFAULT_CURRENCY;
1439
1446
  var granted = false;
1440
1447
  var loaded = /* @__PURE__ */ new Set();
1448
+ var configured = false;
1449
+ var MAX_EARLY = 20;
1450
+ var early = [];
1441
1451
  function resetPixels() {
1442
1452
  pixels = void 0;
1443
1453
  currency = DEFAULT_CURRENCY;
1444
1454
  granted = false;
1445
1455
  optedOut = false;
1446
1456
  loaded.clear();
1457
+ configured = false;
1458
+ early.length = 0;
1447
1459
  }
1448
1460
  var major = (minor) => minor / 100;
1449
1461
  function addScript(src) {
@@ -1471,6 +1483,7 @@ function loadMeta(id) {
1471
1483
  }
1472
1484
  w.fbq.disablePushState = true;
1473
1485
  addScript("https://connect.facebook.net/en_US/fbevents.js");
1486
+ w.fbq("set", "autoConfig", false, id);
1474
1487
  w.fbq("init", id);
1475
1488
  }
1476
1489
  var TT_METHODS = [
@@ -1539,13 +1552,24 @@ function loadGoogle(g) {
1539
1552
  for (const id of ids) {
1540
1553
  w.gtag("config", id, {
1541
1554
  send_page_view: false,
1542
- cookie_domain: cookieDomain
1555
+ cookie_domain: cookieDomain,
1556
+ page_location: scrubbedLocation()
1543
1557
  });
1544
1558
  }
1545
1559
  }
1546
1560
  function onSecretPage(path = typeof location === "undefined" ? "" : location.pathname) {
1547
1561
  return path.startsWith("/orders/") || path.startsWith("/cart/recover");
1548
1562
  }
1563
+ function scrubbedLocation() {
1564
+ const url = new URL(location.href);
1565
+ if (onSecretPage(url.pathname)) {
1566
+ url.search = "";
1567
+ url.hash = "";
1568
+ } else {
1569
+ url.searchParams.delete("token");
1570
+ }
1571
+ return url.toString();
1572
+ }
1549
1573
  function loadAll(p) {
1550
1574
  if (p.meta && !loaded.has("meta")) {
1551
1575
  loaded.add("meta");
@@ -1663,6 +1687,8 @@ function optIn(p) {
1663
1687
  }
1664
1688
  }
1665
1689
  function configurePixels(next, isGranted, opts = {}) {
1690
+ const held = configured ? [] : early.splice(0);
1691
+ configured = true;
1666
1692
  try {
1667
1693
  pixels = next;
1668
1694
  currency = opts.currency ?? DEFAULT_CURRENCY;
@@ -1679,6 +1705,7 @@ function configurePixels(next, isGranted, opts = {}) {
1679
1705
  writeMarker(false);
1680
1706
  }
1681
1707
  }
1708
+ for (const e of held) send2(e);
1682
1709
  } catch {
1683
1710
  }
1684
1711
  }
@@ -1695,7 +1722,23 @@ function markPurchased(orderId) {
1695
1722
  } catch {
1696
1723
  }
1697
1724
  }
1725
+ function copyEvent(e) {
1726
+ try {
1727
+ return structuredClone(e);
1728
+ } catch {
1729
+ return { ...e };
1730
+ }
1731
+ }
1698
1732
  function firePixel(e) {
1733
+ if (!configured) {
1734
+ if (typeof window === "undefined" || onSecretPage()) return;
1735
+ early.push(copyEvent(e));
1736
+ if (early.length > MAX_EARLY) early.shift();
1737
+ return;
1738
+ }
1739
+ send2(e);
1740
+ }
1741
+ function send2(e) {
1699
1742
  try {
1700
1743
  if (!granted || !pixels || onSecretPage()) return;
1701
1744
  loadAll(pixels);
@@ -1810,7 +1853,9 @@ function tiktok(ttq, e) {
1810
1853
  );
1811
1854
  }
1812
1855
  }
1813
- function google(gtag, g, e) {
1856
+ function google(rawGtag, g, e) {
1857
+ const page_location = scrubbedLocation();
1858
+ const gtag = (command, name, params) => rawGtag(command, name, { ...params, page_location });
1814
1859
  const item = (id, quantity, priceMinor) => ({
1815
1860
  item_id: id,
1816
1861
  quantity,
@@ -1900,6 +1945,7 @@ export {
1900
1945
  renderContent,
1901
1946
  resetAnalytics,
1902
1947
  resetPixels,
1948
+ scrubbedLocation,
1903
1949
  track,
1904
1950
  useAccount,
1905
1951
  useCart,