@moonbase.sh/vue 0.2.15 → 0.2.18

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
@@ -57,6 +57,63 @@ function debounce(func, waitMs = 100) {
57
57
  return debounced;
58
58
  }
59
59
 
60
+ // src/utils/iframe-checkout.ts
61
+ var import_zod = require("zod");
62
+ var closeEventSchema = import_zod.z.object({
63
+ source: import_zod.z.literal("moonbase-checkout"),
64
+ event: import_zod.z.literal("close"),
65
+ next: import_zod.z.string().optional()
66
+ });
67
+ var completedEventSchema = import_zod.z.object({
68
+ source: import_zod.z.literal("moonbase-checkout"),
69
+ event: import_zod.z.literal("completed")
70
+ });
71
+ var eventSchema = import_zod.z.discriminatedUnion("event", [closeEventSchema, completedEventSchema]);
72
+ function mountCheckout(endpoint) {
73
+ if (typeof window === "undefined") {
74
+ console.warn("Can not mount checkout server side");
75
+ return;
76
+ }
77
+ const iframe = document.createElement("iframe");
78
+ iframe.id = "moonbase-checkout";
79
+ iframe.src = endpoint;
80
+ iframe.style.position = "fixed";
81
+ iframe.style.top = "0px";
82
+ iframe.style.left = "0px";
83
+ iframe.style.right = "0px";
84
+ iframe.style.border = "none";
85
+ iframe.style.backgroundColor = "transparent";
86
+ iframe.style.zIndex = Number.MAX_SAFE_INTEGER.toString();
87
+ iframe.setAttribute("background", "transparent");
88
+ iframe.setAttribute("frameborder", "0");
89
+ iframe.setAttribute("allowtransparency", "true");
90
+ iframe.height = "100%";
91
+ iframe.width = "100%";
92
+ document.body.append(iframe);
93
+ window.onmessage = function(e) {
94
+ var _a;
95
+ try {
96
+ const evnt = eventSchema.parse(e.data);
97
+ if (evnt.event === "close") {
98
+ if (document.body.contains(iframe))
99
+ document.body.removeChild(iframe);
100
+ window.dispatchEvent(new CustomEvent("moonbase-checkout-closed", {
101
+ detail: evnt
102
+ }));
103
+ }
104
+ if (evnt.event === "completed") {
105
+ window.dispatchEvent(new CustomEvent("moonbase-checkout-completed", {
106
+ detail: evnt
107
+ }));
108
+ }
109
+ } catch (err) {
110
+ if (((_a = e.data) == null ? void 0 : _a.source) === "moonbase-checkout") {
111
+ console.error("Could not parse event:", e.data, err);
112
+ }
113
+ }
114
+ };
115
+ }
116
+
60
117
  // src/context.ts
61
118
  var _StorefrontContextImpl = class _StorefrontContextImpl {
62
119
  constructor(configuration, client, stateFactory) {
@@ -125,6 +182,21 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
125
182
  }
126
183
  const _2 = this.updateStorefront();
127
184
  const _3 = this.updateUser();
185
+ if (window.location) {
186
+ const urlParams2 = new URLSearchParams(window.location.search);
187
+ const intent = urlParams2.get("mb_intent");
188
+ const endpoint = urlParams2.get("mb_endpoint");
189
+ if (intent === "checkout" && urlParams2.get("mb_complete") && endpoint) {
190
+ const parameters = Object.fromEntries([...urlParams2.entries()].filter(([key]) => !key.startsWith("mb_")));
191
+ const finalEndpoint = new URL(decodeURIComponent(endpoint));
192
+ for (const prop of Object.keys(parameters)) {
193
+ finalEndpoint.searchParams.append(prop, parameters[prop]);
194
+ }
195
+ mountCheckout(finalEndpoint.toString());
196
+ window.history.replaceState(null, "", window.location.href.split("?")[0]);
197
+ }
198
+ }
199
+ window.addEventListener("moonbase-checkout-completed", () => this.resetOrder());
128
200
  }
129
201
  get loadedStorefrontPromise() {
130
202
  return new Promise((resolve) => {
@@ -142,6 +214,12 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
142
214
  }, { immediate: true });
143
215
  });
144
216
  }
