brainerce 1.0.1 → 1.0.2

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.
@@ -135,19 +135,16 @@ import { getCartTotals } from 'brainerce';
135
135
  const totals = getCartTotals(cart);
136
136
  // { subtotal: 59.98, discount: 10, shipping: 0, total: 49.98 }
137
137
 
138
- // ⚠️ LocalCart vs Cart - KEY DIFFERENCES:
139
- // Server Cart has: id, itemCount, subtotal, discountAmount
140
- // Guest LocalCart has NONE of these! Only: items, couponCode, customer
141
- // To check type: if ('id' in cart) { /* server Cart */ } else { /* LocalCart */ }
142
- // Item count for both: cart.items.length
138
+ // All smart* methods return a server Cart (even for guests via session carts)
139
+ // Cart has: id, itemCount, subtotal, discountAmount, items, couponCode
143
140
  ```
144
141
 
145
142
  ### 🏷️ Coupon Code (Add to Cart Page!)
146
143
 
147
144
  ```typescript
148
- // Apply coupon - only works on server Cart (has 'id' field)
149
- // Server cart is created after startGuestCheckout() or for logged-in users
150
- const updatedCart = await client.applyCoupon(cartId, 'SAVE20');
145
+ // Apply coupon to cart
146
+ const cart = await client.smartGetCart();
147
+ const updatedCart = await client.applyCoupon(cart.id, 'SAVE20');
151
148
  console.log(updatedCart.discountAmount); // "10.00" (string)
152
149
  console.log(updatedCart.couponCode); // "SAVE20"
153
150
 
@@ -682,7 +679,7 @@ waitResult.orderNumber waitResult.status.orderNumber (nested in Pa
682
679
  variant.attributes.map(...) Object.entries(variant.attributes || {}) (it's an object!)
683
680
  categorySuggestion.slug // ❌ doesn't exist! Only: id, name, productCount
684
681
  order.status === 'COMPLETED' order.status === 'delivered' (OrderStatus is lowercase!)
685
- getCartTotals(localCart) // LocalCart has no subtotal! Calculate manually
682
+ getCartTotals(cart) // Works all carts are server carts now
686
683
  result.checkoutId (guest checkout) // ⚠️ Check result.tracked first! It's a union type
687
684
  ```
688
685
 
@@ -690,27 +687,10 @@ result.checkoutId (guest checkout) // ⚠️ Check result.tracked first! It's
690
687
 
691
688
  - **OrderItem** (from orders): Flat structure — `item.price`, `item.name`, `item.image`
692
689
  - **CartItem / CheckoutLineItem**: Nested structure — `item.unitPrice`, `item.product.name`, `item.product.images`
693
- - **`getCartTotals()`** only works with **server Cart** (has `subtotal`/`discountAmount`). For **LocalCart**, calculate manually:
694
- ```typescript
695
- const subtotal = cart.items.reduce(
696
- (sum, item) => sum + parseFloat(item.price || '0') * item.quantity,
697
- 0
698
- );
699
- ```
690
+ - **`getCartTotals()`** works on all carts — guests now use server-side session carts with full `subtotal`/`discountAmount` fields.
700
691
  - **`GuestCheckoutStartResponse`** is a union type — always check `result.tracked` before accessing `result.checkoutId`
701
692
  - **`WaitForOrderResult`** has `result.status.orderNumber`, NOT `result.orderNumber`. But `completeGuestCheckout()` returns `GuestOrderResponse` which DOES have `result.orderNumber` directly.
702
- - **⚠️ HYDRATION: Never use `useState(client.getLocalCart())`** in Next.js causes hydration mismatch! Server has no localStorage (empty cart) but client does (real cart). Always start with empty state and load in `useEffect`:
703
-
704
- ```typescript
705
- // ❌ WRONG — hydration mismatch!
706
- const [cart, setCart] = useState(client.getLocalCart());
707
-
708
- // ✅ CORRECT — load after hydration
709
- const [cart, setCart] = useState<LocalCart>({ items: [], updatedAt: '' });
710
- useEffect(() => {
711
- setCart(client.getLocalCart());
712
- }, []);
713
- ```
693
+ - **Cart state**: Use `useState<Cart | null>(null)` and load with `smartGetCart()` in `useEffect` all carts are server-side now, no hydration mismatch issues.
714
694
 
715
695
  ---
716
696