@moonbase.sh/vue 0.4.24 → 0.4.26

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
@@ -19,8 +19,8 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
20
 
21
21
  // src/index.ts
22
- var src_exports = {};
23
- __export(src_exports, {
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
24
  createStorefront: () => createStorefront,
25
25
  storefrontKey: () => storefrontKey,
26
26
  useActivationRequest: () => useActivationRequest,
@@ -29,15 +29,18 @@ __export(src_exports, {
29
29
  useBundles: () => useBundles,
30
30
  useCart: () => useCart,
31
31
  useCheckout: () => useCheckout,
32
+ useEligibleOffers: () => useEligibleOffers,
32
33
  useInventory: () => useInventory,
34
+ useOffer: () => useOffer,
35
+ useOffers: () => useOffers,
33
36
  useProduct: () => useProduct,
34
37
  useProducts: () => useProducts,
35
38
  useVendor: () => useVendor,
36
39
  useVoucher: () => useVoucher
37
40
  });
38
- module.exports = __toCommonJS(src_exports);
39
- var import_storefront_api7 = require("@moonbase.sh/storefront-api");
40
- var import_vue13 = require("vue");
41
+ module.exports = __toCommonJS(index_exports);
42
+ var import_storefront_api8 = require("@moonbase.sh/storefront-api");
43
+ var import_vue15 = require("vue");
41
44
 
42
45
  // src/context.ts
43
46
  var import_storefront_api2 = require("@moonbase.sh/storefront-api");
@@ -155,7 +158,8 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
155
158
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
156
159
  suggestedCurrency: "",
157
160
  bundles: [],
158
- products: []
161
+ products: [],
162
+ offers: []
159
163
  });
160
164
  this.loadedStorefront = stateFactory(`${_StorefrontContextImpl.storefrontKey}_loaded`, false);
161
165
  this.utm = stateFactory(_StorefrontContextImpl.utmKey, void 0);
@@ -208,7 +212,8 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
208
212
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
209
213
  suggestedCurrency: "",
210
214
  bundles: [],
211
- products: []
215
+ products: [],
216
+ offers: []
212
217
  });
213
218
  this.loadedStorefront = stateFactory(`${_StorefrontContextImpl.storefrontKey}_loaded`, false);
214
219
  }
