@moonbase.sh/react 0.0.0-next-20261111164652 → 0.0.0-next-20261111171316
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 +15 -2
- package/dist/index.cjs +46 -17
- package/dist/index.d.cts +36 -2
- package/dist/index.d.ts +36 -2
- package/dist/index.js +38 -11
- package/package.json +2 -4
package/README.md
CHANGED
|
@@ -64,7 +64,8 @@ export function Shop() {
|
|
|
64
64
|
```
|
|
65
65
|
|
|
66
66
|
Hooks return plain values, and a component renders again only when the part of the storefront it reads changes:
|
|
67
|
-
adding to the cart renders the cart's components, not every product card.
|
|
67
|
+
adding to the cart renders the cart's components, not every product card. So a product card prices itself with
|
|
68
|
+
`usePrice` and adds to the cart with `useCartActions`, rather than reading the whole cart with `useCart`. Every function a hook returns keeps its
|
|
68
69
|
identity across renders, so it can go in a dependency list or to a memoized child.
|
|
69
70
|
|
|
70
71
|
## Main hooks
|
|
@@ -79,7 +80,11 @@ identity across renders, so it can go in a dependency list or to a memoized chil
|
|
|
79
80
|
- `usePromotions()` - the sales currently running across the catalog. Already filtered by the API to what is live and
|
|
80
81
|
applies to the current visitor, so render them as they are.
|
|
81
82
|
- `useCart()` - the lines, totals and price breakdown, `soldOutItems`, and `addToCart`, `setQuantity`,
|
|
82
|
-
`removeFromCart` and `checkout`.
|
|
83
|
+
`removeFromCart` and `checkout`. It renders again on every change to the cart.
|
|
84
|
+
- `usePrice(variation)` - a pricing option's `price`, `originalPrice` and `saving` (the last two only with a discount),
|
|
85
|
+
written in the visitor's currency as the checkout writes them, plus `amount` for adding prices up. `useCurrency()` is
|
|
86
|
+
that currency, and `useCartActions()` is `addToCart` and the rest without the cart. None of the three renders again
|
|
87
|
+
when the cart changes, so they suit product cards.
|
|
83
88
|
- `useEligibleOffers()`, `useLockedOffers()`, `useCartOffer()` - the upsells the cart qualifies for, the offers it is
|
|
84
89
|
short of and by how much, and the cart offer it has earned or is working toward.
|
|
85
90
|
- `useCheckout()` - buying straight away without the cart: `const checkout = useCheckout()`, then
|
|
@@ -235,6 +240,14 @@ tab.
|
|
|
235
240
|
|
|
236
241
|
## Server rendering
|
|
237
242
|
|
|
243
|
+
`fetchCatalog(endpoint, options)` fetches the catalog on a server, as anyone sees it, for the `initialCatalog` a page's
|
|
244
|
+
storefront starts from (below). Its `fetch` option goes to `fetch` with the request, so a Next.js page can cache the
|
|
245
|
+
catalog on its own schedule rather than revalidating the whole route:
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
const catalog = await fetchCatalog('https://demo.moonbase.sh', { fetch: { next: { revalidate: 3600 } } })
|
|
249
|
+
```
|
|
250
|
+
|
|
238
251
|
The storefront can be made on a server, where it has no visitor: no cart, nobody signed in, and nothing sent. A
|
|
239
252
|
storefront made at module level in a client module, as above, renders on the server with an empty catalog, so show your
|
|
240
253
|
own fallback prices until it loads, and the browser takes over from there. The browser hydrates against that same
|
package/dist/index.cjs
CHANGED
|
@@ -29,10 +29,12 @@ __export(index_exports, {
|
|
|
29
29
|
useBundle: () => useBundle,
|
|
30
30
|
useBundles: () => useBundles,
|
|
31
31
|
useCart: () => useCart,
|
|
32
|
+
useCartActions: () => useCartActions,
|
|
32
33
|
useCartOffer: () => useCartOffer,
|
|
33
34
|
useCatalog: () => useCatalog,
|
|
34
35
|
useCheckout: () => useCheckout,
|
|
35
36
|
useCommunicationPreferences: () => useCommunicationPreferences,
|
|
37
|
+
useCurrency: () => useCurrency,
|
|
36
38
|
useEligibleOffers: () => useEligibleOffers,
|
|
37
39
|
useGroups: () => useGroups,
|
|
38
40
|
useInventory: () => useInventory,
|
|
@@ -40,6 +42,7 @@ __export(index_exports, {
|
|
|
40
42
|
useOffer: () => useOffer,
|
|
41
43
|
useOffers: () => useOffers,
|
|
42
44
|
useOrders: () => useOrders,
|
|
45
|
+
usePrice: () => usePrice,
|
|
43
46
|
useProduct: () => useProduct,
|
|
44
47
|
useProducts: () => useProducts,
|
|
45
48
|
usePromotions: () => usePromotions,
|
|
@@ -51,10 +54,10 @@ __export(index_exports, {
|
|
|
51
54
|
useVoucher: () => useVoucher
|
|
52
55
|
});
|
|
53
56
|
module.exports = __toCommonJS(index_exports);
|
|
54
|
-
var
|
|
57
|
+
var import_storefront_api12 = require("@moonbase.sh/storefront-api");
|
|
55
58
|
|
|
56
59
|
// package.json
|
|
57
|
-
var version = "0.0.0-next-
|
|
60
|
+
var version = "0.0.0-next-20261111171316";
|
|
58
61
|
|
|
59
62
|
// src/storefront.ts
|
|
60
63
|
var import_storefront_api5 = require("@moonbase.sh/storefront-api");
|
|
@@ -148,7 +151,6 @@ var import_storefront_api4 = require("@moonbase.sh/storefront-api");
|
|
|
148
151
|
|
|
149
152
|
// src/checkout.ts
|
|
150
153
|
var import_storefront_api2 = require("@moonbase.sh/storefront-api");
|
|
151
|
-
var import_uuid = require("uuid");
|
|
152
154
|
var wrapperId = "moonbase-checkout-wrapper";
|
|
153
155
|
function checkoutInProgress() {
|
|
154
156
|
return typeof window !== "undefined" && !!document.getElementById(wrapperId);
|
|
@@ -229,13 +231,13 @@ async function checkout(storefront, items, options = {}) {
|
|
|
229
231
|
let resumed = await (0, import_storefront_api2.resumePurchase)(storefront.client, userId, options.couponCode);
|
|
230
232
|
let order;
|
|
231
233
|
try {
|
|
232
|
-
order = await pushOrder((_d = resumed == null ? void 0 : resumed.orderId) != null ? _d : (0,
|
|
234
|
+
order = await pushOrder((_d = resumed == null ? void 0 : resumed.orderId) != null ? _d : (0, import_storefront_api2.randomUuid)());
|
|
233
235
|
} catch (err) {
|
|
234
236
|
if (!resumed || !(0, import_storefront_api2.isClosedOrderRefusal)(err))
|
|
235
237
|
throw err;
|
|
236
238
|
(0, import_storefront_api2.forgetPurchase)(resumed.orderId);
|
|
237
239
|
resumed = null;
|
|
238
|
-
order = await pushOrder((0,
|
|
240
|
+
order = await pushOrder((0, import_storefront_api2.randomUuid)());
|
|
239
241
|
}
|
|
240
242
|
await (0, import_storefront_api2.recordCheckoutGroups)(storefront.client, order.id, options.groupIds);
|
|
241
243
|
let ownCoupon = (_e = resumed == null ? void 0 : resumed.couponCode) != null ? _e : null;
|
|
@@ -280,7 +282,6 @@ async function checkout(storefront, items, options = {}) {
|
|
|
280
282
|
|
|
281
283
|
// src/order.ts
|
|
282
284
|
var import_storefront_api3 = require("@moonbase.sh/storefront-api");
|
|
283
|
-
var import_uuid2 = require("uuid");
|
|
284
285
|
function emptyCatalog() {
|
|
285
286
|
return {
|
|
286
287
|
suggestedCurrency: "",
|
|
@@ -293,7 +294,7 @@ function emptyCatalog() {
|
|
|
293
294
|
}
|
|
294
295
|
function newOrder(currency = "", items = []) {
|
|
295
296
|
return {
|
|
296
|
-
id: (0,
|
|
297
|
+
id: (0, import_storefront_api3.randomUuid)(),
|
|
297
298
|
status: import_storefront_api3.OrderStatus.Open,
|
|
298
299
|
currency,
|
|
299
300
|
items,
|
|
@@ -1464,6 +1465,12 @@ function useCart() {
|
|
|
1464
1465
|
};
|
|
1465
1466
|
}, [storefront, catalog, order]);
|
|
1466
1467
|
}
|
|
1468
|
+
function useCurrency() {
|
|
1469
|
+
return useStorefrontState((state) => (0, import_storefront_api8.resolveDisplayCurrency)(state.order, state.catalog));
|
|
1470
|
+
}
|
|
1471
|
+
function useCartActions() {
|
|
1472
|
+
return useStorefront().cart;
|
|
1473
|
+
}
|
|
1467
1474
|
|
|
1468
1475
|
// src/hooks/useCheckout.ts
|
|
1469
1476
|
var import_react6 = require("react");
|
|
@@ -1584,12 +1591,31 @@ function useOrders() {
|
|
|
1584
1591
|
}), [storefront]);
|
|
1585
1592
|
}
|
|
1586
1593
|
|
|
1587
|
-
// src/hooks/
|
|
1594
|
+
// src/hooks/usePrice.ts
|
|
1595
|
+
var import_storefront_api10 = require("@moonbase.sh/storefront-api");
|
|
1588
1596
|
var import_react11 = require("react");
|
|
1597
|
+
function usePrice(variation, format) {
|
|
1598
|
+
const currency = useCurrency();
|
|
1599
|
+
return (0, import_react11.useMemo)(() => {
|
|
1600
|
+
const amount = variation == null ? void 0 : variation.price[currency];
|
|
1601
|
+
const original = (variation == null ? void 0 : variation.hasDiscount) ? variation.originalPrice[currency] : void 0;
|
|
1602
|
+
const saving = amount !== void 0 && original !== void 0 ? Number((original - amount).toFixed(6)) : void 0;
|
|
1603
|
+
return {
|
|
1604
|
+
currency,
|
|
1605
|
+
price: amount === void 0 ? void 0 : (0, import_storefront_api10.formatMoney)(amount, currency, format),
|
|
1606
|
+
originalPrice: original === void 0 ? void 0 : (0, import_storefront_api10.formatMoney)(original, currency, format),
|
|
1607
|
+
saving: saving === void 0 || saving <= 0 ? void 0 : (0, import_storefront_api10.formatMoney)(saving, currency, format),
|
|
1608
|
+
amount
|
|
1609
|
+
};
|
|
1610
|
+
}, [variation, currency, format]);
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
// src/hooks/useReviews.ts
|
|
1614
|
+
var import_react12 = require("react");
|
|
1589
1615
|
function useReviews() {
|
|
1590
1616
|
const storefront = useStorefront();
|
|
1591
1617
|
const enabled = useStorefrontState((state) => !!state.catalog.reviewsEnabled);
|
|
1592
|
-
return (0,
|
|
1618
|
+
return (0, import_react12.useMemo)(() => {
|
|
1593
1619
|
const reviews = storefront.client.reviews;
|
|
1594
1620
|
return {
|
|
1595
1621
|
enabled,
|
|
@@ -1604,11 +1630,11 @@ function useReviews() {
|
|
|
1604
1630
|
}
|
|
1605
1631
|
|
|
1606
1632
|
// src/hooks/useVendor.ts
|
|
1607
|
-
var
|
|
1633
|
+
var import_react13 = require("react");
|
|
1608
1634
|
function useVendor() {
|
|
1609
1635
|
const storefront = useStorefront();
|
|
1610
|
-
const [state, setState] = (0,
|
|
1611
|
-
(0,
|
|
1636
|
+
const [state, setState] = (0, import_react13.useState)({ vendor: null, loading: true });
|
|
1637
|
+
(0, import_react13.useEffect)(() => {
|
|
1612
1638
|
let current = true;
|
|
1613
1639
|
storefront.client.vendor.get().then((vendor) => current && setState({ vendor, loading: false })).catch(() => current && setState({ vendor: null, loading: false }));
|
|
1614
1640
|
return () => {
|
|
@@ -1619,16 +1645,16 @@ function useVendor() {
|
|
|
1619
1645
|
}
|
|
1620
1646
|
|
|
1621
1647
|
// src/hooks/useVoucher.ts
|
|
1622
|
-
var
|
|
1623
|
-
var
|
|
1648
|
+
var import_storefront_api11 = require("@moonbase.sh/storefront-api");
|
|
1649
|
+
var import_react14 = require("react");
|
|
1624
1650
|
function useVoucher() {
|
|
1625
1651
|
const storefront = useStorefront();
|
|
1626
|
-
return (0,
|
|
1652
|
+
return (0, import_react14.useMemo)(() => {
|
|
1627
1653
|
const known = async (lookup) => {
|
|
1628
1654
|
try {
|
|
1629
1655
|
return await lookup;
|
|
1630
1656
|
} catch (err) {
|
|
1631
|
-
if (err instanceof
|
|
1657
|
+
if (err instanceof import_storefront_api11.NotFoundError)
|
|
1632
1658
|
throw new Error("Voucher code invalid");
|
|
1633
1659
|
throw err;
|
|
1634
1660
|
}
|
|
@@ -1651,7 +1677,7 @@ function createStorefront(endpoint, options = {}) {
|
|
|
1651
1677
|
// Named in `x-mb-client` ahead of `@moonbase.sh/storefront-api`, after any SDK wrapping this one.
|
|
1652
1678
|
sdk: `${(_a = config.sdk) != null ? _a : ""} @moonbase.sh/react/${version}`.trim().replace(/\s+/g, " ")
|
|
1653
1679
|
};
|
|
1654
|
-
return new MoonbaseStorefront(configuration, new
|
|
1680
|
+
return new MoonbaseStorefront(configuration, new import_storefront_api12.MoonbaseClient(configuration), { initialCatalog, autoApplyItemOffers });
|
|
1655
1681
|
}
|
|
1656
1682
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1657
1683
|
0 && (module.exports = {
|
|
@@ -1663,10 +1689,12 @@ function createStorefront(endpoint, options = {}) {
|
|
|
1663
1689
|
useBundle,
|
|
1664
1690
|
useBundles,
|
|
1665
1691
|
useCart,
|
|
1692
|
+
useCartActions,
|
|
1666
1693
|
useCartOffer,
|
|
1667
1694
|
useCatalog,
|
|
1668
1695
|
useCheckout,
|
|
1669
1696
|
useCommunicationPreferences,
|
|
1697
|
+
useCurrency,
|
|
1670
1698
|
useEligibleOffers,
|
|
1671
1699
|
useGroups,
|
|
1672
1700
|
useInventory,
|
|
@@ -1674,6 +1702,7 @@ function createStorefront(endpoint, options = {}) {
|
|
|
1674
1702
|
useOffer,
|
|
1675
1703
|
useOffers,
|
|
1676
1704
|
useOrders,
|
|
1705
|
+
usePrice,
|
|
1677
1706
|
useProduct,
|
|
1678
1707
|
useProducts,
|
|
1679
1708
|
usePromotions,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { User, Address, SignUpOptions, SignUpResult, CommunicationPreferences, UserAccountConfirmed, ConnectableAccountProvider, CheckoutRedirectMode, StorefrontProduct, StorefrontBundle, PricingVariation, StorefrontOffer, LineItem, MoonbaseConfiguration, Storefront, MoonbaseClient, Order, UrchinTrackingModule, OpenOrder, Money, CheckoutPaymentFailure, CompletedOrder, AdConsentReport, AdSignals, OfferConditionContext, StorefrontPromotion, OfferProgress, ActivationRequest, CartTotal, SubscribeInput, CommunicationPreferencesInput, Activation, ActivationMethod, Download, SubscriptionCancellationFeedback, Subscription, Page, ProductReviewSort, SubmitReviewRequest, ReviewVoteDirection, Vendor, Voucher } from '@moonbase.sh/storefront-api';
|
|
1
|
+
import { User, Address, SignUpOptions, SignUpResult, CommunicationPreferences, UserAccountConfirmed, ConnectableAccountProvider, CheckoutRedirectMode, StorefrontProduct, StorefrontBundle, PricingVariation, StorefrontOffer, LineItem, MoonbaseConfiguration, Storefront, MoonbaseClient, Order, UrchinTrackingModule, OpenOrder, Money, CheckoutPaymentFailure, CompletedOrder, AdConsentReport, AdSignals, OfferConditionContext, StorefrontPromotion, OfferProgress, ActivationRequest, CartTotal, SubscribeInput, CommunicationPreferencesInput, Activation, ActivationMethod, Download, SubscriptionCancellationFeedback, Subscription, Page, PriceFormat, ProductReviewSort, SubmitReviewRequest, ReviewVoteDirection, Vendor, Voucher } from '@moonbase.sh/storefront-api';
|
|
2
2
|
export * from '@moonbase.sh/storefront-api';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
|
|
@@ -510,6 +510,17 @@ interface Cart extends StorefrontCart {
|
|
|
510
510
|
}
|
|
511
511
|
/** The cart, and changing it. Every function keeps its identity across renders. */
|
|
512
512
|
declare function useCart(): Cart;
|
|
513
|
+
/**
|
|
514
|
+
* The currency the cart shows and checks out in: the one the store suggests for this visitor. Renders again
|
|
515
|
+
* only when that changes, not on every change to the cart, so a product card can price itself without
|
|
516
|
+
* `useCart`.
|
|
517
|
+
*/
|
|
518
|
+
declare function useCurrency(): string;
|
|
519
|
+
/**
|
|
520
|
+
* Changing the cart, `addToCart` and the rest, without reading it: a component using only this never
|
|
521
|
+
* renders again because the cart changed. The same object every time.
|
|
522
|
+
*/
|
|
523
|
+
declare function useCartActions(): StorefrontCart;
|
|
513
524
|
|
|
514
525
|
/**
|
|
515
526
|
* Buying straight away, without the cart: the function a "Buy now" button calls.
|
|
@@ -602,6 +613,29 @@ interface Orders {
|
|
|
602
613
|
/** The signed-in customer's purchases, for an account area of your own. */
|
|
603
614
|
declare function useOrders(): Orders;
|
|
604
615
|
|
|
616
|
+
/** A pricing option's price in the visitor's currency, written the way the checkout writes it. */
|
|
617
|
+
interface VariationPrice {
|
|
618
|
+
/** The visitor's currency, which the cart and the checkout use too. */
|
|
619
|
+
currency: string;
|
|
620
|
+
/** What it costs, or undefined until the catalog has a price for it in this currency. */
|
|
621
|
+
price: string | undefined;
|
|
622
|
+
/** What it cost before its discount, when it has one. */
|
|
623
|
+
originalPrice: string | undefined;
|
|
624
|
+
/** What its discount takes off, when it has one. */
|
|
625
|
+
saving: string | undefined;
|
|
626
|
+
/** What it costs as a number, for adding prices up. */
|
|
627
|
+
amount: number | undefined;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* A pricing option's price, before and after its discount, in the visitor's currency. Renders again when
|
|
631
|
+
* the currency or the option changes, not when the cart does.
|
|
632
|
+
*
|
|
633
|
+
* ```tsx
|
|
634
|
+
* const { price, originalPrice } = usePrice(product.defaultVariation)
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
declare function usePrice(variation: PricingVariation | null | undefined, format?: PriceFormat): VariationPrice;
|
|
638
|
+
|
|
605
639
|
type Reviews = MoonbaseClient['reviews'];
|
|
606
640
|
interface ProductReviews {
|
|
607
641
|
/** Whether the store takes reviews. Off until the catalog has loaded, and on older backends. */
|
|
@@ -690,4 +724,4 @@ declare function useStorefrontEvent<K extends keyof StorefrontEvents>(event: K,
|
|
|
690
724
|
*/
|
|
691
725
|
declare function createStorefront(endpoint: string, options?: StorefrontOptions): MoonbaseStorefront;
|
|
692
726
|
|
|
693
|
-
export { type ActivationRequestState, type AddToCartOptions, type AttributionConsentStatus, type Auth, type Cart, type CartCheckoutOptions, type CartItem, type CartOfferState, type CheckoutItem, type CheckoutOptions, type CheckoutRedirectOptions, type CheckoutResult, type Communications, type Groups, type LockedOffer, MoonbaseStorefront, type Orders, type ProductReviews, type StorefrontAuth, type StorefrontCart, type StorefrontEvents, type StorefrontInventory, type StorefrontOptions, StorefrontProvider, type StorefrontProviderProps, type StorefrontState, type VendorState, type Vouchers, createStorefront, useActivationRequest, useAuth, useBundle, useBundles, useCart, useCartOffer, useCatalog, useCheckout, useCommunicationPreferences, useEligibleOffers, useGroups, useInventory, useLockedOffers, useOffer, useOffers, useOrders, useProduct, useProducts, usePromotions, useReviews, useStorefront, useStorefrontEvent, useStorefrontState, useVendor, useVoucher };
|
|
727
|
+
export { type ActivationRequestState, type AddToCartOptions, type AttributionConsentStatus, type Auth, type Cart, type CartCheckoutOptions, type CartItem, type CartOfferState, type CheckoutItem, type CheckoutOptions, type CheckoutRedirectOptions, type CheckoutResult, type Communications, type Groups, type LockedOffer, MoonbaseStorefront, type Orders, type ProductReviews, type StorefrontAuth, type StorefrontCart, type StorefrontEvents, type StorefrontInventory, type StorefrontOptions, StorefrontProvider, type StorefrontProviderProps, type StorefrontState, type VariationPrice, type VendorState, type Vouchers, createStorefront, useActivationRequest, useAuth, useBundle, useBundles, useCart, useCartActions, useCartOffer, useCatalog, useCheckout, useCommunicationPreferences, useCurrency, useEligibleOffers, useGroups, useInventory, useLockedOffers, useOffer, useOffers, useOrders, usePrice, useProduct, useProducts, usePromotions, useReviews, useStorefront, useStorefrontEvent, useStorefrontState, useVendor, useVoucher };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { User, Address, SignUpOptions, SignUpResult, CommunicationPreferences, UserAccountConfirmed, ConnectableAccountProvider, CheckoutRedirectMode, StorefrontProduct, StorefrontBundle, PricingVariation, StorefrontOffer, LineItem, MoonbaseConfiguration, Storefront, MoonbaseClient, Order, UrchinTrackingModule, OpenOrder, Money, CheckoutPaymentFailure, CompletedOrder, AdConsentReport, AdSignals, OfferConditionContext, StorefrontPromotion, OfferProgress, ActivationRequest, CartTotal, SubscribeInput, CommunicationPreferencesInput, Activation, ActivationMethod, Download, SubscriptionCancellationFeedback, Subscription, Page, ProductReviewSort, SubmitReviewRequest, ReviewVoteDirection, Vendor, Voucher } from '@moonbase.sh/storefront-api';
|
|
1
|
+
import { User, Address, SignUpOptions, SignUpResult, CommunicationPreferences, UserAccountConfirmed, ConnectableAccountProvider, CheckoutRedirectMode, StorefrontProduct, StorefrontBundle, PricingVariation, StorefrontOffer, LineItem, MoonbaseConfiguration, Storefront, MoonbaseClient, Order, UrchinTrackingModule, OpenOrder, Money, CheckoutPaymentFailure, CompletedOrder, AdConsentReport, AdSignals, OfferConditionContext, StorefrontPromotion, OfferProgress, ActivationRequest, CartTotal, SubscribeInput, CommunicationPreferencesInput, Activation, ActivationMethod, Download, SubscriptionCancellationFeedback, Subscription, Page, PriceFormat, ProductReviewSort, SubmitReviewRequest, ReviewVoteDirection, Vendor, Voucher } from '@moonbase.sh/storefront-api';
|
|
2
2
|
export * from '@moonbase.sh/storefront-api';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
4
|
|
|
@@ -510,6 +510,17 @@ interface Cart extends StorefrontCart {
|
|
|
510
510
|
}
|
|
511
511
|
/** The cart, and changing it. Every function keeps its identity across renders. */
|
|
512
512
|
declare function useCart(): Cart;
|
|
513
|
+
/**
|
|
514
|
+
* The currency the cart shows and checks out in: the one the store suggests for this visitor. Renders again
|
|
515
|
+
* only when that changes, not on every change to the cart, so a product card can price itself without
|
|
516
|
+
* `useCart`.
|
|
517
|
+
*/
|
|
518
|
+
declare function useCurrency(): string;
|
|
519
|
+
/**
|
|
520
|
+
* Changing the cart, `addToCart` and the rest, without reading it: a component using only this never
|
|
521
|
+
* renders again because the cart changed. The same object every time.
|
|
522
|
+
*/
|
|
523
|
+
declare function useCartActions(): StorefrontCart;
|
|
513
524
|
|
|
514
525
|
/**
|
|
515
526
|
* Buying straight away, without the cart: the function a "Buy now" button calls.
|
|
@@ -602,6 +613,29 @@ interface Orders {
|
|
|
602
613
|
/** The signed-in customer's purchases, for an account area of your own. */
|
|
603
614
|
declare function useOrders(): Orders;
|
|
604
615
|
|
|
616
|
+
/** A pricing option's price in the visitor's currency, written the way the checkout writes it. */
|
|
617
|
+
interface VariationPrice {
|
|
618
|
+
/** The visitor's currency, which the cart and the checkout use too. */
|
|
619
|
+
currency: string;
|
|
620
|
+
/** What it costs, or undefined until the catalog has a price for it in this currency. */
|
|
621
|
+
price: string | undefined;
|
|
622
|
+
/** What it cost before its discount, when it has one. */
|
|
623
|
+
originalPrice: string | undefined;
|
|
624
|
+
/** What its discount takes off, when it has one. */
|
|
625
|
+
saving: string | undefined;
|
|
626
|
+
/** What it costs as a number, for adding prices up. */
|
|
627
|
+
amount: number | undefined;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* A pricing option's price, before and after its discount, in the visitor's currency. Renders again when
|
|
631
|
+
* the currency or the option changes, not when the cart does.
|
|
632
|
+
*
|
|
633
|
+
* ```tsx
|
|
634
|
+
* const { price, originalPrice } = usePrice(product.defaultVariation)
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
declare function usePrice(variation: PricingVariation | null | undefined, format?: PriceFormat): VariationPrice;
|
|
638
|
+
|
|
605
639
|
type Reviews = MoonbaseClient['reviews'];
|
|
606
640
|
interface ProductReviews {
|
|
607
641
|
/** Whether the store takes reviews. Off until the catalog has loaded, and on older backends. */
|
|
@@ -690,4 +724,4 @@ declare function useStorefrontEvent<K extends keyof StorefrontEvents>(event: K,
|
|
|
690
724
|
*/
|
|
691
725
|
declare function createStorefront(endpoint: string, options?: StorefrontOptions): MoonbaseStorefront;
|
|
692
726
|
|
|
693
|
-
export { type ActivationRequestState, type AddToCartOptions, type AttributionConsentStatus, type Auth, type Cart, type CartCheckoutOptions, type CartItem, type CartOfferState, type CheckoutItem, type CheckoutOptions, type CheckoutRedirectOptions, type CheckoutResult, type Communications, type Groups, type LockedOffer, MoonbaseStorefront, type Orders, type ProductReviews, type StorefrontAuth, type StorefrontCart, type StorefrontEvents, type StorefrontInventory, type StorefrontOptions, StorefrontProvider, type StorefrontProviderProps, type StorefrontState, type VendorState, type Vouchers, createStorefront, useActivationRequest, useAuth, useBundle, useBundles, useCart, useCartOffer, useCatalog, useCheckout, useCommunicationPreferences, useEligibleOffers, useGroups, useInventory, useLockedOffers, useOffer, useOffers, useOrders, useProduct, useProducts, usePromotions, useReviews, useStorefront, useStorefrontEvent, useStorefrontState, useVendor, useVoucher };
|
|
727
|
+
export { type ActivationRequestState, type AddToCartOptions, type AttributionConsentStatus, type Auth, type Cart, type CartCheckoutOptions, type CartItem, type CartOfferState, type CheckoutItem, type CheckoutOptions, type CheckoutRedirectOptions, type CheckoutResult, type Communications, type Groups, type LockedOffer, MoonbaseStorefront, type Orders, type ProductReviews, type StorefrontAuth, type StorefrontCart, type StorefrontEvents, type StorefrontInventory, type StorefrontOptions, StorefrontProvider, type StorefrontProviderProps, type StorefrontState, type VariationPrice, type VendorState, type Vouchers, createStorefront, useActivationRequest, useAuth, useBundle, useBundles, useCart, useCartActions, useCartOffer, useCatalog, useCheckout, useCommunicationPreferences, useCurrency, useEligibleOffers, useGroups, useInventory, useLockedOffers, useOffer, useOffers, useOrders, usePrice, useProduct, useProducts, usePromotions, useReviews, useStorefront, useStorefrontEvent, useStorefrontState, useVendor, useVoucher };
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { MoonbaseClient } from "@moonbase.sh/storefront-api";
|
|
3
3
|
|
|
4
4
|
// package.json
|
|
5
|
-
var version = "0.0.0-next-
|
|
5
|
+
var version = "0.0.0-next-20261111171316";
|
|
6
6
|
|
|
7
7
|
// src/storefront.ts
|
|
8
8
|
import {
|
|
@@ -152,6 +152,7 @@ import {
|
|
|
152
152
|
lineItemId,
|
|
153
153
|
MoonbaseError,
|
|
154
154
|
mountCheckout,
|
|
155
|
+
randomUuid,
|
|
155
156
|
recordCheckoutGroups,
|
|
156
157
|
rememberPurchase,
|
|
157
158
|
resolveRedirect,
|
|
@@ -162,7 +163,6 @@ import {
|
|
|
162
163
|
watchForTakeover,
|
|
163
164
|
withoutHandshakeParams
|
|
164
165
|
} from "@moonbase.sh/storefront-api";
|
|
165
|
-
import { v4 as uuidv4 } from "uuid";
|
|
166
166
|
var wrapperId = "moonbase-checkout-wrapper";
|
|
167
167
|
function checkoutInProgress() {
|
|
168
168
|
return typeof window !== "undefined" && !!document.getElementById(wrapperId);
|
|
@@ -243,13 +243,13 @@ async function checkout(storefront, items, options = {}) {
|
|
|
243
243
|
let resumed = await resumePurchase(storefront.client, userId, options.couponCode);
|
|
244
244
|
let order;
|
|
245
245
|
try {
|
|
246
|
-
order = await pushOrder((_d = resumed == null ? void 0 : resumed.orderId) != null ? _d :
|
|
246
|
+
order = await pushOrder((_d = resumed == null ? void 0 : resumed.orderId) != null ? _d : randomUuid());
|
|
247
247
|
} catch (err) {
|
|
248
248
|
if (!resumed || !isClosedOrderRefusal(err))
|
|
249
249
|
throw err;
|
|
250
250
|
forgetPurchase2(resumed.orderId);
|
|
251
251
|
resumed = null;
|
|
252
|
-
order = await pushOrder(
|
|
252
|
+
order = await pushOrder(randomUuid());
|
|
253
253
|
}
|
|
254
254
|
await recordCheckoutGroups(storefront.client, order.id, options.groupIds);
|
|
255
255
|
let ownCoupon = (_e = resumed == null ? void 0 : resumed.couponCode) != null ? _e : null;
|
|
@@ -293,8 +293,7 @@ async function checkout(storefront, items, options = {}) {
|
|
|
293
293
|
}
|
|
294
294
|
|
|
295
295
|
// src/order.ts
|
|
296
|
-
import { CartUtils as CartUtils2, OfferUtils, OrderStatus, resolveDisplayCurrency } from "@moonbase.sh/storefront-api";
|
|
297
|
-
import { v4 as uuidv42 } from "uuid";
|
|
296
|
+
import { CartUtils as CartUtils2, OfferUtils, OrderStatus, randomUuid as randomUuid2, resolveDisplayCurrency } from "@moonbase.sh/storefront-api";
|
|
298
297
|
function emptyCatalog() {
|
|
299
298
|
return {
|
|
300
299
|
suggestedCurrency: "",
|
|
@@ -307,7 +306,7 @@ function emptyCatalog() {
|
|
|
307
306
|
}
|
|
308
307
|
function newOrder(currency = "", items = []) {
|
|
309
308
|
return {
|
|
310
|
-
id:
|
|
309
|
+
id: randomUuid2(),
|
|
311
310
|
status: OrderStatus.Open,
|
|
312
311
|
currency,
|
|
313
312
|
items,
|
|
@@ -1478,6 +1477,12 @@ function useCart() {
|
|
|
1478
1477
|
};
|
|
1479
1478
|
}, [storefront, catalog, order]);
|
|
1480
1479
|
}
|
|
1480
|
+
function useCurrency() {
|
|
1481
|
+
return useStorefrontState((state) => resolveDisplayCurrency5(state.order, state.catalog));
|
|
1482
|
+
}
|
|
1483
|
+
function useCartActions() {
|
|
1484
|
+
return useStorefront().cart;
|
|
1485
|
+
}
|
|
1481
1486
|
|
|
1482
1487
|
// src/hooks/useCheckout.ts
|
|
1483
1488
|
import { useCallback as useCallback2 } from "react";
|
|
@@ -1598,12 +1603,31 @@ function useOrders() {
|
|
|
1598
1603
|
}), [storefront]);
|
|
1599
1604
|
}
|
|
1600
1605
|
|
|
1601
|
-
// src/hooks/
|
|
1606
|
+
// src/hooks/usePrice.ts
|
|
1607
|
+
import { formatMoney } from "@moonbase.sh/storefront-api";
|
|
1602
1608
|
import { useMemo as useMemo8 } from "react";
|
|
1609
|
+
function usePrice(variation, format) {
|
|
1610
|
+
const currency = useCurrency();
|
|
1611
|
+
return useMemo8(() => {
|
|
1612
|
+
const amount = variation == null ? void 0 : variation.price[currency];
|
|
1613
|
+
const original = (variation == null ? void 0 : variation.hasDiscount) ? variation.originalPrice[currency] : void 0;
|
|
1614
|
+
const saving = amount !== void 0 && original !== void 0 ? Number((original - amount).toFixed(6)) : void 0;
|
|
1615
|
+
return {
|
|
1616
|
+
currency,
|
|
1617
|
+
price: amount === void 0 ? void 0 : formatMoney(amount, currency, format),
|
|
1618
|
+
originalPrice: original === void 0 ? void 0 : formatMoney(original, currency, format),
|
|
1619
|
+
saving: saving === void 0 || saving <= 0 ? void 0 : formatMoney(saving, currency, format),
|
|
1620
|
+
amount
|
|
1621
|
+
};
|
|
1622
|
+
}, [variation, currency, format]);
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
// src/hooks/useReviews.ts
|
|
1626
|
+
import { useMemo as useMemo9 } from "react";
|
|
1603
1627
|
function useReviews() {
|
|
1604
1628
|
const storefront = useStorefront();
|
|
1605
1629
|
const enabled = useStorefrontState((state) => !!state.catalog.reviewsEnabled);
|
|
1606
|
-
return
|
|
1630
|
+
return useMemo9(() => {
|
|
1607
1631
|
const reviews = storefront.client.reviews;
|
|
1608
1632
|
return {
|
|
1609
1633
|
enabled,
|
|
@@ -1634,10 +1658,10 @@ function useVendor() {
|
|
|
1634
1658
|
|
|
1635
1659
|
// src/hooks/useVoucher.ts
|
|
1636
1660
|
import { NotFoundError as NotFoundError2 } from "@moonbase.sh/storefront-api";
|
|
1637
|
-
import { useMemo as
|
|
1661
|
+
import { useMemo as useMemo10 } from "react";
|
|
1638
1662
|
function useVoucher() {
|
|
1639
1663
|
const storefront = useStorefront();
|
|
1640
|
-
return
|
|
1664
|
+
return useMemo10(() => {
|
|
1641
1665
|
const known = async (lookup) => {
|
|
1642
1666
|
try {
|
|
1643
1667
|
return await lookup;
|
|
@@ -1676,10 +1700,12 @@ export {
|
|
|
1676
1700
|
useBundle,
|
|
1677
1701
|
useBundles,
|
|
1678
1702
|
useCart,
|
|
1703
|
+
useCartActions,
|
|
1679
1704
|
useCartOffer,
|
|
1680
1705
|
useCatalog,
|
|
1681
1706
|
useCheckout,
|
|
1682
1707
|
useCommunicationPreferences,
|
|
1708
|
+
useCurrency,
|
|
1683
1709
|
useEligibleOffers,
|
|
1684
1710
|
useGroups,
|
|
1685
1711
|
useInventory,
|
|
@@ -1687,6 +1713,7 @@ export {
|
|
|
1687
1713
|
useOffer,
|
|
1688
1714
|
useOffers,
|
|
1689
1715
|
useOrders,
|
|
1716
|
+
usePrice,
|
|
1690
1717
|
useProduct,
|
|
1691
1718
|
useProducts,
|
|
1692
1719
|
usePromotions,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moonbase.sh/react",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next-
|
|
4
|
+
"version": "0.0.0-next-20261111171316",
|
|
5
5
|
"description": "Package to let you build React storefronts with Moonbase.sh as payment and delivery provider",
|
|
6
6
|
"author": "Tobias Lønnerød Madsen <m@dsen.tv>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -16,15 +16,13 @@
|
|
|
16
16
|
"react": ">=18"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@moonbase.sh/storefront-api": "0.0.0-next-
|
|
20
|
-
"uuid": "^9.0.1"
|
|
19
|
+
"@moonbase.sh/storefront-api": "0.0.0-next-20261111171316"
|
|
21
20
|
},
|
|
22
21
|
"devDependencies": {
|
|
23
22
|
"@types/node": "^22.5.4",
|
|
24
23
|
"@types/react": "^19.3.0",
|
|
25
24
|
"@types/react-dom": "^19.3.0",
|
|
26
25
|
"@types/react-test-renderer": "^19.3.0",
|
|
27
|
-
"@types/uuid": "^9.0.8",
|
|
28
26
|
"react": "19.3.0",
|
|
29
27
|
"react-dom": "19.3.0",
|
|
30
28
|
"react-test-renderer": "19.3.0",
|