@moonbase.sh/vue 0.1.31 → 0.1.34

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
@@ -22,7 +22,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  var src_exports = {};
23
23
  __export(src_exports, {
24
24
  createStorefront: () => createStorefront,
25
- findBestPrice: () => findBestPrice,
26
25
  storefrontKey: () => storefrontKey,
27
26
  useAuth: () => useAuth,
28
27
  useBundle: () => useBundle,
@@ -32,7 +31,7 @@ __export(src_exports, {
32
31
  useProducts: () => useProducts
33
32
  });
34
33
  module.exports = __toCommonJS(src_exports);
35
- var import_api_client3 = require("@moonbase.sh/api-client");
34
+ var import_api_client2 = require("@moonbase.sh/api-client");
36
35
 
37
36
  // src/context.ts
38
37
  var import_api_client = require("@moonbase.sh/api-client");
@@ -66,6 +65,7 @@ var StorefrontContextImpl = class {
66
65
  const cachedOrderJson = localStorage.getItem("moonbase_session");
67
66
  if (cachedOrderJson) {
68
67
  this.currentOrder = (0, import_vue.ref)(JSON.parse(cachedOrderJson));
68
+ const _1 = this.refreshOrder();
69
69
  } else {
70
70
  this.currentOrder = (0, import_vue.ref)({
71
71
  id: (0, import_uuid.v4)(),
@@ -93,7 +93,7 @@ var StorefrontContextImpl = class {
93
93
  }
94
94
  install(app) {
95
95
  app.provide(storefrontKey, this);
96
- console.log("Storefront installed");
96
+ console.log("Moonbase.sh storefront installed");
97
97
  }
98
98
  async updateUser() {
99
99
  try {
@@ -131,6 +131,33 @@ var StorefrontContextImpl = class {
131
131
  break;
132
132
  }
133
133
  }
134
+ async refreshOrder() {
135
+ try {
136
+ const latestOrder = await this.client.orders.get(this.currentOrder.value.id);
137
+ if (latestOrder) {
138
+ if (latestOrder.status !== import_api_client.OrderStatus.Open) {
139
+ this.resetOrder();
140
+ } else {
141
+ localStorage.setItem("moonbase_session", JSON.stringify(latestOrder));
142
+ this.currentOrder.value = latestOrder;
143
+ }
144
+ }
145
+ } catch (err) {
146
+ if (err != null)
147
+ this.resetOrder();
148
+ }
149
+ }
150
+ resetOrder() {
151
+ var _a;
152
+ this.currentOrder.value = {
153
+ id: (0, import_uuid.v4)(),
154
+ currency: ((_a = this.storefront.value) == null ? void 0 : _a.suggestedCurrency) || "",
155
+ status: import_api_client.OrderStatus.Open,
156
+ items: [],
157
+ couponsApplied: []
158
+ };
159
+ localStorage.setItem("moonbase_session", JSON.stringify(this.currentOrder.value));
160
+ }
134
161
  };
135
162
 
136
163
  // src/index.ts
@@ -173,17 +200,7 @@ function useProducts() {
173
200
  }
174
201
 
175
202
  // src/composables/useCart.ts
176
- var import_api_client2 = require("@moonbase.sh/api-client");
177
203
  var import_vue6 = require("vue");
178
-
179
- // src/utils/findBestPrice.ts
180
- function findBestPrice(variations, currency, model) {
181
- const prices = variations.filter((v) => v.model === model && v.prices[currency] !== void 0).map((v) => v.prices[currency]);
182
- prices.sort((a, b) => b - a);
183
- return prices.length === 0 ? 0 : prices[0];
184
- }
185
-
186
- // src/composables/useCart.ts
187
204
  function useCart() {
188
205
  const storefront = (0, import_vue6.inject)(storefrontKey);
189
206
  if (!storefront)
@@ -194,37 +211,45 @@ function useCart() {
194
211
  total: (0, import_vue6.computed)(() => {
195
212
  const currency = storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency;
196
213
  const total = storefront.currentOrder.value.items.reduce((agg, item) => {
197
- var _a, _b;
198
- let bestPrice;
214
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
215
+ let price;
199
216
  if (item.type === "Product") {
200
- const variations = ((_a = useProduct(item.productId).value) == null ? void 0 : _a.pricing) || [];
201
- bestPrice = findBestPrice(variations, currency, item.pricingModel);
217
+ const product = useProduct(item.productId);
218
+ const variations = ((_a = product.value) == null ? void 0 : _a.variations) || [];
219
+ const pickedVariation = variations.find((v) => v.id === item.variationId);
220
+ price = (_e = (_d = pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _d : (_c = (_b = product.value) == null ? void 0 : _b.defaultVariation) == null ? void 0 : _c.price[currency]) != null ? _e : 0;
202
221
  } else {
203
- const variations = ((_b = useBundle(item.bundleId).value) == null ? void 0 : _b.pricing) || [];
204
- bestPrice = findBestPrice(variations, currency, import_api_client2.PricingModel.Purchase);
222
+ const bundle = useBundle(item.bundleId);
223
+ const variations = ((_f = bundle.value) == null ? void 0 : _f.variations) || [];
224
+ const pickedVariation = variations.find((v) => v.id === item.variationId);
225
+ price = (_j = (_i = pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _i : (_h = (_g = bundle.value) == null ? void 0 : _g.defaultVariation) == null ? void 0 : _h.price[currency]) != null ? _j : 0;
205
226
  }
206
- return agg + bestPrice * item.quantity;
227
+ return agg + price * item.quantity;
207
228
  }, 0);
208
229
  return { amount: total, currency };
209
230
  }),
210
- addToCart: (item, model = import_api_client2.PricingModel.Purchase) => {
211
- let lineItem = storefront.currentOrder.value.items.find((i) => i.type === "Product" && i.productId === item.id && i.pricingModel === model || i.type === "Bundle" && i.bundleId === item.id);
231
+ addToCart: (item, variation) => {
232
+ variation != null ? variation : variation = item.defaultVariation;
233
+ if (!variation)
234
+ throw new Error("Added item does not have a default variation, and none have been specified");
235
+ let lineItem = storefront.currentOrder.value.items.find((i) => i.type === "Product" && i.productId === item.id && i.variationId === variation.id || i.type === "Bundle" && i.bundleId === item.id && i.variationId === variation.id);
212
236
  if (!lineItem) {
213
237
  if (item.type === "bundle") {
214
238
  lineItem = {
215
239
  type: "Bundle",
216
240
  bundleId: item.id,
217
241
  bundle: item,
218
- quantity: 1
242
+ quantity: 1,
243
+ variationId: variation.id
219
244
  };
220
245
  storefront.currentOrder.value.items.push(lineItem);
221
246
  } else if (item.type === "product") {
222
247
  lineItem = {
223
248
  type: "Product",
224
- pricingModel: model,
225
249
  productId: item.id,
226
250
  product: item,
227
- quantity: 1
251
+ quantity: 1,
252
+ variationId: variation.id
228
253
  };
229
254
  storefront.currentOrder.value.items.push(lineItem);
230
255
  }
@@ -267,6 +292,18 @@ function useAuth() {
267
292
  const _ = storefront.updateStorefront();
268
293
  return user;
269
294
  },
295
+ signUp: async (name, email, password, acceptedPrivacyPolicy, acceptedTermsAndConditions) => {
296
+ const user = await storefront.client.identity.signUp(
297
+ name,
298
+ email,
299
+ password,
300
+ acceptedPrivacyPolicy,
301
+ acceptedTermsAndConditions
302
+ );
303
+ storefront.currentUser.value = user;
304
+ const _ = storefront.updateStorefront();
305
+ return user;
306
+ },
270
307
  signOut: () => {
271
308
  storefront.client.tokenStore.setUser(null);
272
309
  storefront.currentUser.value = null;
@@ -296,12 +333,11 @@ function createStorefront(endpoint) {
296
333
  const configuration = {
297
334
  endpoint
298
335
  };
299
- return new StorefrontContextImpl(configuration, new import_api_client3.MoonbaseClient(configuration));
336
+ return new StorefrontContextImpl(configuration, new import_api_client2.MoonbaseClient(configuration));
300
337
  }
301
338
  // Annotate the CommonJS export names for ESM import in node:
302
339
  0 && (module.exports = {
303
340
  createStorefront,
304
- findBestPrice,
305
341
  storefrontKey,
306
342
  useAuth,
307
343
  useBundle,
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { Storefront, Order, User, MoonbaseClient, StorefrontBundle, StorefrontProduct, PricingModel, PricingVariation, OpenLineItem } from '@moonbase.sh/api-client';
1
+ import { Storefront, Order, User, MoonbaseClient, StorefrontBundle, StorefrontProduct, PricingVariation, OpenLineItem } from '@moonbase.sh/api-client';
2
2
  export * from '@moonbase.sh/api-client';
3
3
  import * as vue from 'vue';
4
4
  import { Ref, App, InjectionKey } from 'vue';
@@ -37,7 +37,7 @@ declare function useCart(): {
37
37
  amount: number;
38
38
  currency: string;
39
39
  }>;
40
- addToCart: (item: StorefrontProduct | StorefrontBundle, model?: PricingModel) => void;
40
+ addToCart: (item: StorefrontProduct | StorefrontBundle, variation?: PricingVariation) => void;
41
41
  removeFromCart: (cartItem: CartItem) => void;
42
42
  checkout: (returnUrl: string) => Promise<void>;
43
43
  };
@@ -56,6 +56,12 @@ declare function useAuth(): {
56
56
  name: string;
57
57
  tenantId: string;
58
58
  }>;
59
+ signUp: (name: string, email: string, password: string, acceptedPrivacyPolicy: boolean, acceptedTermsAndConditions: boolean) => Promise<{
60
+ id: string;
61
+ email: string;
62
+ name: string;
63
+ tenantId: string;
64
+ }>;
59
65
  signOut: () => void;
60
66
  update: (name: string, email: string, emailConfirmationToken?: string) => Promise<{
61
67
  needsEmailConfirmationToken: boolean;
@@ -67,12 +73,10 @@ declare function useAuth(): {
67
73
 
68
74
  declare const storefrontKey: InjectionKey<StorefrontContext>;
69
75
 
70
- declare function findBestPrice(variations: PricingVariation[], currency: string, model: PricingModel): number;
71
-
72
76
  interface Cart {
73
77
  items: CartItem[];
74
78
  }
75
79
  type CartItem = OpenLineItem & {};
76
80
  declare function createStorefront(endpoint: string): StorefrontContext;
77
81
 
78
- export { Cart, CartItem, createStorefront, findBestPrice, storefrontKey, useAuth, useBundle, useBundles, useCart, useProduct, useProducts };
82
+ export { Cart, CartItem, createStorefront, storefrontKey, useAuth, useBundle, useBundles, useCart, useProduct, useProducts };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Storefront, Order, User, MoonbaseClient, StorefrontBundle, StorefrontProduct, PricingModel, PricingVariation, OpenLineItem } from '@moonbase.sh/api-client';
1
+ import { Storefront, Order, User, MoonbaseClient, StorefrontBundle, StorefrontProduct, PricingVariation, OpenLineItem } from '@moonbase.sh/api-client';
2
2
  export * from '@moonbase.sh/api-client';
3
3
  import * as vue from 'vue';
4
4
  import { Ref, App, InjectionKey } from 'vue';
@@ -37,7 +37,7 @@ declare function useCart(): {
37
37
  amount: number;
38
38
  currency: string;
39
39
  }>;
40
- addToCart: (item: StorefrontProduct | StorefrontBundle, model?: PricingModel) => void;
40
+ addToCart: (item: StorefrontProduct | StorefrontBundle, variation?: PricingVariation) => void;
41
41
  removeFromCart: (cartItem: CartItem) => void;
42
42
  checkout: (returnUrl: string) => Promise<void>;
43
43
  };
@@ -56,6 +56,12 @@ declare function useAuth(): {
56
56
  name: string;
57
57
  tenantId: string;
58
58
  }>;
59
+ signUp: (name: string, email: string, password: string, acceptedPrivacyPolicy: boolean, acceptedTermsAndConditions: boolean) => Promise<{
60
+ id: string;
61
+ email: string;
62
+ name: string;
63
+ tenantId: string;
64
+ }>;
59
65
  signOut: () => void;
60
66
  update: (name: string, email: string, emailConfirmationToken?: string) => Promise<{
61
67
  needsEmailConfirmationToken: boolean;
@@ -67,12 +73,10 @@ declare function useAuth(): {
67
73
 
68
74
  declare const storefrontKey: InjectionKey<StorefrontContext>;
69
75
 
70
- declare function findBestPrice(variations: PricingVariation[], currency: string, model: PricingModel): number;
71
-
72
76
  interface Cart {
73
77
  items: CartItem[];
74
78
  }
75
79
  type CartItem = OpenLineItem & {};
76
80
  declare function createStorefront(endpoint: string): StorefrontContext;
77
81
 
78
- export { Cart, CartItem, createStorefront, findBestPrice, storefrontKey, useAuth, useBundle, useBundles, useCart, useProduct, useProducts };
82
+ export { Cart, CartItem, createStorefront, storefrontKey, useAuth, useBundle, useBundles, useCart, useProduct, useProducts };
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ var StorefrontContextImpl = class {
33
33
  const cachedOrderJson = localStorage.getItem("moonbase_session");
34
34
  if (cachedOrderJson) {
35
35
  this.currentOrder = ref(JSON.parse(cachedOrderJson));
36
+ const _1 = this.refreshOrder();
36
37
  } else {
37
38
  this.currentOrder = ref({
38
39
  id: uuidv4(),
@@ -60,7 +61,7 @@ var StorefrontContextImpl = class {
60
61
  }
61
62
  install(app) {
62
63
  app.provide(storefrontKey, this);
63
- console.log("Storefront installed");
64
+ console.log("Moonbase.sh storefront installed");
64
65
  }
65
66
  async updateUser() {
66
67
  try {
@@ -98,6 +99,33 @@ var StorefrontContextImpl = class {
98
99
  break;
99
100
  }
100
101
  }
102
+ async refreshOrder() {
103
+ try {
104
+ const latestOrder = await this.client.orders.get(this.currentOrder.value.id);
105
+ if (latestOrder) {
106
+ if (latestOrder.status !== OrderStatus.Open) {
107
+ this.resetOrder();
108
+ } else {
109
+ localStorage.setItem("moonbase_session", JSON.stringify(latestOrder));
110
+ this.currentOrder.value = latestOrder;
111
+ }
112
+ }
113
+ } catch (err) {
114
+ if (err != null)
115
+ this.resetOrder();
116
+ }
117
+ }
118
+ resetOrder() {
119
+ var _a;
120
+ this.currentOrder.value = {
121
+ id: uuidv4(),
122
+ currency: ((_a = this.storefront.value) == null ? void 0 : _a.suggestedCurrency) || "",
123
+ status: OrderStatus.Open,
124
+ items: [],
125
+ couponsApplied: []
126
+ };
127
+ localStorage.setItem("moonbase_session", JSON.stringify(this.currentOrder.value));
128
+ }
101
129
  };
102
130
 
103
131
  // src/index.ts
@@ -140,17 +168,7 @@ function useProducts() {
140
168
  }
141
169
 
142
170
  // src/composables/useCart.ts
143
- import { PricingModel } from "@moonbase.sh/api-client";
144
171
  import { computed as computed5, inject as inject5 } from "vue";
145
-
146
- // src/utils/findBestPrice.ts
147
- function findBestPrice(variations, currency, model) {
148
- const prices = variations.filter((v) => v.model === model && v.prices[currency] !== void 0).map((v) => v.prices[currency]);
149
- prices.sort((a, b) => b - a);
150
- return prices.length === 0 ? 0 : prices[0];
151
- }
152
-
153
- // src/composables/useCart.ts
154
172
  function useCart() {
155
173
  const storefront = inject5(storefrontKey);
156
174
  if (!storefront)
@@ -161,37 +179,45 @@ function useCart() {
161
179
  total: computed5(() => {
162
180
  const currency = storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency;
163
181
  const total = storefront.currentOrder.value.items.reduce((agg, item) => {
164
- var _a, _b;
165
- let bestPrice;
182
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
183
+ let price;
166
184
  if (item.type === "Product") {
167
- const variations = ((_a = useProduct(item.productId).value) == null ? void 0 : _a.pricing) || [];
168
- bestPrice = findBestPrice(variations, currency, item.pricingModel);
185
+ const product = useProduct(item.productId);
186
+ const variations = ((_a = product.value) == null ? void 0 : _a.variations) || [];
187
+ const pickedVariation = variations.find((v) => v.id === item.variationId);
188
+ price = (_e = (_d = pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _d : (_c = (_b = product.value) == null ? void 0 : _b.defaultVariation) == null ? void 0 : _c.price[currency]) != null ? _e : 0;
169
189
  } else {
170
- const variations = ((_b = useBundle(item.bundleId).value) == null ? void 0 : _b.pricing) || [];
171
- bestPrice = findBestPrice(variations, currency, PricingModel.Purchase);
190
+ const bundle = useBundle(item.bundleId);
191
+ const variations = ((_f = bundle.value) == null ? void 0 : _f.variations) || [];
192
+ const pickedVariation = variations.find((v) => v.id === item.variationId);
193
+ price = (_j = (_i = pickedVariation == null ? void 0 : pickedVariation.price[currency]) != null ? _i : (_h = (_g = bundle.value) == null ? void 0 : _g.defaultVariation) == null ? void 0 : _h.price[currency]) != null ? _j : 0;
172
194
  }
173
- return agg + bestPrice * item.quantity;
195
+ return agg + price * item.quantity;
174
196
  }, 0);
175
197
  return { amount: total, currency };
176
198
  }),
177
- addToCart: (item, model = PricingModel.Purchase) => {
178
- let lineItem = storefront.currentOrder.value.items.find((i) => i.type === "Product" && i.productId === item.id && i.pricingModel === model || i.type === "Bundle" && i.bundleId === item.id);
199
+ addToCart: (item, variation) => {
200
+ variation != null ? variation : variation = item.defaultVariation;
201
+ if (!variation)
202
+ throw new Error("Added item does not have a default variation, and none have been specified");
203
+ let lineItem = storefront.currentOrder.value.items.find((i) => i.type === "Product" && i.productId === item.id && i.variationId === variation.id || i.type === "Bundle" && i.bundleId === item.id && i.variationId === variation.id);
179
204
  if (!lineItem) {
180
205
  if (item.type === "bundle") {
181
206
  lineItem = {
182
207
  type: "Bundle",
183
208
  bundleId: item.id,
184
209
  bundle: item,
185
- quantity: 1
210
+ quantity: 1,
211
+ variationId: variation.id
186
212
  };
187
213
  storefront.currentOrder.value.items.push(lineItem);
188
214
  } else if (item.type === "product") {
189
215
  lineItem = {
190
216
  type: "Product",
191
- pricingModel: model,
192
217
  productId: item.id,
193
218
  product: item,
194
- quantity: 1
219
+ quantity: 1,
220
+ variationId: variation.id
195
221
  };
196
222
  storefront.currentOrder.value.items.push(lineItem);
197
223
  }
@@ -234,6 +260,18 @@ function useAuth() {
234
260
  const _ = storefront.updateStorefront();
235
261
  return user;
236
262
  },
263
+ signUp: async (name, email, password, acceptedPrivacyPolicy, acceptedTermsAndConditions) => {
264
+ const user = await storefront.client.identity.signUp(
265
+ name,
266
+ email,
267
+ password,
268
+ acceptedPrivacyPolicy,
269
+ acceptedTermsAndConditions
270
+ );
271
+ storefront.currentUser.value = user;
272
+ const _ = storefront.updateStorefront();
273
+ return user;
274
+ },
237
275
  signOut: () => {
238
276
  storefront.client.tokenStore.setUser(null);
239
277
  storefront.currentUser.value = null;
@@ -267,7 +305,6 @@ function createStorefront(endpoint) {
267
305
  }
268
306
  export {
269
307
  createStorefront,
270
- findBestPrice,
271
308
  storefrontKey,
272
309
  useAuth,
273
310
  useBundle,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moonbase.sh/vue",
3
3
  "type": "module",
4
- "version": "0.1.31",
4
+ "version": "0.1.34",
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",
@@ -15,7 +15,7 @@
15
15
  "dependencies": {
16
16
  "@vue/devtools-api": "^6.5.0",
17
17
  "uuid": "^9.0.0",
18
- "@moonbase.sh/api-client": "0.1.31"
18
+ "@moonbase.sh/api-client": "0.1.34"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "vue": "^3.2.0"