217
+ onCheckoutCompleted(callback) {
218
+ window.addEventListener("moonbase-checkout-completed", () => callback());
219
+ }
220
+ onCheckoutClosed(callback) {
221
+ window.addEventListener("moonbase-checkout-closed", (e) => callback(e.detail.next));
222
+ }
145
223
  install(app) {
146
224
  app.provide(storefrontKey, this);
147
225
  console.log("Moonbase.sh storefront installed, learn more at https://moonbase.sh/docs");
@@ -529,11 +607,20 @@ function useCart(context) {
529
607
  const updatedOrder = await storefront.client.orders.pushContent(storefront.currentOrder.value, {
530
608
  returnUrl: absoluteReturnUrl
531
609
  });
532
- storefront.currentOrder.value = updatedOrder;
533
- if (updatedOrder.checkoutUrl && typeof window !== "undefined")
610
+ if (updatedOrder.embeddedCheckoutUrl && typeof window !== "undefined") {
611
+ const embeddedUrl = updatedOrder.embeddedCheckoutUrl;
612
+ return new Promise((resolve) => {
613
+ mountCheckout(embeddedUrl.replace("acme-co.beta.moonbase.sh", "localhost:3002"));
614
+ window.addEventListener("moonbase-checkout-closed", (e) => {
615
+ resolve({ next: e.detail.next });
616
+ });
617
+ });
618
+ } else if (updatedOrder.checkoutUrl && typeof window !== "undefined") {
534
619
  window.location.href = updatedOrder.checkoutUrl;
535
- else
620
+ return { next: void 0 };
621
+ } else {
536
622
  throw new Error("No checkout URL found");
623
+ }
537
624
  }
538
625
  };
539
626
  }
