@konfiqo/storefront-react 0.6.1 → 0.8.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
@@ -57,8 +57,8 @@ declare function isDeadCartError(e: unknown): boolean;
57
57
  */
58
58
  declare function defaultCartStorage(): CartIdStorage | null;
59
59
 
60
- /** Client options the provider accepts: no secret keys, no buyer IP. */
61
- type ClientConfig = Omit<StorefrontClientOptions, 'serverKey' | 'trustedStorefrontKey' | 'buyerIp'>;
60
+ /** Client options the provider accepts: no secret keys, no buyer IP or user agent. */
61
+ type ClientConfig = Omit<StorefrontClientOptions, 'serverKey' | 'trustedStorefrontKey' | 'buyerIp' | 'buyerUserAgent'>;
62
62
  /** Props of {@link StorefrontProvider}. */
63
63
  interface StorefrontProviderProps {
64
64
  /**
@@ -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
@@ -596,4 +610,4 @@ interface AdPixelsLoaderProps {
596
610
  */
597
611
  declare function AdPixelsLoader({ pixels, hostOnly, currency, pathname, }: AdPixelsLoaderProps): null;
598
612
 
599
- 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
@@ -57,8 +57,8 @@ declare function isDeadCartError(e: unknown): boolean;
57
57
  */
58
58
  declare function defaultCartStorage(): CartIdStorage | null;
59
59
 
60
- /** Client options the provider accepts: no secret keys, no buyer IP. */
61
- type ClientConfig = Omit<StorefrontClientOptions, 'serverKey' | 'trustedStorefrontKey' | 'buyerIp'>;
60
+ /** Client options the provider accepts: no secret keys, no buyer IP or user agent. */
61
+ type ClientConfig = Omit<StorefrontClientOptions, 'serverKey' | 'trustedStorefrontKey' | 'buyerIp' | 'buyerUserAgent'>;
62
62
  /** Props of {@link StorefrontProvider}. */
63
63
  interface StorefrontProviderProps {
64
64
  /**
@@ -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
@@ -596,4 +610,4 @@ interface AdPixelsLoaderProps {
596
610
  */
597
611
  declare function AdPixelsLoader({ pixels, hostOnly, currency, pathname, }: AdPixelsLoaderProps): null;
598
612
 
599
- 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]
@@ -1476,6 +1483,7 @@ function loadMeta(id) {
1476
1483
  }
1477
1484
  w.fbq.disablePushState = true;
1478
1485
  addScript("https://connect.facebook.net/en_US/fbevents.js");
1486
+ w.fbq("set", "autoConfig", false, id);
1479
1487
  w.fbq("init", id);
1480
1488
  }
1481
1489
  var TT_METHODS = [
@@ -1544,13 +1552,24 @@ function loadGoogle(g) {
1544
1552
  for (const id of ids) {
1545
1553
  w.gtag("config", id, {
1546
1554
  send_page_view: false,
1547
- cookie_domain: cookieDomain
1555
+ cookie_domain: cookieDomain,
1556
+ page_location: scrubbedLocation()
1548
1557
  });
1549
1558
  }
1550
1559
  }
1551
1560
  function onSecretPage(path = typeof location === "undefined" ? "" : location.pathname) {
1552
1561
  return path.startsWith("/orders/") || path.startsWith("/cart/recover");
1553
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
+ }
1554
1573
  function loadAll(p) {
1555
1574
  if (p.meta && !loaded.has("meta")) {
1556
1575
  loaded.add("meta");
@@ -1703,10 +1722,17 @@ function markPurchased(orderId) {
1703
1722
  } catch {
1704
1723
  }
1705
1724
  }
1725
+ function copyEvent(e) {
1726
+ try {
1727
+ return structuredClone(e);
1728
+ } catch {
1729
+ return { ...e };
1730
+ }
1731
+ }
1706
1732
  function firePixel(e) {
1707
1733
  if (!configured) {
1708
1734
  if (typeof window === "undefined" || onSecretPage()) return;
1709
- early.push(e);
1735
+ early.push(copyEvent(e));
1710
1736
  if (early.length > MAX_EARLY) early.shift();
1711
1737
  return;
1712
1738
  }
@@ -1827,7 +1853,9 @@ function tiktok(ttq, e) {
1827
1853
  );
1828
1854
  }
1829
1855
  }
1830
- 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 });
1831
1859
  const item = (id, quantity, priceMinor) => ({
1832
1860
  item_id: id,
1833
1861
  quantity,
@@ -1917,6 +1945,7 @@ export {
1917
1945
  renderContent,
1918
1946
  resetAnalytics,
1919
1947
  resetPixels,
1948
+ scrubbedLocation,
1920
1949
  track,
1921
1950
  useAccount,
1922
1951
  useCart,