@moonbase.sh/storefront-api 0.3.20 → 0.3.22

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
@@ -1007,20 +1007,76 @@ var MoonbaseApi = class {
1007
1007
 
1008
1008
  // src/utils/tokenStore.ts
1009
1009
  var import_cross_fetch3 = __toESM(require("cross-fetch"), 1);
1010
+
1011
+ // src/utils/store.ts
1012
+ var LocalStorageStore = class {
1013
+ get(key) {
1014
+ const item = localStorage.getItem(key);
1015
+ if (item) {
1016
+ return JSON.parse(item);
1017
+ }
1018
+ return null;
1019
+ }
1020
+ set(key, item) {
1021
+ localStorage.setItem(key, JSON.stringify(item));
1022
+ }
1023
+ remove(key) {
1024
+ localStorage.removeItem(key);
1025
+ }
1026
+ listen(key, callback) {
1027
+ if (typeof window !== "undefined") {
1028
+ window.addEventListener("storage", (event) => {
1029
+ if (event.key === key) {
1030
+ callback(event.newValue ? JSON.parse(event.newValue) : null);
1031
+ }
1032
+ });
1033
+ }
1034
+ }
1035
+ };
1036
+ var InMemoryStore = class {
1037
+ constructor() {
1038
+ this.store = {};
1039
+ this.listeners = {};
1040
+ }
1041
+ get(key) {
1042
+ var _a;
1043
+ return (_a = this.store[key]) != null ? _a : null;
1044
+ }
1045
+ set(key, item) {
1046
+ this.store[key] = item;
1047
+ for (const listener of this.listeners[key] || []) {
1048
+ listener(item);
1049
+ }
1050
+ }
1051
+ remove(key) {
1052
+ delete this.store[key];
1053
+ for (const listener of this.listeners[key] || []) {
1054
+ listener(null);
1055
+ }
1056
+ }
1057
+ listen(key, callback) {
1058
+ if (!this.listeners[key]) this.listeners[key] = [];
1059
+ this.listeners[key].push(callback);
1060
+ }
1061
+ };
1062
+
1063
+ // src/utils/tokenStore.ts
1010
1064
  var _TokenStore = class _TokenStore {
1011
1065
  constructor(configuration) {
1012
1066
  this.configuration = configuration;
1013
1067
  this.tokens = null;
1014
1068
  this.refreshTimeoutId = null;
1015
1069
  this.refreshPromise = null;
1016
- if (typeof window !== "undefined") {
1017
- window.addEventListener("storage", (event) => this.handleStorageUpdate(event));
1018
- const storedTokens = localStorage.getItem(_TokenStore.storageKey);
1019
- if (storedTokens) {
1020
- this.tokens = JSON.parse(storedTokens);
1021
- this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1022
- }
1070
+ var _a;
1071
+ this.store = ((_a = configuration.store) != null ? _a : localStorage) ? new LocalStorageStore() : new InMemoryStore();
1072
+ const storedToken = this.store.get(_TokenStore.storageKey);
1073
+ if (storedToken) {
1074
+ this.tokens = {
1075
+ ...storedToken,
1076
+ expiresAt: new Date(storedToken.expiresAt)
1077
+ };
1023
1078
  }
1079
+ this.store.listen(_TokenStore.storageKey, (updatedTokens) => this.handleStorageUpdate(updatedTokens));
1024
1080
  }
1025
1081
  get user() {
1026
1082
  if (this.tokens)
@@ -1043,8 +1099,8 @@ var _TokenStore = class _TokenStore {
1043
1099
  setUser(user) {
1044
1100
  if (user === null) {
1045
1101
  this.tokens = null;
1046
- if (typeof window !== "undefined" && localStorage)
1047
- localStorage.removeItem(_TokenStore.storageKey);
1102
+ if (typeof window !== "undefined")
1103
+ this.store.remove(_TokenStore.storageKey);
1048
1104
  if (this.refreshTimeoutId != null)
1049
1105
  window.clearTimeout(this.refreshTimeoutId);
1050
1106
  return null;
@@ -1054,8 +1110,8 @@ var _TokenStore = class _TokenStore {
1054
1110
  // Hardcoded 15 minutes now, might want to check the JWT tho
1055
1111
  expiresAt: new Date((/* @__PURE__ */ new Date()).getTime() + 15 * 60 * 1e3)
1056
1112
  };
1057
- if (typeof window !== "undefined" && localStorage)
1058
- localStorage.setItem(_TokenStore.storageKey, JSON.stringify(this.tokens));
1113
+ if (typeof window !== "undefined")
1114
+ this.store.set(_TokenStore.storageKey, this.tokens);
1059
1115
  if (this.refreshTimeoutId != null)
1060
1116
  window.clearTimeout(this.refreshTimeoutId);
1061
1117
  this.refreshTimeoutId = window.setTimeout(() => {
@@ -1088,16 +1144,12 @@ var _TokenStore = class _TokenStore {
1088
1144
  const result = identitySchema.parse(await response.json());
1089
1145
  return this.setUser(result);
1090
1146
  }
1091
- handleStorageUpdate(event) {
1092
- switch (event.key) {
1093
- case _TokenStore.storageKey:
1094
- this.tokens = event.newValue ? JSON.parse(event.newValue) : null;
1095
- if (this.refreshTimeoutId != null)
1096
- window.clearTimeout(this.refreshTimeoutId);
1097
- if (this.tokens) {
1098
- this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1099
- }
1100
- break;
1147
+ handleStorageUpdate(updatedTokens) {
1148
+ this.tokens = updatedTokens;
1149
+ if (this.refreshTimeoutId != null)
1150
+ window.clearTimeout(this.refreshTimeoutId);
1151
+ if (this.tokens) {
1152
+ this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1101
1153
  }
1102
1154
  }
1103
1155
  };
@@ -1166,10 +1218,9 @@ __export(schemas_exports3, {
1166
1218
  // src/index.ts
1167
1219
  var MoonbaseClient = class {
1168
1220
  constructor(configuration) {
1169
- var _a;
1170
1221
  this.configuration = configuration;
1171
1222
  this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
1172
- this.tokenStore = (_a = configuration.tokenStore) != null ? _a : new TokenStore(configuration);
1223
+ this.tokenStore = new TokenStore(configuration);
1173
1224
  this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore);
1174
1225
  this.storefront = new StorefrontEndpoints(this.api, this.configuration);
1175
1226
  this.identity = new IdentityEndpoints(this.api, this.tokenStore);
package/dist/index.d.cts CHANGED
@@ -236,6 +236,7 @@ declare class TokenStore implements ITokenStore {
236
236
  private tokens;
237
237
  private refreshTimeoutId;
238
238
  private refreshPromise;
239
+ private readonly store;
239
240
  static readonly storageKey = "moonbase_auth";
240
241
  constructor(configuration: MoonbaseConfiguration);
241
242
  get user(): User | null;
@@ -18538,6 +18539,13 @@ declare class VoucherEndpoints {
18538
18539
  redeem(code: string): Promise<Voucher>;
18539
18540
  }
18540
18541
 
18542
+ interface IStore {
18543
+ get<TItem>(key: string): TItem | null;
18544
+ set<TItem>(key: string, item: TItem): void;
18545
+ remove(key: string): void;
18546
+ listen<TItem>(key: string, callback: (item: TItem | null) => void): void;
18547
+ }
18548
+
18541
18549
  declare namespace schemas {
18542
18550
  export { schemas$2 as identity, schemas$1 as orders };
18543
18551
  }
@@ -22639,7 +22647,7 @@ interface MoonbaseConfiguration {
22639
22647
  endpoint: string;
22640
22648
  persistUtm?: boolean;
22641
22649
  includeManifests?: boolean;
22642
- tokenStore?: ITokenStore;
22650
+ store?: IStore;
22643
22651
  }
22644
22652
  declare class MoonbaseClient {
22645
22653
  private readonly configuration;
package/dist/index.d.ts CHANGED
@@ -236,6 +236,7 @@ declare class TokenStore implements ITokenStore {
236
236
  private tokens;
237
237
  private refreshTimeoutId;
238
238
  private refreshPromise;
239
+ private readonly store;
239
240
  static readonly storageKey = "moonbase_auth";
240
241
  constructor(configuration: MoonbaseConfiguration);
241
242
  get user(): User | null;
@@ -18538,6 +18539,13 @@ declare class VoucherEndpoints {
18538
18539
  redeem(code: string): Promise<Voucher>;
18539
18540
  }
18540
18541
 
18542
+ interface IStore {
18543
+ get<TItem>(key: string): TItem | null;
18544
+ set<TItem>(key: string, item: TItem): void;
18545
+ remove(key: string): void;
18546
+ listen<TItem>(key: string, callback: (item: TItem | null) => void): void;
18547
+ }
18548
+
18541
18549
  declare namespace schemas {
18542
18550
  export { schemas$2 as identity, schemas$1 as orders };
18543
18551
  }
@@ -22639,7 +22647,7 @@ interface MoonbaseConfiguration {
22639
22647
  endpoint: string;
22640
22648
  persistUtm?: boolean;
22641
22649
  includeManifests?: boolean;
22642
- tokenStore?: ITokenStore;
22650
+ store?: IStore;
22643
22651
  }
22644
22652
  declare class MoonbaseClient {
22645
22653
  private readonly configuration;
package/dist/index.js CHANGED
@@ -959,20 +959,76 @@ var MoonbaseApi = class {
959
959
 
960
960
  // src/utils/tokenStore.ts
961
961
  import fetch3 from "cross-fetch";
962
+
963
+ // src/utils/store.ts
964
+ var LocalStorageStore = class {
965
+ get(key) {
966
+ const item = localStorage.getItem(key);
967
+ if (item) {
968
+ return JSON.parse(item);
969
+ }
970
+ return null;
971
+ }
972
+ set(key, item) {
973
+ localStorage.setItem(key, JSON.stringify(item));
974
+ }
975
+ remove(key) {
976
+ localStorage.removeItem(key);
977
+ }
978
+ listen(key, callback) {
979
+ if (typeof window !== "undefined") {
980
+ window.addEventListener("storage", (event) => {
981
+ if (event.key === key) {
982
+ callback(event.newValue ? JSON.parse(event.newValue) : null);
983
+ }
984
+ });
985
+ }
986
+ }
987
+ };
988
+ var InMemoryStore = class {
989
+ constructor() {
990
+ this.store = {};
991
+ this.listeners = {};
992
+ }
993
+ get(key) {
994
+ var _a;
995
+ return (_a = this.store[key]) != null ? _a : null;
996
+ }
997
+ set(key, item) {
998
+ this.store[key] = item;
999
+ for (const listener of this.listeners[key] || []) {
1000
+ listener(item);
1001
+ }
1002
+ }
1003
+ remove(key) {
1004
+ delete this.store[key];
1005
+ for (const listener of this.listeners[key] || []) {
1006
+ listener(null);
1007
+ }
1008
+ }
1009
+ listen(key, callback) {
1010
+ if (!this.listeners[key]) this.listeners[key] = [];
1011
+ this.listeners[key].push(callback);
1012
+ }
1013
+ };
1014
+
1015
+ // src/utils/tokenStore.ts
962
1016
  var _TokenStore = class _TokenStore {
963
1017
  constructor(configuration) {
964
1018
  this.configuration = configuration;
965
1019
  this.tokens = null;
966
1020
  this.refreshTimeoutId = null;
967
1021
  this.refreshPromise = null;
968
- if (typeof window !== "undefined") {
969
- window.addEventListener("storage", (event) => this.handleStorageUpdate(event));
970
- const storedTokens = localStorage.getItem(_TokenStore.storageKey);
971
- if (storedTokens) {
972
- this.tokens = JSON.parse(storedTokens);
973
- this.tokens.expiresAt = new Date(this.tokens.expiresAt);
974
- }
1022
+ var _a;
1023
+ this.store = ((_a = configuration.store) != null ? _a : localStorage) ? new LocalStorageStore() : new InMemoryStore();
1024
+ const storedToken = this.store.get(_TokenStore.storageKey);
1025
+ if (storedToken) {
1026
+ this.tokens = {
1027
+ ...storedToken,
1028
+ expiresAt: new Date(storedToken.expiresAt)
1029
+ };
975
1030
  }
1031
+ this.store.listen(_TokenStore.storageKey, (updatedTokens) => this.handleStorageUpdate(updatedTokens));
976
1032
  }
977
1033
  get user() {
978
1034
  if (this.tokens)
@@ -995,8 +1051,8 @@ var _TokenStore = class _TokenStore {
995
1051
  setUser(user) {
996
1052
  if (user === null) {
997
1053
  this.tokens = null;
998
- if (typeof window !== "undefined" && localStorage)
999
- localStorage.removeItem(_TokenStore.storageKey);
1054
+ if (typeof window !== "undefined")
1055
+ this.store.remove(_TokenStore.storageKey);
1000
1056
  if (this.refreshTimeoutId != null)
1001
1057
  window.clearTimeout(this.refreshTimeoutId);
1002
1058
  return null;
@@ -1006,8 +1062,8 @@ var _TokenStore = class _TokenStore {
1006
1062
  // Hardcoded 15 minutes now, might want to check the JWT tho
1007
1063
  expiresAt: new Date((/* @__PURE__ */ new Date()).getTime() + 15 * 60 * 1e3)
1008
1064
  };
1009
- if (typeof window !== "undefined" && localStorage)
1010
- localStorage.setItem(_TokenStore.storageKey, JSON.stringify(this.tokens));
1065
+ if (typeof window !== "undefined")
1066
+ this.store.set(_TokenStore.storageKey, this.tokens);
1011
1067
  if (this.refreshTimeoutId != null)
1012
1068
  window.clearTimeout(this.refreshTimeoutId);
1013
1069
  this.refreshTimeoutId = window.setTimeout(() => {
@@ -1040,16 +1096,12 @@ var _TokenStore = class _TokenStore {
1040
1096
  const result = identitySchema.parse(await response.json());
1041
1097
  return this.setUser(result);
1042
1098
  }
1043
- handleStorageUpdate(event) {
1044
- switch (event.key) {
1045
- case _TokenStore.storageKey:
1046
- this.tokens = event.newValue ? JSON.parse(event.newValue) : null;
1047
- if (this.refreshTimeoutId != null)
1048
- window.clearTimeout(this.refreshTimeoutId);
1049
- if (this.tokens) {
1050
- this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1051
- }
1052
- break;
1099
+ handleStorageUpdate(updatedTokens) {
1100
+ this.tokens = updatedTokens;
1101
+ if (this.refreshTimeoutId != null)
1102
+ window.clearTimeout(this.refreshTimeoutId);
1103
+ if (this.tokens) {
1104
+ this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1053
1105
  }
1054
1106
  }
1055
1107
  };
@@ -1118,10 +1170,9 @@ __export(schemas_exports3, {
1118
1170
  // src/index.ts
1119
1171
  var MoonbaseClient = class {
1120
1172
  constructor(configuration) {
1121
- var _a;
1122
1173
  this.configuration = configuration;
1123
1174
  this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
1124
- this.tokenStore = (_a = configuration.tokenStore) != null ? _a : new TokenStore(configuration);
1175
+ this.tokenStore = new TokenStore(configuration);
1125
1176
  this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore);
1126
1177
  this.storefront = new StorefrontEndpoints(this.api, this.configuration);
1127
1178
  this.identity = new IdentityEndpoints(this.api, this.tokenStore);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moonbase.sh/storefront-api",
3
3
  "type": "module",
4
- "version": "0.3.20",
4
+ "version": "0.3.22",
5
5
  "description": "Package to let you build storefronts with Moonbase.sh as payment and delivery provider",
6
6
  "author": "Tobias Lønnerød Madsen <m@dsen.tv>",
7
7
  "license": "MIT",