package/dist/index.d.cts CHANGED
@@ -19,6 +19,9 @@ interface StorefrontContext extends Pick<Plugin, keyof Plugin> {
19
19
  updateUser: () => Promise<void>;
20
20
  updateStorefront: () => Promise<void>;
21
21
  pushOrderContent: () => Promise<void>;
22
+ resetOrder: () => void;
23
+ onCheckoutCompleted: (callback: () => void) => void;
24
+ onCheckoutClosed: (callback: (next: string | undefined) => void) => void;
22
25
  /**
23
26
  * Called automatically by `app.use(storefront)`. Should not be called manually by
24
27
  * the user.
@@ -807,7 +810,9 @@ declare function useCart(context?: StorefrontContext): {
807
810
  addToCart: (item: StorefrontProduct | StorefrontBundle | Ref<StorefrontProduct> | Ref<StorefrontBundle>, variation?: PricingVariation) => void;
808
811
  setQuantity: (cartItem: CartItem, quantity: number) => void;
809
812
  removeFromCart: (cartItem: CartItem) => void;
810
- checkout: (returnUrl: string) => Promise<void>;
813
+ checkout: (returnUrl: string) => Promise<{
814
+ next: string | undefined;
815
+ } | undefined>;
811
816
  };
812
817
 
813
818
  declare function useAuth(context?: StorefrontContext): {
package/dist/index.d.ts CHANGED
@@ -19,6 +19,9 @@ interface StorefrontContext extends Pick<Plugin, keyof Plugin> {
19
19
  updateUser: () => Promise<void>;
20
20
  updateStorefront: () => Promise<void>;
21
21
  pushOrderContent: () => Promise<void>;
22
+ resetOrder: () => void;
23
+ onCheckoutCompleted: (callback: () => void) => void;
24
+ onCheckoutClosed: (callback: (next: string | undefined) => void) => void;
22
25
  /**
23
26
  * Called automatically by `app.use(storefront)`. Should not be called manually by
24
27
  * the user.
@@ -807,7 +810,9 @@ declare function useCart(context?: StorefrontContext): {
807
810
  addToCart: (item: StorefrontProduct | StorefrontBundle | Ref<StorefrontProduct> | Ref<StorefrontBundle>, variation?: PricingVariation) => void;
808
811
  setQuantity: (cartItem: CartItem, quantity: number) => void;
809
812
  removeFromCart: (cartItem: CartItem) => void;
810
- checkout: (returnUrl: string) => Promise<void>;
813
+ checkout: (returnUrl: string) => Promise<{
814
+ next: string | undefined;
815
+ } | undefined>;
811
816
  };
812
817
 
813
818
  declare function useAuth(context?: StorefrontContext): {
package/dist/index.js CHANGED
@@ -22,6 +22,63 @@ function debounce(func, waitMs = 100) {
22
22
  return debounced;
23
23
  }
24
24
 
25
+ // src/utils/iframe-checkout.ts
26
+ import { z } from "zod";
27
+ var closeEventSchema = z.object({
28
+ source: z.literal("moonbase-checkout"),
29
+ event: z.literal("close"),
30
+ next: z.string().optional()
31
+ });
32
+ var completedEventSchema = z.object({
33
+ source: z.literal("moonbase-checkout"),
34
+ event: z.literal("completed")
35
+ });
36
+ var eventSchema = z.discriminatedUnion("event", [closeEventSchema, completedEventSchema]);
37
+ function mountCheckout(endpoint) {
38
+ if (typeof window === "undefined") {
39
+ console.warn("Can not mount checkout server side");
40
+ return;
41
+ }
42
+ const iframe = document.createElement("iframe");
43
+ iframe.id = "moonbase-checkout";
44
+ iframe.src = endpoint;
45
+ iframe.style.position = "fixed";
46
+ iframe.style.top = "0px";
47
+ iframe.style.left = "0px";
48
+ iframe.style.right = "0px";
49
+ iframe.style.border = "none";
50
+ iframe.style.backgroundColor = "transparent";
51
+ iframe.style.zIndex = Number.MAX_SAFE_INTEGER.toString();
52
+ iframe.setAttribute("background", "transparent");
53
+ iframe.setAttribute("frameborder", "0");
54
+ iframe.setAttribute("allowtransparency", "true");
55
+ iframe.height = "100%";
56
+ iframe.width = "100%";
57
+ document.body.append(iframe);
58
+ window.onmessage = function(e) {
59
+ var _a;
60
+ try {
61
+ const evnt = eventSchema.parse(e.data);
62
+ if (evnt.event === "close") {
63
+ if (document.body.contains(iframe))
64
+ document.body.removeChild(iframe);
65
+ window.dispatchEvent(new CustomEvent("moonbase-checkout-closed", {
66
+ detail: evnt
67
+ }));
68
+ }
69
+ if (evnt.event === "completed") {
70
+ window.dispatchEvent(new CustomEvent("moonbase-checkout-completed", {
71
+ detail: evnt
72
+ }));
73
+ }
74
+ } catch (err) {
75
+ if (((_a = e.data) == null ? void 0 : _a.source) === "moonbase-checkout") {
76
+ console.error("Could not parse event:", e.data, err);
77
+ }
78
+ }
79
+ };
80
+ }
81
+
25
82
  // src/context.ts
26
83
  var _StorefrontContextImpl = class _StorefrontContextImpl {
27
84
  constructor(configuration, client, stateFactory) {
@@ -90,6 +147,21 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
90
147
  }
91
148
  const _2 = this.updateStorefront();
92
149
  const _3 = this.updateUser();
150
+ if (window.location) {
151
+ const urlParams2 = new URLSearchParams(window.location.search);
152
+ const intent = urlParams2.get("mb_intent");
153
+ const endpoint = urlParams2.get("mb_endpoint");
154
+ if (intent === "checkout" && urlParams2.get("mb_complete") && endpoint) {
155
+ const parameters = Object.fromEntries([...urlParams2.entries()].filter(([key]) => !key.startsWith("mb_")));
156
+ const finalEndpoint = new URL(decodeURIComponent(endpoint));
157
+ for (const prop of Object.keys(parameters)) {
158
+ finalEndpoint.searchParams.append(prop, parameters[prop]);
159
+ }
160
+ mountCheckout(finalEndpoint.toString());
161
+ window.history.replaceState(null, "", window.location.href.split("?")[0]);
162
+ }
163
+ }
164
+ window.addEventListener("moonbase-checkout-completed", () => this.resetOrder());
93
165
  }
94
166
  get loadedStorefrontPromise() {
95
167
  return new Promise((resolve) => {
@@ -107,6 +179,12 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
107
179
  }, { immediate: true });
108
180
  });
109
181
  }
182
+ onCheckoutCompleted(callback) {
183
+ window.addEventListener("moonbase-checkout-completed", () => callback());
184
+ }
185
+ onCheckoutClosed(callback) {
186
+ window.addEventListener("moonbase-checkout-closed", (e) => callback(e.detail.next));
187
+ }
110
188
  install(app) {
111
189
  app.provide(storefrontKey, this);
112
190
  console.log("Moonbase.sh storefront installed, learn more at https://moonbase.sh/docs");
@@ -494,11 +572,20 @@ function useCart(context) {
494
572
  const updatedOrder = await storefront.client.orders.pushContent(storefront.currentOrder.value, {
495
573
  returnUrl: absoluteReturnUrl
496
574
  });
497
- storefront.currentOrder.value = updatedOrder;
498
- if (updatedOrder.checkoutUrl && typeof window !== "undefined")
575
+ if (updatedOrder.embeddedCheckoutUrl && typeof window !== "undefined") {
576
+ const embeddedUrl = updatedOrder.embeddedCheckoutUrl;
577
+ return new Promise((resolve) => {
578
+ mountCheckout(embeddedUrl.replace("acme-co.beta.moonbase.sh", "localhost:3002"));
579
+ window.addEventListener("moonbase-checkout-closed", (e) => {
580
+ resolve({ next: e.detail.next });
581
+ });
582
+ });
583
+ } else if (updatedOrder.checkoutUrl && typeof window !== "undefined") {
499
584
  window.location.href = updatedOrder.checkoutUrl;
500
- else
585
+ return { next: void 0 };
586
+ } else {
501
587
  throw new Error("No checkout URL found");
588
+ }
502
589
  }
503
590
  };
504
591
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moonbase.sh/vue",
3
3
  "type": "module",
4
- "version": "0.2.15",
4
+ "version": "0.2.18",
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",
@@ -18,7 +18,8 @@
18
18
  "dependencies": {
19
19
  "@vue/devtools-api": "^6.5.1",
20
20
  "uuid": "^9.0.1",
21
- "@moonbase.sh/storefront-api": "0.2.15"
21
+ "zod": "^3.21.4",
22
+ "@moonbase.sh/storefront-api": "0.2.18"
22
23
  },
23
24
  "devDependencies": {
24
25
  "@types/uuid": "^9.0.7",