@behio/storefront-sdk 2.0.0 → 2.0.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/README.md CHANGED
@@ -13,6 +13,11 @@ Behio gives you a complete e-commerce backend (products, inventory, orders, cust
13
13
 
14
14
  ## Install
15
15
 
16
+ React cart mutations in 2.0.1 cancel older cart reads before writing and before
17
+ accepting the server response. This includes adding, clearing, applying or
18
+ removing a discount, and merging baskets. A delayed read cannot restore an old
19
+ basket after one of those actions succeeds.
20
+
16
21
  ```bash
17
22
  npm install @behio/storefront-sdk
18
23
  ```
package/dist/react.js CHANGED
@@ -2566,24 +2566,24 @@ function useCart(options) {
2566
2566
  // Only fetch if we have a cart session or are logged in
2567
2567
  enabled: options?.enabled !== false && (!!client.getCartSession() || !!client.getAccessToken())
2568
2568
  });
2569
+ const cancelCartRead = () => queryClient.cancelQueries({ queryKey: [...CART_KEY] });
2570
+ const acceptCart = async (result) => {
2571
+ await cancelCartRead();
2572
+ queryClient.setQueryData([...CART_KEY], result);
2573
+ void refreshBundleQueries(queryClient);
2574
+ };
2569
2575
  const addMutation = (0, import_react_query12.useMutation)({
2570
2576
  mutationFn: async ({ productId, quantity }) => {
2571
2577
  return unwrap(client.cart.addItem({ productId, quantity: quantity ?? 1 }));
2572
2578
  },
2573
- onSuccess: (result) => {
2579
+ onMutate: cancelCartRead,
2580
+ onSuccess: async (result) => {
2574
2581
  if (result.newSessionToken) {
2575
2582
  storage.set(STORAGE_KEYS.CART_SESSION, result.newSessionToken);
2576
2583
  }
2577
- queryClient.setQueryData([...CART_KEY], result);
2578
- void refreshBundleQueries(queryClient);
2584
+ await acceptCart(result);
2579
2585
  }
2580
2586
  });
2581
- const cancelCartRead = () => queryClient.cancelQueries({ queryKey: [...CART_KEY] });
2582
- const acceptCart = async (result) => {
2583
- await cancelCartRead();
2584
- queryClient.setQueryData([...CART_KEY], result);
2585
- void refreshBundleQueries(queryClient);
2586
- };
2587
2587
  const updateMutation = (0, import_react_query12.useMutation)({
2588
2588
  mutationFn: async ({ itemId, quantity }) => {
2589
2589
  return unwrap(client.cart.updateQuantity(itemId, quantity));
@@ -2606,7 +2606,9 @@ function useCart(options) {
2606
2606
  });
2607
2607
  const clearMutation = (0, import_react_query12.useMutation)({
2608
2608
  mutationFn: () => unwrap(client.cart.clear()),
2609
- onSuccess: () => {
2609
+ onMutate: cancelCartRead,
2610
+ onSuccess: async () => {
2611
+ await cancelCartRead();
2610
2612
  queryClient.setQueryData([...CART_KEY], null);
2611
2613
  client.clearCartSession();
2612
2614
  storage.remove(STORAGE_KEYS.CART_SESSION);
@@ -2615,19 +2617,19 @@ function useCart(options) {
2615
2617
  });
2616
2618
  const applyDiscountMutation = (0, import_react_query12.useMutation)({
2617
2619
  mutationFn: (code) => unwrap(client.cart.applyDiscount(code)),
2618
- onSuccess: (result) => {
2619
- queryClient.setQueryData([...CART_KEY], result);
2620
- }
2620
+ onMutate: cancelCartRead,
2621
+ onSuccess: acceptCart
2621
2622
  });
2622
2623
  const removeDiscountMutation = (0, import_react_query12.useMutation)({
2623
2624
  mutationFn: () => unwrap(client.cart.removeDiscount()),
2624
- onSuccess: (result) => {
2625
- queryClient.setQueryData([...CART_KEY], result);
2626
- }
2625
+ onMutate: cancelCartRead,
2626
+ onSuccess: acceptCart
2627
2627
  });
2628
2628
  const mergeMutation = (0, import_react_query12.useMutation)({
2629
2629
  mutationFn: () => unwrap(client.cart.merge()),
2630
- onSuccess: (result) => {
2630
+ onMutate: cancelCartRead,
2631
+ onSuccess: async (result) => {
2632
+ await cancelCartRead();
2631
2633
  queryClient.setQueryData([...CART_KEY], result);
2632
2634
  client.clearCartSession();
2633
2635
  storage.remove(STORAGE_KEYS.CART_SESSION);
package/dist/react.mjs CHANGED
@@ -2456,24 +2456,24 @@ function useCart(options) {
2456
2456
  // Only fetch if we have a cart session or are logged in
2457
2457
  enabled: options?.enabled !== false && (!!client.getCartSession() || !!client.getAccessToken())
2458
2458
  });
2459
+ const cancelCartRead = () => queryClient.cancelQueries({ queryKey: [...CART_KEY] });
2460
+ const acceptCart = async (result) => {
2461
+ await cancelCartRead();
2462
+ queryClient.setQueryData([...CART_KEY], result);
2463
+ void refreshBundleQueries(queryClient);
2464
+ };
2459
2465
  const addMutation = useMutation({
2460
2466
  mutationFn: async ({ productId, quantity }) => {
2461
2467
  return unwrap(client.cart.addItem({ productId, quantity: quantity ?? 1 }));
2462
2468
  },
2463
- onSuccess: (result) => {
2469
+ onMutate: cancelCartRead,
2470
+ onSuccess: async (result) => {
2464
2471
  if (result.newSessionToken) {
2465
2472
  storage.set(STORAGE_KEYS.CART_SESSION, result.newSessionToken);
2466
2473
  }
2467
- queryClient.setQueryData([...CART_KEY], result);
2468
- void refreshBundleQueries(queryClient);
2474
+ await acceptCart(result);
2469
2475
  }
2470
2476
  });
2471
- const cancelCartRead = () => queryClient.cancelQueries({ queryKey: [...CART_KEY] });
2472
- const acceptCart = async (result) => {
2473
- await cancelCartRead();
2474
- queryClient.setQueryData([...CART_KEY], result);
2475
- void refreshBundleQueries(queryClient);
2476
- };
2477
2477
  const updateMutation = useMutation({
2478
2478
  mutationFn: async ({ itemId, quantity }) => {
2479
2479
  return unwrap(client.cart.updateQuantity(itemId, quantity));
@@ -2496,7 +2496,9 @@ function useCart(options) {
2496
2496
  });
2497
2497
  const clearMutation = useMutation({
2498
2498
  mutationFn: () => unwrap(client.cart.clear()),
2499
- onSuccess: () => {
2499
+ onMutate: cancelCartRead,
2500
+ onSuccess: async () => {
2501
+ await cancelCartRead();
2500
2502
  queryClient.setQueryData([...CART_KEY], null);
2501
2503
  client.clearCartSession();
2502
2504
  storage.remove(STORAGE_KEYS.CART_SESSION);
@@ -2505,19 +2507,19 @@ function useCart(options) {
2505
2507
  });
2506
2508
  const applyDiscountMutation = useMutation({
2507
2509
  mutationFn: (code) => unwrap(client.cart.applyDiscount(code)),
2508
- onSuccess: (result) => {
2509
- queryClient.setQueryData([...CART_KEY], result);
2510
- }
2510
+ onMutate: cancelCartRead,
2511
+ onSuccess: acceptCart
2511
2512
  });
2512
2513
  const removeDiscountMutation = useMutation({
2513
2514
  mutationFn: () => unwrap(client.cart.removeDiscount()),
2514
- onSuccess: (result) => {
2515
- queryClient.setQueryData([...CART_KEY], result);
2516
- }
2515
+ onMutate: cancelCartRead,
2516
+ onSuccess: acceptCart
2517
2517
  });
2518
2518
  const mergeMutation = useMutation({
2519
2519
  mutationFn: () => unwrap(client.cart.merge()),
2520
- onSuccess: (result) => {
2520
+ onMutate: cancelCartRead,
2521
+ onSuccess: async (result) => {
2522
+ await cancelCartRead();
2521
2523
  queryClient.setQueryData([...CART_KEY], result);
2522
2524
  client.clearCartSession();
2523
2525
  storage.remove(STORAGE_KEYS.CART_SESSION);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@behio/storefront-sdk",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "TypeScript SDK for Behio headless e-commerce: core client + React hooks",
5
5
  "author": "Behio",
6
6
  "license": "MIT",