@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.js CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  NotAuthenticatedError,
8
8
  NotAuthorizedError,
9
9
  NotFoundError,
10
+ OfferUtils,
10
11
  OrderStatus,
11
12
  schemas as schemas2,
12
13
  TokenStore
@@ -125,7 +126,8 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
125
126
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
126
127
  suggestedCurrency: "",
127
128
  bundles: [],
128
- products: []
129
+ products: [],
130
+ offers: []
129
131
  });
130
132
  this.loadedStorefront = stateFactory(`${_StorefrontContextImpl.storefrontKey}_loaded`, false);
131
133
  this.utm = stateFactory(_StorefrontContextImpl.utmKey, void 0);
@@ -178,7 +180,8 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
178
180
  this.storefront = stateFactory(_StorefrontContextImpl.storefrontKey, {
179
181
  suggestedCurrency: "",
180
182
  bundles: [],
181
- products: []
183
+ products: [],
184
+ offers: []
182
185
  });
183
186
  this.loadedStorefront = stateFactory(`${_StorefrontContextImpl.storefrontKey}_loaded`, false);
184
187
  }
@@ -350,28 +353,35 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
350
353
  enrichLineItems(lineItems) {
351
354
  if (this.storefront) {
352
355
  return lineItems.map((item) => {
353
- var _a, _b;
356
+ var _a, _b, _c;
357
+ const offer = (_a = this.storefront.value.offers) == null ? void 0 : _a.find((o) => o.id === item.offerId);
354
358
  if (item.type === "Product") {
355
359
  const product = this.storefront.value.products.find((p) => p.id === item.productId);
356
360
  const variations = (product == null ? void 0 : product.variations) || [];
357
- const variation = variations.find((v) => v.id === item.variationId);
361
+ let variation = variations.find((v) => v.id === item.variationId);
362
+ if (offer && variation && OfferUtils.eligible(offer, this.currentOrder.value)) {
363
+ variation = OfferUtils.applyToVariation(offer, variation);
364
+ }
358
365
  return {
359
366
  ...item,
360
367
  product,
361
368
  variation,
362
369
  price: variation == null ? void 0 : variation.price,
363
- appliedDiscount: (_a = variation == null ? void 0 : variation.discount) != null ? _a : item.appliedDiscount
370
+ appliedDiscount: (_b = variation == null ? void 0 : variation.discount) != null ? _b : item.appliedDiscount
364
371
  };
365
372
  } else {
366
373
  const bundle = this.storefront.value.bundles.find((b) => b.id === item.bundleId);
367
374
  const variations = (bundle == null ? void 0 : bundle.variations) || [];
368
- const variation = variations.find((v) => v.id === item.variationId);
375
+ let variation = variations.find((v) => v.id === item.variationId);
376
+ if (offer && variation && OfferUtils.eligible(offer, this.currentOrder.value)) {
377
+ variation = OfferUtils.applyToVariation(offer, variation);
378
+ }
369
379
  return {
370
380
  ...item,
371
381
  bundle,
372
382
  variation,
373
383
  price: variation == null ? void 0 : variation.price,
374
- appliedDiscount: (_b = variation == null ? void 0 : variation.discount) != null ? _b : item.appliedDiscount
384
+ appliedDiscount: (_c = variation == null ? void 0 : variation.discount) != null ? _c : item.appliedDiscount
375
385
  };
376
386
  }
377
387
  });
@@ -572,7 +582,7 @@ function useBundles(context) {
572
582
  }
573
583
 
574
584
  // src/composables/useCart.ts
575
- import { MoonbaseError } from "@moonbase.sh/storefront-api";
585
+ import { DiscountUtils, MoonbaseError, OfferUtils as OfferUtils2 } from "@moonbase.sh/storefront-api";
576
586
  import { computed as computed4, inject as inject5, unref } from "vue";
577
587
  function useCart(context) {
578
588
  const storefront = context != null ? context : inject5(storefrontKey);
@@ -581,7 +591,7 @@ function useCart(context) {
581
591
  const total = computed4(() => {
582
592
  const currency = storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency;
583
593
  const total2 = storefront.currentOrder.value.items.reduce((agg, item) => {
584
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
594
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
585
595
  let price;
586
596
  if (item.type === "Product") {
587
597
  const product = storefront.storefront.value.products.find((p) => p.id === item.productId);
@@ -596,70 +606,113 @@ function useCart(context) {
596
606
  const pickedVariation = variations.find((v) => v.id === item.variationId);
597
607
  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;
598
608
  }
609
+ const offer = (_j = storefront.storefront.value.offers) == null ? void 0 : _j.find((o) => o.id === item.offerId);
610
+ if (offer && OfferUtils2.eligible(offer, storefront.currentOrder.value)) {
611
+ const offerDiscount = DiscountUtils.apply(offer.discount, { [currency]: price });
612
+ price -= offerDiscount[currency];
613
+ }
599
614
  return agg + price * item.quantity;
600
615
  }, 0);
601
616
  return { amount: total2, currency };
602
617
  });
603
- return {
604
- items: computed4(() => storefront.enrichLineItems(storefront.currentOrder.value.items)),
605
- currency: computed4(() => storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency),
606
- total,
607
- addToCart: (item, variation) => {
608
- var _a, _b;
609
- item = unref(item);
610
- variation != null ? variation : variation = item.defaultVariation;
611
- if (!variation)
612
- throw new Error("Added item does not have a default variation, and none have been specified");
613
- const id = `${item.type === "bundle" ? "b" : "p"}/${item.id}/${variation.id}`;
614
- let lineItem = storefront.currentOrder.value.items.find((i) => i.id === id);
615
- if (!lineItem) {
616
- if (item.type === "bundle") {
617
- lineItem = {
618
- id,
619
- type: "Bundle",
620
- bundleId: item.id,
621
- bundle: item,
622
- quantity: 1,
623
- variation,
624
- variationId: variation.id,
625
- price: variation.price,
626
- appliedDiscount: variation.discount,
627
- isDefaultVariation: variation.id === ((_a = item.defaultVariation) == null ? void 0 : _a.id)
628
- };
629
- storefront.currentOrder.value.items.push(lineItem);
630
- } else if (item.type === "product") {
631
- lineItem = {
618
+ function addToCart(item, variation, offer, options) {
619
+ var _a, _b, _c;
620
+ item = unref(item);
621
+ variation != null ? variation : variation = item.defaultVariation;
622
+ if (!variation)
623
+ throw new Error("Added item does not have a default variation, and none have been specified");
624
+ const id = `${item.type === "Bundle" ? "b" : "p"}/${item.id}/${variation.id}`;
625
+ let lineItem = storefront == null ? void 0 : storefront.currentOrder.value.items.find((i) => i.id === id);
626
+ if (!lineItem) {
627
+ if (item.type === "Bundle") {
628
+ lineItem = {
629
+ id,
630
+ type: "Bundle",
631
+ bundleId: item.id,
632
+ bundle: item,
633
+ quantity: 1,
634
+ variation,
635
+ variationId: variation.id,
636
+ offerId: offer == null ? void 0 : offer.id,
637
+ price: variation.price,
638
+ appliedDiscount: variation.discount,
639
+ isDefaultVariation: variation.id === ((_a = item.defaultVariation) == null ? void 0 : _a.id)
640
+ };
641
+ if ((options == null ? void 0 : options.bundleBehaviour) === "replace") {
642
+ 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));
643
+ for (const productLineItem of lineItemsToReplace != null ? lineItemsToReplace : []) {
644
+ const index = storefront == null ? void 0 : storefront.currentOrder.value.items.findIndex((i) => i.id === productLineItem.id);
645
+ if (index !== void 0)
646
+ storefront == null ? void 0 : storefront.currentOrder.value.items.splice(index, 1);
647
+ }
648
+ lineItem.replaced = lineItemsToReplace == null ? void 0 : lineItemsToReplace.filter((i) => i.type === "Product").map((i) => `${i.productId}/${i.variationId}`);
649
+ }
650
+ storefront == null ? void 0 : storefront.currentOrder.value.items.push(lineItem);
651
+ } else if (item.type === "Product") {
652
+ lineItem = {
653
+ id,
654
+ type: "Product",
655
+ productId: item.id,
656
+ product: item,
657
+ quantity: 1,
658
+ variation,
659
+ variationId: variation.id,
660
+ offerId: offer == null ? void 0 : offer.id,
661
+ price: variation.price,
662
+ appliedDiscount: variation.discount,
663
+ isDefaultVariation: variation.id === ((_b = item.defaultVariation) == null ? void 0 : _b.id)
664
+ };
665
+ storefront == null ? void 0 : storefront.currentOrder.value.items.push(lineItem);
666
+ } else {
667
+ throw new MoonbaseError("Invalid input", `Could not handle item of type '${item.type}'`);
668
+ }
669
+ } else {
670
+ lineItem.quantity += 1;
671
+ (_c = lineItem.offerId) != null ? _c : lineItem.offerId = offer == null ? void 0 : offer.id;
672
+ if (!lineItem.variation && variation)
673
+ lineItem.variation = variation;
674
+ if (!lineItem.price && variation)
675
+ lineItem.price = variation.price;
676
+ if (!lineItem.appliedDiscount && variation.discount)
677
+ lineItem.appliedDiscount = variation.discount;
678
+ if (lineItem.type === "Product" && item.type === "Product" && !lineItem.product)
679
+ lineItem.product = item;
680
+ if (lineItem.type === "Bundle" && item.type === "Bundle" && !lineItem.bundle)
681
+ lineItem.bundle = item;
682
+ }
683
+ const _ = storefront == null ? void 0 : storefront.pushOrderContent();
684
+ return lineItem;
685
+ }
686
+ function addReplacedBundleProducts(cartItem) {
687
+ var _a, _b;
688
+ if (cartItem.type === "Bundle" && cartItem.replaced && cartItem.replaced.length > 0) {
689
+ for (const { productId, variationId } of cartItem.replaced.map((composite) => ({ productId: composite.split("/")[0], variationId: composite.split("/")[1] }))) {
690
+ const product = storefront == null ? void 0 : storefront.storefront.value.products.find((p) => p.id === productId);
691
+ const variation = (_a = product == null ? void 0 : product.variations) == null ? void 0 : _a.find((v) => v.id === variationId);
692
+ if (product && variation && !(storefront == null ? void 0 : storefront.currentOrder.value.items.some((i) => i.type === "Product" && i.productId === productId))) {
693
+ const id = `p/${product.id}/${variationId}`;
694
+ storefront == null ? void 0 : storefront.currentOrder.value.items.push({
632
695
  id,
633
696
  type: "Product",
634
- productId: item.id,
635
- product: item,
697
+ productId: product.id,
698
+ product,
636
699
  quantity: 1,
637
700
  variation,
638
- variationId: variation.id,
701
+ variationId,
702
+ // offerId: offer?.id, // TODO: Should we handle this?
639
703
  price: variation.price,
640
704
  appliedDiscount: variation.discount,
641
- isDefaultVariation: variation.id === ((_b = item.defaultVariation) == null ? void 0 : _b.id)
642
- };
643
- storefront.currentOrder.value.items.push(lineItem);
644
- } else {
645
- throw new MoonbaseError("Invalid input", `Could not handle item of type '${item.type}'`);
705
+ isDefaultVariation: variation.id === ((_b = product.defaultVariation) == null ? void 0 : _b.id)
706
+ });
646
707
  }
647
- } else {
648
- lineItem.quantity += 1;
649
- if (!lineItem.variation && variation)
650
- lineItem.variation = variation;
651
- if (!lineItem.price && variation)
652
- lineItem.price = variation.price;
653
- if (!lineItem.appliedDiscount && variation.discount)
654
- lineItem.appliedDiscount = variation.discount;
655
- if (lineItem.type === "Product" && item.type === "product" && !lineItem.product)
656
- lineItem.product = item;
657
- if (lineItem.type === "Bundle" && item.type === "bundle" && !lineItem.bundle)
658
- lineItem.bundle = item;
659
708
  }
660
- const _ = storefront.pushOrderContent();
661
- return lineItem;
662
- },
709
+ }
710
+ }
711
+ return {
712
+ items: computed4(() => storefront.enrichLineItems(storefront.currentOrder.value.items)),
713
+ currency: computed4(() => storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency),
714
+ total,
715
+ addToCart,
663
716
  setQuantity: (cartItem, quantity) => {
664
717
  const cartItemRef = storefront.currentOrder.value.items.find((i) => i.id === cartItem.id);
665
718
  if (!cartItemRef)
@@ -668,6 +721,7 @@ function useCart(context) {
668
721
  if (cartItemRef.quantity <= 0) {
669
722
  const index = storefront.currentOrder.value.items.findIndex((i) => i.id === cartItemRef.id);
670
723
  storefront.currentOrder.value.items.splice(index, 1);
724
+ addReplacedBundleProducts(cartItem);
671
725
  }
672
726
  const _ = storefront.pushOrderContent();
673
727
  return cartItem;
@@ -675,6 +729,7 @@ function useCart(context) {
675
729
  removeFromCart: (cartItem) => {
676
730
  const index = storefront.currentOrder.value.items.findIndex((i) => i.id === cartItem.id);
677
731
  storefront.currentOrder.value.items.splice(index, 1);
732
+ addReplacedBundleProducts(cartItem);
678
733
  const _ = storefront.pushOrderContent();
679
734
  },
680
735
  checkout: async (options) => {
@@ -722,8 +777,8 @@ async function useCheckout(items, options, context) {
722
777
  const variation = (_a = item.variation) != null ? _a : target.defaultVariation;
723
778
  if (!variation)
724
779
  throw new Error("Added item does not have a default variation, and none have been specified");
725
- const id = `${target.type === "bundle" ? "b" : "p"}/${target.id}/${variation.id}`;
726
- if (target.type === "bundle") {
780
+ const id = `${target.type === "Bundle" ? "b" : "p"}/${target.id}/${variation.id}`;
781
+ if (target.type === "Bundle") {
727
782
  checkoutSession.push({
728
783
  id,
729
784
  type: "Bundle",
@@ -735,7 +790,7 @@ async function useCheckout(items, options, context) {
735
790
  price: variation.price,
736
791
  appliedDiscount: variation.discount
737
792
  });
738
- } else if (target.type === "product") {
793
+ } else if (target.type === "Product") {
739
794
  checkoutSession.push({
740
795
  id,
741
796
  type: "Product",
@@ -850,28 +905,70 @@ function useInventory(context) {
850
905
  };
851
906
  }
852
907
 
853
- // src/composables/useProduct.ts
908
+ // src/composables/useOffer.ts
854
909
  import { computed as computed5, inject as inject8 } from "vue";
855
- function useProduct(productId, context) {
910
+ function useOffer(offerId, context) {
856
911
  const storefront = context != null ? context : inject8(storefrontKey);
857
912
  if (!storefront)
858
913
  throw new Error("No storefront configured");
859
- return computed5(() => storefront.storefront.value.products.find((p) => p.id === productId) || null);
914
+ return computed5(() => {
915
+ var _a;
916
+ return ((_a = storefront.storefront.value.offers) == null ? void 0 : _a.find((o) => o.id === offerId)) || null;
917
+ });
860
918
  }
861
919
 
862
- // src/composables/useProducts.ts
920
+ // src/composables/useOffers.ts
921
+ import { OfferUtils as OfferUtils3 } from "@moonbase.sh/storefront-api";
863
922
  import { computed as computed6, inject as inject9 } from "vue";
864
- function useProducts(context) {
923
+ function useOffers(context) {
865
924
  const storefront = context != null ? context : inject9(storefrontKey);
866
925
  if (!storefront)
867
926
  throw new Error("No storefront configured");
868
- return computed6(() => storefront.storefront.value.products);
927
+ return computed6(() => {
928
+ var _a;
929
+ return (_a = storefront.storefront.value.offers) != null ? _a : [];
930
+ });
931
+ }
932
+ function useEligibleOffers(context) {
933
+ const storefront = context != null ? context : inject9(storefrontKey);
934
+ if (!storefront)
935
+ throw new Error("No storefront configured");
936
+ return computed6(() => {
937
+ var _a;
938
+ return ((_a = storefront.storefront.value.offers) != null ? _a : []).filter((o) => OfferUtils3.eligible(o, storefront.currentOrder.value) && !offerTargetIsInCart(o, storefront.currentOrder.value));
939
+ });
940
+ }
941
+ function offerTargetIsInCart(offer, order) {
942
+ if (order.items.some(
943
+ (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
944
+ )) {
945
+ return true;
946
+ }
947
+ return false;
948
+ }
949
+
950
+ // src/composables/useProduct.ts
951
+ import { computed as computed7, inject as inject10 } from "vue";
952
+ function useProduct(productId, context) {
953
+ const storefront = context != null ? context : inject10(storefrontKey);
954
+ if (!storefront)
955
+ throw new Error("No storefront configured");
956
+ return computed7(() => storefront.storefront.value.products.find((p) => p.id === productId) || null);
957
+ }
958
+
959
+ // src/composables/useProducts.ts
960
+ import { computed as computed8, inject as inject11 } from "vue";
961
+ function useProducts(context) {
962
+ const storefront = context != null ? context : inject11(storefrontKey);
963
+ if (!storefront)
964
+ throw new Error("No storefront configured");
965
+ return computed8(() => storefront.storefront.value.products);
869
966
  }
870
967
 
871
968
  // src/composables/useVendor.ts
872
- import { inject as inject10, ref as ref2 } from "vue";
969
+ import { inject as inject12, ref as ref2 } from "vue";
873
970
  function useVendor(context) {
874
- const storefront = context != null ? context : inject10(storefrontKey);
971
+ const storefront = context != null ? context : inject12(storefrontKey);
875
972
  if (!storefront)
876
973
  throw new Error("No storefront configured");
877
974
  const vendor = ref2(null);
@@ -888,9 +985,9 @@ function useVendor(context) {
888
985
 
889
986
  // src/composables/useVoucher.ts
890
987
  import { NotFoundError as NotFoundError2 } from "@moonbase.sh/storefront-api";
891
- import { inject as inject11 } from "vue";
988
+ import { inject as inject13 } from "vue";
892
989
  function useVoucher(context) {
893
- const storefront = context != null ? context : inject11(storefrontKey);
990
+ const storefront = context != null ? context : inject13(storefrontKey);
894
991
  if (!storefront)
895
992
  throw new Error("No storefront configured");
896
993
  return {
@@ -938,7 +1035,10 @@ export {
938
1035
  useBundles,
939
1036
  useCart,
940
1037
  useCheckout,
1038
+ useEligibleOffers,
941
1039
  useInventory,
1040
+ useOffer,
1041
+ useOffers,
942
1042
  useProduct,
943
1043
  useProducts,
944
1044
  useVendor,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moonbase.sh/vue",
3
3
  "type": "module",
4
- "version": "0.4.24",
4
+ "version": "0.4.26",
5
5
  "description": "Package to let you build vue.js storefronts with Moonbase.sh as payment and delivery provider",
6
6
  "author": "Tobias Lønnerød Madsen <m@dsen.tv>",
7
7
  "license": "MIT",
@@ -19,14 +19,14 @@
19
19
  "@vue/devtools-api": "^6.6.3",
20
20
  "uuid": "^9.0.1",
21
21
  "zod": "^3.23.8",
22
- "@moonbase.sh/storefront-api": "0.4.24"
22
+ "@moonbase.sh/storefront-api": "0.4.26"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/uuid": "^9.0.8",
26
26
  "rimraf": "^5.0.10",
27
- "tsup": "^7.2.0",
28
- "typescript": "~5.1.6",
29
- "vue": "^3.5.3"
27
+ "tsup": "^8.5.0",
28
+ "typescript": "~5.8.3",
29
+ "vue": "^3.5.17"
30
30
  },
31
31
  "scripts": {
32
32
  "build": "tsup src/index.ts --format esm,cjs --dts",