@moonbase.sh/vue 0.3.4 → 0.3.6

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.cjs CHANGED
@@ -65,7 +65,8 @@ var import_zod = require("zod");
65
65
  var closeEventSchema = import_zod.z.object({
66
66
  source: import_zod.z.literal("moonbase-checkout"),
67
67
  event: import_zod.z.literal("close"),
68
- intent: import_zod.z.string().optional()
68
+ intent: import_zod.z.string().optional(),
69
+ completed: import_zod.z.boolean()
69
70
  });
70
71
  var completedEventSchema = import_zod.z.object({
71
72
  source: import_zod.z.literal("moonbase-checkout"),
@@ -255,7 +256,7 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
255
256
  closeCheckout();
256
257
  }
257
258
  onCheckoutInitiated(callback) {
258
- window.addEventListener("moonbase-checkout-initiated", (e) => callback(e.detail.order));
259
+ window.addEventListener("moonbase-checkout-initiated", (e) => callback(e.detail.order, e.detail.total));
259
260
  }
260
261
  onCheckoutCompleted(callback) {
261
262
  window.addEventListener("moonbase-checkout-completed", (e) => callback(e.detail.order));
@@ -568,6 +569,28 @@ function useCart(context) {
568
569
  const storefront = context != null ? context : (0, import_vue6.inject)(storefrontKey);
569
570
  if (!storefront)
570
571
  throw new Error("No storefront configured");
572
+ const total = (0, import_vue6.computed)(() => {
573
+ const currency = storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency;
574
+ const total2 = storefront.currentOrder.value.items.reduce((agg, item) => {
575
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
576
+ let price;
577
+ if (item.type === "Product") {
578
+ const product = storefront.storefront.value.products.find((p) => p.id === item.productId);
579
+ const variations = (product == null ? void 0 : product.variations) || [];
580
+ const pickedVariation = variations.find((v) => v.id === item.variationId);
581
+ const eligiblePricingTiers = (_b = (_a = pickedVariation == null ? void 0 : pickedVariation.pricingTiers) == null ? void 0 : _a.filter((tier) => item.quantity >= tier.minQuantity)) != null ? _b : [];
582
+ const matchingPricingTier = eligiblePricingTiers.length > 0 ? eligiblePricingTiers[eligiblePricingTiers.length - 1] : null;
583
+ price = (_f = (_e = (_c = matchingPricingTier == null ? void 0 : matchingPricingTier.price[currency]) != null ? _c : pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _e : (_d = product == null ? void 0 : product.defaultVariation) == null ? void 0 : _d.price[currency]) != null ? _f : 0;
584
+ } else {
585
+ const bundle = storefront.storefront.value.bundles.find((b) => b.id === item.bundleId);
586
+ const variations = (bundle == null ? void 0 : bundle.variations) || [];
587
+ const pickedVariation = variations.find((v) => v.id === item.variationId);
588
+ price = (_i = (_h = pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _h : (_g = bundle == null ? void 0 : bundle.defaultVariation) == null ? void 0 : _g.price[currency]) != null ? _i : 0;
589
+ }
590
+ return agg + price * item.quantity;
591
+ }, 0);
592
+ return { amount: total2, currency };
593
+ });
571
594
  return {
572
595
  items: (0, import_vue6.computed)(() => storefront.currentOrder.value.items.map((item) => {
573
596
  var _a, _b;
@@ -596,28 +619,7 @@ function useCart(context) {
596
619
  }
597
620
  })),
598
621
  currency: (0, import_vue6.computed)(() => storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency),
599
- total: (0, import_vue6.computed)(() => {
600
- const currency = storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency;
601
- const total = storefront.currentOrder.value.items.reduce((agg, item) => {
602
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
603
- let price;
604
- if (item.type === "Product") {
605
- const product = storefront.storefront.value.products.find((p) => p.id === item.productId);
606
- const variations = (product == null ? void 0 : product.variations) || [];
607
- const pickedVariation = variations.find((v) => v.id === item.variationId);
608
- const eligiblePricingTiers = (_b = (_a = pickedVariation == null ? void 0 : pickedVariation.pricingTiers) == null ? void 0 : _a.filter((tier) => item.quantity >= tier.minQuantity)) != null ? _b : [];
609
- const matchingPricingTier = eligiblePricingTiers.length > 0 ? eligiblePricingTiers[eligiblePricingTiers.length - 1] : null;
610
- price = (_f = (_e = (_c = matchingPricingTier == null ? void 0 : matchingPricingTier.price[currency]) != null ? _c : pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _e : (_d = product == null ? void 0 : product.defaultVariation) == null ? void 0 : _d.price[currency]) != null ? _f : 0;
611
- } else {
612
- const bundle = storefront.storefront.value.bundles.find((b) => b.id === item.bundleId);
613
- const variations = (bundle == null ? void 0 : bundle.variations) || [];
614
- const pickedVariation = variations.find((v) => v.id === item.variationId);
615
- price = (_i = (_h = pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _h : (_g = bundle == null ? void 0 : bundle.defaultVariation) == null ? void 0 : _g.price[currency]) != null ? _i : 0;
616
- }
617
- return agg + price * item.quantity;
618
- }, 0);
619
- return { amount: total, currency };
620
- }),
622
+ total,
621
623
  addToCart: (item, variation) => {
622
624
  var _a, _b;
623
625
  item = (0, import_vue6.unref)(item);
@@ -689,19 +691,20 @@ function useCart(context) {
689
691
  returnUrl: absoluteReturnUrl
690
692
  }, storefront.hasUtm ? storefront.utm.value : void 0);
691
693
  window.dispatchEvent(new CustomEvent("moonbase-checkout-initiated", {
692
- detail: { order: updatedOrder }
694
+ detail: { order: updatedOrder, total: total.value }
693
695
  }));
694
696
  if (!options.redirect && updatedOrder.embeddedCheckoutUrl && typeof window !== "undefined") {
695
697
  const embeddedUrl = updatedOrder.embeddedCheckoutUrl;
696
698
  return new Promise((resolve) => {
697
699
  mountCheckout(embeddedUrl.replace("acme-co.beta.moonbase.sh", "localhost:3002"));
698
700
  window.addEventListener("moonbase-checkout-closed", (e) => {
699
- resolve({ next: e.detail.next });
700
- });
701
+ const evnt = e;
702
+ resolve({ next: evnt.detail.intent, completed: evnt.detail.completed });
703
+ }, { once: true });
701
704
  });
702
705
  } else if (options.redirect && updatedOrder.checkoutUrl && typeof window !== "undefined") {
703
706
  window.location.href = updatedOrder.checkoutUrl;
704
- return { next: void 0 };
707
+ return { next: void 0, completed: false };
705
708
  } else {
706
709
  throw new Error("No checkout URL found");
707
710
  }
@@ -763,19 +766,23 @@ async function useCheckout(items, options, context) {
763
766
  returnUrl: absoluteReturnUrl
764
767
  }, storefront.hasUtm ? storefront.utm.value : void 0);
765
768
  window.dispatchEvent(new CustomEvent("moonbase-checkout-initiated", {
766
- detail: { order: updatedOrder }
769
+ detail: { order: updatedOrder, total: { amount: items.reduce((a, item) => {
770
+ var _a2, _b2;
771
+ return a + ((_b2 = (_a2 = item.variation) == null ? void 0 : _a2.price[updatedOrder.currency]) != null ? _b2 : 0);
772
+ }, 0), currency: updatedOrder.currency } }
767
773
  }));
768
774
  if (!options.redirect && updatedOrder.embeddedCheckoutUrl && typeof window !== "undefined") {
769
775
  const embeddedUrl = updatedOrder.embeddedCheckoutUrl;
770
776
  return new Promise((resolve) => {
771
777
  mountCheckout(embeddedUrl.replace("acme-co.beta.moonbase.sh", "localhost:3002"));
772
778
  window.addEventListener("moonbase-checkout-closed", (e) => {
773
- resolve({ next: e.detail.next });
774
- });
779
+ const evnt = e;
780
+ resolve({ next: evnt.detail.intent, completed: evnt.detail.completed });
781
+ }, { once: true });
775
782
  });
776
783
  } else if (options.redirect && updatedOrder.checkoutUrl && typeof window !== "undefined") {
777
784
  window.location.href = updatedOrder.checkoutUrl;
778
- return Promise.resolve({ next: void 0 });
785
+ return Promise.resolve({ next: void 0, completed: false });
779
786
  } else {
780
787
  throw new Error("No checkout URL found");
781
788
  }
@@ -816,6 +823,33 @@ function useInventory(context) {
816
823
  if (!download.path)
817
824
  throw new Error("Product is not downloadable");
818
825
  window.location.href = await storefront.client.inventory.products.getDownloadUrl(download.path);
826
+ },
827
+ getSubscriptions: async (nextUrl) => {
828
+ return storefront.client.inventory.subscriptions.get(nextUrl);
829
+ },
830
+ getSubscription: async (subscriptionId) => {
831
+ return storefront.client.inventory.subscriptions.getById(subscriptionId);
832
+ },
833
+ cancelSubscription: async (subscriptionId) => {
834
+ return storefront.client.inventory.subscriptions.cancel(subscriptionId);
835
+ },
836
+ renewSubscription: async (subscriptionId, options) => {
837
+ var _a;
838
+ const fallbackPath = typeof window !== "undefined" ? window.location.pathname : "";
839
+ const absoluteReturnUrl = new URL((_a = options.returnUrl) != null ? _a : fallbackPath, document.baseURI).href;
840
+ const { location: url } = await storefront.client.inventory.subscriptions.renew(subscriptionId, absoluteReturnUrl, !options.redirect);
841
+ if (options.redirect) {
842
+ window.location.href = url;
843
+ return Promise.resolve({ next: void 0, completed: false });
844
+ } else {
845
+ return new Promise((resolve) => {
846
+ mountCheckout(url.replace("acme-co.beta.moonbase.sh", "localhost:3002"));
847
+ window.addEventListener("moonbase-checkout-closed", (e) => {
848
+ const evnt = e;
849
+ resolve({ next: evnt.detail.intent, completed: evnt.detail.completed });
850
+ }, { once: true });
851
+ });
852
+ }
819
853
  }
820
854
  };
821
855
  }