@@ -380,28 +385,35 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
380
385
  enrichLineItems(lineItems) {
381
386
  if (this.storefront) {
382
387
  return lineItems.map((item) => {
383
- var _a, _b;
388
+ var _a, _b, _c;
389
+ const offer = (_a = this.storefront.value.offers) == null ? void 0 : _a.find((o) => o.id === item.offerId);
384
390
  if (item.type === "Product") {
385
391
  const product = this.storefront.value.products.find((p) => p.id === item.productId);
386
392
  const variations = (product == null ? void 0 : product.variations) || [];
387
- const variation = variations.find((v) => v.id === item.variationId);
393
+ let variation = variations.find((v) => v.id === item.variationId);
394
+ if (offer && variation && import_storefront_api2.OfferUtils.eligible(offer, this.currentOrder.value)) {
395
+ variation = import_storefront_api2.OfferUtils.applyToVariation(offer, variation);
396
+ }
388
397
  return {
389
398
  ...item,
390
399
  product,
391
400
  variation,
392
401
  price: variation == null ? void 0 : variation.price,
393
- appliedDiscount: (_a = variation == null ? void 0 : variation.discount) != null ? _a : item.appliedDiscount
402
+ appliedDiscount: (_b = variation == null ? void 0 : variation.discount) != null ? _b : item.appliedDiscount
394
403
  };
395
404
  } else {
396
405
  const bundle = this.storefront.value.bundles.find((b) => b.id === item.bundleId);
397
406
  const variations = (bundle == null ? void 0 : bundle.variations) || [];
398
- const variation = variations.find((v) => v.id === item.variationId);
407
+ let variation = variations.find((v) => v.id === item.variationId);
408
+ if (offer && variation && import_storefront_api2.OfferUtils.eligible(offer, this.currentOrder.value)) {
409
+ variation = import_storefront_api2.OfferUtils.applyToVariation(offer, variation);
410
+ }
399
411
  return {
400
412
  ...item,
401
413
  bundle,
402
414
  variation,
403
415
  price: variation == null ? void 0 : variation.price,
404
- appliedDiscount: (_b = variation == null ? void 0 : variation.discount) != null ? _b : item.appliedDiscount
416
+ appliedDiscount: (_c = variation == null ? void 0 : variation.discount) != null ? _c : item.appliedDiscount
405
417
  };
406
418
  }
407
419
  });
@@ -611,7 +623,7 @@ function useCart(context) {
611
623
  const total = (0, import_vue6.computed)(() => {
612
624
  const currency = storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency;
613
625
  const total2 = storefront.currentOrder.value.items.reduce((agg, item) => {
614
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
626
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
615
627
  let price;
616
628
  if (item.type === "Product") {
617
629
  const product = storefront.storefront.value.products.find((p) => p.id === item.productId);
@@ -626,70 +638,113 @@ function useCart(context) {
626
638
  const pickedVariation = variations.find((v) => v.id === item.variationId);
627
639
  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;
628
640
  }
641
+ const offer = (_j = storefront.storefront.value.offers) == null ? void 0 : _j.find((o) => o.id === item.offerId);
642
+ if (offer && import_storefront_api4.OfferUtils.eligible(offer, storefront.currentOrder.value)) {
643
+ const offerDiscount = import_storefront_api4.DiscountUtils.apply(offer.discount, { [currency]: price });
644
+ price -= offerDiscount[currency];
645
+ }
629
646
  return agg + price * item.quantity;
630
647
  }, 0);
631
648
  return { amount: total2, currency };
632
649
  });
633
- return {
634
- items: (0, import_vue6.computed)(() => storefront.enrichLineItems(storefront.currentOrder.value.items)),
635
- currency: (0, import_vue6.computed)(() => storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency),
636
- total,
637
- addToCart: (item, variation) => {
638
- var _a, _b;
639
- item = (0, import_vue6.unref)(item);
640
- variation != null ? variation : variation = item.defaultVariation;
641
- if (!variation)
642
- throw new Error("Added item does not have a default variation, and none have been specified");
643
- const id = `${item.type === "bundle" ? "b" : "p"}/${item.id}/${variation.id}`;
644
- let lineItem = storefront.currentOrder.value.items.find((i) => i.id === id);
645
- if (!lineItem) {
646
- if (item.type === "bundle") {
647
- lineItem = {
648
- id,
649
- type: "Bundle",
650
- bundleId: item.id,
651
- bundle: item,
652
- quantity: 1,
653
- variation,
654
- variationId: variation.id,
655
- price: variation.price,
656
- appliedDiscount: variation.discount,
657
- isDefaultVariation: variation.id === ((_a = item.defaultVariation) == null ? void 0 : _a.id)
658
- };
659
- storefront.currentOrder.value.items.push(lineItem);
660
- } else if (item.type === "product") {
661
- lineItem = {
650
+ function addToCart(item, variation, offer, options) {
651
+ var _a, _b, _c;
652
+ item = (0, import_vue6.unref)(item);
653
+ variation != null ? variation : variation = item.defaultVariation;
654
+ if (!variation)
655
+ throw new Error("Added item does not have a default variation, and none have been specified");
656
+ const id = `${item.type === "Bundle" ? "b" : "p"}/${item.id}/${variation.id}`;
657
+ let lineItem = storefront == null ? void 0 : storefront.currentOrder.value.items.find((i) => i.id === id);
658
+ if (!lineItem) {
659
+ if (item.type === "Bundle") {
660
+ lineItem = {
661
+ id,
662
+ type: "Bundle",
663
+ bundleId: item.id,
664
+ bundle: item,
665
+ quantity: 1,
666
+ variation,
667
+ variationId: variation.id,
668
+ offerId: offer == null ? void 0 : offer.id,
669
+ price: variation.price,
670
+ appliedDiscount: variation.discount,
671
+ isDefaultVariation: variation.id === ((_a = item.defaultVariation) == null ? void 0 : _a.id)
672
+ };
673
+ if ((options == null ? void 0 : options.bundleBehaviour) === "replace") {
674
+ const lineItemsToReplace = storefront == null ? void 0 : storefront.currentOrder.value.items.filter((i) => i.type === "Product" && item.products.some((p) => p.included && p.id === i.productId));
675
+ for (const productLineItem of lineItemsToReplace != null ? lineItemsToReplace : []) {
676
+ const index = storefront == null ? void 0 : storefront.currentOrder.value.items.findIndex((i) => i.id === productLineItem.id);
677
+ if (index !== void 0)
678
+ storefront == null ? void 0 : storefront.currentOrder.value.items.splice(index, 1);
679
+ }
680
+ lineItem.replaced = lineItemsToReplace == null ? void 0 : lineItemsToReplace.filter((i) => i.type === "Product").map((i) => `${i.productId}/${i.variationId}`);
681
+ }
682
+ storefront == null ? void 0 : storefront.currentOrder.value.items.push(lineItem);
683
+ } else if (item.type === "Product") {
684
+ lineItem = {
685
+ id,
686
+ type: "Product",
687
+ productId: item.id,
688
+ product: item,
689
+ quantity: 1,
690
+ variation,
691
+ variationId: variation.id,
692
+ offerId: offer == null ? void 0 : offer.id,
693
+ price: variation.price,
694
+ appliedDiscount: variation.discount,
695
+ isDefaultVariation: variation.id === ((_b = item.defaultVariation) == null ? void 0 : _b.id)
696
+ };
697
+ storefront == null ? void 0 : storefront.currentOrder.value.items.push(lineItem);
698
+ } else {
699
+ throw new import_storefront_api4.MoonbaseError("Invalid input", `Could not handle item of type '${item.type}'`);
700
+ }
701
+ } else {
702
+ lineItem.quantity += 1;
703
+ (_c = lineItem.offerId) != null ? _c : lineItem.offerId = offer == null ? void 0 : offer.id;
704
+ if (!lineItem.variation && variation)
705
+ lineItem.variation = variation;
706
+ if (!lineItem.price && variation)
707
+ lineItem.price = variation.price;
708
+ if (!lineItem.appliedDiscount && variation.discount)
709
+ lineItem.appliedDiscount = variation.discount;
710
+ if (lineItem.type === "Product" && item.type === "Product" && !lineItem.product)
711
+ lineItem.product = item;
712
+ if (lineItem.type === "Bundle" && item.type === "Bundle" && !lineItem.bundle)
713
+ lineItem.bundle = item;
714
+ }
715
+ const _ = storefront == null ? void 0 : storefront.pushOrderContent();
716
+ return lineItem;
717
+ }
718
+ function addReplacedBundleProducts(cartItem) {
719
+ var _a, _b;
720
+ if (cartItem.type === "Bundle" && cartItem.replaced && cartItem.replaced.length > 0) {
721
+ for (const { productId, variationId } of cartItem.replaced.map((composite) => ({ productId: composite.split("/")[0], variationId: composite.split("/")[1] }))) {
722
+ const product = storefront == null ? void 0 : storefront.storefront.value.products.find((p) => p.id === productId);
723
+ const variation = (_a = product == null ? void 0 : product.variations) == null ? void 0 : _a.find((v) => v.id === variationId);
724
+ if (product && variation && !(storefront == null ? void 0 : storefront.currentOrder.value.items.some((i) => i.type === "Product" && i.productId === productId))) {
725
+ const id = `p/${product.id}/${variationId}`;
726
+ storefront == null ? void 0 : storefront.currentOrder.value.items.push({
662
727
  id,
663
728
  type: "Product",
664
- productId: item.id,
665
- product: item,
729
+ productId: product.id,
730
+ product,
666
731
  quantity: 1,
667
732
  variation,
668
- variationId: variation.id,
733
+ variationId,
734
+ // offerId: offer?.id, // TODO: Should we handle this?
669
735
  price: variation.price,
670
736
  appliedDiscount: variation.discount,
671
- isDefaultVariation: variation.id === ((_b = item.defaultVariation) == null ? void 0 : _b.id)
672
- };
673
- storefront.currentOrder.value.items.push(lineItem);
674
- } else {
675
- throw new import_storefront_api4.MoonbaseError("Invalid input", `Could not handle item of type '${item.type}'`);
737
+ isDefaultVariation: variation.id === ((_b = product.defaultVariation) == null ? void 0 : _b.id)
738
+ });
676
739
  }
677
- } else {
678
- lineItem.quantity += 1;
679
- if (!lineItem.variation && variation)
680
- lineItem.variation = variation;
681
- if (!lineItem.price && variation)
682
- lineItem.price = variation.price;
683
- if (!lineItem.appliedDiscount && variation.discount)
684
- lineItem.appliedDiscount = variation.discount;
685
- if (lineItem.type === "Product" && item.type === "product" && !lineItem.product)
686
- lineItem.product = item;
687
- if (lineItem.type === "Bundle" && item.type === "bundle" && !lineItem.bundle)
688
- lineItem.bundle = item;
689
740
  }
690
- const _ = storefront.pushOrderContent();
691
- return lineItem;
692
- },
741
+ }
742
+ }
743
+ return {
744
+ items: (0, import_vue6.computed)(() => storefront.enrichLineItems(storefront.currentOrder.value.items)),
745
+ currency: (0, import_vue6.computed)(() => storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency),
746
+ total,
747
+ addToCart,
693
748
  setQuantity: (cartItem, quantity) => {
694
749
  const cartItemRef = storefront.currentOrder.value.items.find((i) => i.id === cartItem.id);
695
750
  if (!cartItemRef)
@@ -698,6 +753,7 @@ function useCart(context) {
698
753
  if (cartItemRef.quantity <= 0) {
699
754
  const index = storefront.currentOrder.value.items.findIndex((i) => i.id === cartItemRef.id);
700
755
  storefront.currentOrder.value.items.splice(index, 1);
756
+ addReplacedBundleProducts(cartItem);
701
757
  }
702
758
  const _ = storefront.pushOrderContent();
703
759
  return cartItem;
@@ -705,6 +761,7 @@ function useCart(context) {
705
761
  removeFromCart: (cartItem) => {
706
762
  const index = storefront.currentOrder.value.items.findIndex((i) => i.id === cartItem.id);
707
763
  storefront.currentOrder.value.items.splice(index, 1);
764
+ addReplacedBundleProducts(cartItem);
708
765
  const _ = storefront.pushOrderContent();
709
766
  },
710
767
  checkout: async (options) => {
@@ -752,8 +809,8 @@ async function useCheckout(items, options, context) {
752
809
  const variation = (_a = item.variation) != null ? _a : target.defaultVariation;
753
810
  if (!variation)
754
811
  throw new Error("Added item does not have a default variation, and none have been specified");
755
- const id = `${target.type === "bundle" ? "b" : "p"}/${target.id}/${variation.id}`;
756
- if (target.type === "bundle") {
812
+ const id = `${target.type === "Bundle" ? "b" : "p"}/${target.id}/${variation.id}`;
813
+ if (target.type === "Bundle") {
757
814
  checkoutSession.push({
758
815
  id,
759
816
  type: "Bundle",
@@ -765,7 +822,7 @@ async function useCheckout(items, options, context) {
765
822
  price: variation.price,
766
823
  appliedDiscount: variation.discount
767
824
  });
768
- } else if (target.type === "product") {
825
+ } else if (target.type === "Product") {
769
826
  checkoutSession.push({
770
827
  id,
771
828
  type: "Product",
@@ -880,32 +937,74 @@ function useInventory(context) {
880
937
  };
881
938
  }
882
939
 
883
- // src/composables/useProduct.ts
940
+ // src/composables/useOffer.ts
884
941
  var import_vue9 = require("vue");
885
- function useProduct(productId, context) {
942
+ function useOffer(offerId, context) {
886
943
  const storefront = context != null ? context : (0, import_vue9.inject)(storefrontKey);
887
944
  if (!storefront)
888
945
  throw new Error("No storefront configured");
889
- return (0, import_vue9.computed)(() => storefront.storefront.value.products.find((p) => p.id === productId) || null);
946
+ return (0, import_vue9.computed)(() => {
947
+ var _a;
948
+ return ((_a = storefront.storefront.value.offers) == null ? void 0 : _a.find((o) => o.id === offerId)) || null;
949
+ });
890
950
  }
891
951
 
892
- // src/composables/useProducts.ts
952
+ // src/composables/useOffers.ts
953
+ var import_storefront_api6 = require("@moonbase.sh/storefront-api");
893
954
  var import_vue10 = require("vue");
894
- function useProducts(context) {
955
+ function useOffers(context) {
956
+ const storefront = context != null ? context : (0, import_vue10.inject)(storefrontKey);
957
+ if (!storefront)
958
+ throw new Error("No storefront configured");
959
+ return (0, import_vue10.computed)(() => {
960
+ var _a;
961
+ return (_a = storefront.storefront.value.offers) != null ? _a : [];
962
+ });
963
+ }
964
+ function useEligibleOffers(context) {
895
965
  const storefront = context != null ? context : (0, import_vue10.inject)(storefrontKey);
896
966
  if (!storefront)
897
967
  throw new Error("No storefront configured");
898
- return (0, import_vue10.computed)(() => storefront.storefront.value.products);
968
+ return (0, import_vue10.computed)(() => {
969
+ var _a;
970
+ return ((_a = storefront.storefront.value.offers) != null ? _a : []).filter((o) => import_storefront_api6.OfferUtils.eligible(o, storefront.currentOrder.value) && !offerTargetIsInCart(o, storefront.currentOrder.value));
971
+ });
972
+ }
973
+ function offerTargetIsInCart(offer, order) {
974
+ if (order.items.some(
975
+ (i) => i.type === "Product" && offer.target.type === "Product" && i.productId === offer.target.id || i.type === "Bundle" && offer.target.type === "Bundle" && i.bundleId === offer.target.id
976
+ )) {
977
+ return true;
978
+ }
979
+ return false;
899
980
  }
900
981
 
901
- // src/composables/useVendor.ts
982
+ // src/composables/useProduct.ts
902
983
  var import_vue11 = require("vue");
903
- function useVendor(context) {
984
+ function useProduct(productId, context) {
904
985
  const storefront = context != null ? context : (0, import_vue11.inject)(storefrontKey);
905
986
  if (!storefront)
906
987
  throw new Error("No storefront configured");
907
- const vendor = (0, import_vue11.ref)(null);
908
- const loading = (0, import_vue11.ref)(true);
988
+ return (0, import_vue11.computed)(() => storefront.storefront.value.products.find((p) => p.id === productId) || null);
989
+ }
990
+
991
+ // src/composables/useProducts.ts
992
+ var import_vue12 = require("vue");
993
+ function useProducts(context) {
994
+ const storefront = context != null ? context : (0, import_vue12.inject)(storefrontKey);
995
+ if (!storefront)
996
+ throw new Error("No storefront configured");
997
+ return (0, import_vue12.computed)(() => storefront.storefront.value.products);
998
+ }
999
+
1000
+ // src/composables/useVendor.ts
1001
+ var import_vue13 = require("vue");
1002
+ function useVendor(context) {
1003
+ const storefront = context != null ? context : (0, import_vue13.inject)(storefrontKey);
1004
+ if (!storefront)
1005
+ throw new Error("No storefront configured");
1006
+ const vendor = (0, import_vue13.ref)(null);
1007
+ const loading = (0, import_vue13.ref)(true);
909
1008
  storefront.client.vendor.get().then((v) => {
910
1009
  vendor.value = v;
911
1010
  loading.value = false;
@@ -917,10 +1016,10 @@ function useVendor(context) {
917
1016
  }
918
1017
 
919
1018
  // src/composables/useVoucher.ts
920
- var import_storefront_api6 = require("@moonbase.sh/storefront-api");
921
- var import_vue12 = require("vue");
1019
+ var import_storefront_api7 = require("@moonbase.sh/storefront-api");
1020
+ var import_vue14 = require("vue");
922
1021
  function useVoucher(context) {
923
- const storefront = context != null ? context : (0, import_vue12.inject)(storefrontKey);
1022
+ const storefront = context != null ? context : (0, import_vue14.inject)(storefrontKey);
924
1023
  if (!storefront)
925
1024
  throw new Error("No storefront configured");
926
1025
  return {
@@ -928,7 +1027,7 @@ function useVoucher(context) {
928
1027
  try {
929
1028
  return await storefront.client.vouchers.peek(code);
930
1029
  } catch (e) {
931
- if (e instanceof import_storefront_api6.NotFoundError)
1030
+ if (e instanceof import_storefront_api7.NotFoundError)
932
1031
  throw new Error("Voucher code invalid");
933
1032
  throw e;
934
1033
  }
@@ -937,7 +1036,7 @@ function useVoucher(context) {
937
1036
  try {
938
1037
  return await storefront.client.vouchers.redeem(code);
939
1038
  } catch (e) {
940
- if (e instanceof import_storefront_api6.NotFoundError)
1039
+ if (e instanceof import_storefront_api7.NotFoundError)
941
1040
  throw new Error("Voucher code invalid");
942
1041
  throw e;
943
1042
  }
@@ -946,7 +1045,7 @@ function useVoucher(context) {
946
1045
  }
947
1046
 
948
1047
  // src/index.ts
949
- __reExport(src_exports, require("@moonbase.sh/storefront-api"), module.exports);
1048
+ __reExport(index_exports, require("@moonbase.sh/storefront-api"), module.exports);
950
1049
  function createStorefront(endpoint, stateFactory, config) {
951
1050
  const configuration = {
952
1051
  ...config != null ? config : {},
@@ -954,9 +1053,9 @@ function createStorefront(endpoint, stateFactory, config) {
954
1053
  };
955
1054
  return new StorefrontContextImpl(
956
1055
  configuration,
957
- new import_storefront_api7.MoonbaseClient(configuration),
1056
+ new import_storefront_api8.MoonbaseClient(configuration),
958
1057
  // Default to vue refs but allow stuff like Nuxt useState wrappers
959
- stateFactory || ((_, state) => (0, import_vue13.ref)(state))
1058
+ stateFactory || ((_, state) => (0, import_vue15.ref)(state))
960
1059
  );
961
1060
  }
962
1061
  // Annotate the CommonJS export names for ESM import in node:
@@ -969,7 +1068,10 @@ function createStorefront(endpoint, stateFactory, config) {
969
1068
  useBundles,
970
1069
  useCart,
971
1070
  useCheckout,
1071
+ useEligibleOffers,
972
1072
  useInventory,
1073
+ useOffer,
1074
+ useOffers,
973
1075
  useProduct,
974
1076
  useProducts,
975
1077
  useVendor,