@moonbase.sh/vue 0.1.41 → 0.1.42

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
@@ -32,11 +32,11 @@ __export(src_exports, {
32
32
  });
33
33
  module.exports = __toCommonJS(src_exports);
34
34
  var import_api_client2 = require("@moonbase.sh/api-client");
35
+ var import_vue7 = require("vue");
35
36
 
36
37
  // src/context.ts
37
38
  var import_api_client = require("@moonbase.sh/api-client");
38
39
  var import_uuid = require("uuid");
39
- var import_vue = require("vue");
40
40
 
41
41
  // src/symbols.ts
42
42
  var storefrontKey = Symbol("storefront");
@@ -55,35 +55,36 @@ function debounce(func, waitMs = 100) {
55
55
 
56
56
  // src/context.ts
57
57
  var _StorefrontContextImpl = class _StorefrontContextImpl {
58
- constructor(configuration, client) {
58
+ constructor(configuration, client, stateFactory) {
59
59
  this.configuration = configuration;
60
60
  this.client = client;
61
- this.currentUser = (0, import_vue.ref)(null);
62
- this.loadedUser = (0, import_vue.ref)(false);
61
+ this.stateFactory = stateFactory;
63
62
  this.debouncedPushOrderContent = debounce((order) => this.client.orders.pushContent(order), 500);
63
+ this.currentUser = stateFactory("moonbase_user", null);
64
+ this.loadedUser = stateFactory("moonbase_loaded_user", false);
64
65
  if (typeof window === "undefined") {
65
- this.currentOrder = (0, import_vue.ref)({
66
+ this.currentOrder = stateFactory("moonbase_current_order", {
66
67
  id: (0, import_uuid.v4)(),
67
68
  status: import_api_client.OrderStatus.Open,
68
69
  currency: "",
69
70
  items: [],
70
71
  couponsApplied: []
71
72
  });
72
- this.storefront = (0, import_vue.ref)({
73
+ this.storefront = stateFactory("moonbase_storefront", {
73
74
  suggestedCurrency: "",
74
75
  bundles: [],
75
76
  products: []
76
77
  });
77
- this.loadedStorefront = (0, import_vue.ref)(false);
78
+ this.loadedStorefront = stateFactory("moonbase_loaded_storefront", false);
78
79
  return;
79
80
  }
80
81
  window.addEventListener("storage", (event) => this.handleStorageUpdate(event));
81
82
  const cachedOrderJson = localStorage.getItem("moonbase_session");
82
83
  if (cachedOrderJson) {
83
- this.currentOrder = (0, import_vue.ref)(JSON.parse(cachedOrderJson));
84
+ this.currentOrder = stateFactory("moonbase_current_order", JSON.parse(cachedOrderJson));
84
85
  const _1 = this.refreshOrder();
85
86
  } else {
86
- this.currentOrder = (0, import_vue.ref)({
87
+ this.currentOrder = stateFactory("moonbase_current_order", {
87
88
  id: (0, import_uuid.v4)(),
88
89
  status: import_api_client.OrderStatus.Open,
89
90
  currency: "",
@@ -94,15 +95,15 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
94
95
  }
95
96
  const cachedStorefrontJson = localStorage.getItem(_StorefrontContextImpl.storefrontKey);
96
97
  if (cachedStorefrontJson) {
97
- this.storefront = (0, import_vue.ref)(JSON.parse(cachedStorefrontJson));
98
- this.loadedStorefront = (0, import_vue.ref)(true);
98
+ this.storefront = stateFactory("moonbase_storefront", JSON.parse(cachedStorefrontJson));
99
+ this.loadedStorefront = stateFactory("moonbase_loaded_storefront", true);
99
100
  } else {
100
- this.storefront = (0, import_vue.ref)({
101
+ this.storefront = stateFactory("moonbase_storefront", {
101
102
  suggestedCurrency: "",
102
103
  bundles: [],
103
104
  products: []
104
105
  });
105
- this.loadedStorefront = (0, import_vue.ref)(false);
106
+ this.loadedStorefront = stateFactory("moonbase_loaded_storefront", false);
106
107
  }
107
108
  const _2 = this.updateStorefront();
108
109
  const _3 = this.updateUser();
@@ -187,51 +188,51 @@ var StorefrontContextImpl = _StorefrontContextImpl;
187
188
  __reExport(src_exports, require("@moonbase.sh/api-client"), module.exports);
188
189
 
189
190
  // src/composables/useBundle.ts
190
- var import_vue2 = require("vue");
191
+ var import_vue = require("vue");
191
192
  function useBundle(bundleId) {
192
- const storefront = (0, import_vue2.inject)(storefrontKey);
193
+ const storefront = (0, import_vue.inject)(storefrontKey);
193
194
  if (!storefront)
194
195
  throw new Error("No storefront configured");
195
- return (0, import_vue2.computed)(() => storefront.storefront.value.bundles.find((b) => b.id === bundleId) || null);
196
+ return (0, import_vue.computed)(() => storefront.storefront.value.bundles.find((b) => b.id === bundleId) || null);
196
197
  }
197
198
 
198
199
  // src/composables/useBundles.ts
199
- var import_vue3 = require("vue");
200
+ var import_vue2 = require("vue");
200
201
  function useBundles() {
201
- const storefront = (0, import_vue3.inject)(storefrontKey);
202
+ const storefront = (0, import_vue2.inject)(storefrontKey);
202
203
  if (!storefront)
203
204
  throw new Error("No storefront configured");
204
- return (0, import_vue3.computed)(() => storefront.storefront.value.bundles);
205
+ return (0, import_vue2.computed)(() => storefront.storefront.value.bundles);
205
206
  }
206
207
 
207
208
  // src/composables/useProduct.ts
208
- var import_vue4 = require("vue");
209
+ var import_vue3 = require("vue");
209
210
  function useProduct(productId) {
210
- const storefront = (0, import_vue4.inject)(storefrontKey);
211
+ const storefront = (0, import_vue3.inject)(storefrontKey);
211
212
  if (!storefront)
212
213
  throw new Error("No storefront configured");
213
- return (0, import_vue4.computed)(() => storefront.storefront.value.products.find((p) => p.id === productId) || null);
214
+ return (0, import_vue3.computed)(() => storefront.storefront.value.products.find((p) => p.id === productId) || null);
214
215
  }
215
216
 
216
217
  // src/composables/useProducts.ts
217
- var import_vue5 = require("vue");
218
+ var import_vue4 = require("vue");
218
219
  function useProducts() {
219
- const storefront = (0, import_vue5.inject)(storefrontKey);
220
+ const storefront = (0, import_vue4.inject)(storefrontKey);
220
221
  if (!storefront)
221
222
  throw new Error("No storefront configured");
222
- return (0, import_vue5.computed)(() => storefront.storefront.value.products);
223
+ return (0, import_vue4.computed)(() => storefront.storefront.value.products);
223
224
  }
224
225
 
225
226
  // src/composables/useCart.ts
226
- var import_vue6 = require("vue");
227
+ var import_vue5 = require("vue");
227
228
  function useCart() {
228
- const storefront = (0, import_vue6.inject)(storefrontKey);
229
+ const storefront = (0, import_vue5.inject)(storefrontKey);
229
230
  if (!storefront)
230
231
  throw new Error("No storefront configured");
231
232
  return {
232
- items: (0, import_vue6.computed)(() => storefront.currentOrder.value.items),
233
- currency: (0, import_vue6.computed)(() => storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency),
234
- total: (0, import_vue6.computed)(() => {
233
+ items: (0, import_vue5.computed)(() => storefront.currentOrder.value.items),
234
+ currency: (0, import_vue5.computed)(() => storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency),
235
+ total: (0, import_vue5.computed)(() => {
235
236
  const currency = storefront.currentOrder.value.currency || storefront.storefront.value.suggestedCurrency;
236
237
  const total = storefront.currentOrder.value.items.reduce((agg, item) => {
237
238
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
@@ -305,9 +306,9 @@ function useCart() {
305
306
  }
306
307
 
307
308
  // src/composables/useAuth.ts
308
- var import_vue7 = require("vue");
309
+ var import_vue6 = require("vue");
309
310
  function useAuth() {
310
- const storefront = (0, import_vue7.inject)(storefrontKey);
311
+ const storefront = (0, import_vue6.inject)(storefrontKey);
311
312
  if (!storefront)
312
313
  throw new Error("No storefront configured");
313
314
  return {
@@ -357,11 +358,16 @@ function useAuth() {
357
358
  }
358
359
 
359
360
  // src/index.ts
360
- function createStorefront(endpoint) {
361
+ function createStorefront(endpoint, stateFactory) {
361
362
  const configuration = {
362
363
  endpoint
363
364
  };
364
- return new StorefrontContextImpl(configuration, new import_api_client2.MoonbaseClient(configuration));
365
+ return new StorefrontContextImpl(
366
+ configuration,
367
+ new import_api_client2.MoonbaseClient(configuration),
368
+ // Default to vue refs but allow stuff like Nuxt useState wrappers
369
+ stateFactory || ((_, state) => (0, import_vue7.ref)(state))
370
+ );
365
371
  }
366
372
  // Annotate the CommonJS export names for ESM import in node:
367
373
  0 && (module.exports = {
package/dist/index.d.cts CHANGED
@@ -22,6 +22,7 @@ interface StorefrontContext {
22
22
  */
23
23
  install(app: App): any;
24
24
  }
25
+ type StateFactory = (key: string, state: any) => Ref<typeof state>;
25
26
 
26
27
  declare function useBundle(bundleId: string): Ref<StorefrontBundle | null>;
27
28
 
@@ -103,6 +104,6 @@ interface Cart {
103
104
  items: CartItem[];
104
105
  }
105
106
  type CartItem = OpenLineItem & {};
106
- declare function createStorefront(endpoint: string): StorefrontContext;
107
+ declare function createStorefront(endpoint: string, stateFactory?: StateFactory): StorefrontContext;
107
108
 
108
109
  export { Cart, CartItem, createStorefront, storefrontKey, useAuth, useBundle, useBundles, useCart, useProduct, useProducts };
package/dist/index.d.ts CHANGED
@@ -22,6 +22,7 @@ interface StorefrontContext {
22
22
  */
23
23
  install(app: App): any;
24
24
  }
25
+ type StateFactory = (key: string, state: any) => Ref<typeof state>;
25
26
 
26
27
  declare function useBundle(bundleId: string): Ref<StorefrontBundle | null>;
27
28
 
@@ -103,6 +104,6 @@ interface Cart {
103
104
  items: CartItem[];
104
105
  }
105
106
  type CartItem = OpenLineItem & {};
106
- declare function createStorefront(endpoint: string): StorefrontContext;
107
+ declare function createStorefront(endpoint: string, stateFactory?: StateFactory): StorefrontContext;
107
108
 
108
109
  export { Cart, CartItem, createStorefront, storefrontKey, useAuth, useBundle, useBundles, useCart, useProduct, useProducts };
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // src/index.ts
2
2
  import { MoonbaseClient } from "@moonbase.sh/api-client";
3
+ import { ref } from "vue";
3
4
 
4
5
  // src/context.ts
5
6
  import { NotAuthenticatedError, OrderStatus } from "@moonbase.sh/api-client";
6
7
  import { v4 as uuidv4 } from "uuid";
7
- import { ref } from "vue";
8
8
 
9
9
  // src/symbols.ts
10
10
  var storefrontKey = Symbol("storefront");
@@ -23,35 +23,36 @@ function debounce(func, waitMs = 100) {
23
23
 
24
24
  // src/context.ts
25
25
  var _StorefrontContextImpl = class _StorefrontContextImpl {
26
- constructor(configuration, client) {
26
+ constructor(configuration, client, stateFactory) {
27
27
  this.configuration = configuration;
28
28
  this.client = client;
29
- this.currentUser = ref(null);
30
- this.loadedUser = ref(false);
29
+ this.stateFactory = stateFactory;
31
30
  this.debouncedPushOrderContent = debounce((order) => this.client.orders.pushContent(order), 500);
31
+ this.currentUser = stateFactory("moonbase_user", null);
32
+ this.loadedUser = stateFactory("moonbase_loaded_user", false);
32
33
  if (typeof window === "undefined") {
33
- this.currentOrder = ref({
34
+ this.currentOrder = stateFactory("moonbase_current_order", {
34
35
  id: uuidv4(),
35
36
  status: OrderStatus.Open,
36
37
  currency: "",
37
38
  items: [],
38
39
  couponsApplied: []
39
40
  });
40
- this.storefront = ref({
41
+ this.storefront = stateFactory("moonbase_storefront", {
41
42
  suggestedCurrency: "",
42
43
  bundles: [],
43
44
  products: []
44
45
  });
45
- this.loadedStorefront = ref(false);
46
+ this.loadedStorefront = stateFactory("moonbase_loaded_storefront", false);
46
47
  return;
47
48
  }
48
49
  window.addEventListener("storage", (event) => this.handleStorageUpdate(event));
49
50
  const cachedOrderJson = localStorage.getItem("moonbase_session");
50
51
  if (cachedOrderJson) {
51
- this.currentOrder = ref(JSON.parse(cachedOrderJson));
52
+ this.currentOrder = stateFactory("moonbase_current_order", JSON.parse(cachedOrderJson));
52
53
  const _1 = this.refreshOrder();
53
54
  } else {
54
- this.currentOrder = ref({
55
+ this.currentOrder = stateFactory("moonbase_current_order", {
55
56
  id: uuidv4(),
56
57
  status: OrderStatus.Open,
57
58
  currency: "",
@@ -62,15 +63,15 @@ var _StorefrontContextImpl = class _StorefrontContextImpl {
62
63
  }
63
64
  const cachedStorefrontJson = localStorage.getItem(_StorefrontContextImpl.storefrontKey);
64
65
  if (cachedStorefrontJson) {
65
- this.storefront = ref(JSON.parse(cachedStorefrontJson));
66
- this.loadedStorefront = ref(true);
66
+ this.storefront = stateFactory("moonbase_storefront", JSON.parse(cachedStorefrontJson));
67
+ this.loadedStorefront = stateFactory("moonbase_loaded_storefront", true);
67
68
  } else {
68
- this.storefront = ref({
69
+ this.storefront = stateFactory("moonbase_storefront", {
69
70
  suggestedCurrency: "",
70
71
  bundles: [],
71
72
  products: []
72
73
  });
73
- this.loadedStorefront = ref(false);
74
+ this.loadedStorefront = stateFactory("moonbase_loaded_storefront", false);
74
75
  }
75
76
  const _2 = this.updateStorefront();
76
77
  const _3 = this.updateUser();
@@ -325,11 +326,16 @@ function useAuth() {
325
326
  }
326
327
 
327
328
  // src/index.ts
328
- function createStorefront(endpoint) {
329
+ function createStorefront(endpoint, stateFactory) {
329
330
  const configuration = {
330
331
  endpoint
331
332
  };
332
- return new StorefrontContextImpl(configuration, new MoonbaseClient(configuration));
333
+ return new StorefrontContextImpl(
334
+ configuration,
335
+ new MoonbaseClient(configuration),
336
+ // Default to vue refs but allow stuff like Nuxt useState wrappers
337
+ stateFactory || ((_, state) => ref(state))
338
+ );
333
339
  }
334
340
  export {
335
341
  createStorefront,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moonbase.sh/vue",
3
3
  "type": "module",
4
- "version": "0.1.41",
4
+ "version": "0.1.42",
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,7 @@
18
18
  "dependencies": {
19
19
  "@vue/devtools-api": "^6.5.0",
20
20
  "uuid": "^9.0.0",
21
- "@moonbase.sh/api-client": "0.1.41"
21
+ "@moonbase.sh/api-client": "0.1.42"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/uuid": "^9.0.1",