@moonbase.sh/storefront-api 0.3.20 → 0.3.21

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,50 @@ 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
+
1037
+ // src/utils/tokenStore.ts
1010
1038
  var _TokenStore = class _TokenStore {
1011
1039
  constructor(configuration) {
1012
1040
  this.configuration = configuration;
1013
1041
  this.tokens = null;
1014
1042
  this.refreshTimeoutId = null;
1015
1043
  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
- }
1044
+ var _a;
1045
+ this.store = (_a = configuration.store) != null ? _a : new LocalStorageStore();
1046
+ const storedToken = this.store.get(_TokenStore.storageKey);
1047
+ if (storedToken) {
1048
+ this.tokens = {
1049
+ ...storedToken,
1050
+ expiresAt: new Date(storedToken.expiresAt)
1051
+ };
1023
1052
  }
1053
+ this.store.listen(_TokenStore.storageKey, (updatedTokens) => this.handleStorageUpdate(updatedTokens));
1024
1054
  }
1025
1055
  get user() {
1026
1056
  if (this.tokens)
@@ -1043,8 +1073,8 @@ var _TokenStore = class _TokenStore {
1043
1073
  setUser(user) {
1044
1074
  if (user === null) {
1045
1075
  this.tokens = null;
1046
- if (typeof window !== "undefined" && localStorage)
1047
- localStorage.removeItem(_TokenStore.storageKey);
1076
+ if (typeof window !== "undefined")
1077
+ this.store.remove(_TokenStore.storageKey);
1048
1078
  if (this.refreshTimeoutId != null)
1049
1079
  window.clearTimeout(this.refreshTimeoutId);
1050
1080
  return null;
@@ -1054,8 +1084,8 @@ var _TokenStore = class _TokenStore {
1054
1084
  // Hardcoded 15 minutes now, might want to check the JWT tho
1055
1085
  expiresAt: new Date((/* @__PURE__ */ new Date()).getTime() + 15 * 60 * 1e3)
1056
1086
  };
1057
- if (typeof window !== "undefined" && localStorage)
1058
- localStorage.setItem(_TokenStore.storageKey, JSON.stringify(this.tokens));
1087
+ if (typeof window !== "undefined")
1088
+ this.store.set(_TokenStore.storageKey, this.tokens);
1059
1089
  if (this.refreshTimeoutId != null)
1060
1090
  window.clearTimeout(this.refreshTimeoutId);
1061
1091
  this.refreshTimeoutId = window.setTimeout(() => {
@@ -1088,16 +1118,12 @@ var _TokenStore = class _TokenStore {
1088
1118
  const result = identitySchema.parse(await response.json());
1089
1119
  return this.setUser(result);
1090
1120
  }
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;
1121
+ handleStorageUpdate(updatedTokens) {
1122
+ this.tokens = updatedTokens;
1123
+ if (this.refreshTimeoutId != null)
1124
+ window.clearTimeout(this.refreshTimeoutId);
1125
+ if (this.tokens) {
1126
+ this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1101
1127
  }
1102
1128
  }
1103
1129
  };
@@ -1166,10 +1192,9 @@ __export(schemas_exports3, {
1166
1192
  // src/index.ts
1167
1193
  var MoonbaseClient = class {
1168
1194
  constructor(configuration) {
1169
- var _a;
1170
1195
  this.configuration = configuration;
1171
1196
  this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
1172
- this.tokenStore = (_a = configuration.tokenStore) != null ? _a : new TokenStore(configuration);
1197
+ this.tokenStore = new TokenStore(configuration);
1173
1198
  this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore);
1174
1199
  this.storefront = new StorefrontEndpoints(this.api, this.configuration);
1175
1200
  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,50 @@ 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
+
989
+ // src/utils/tokenStore.ts
962
990
  var _TokenStore = class _TokenStore {
963
991
  constructor(configuration) {
964
992
  this.configuration = configuration;
965
993
  this.tokens = null;
966
994
  this.refreshTimeoutId = null;
967
995
  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
- }
996
+ var _a;
997
+ this.store = (_a = configuration.store) != null ? _a : new LocalStorageStore();
998
+ const storedToken = this.store.get(_TokenStore.storageKey);
999
+ if (storedToken) {
1000
+ this.tokens = {
1001
+ ...storedToken,
1002
+ expiresAt: new Date(storedToken.expiresAt)
1003
+ };
975
1004
  }
1005
+ this.store.listen(_TokenStore.storageKey, (updatedTokens) => this.handleStorageUpdate(updatedTokens));
976
1006
  }
977
1007
  get user() {
978
1008
  if (this.tokens)
@@ -995,8 +1025,8 @@ var _TokenStore = class _TokenStore {
995
1025
  setUser(user) {
996
1026
  if (user === null) {
997
1027
  this.tokens = null;
998
- if (typeof window !== "undefined" && localStorage)
999
- localStorage.removeItem(_TokenStore.storageKey);
1028
+ if (typeof window !== "undefined")
1029
+ this.store.remove(_TokenStore.storageKey);
1000
1030
  if (this.refreshTimeoutId != null)
1001
1031
  window.clearTimeout(this.refreshTimeoutId);
1002
1032
  return null;
@@ -1006,8 +1036,8 @@ var _TokenStore = class _TokenStore {
1006
1036
  // Hardcoded 15 minutes now, might want to check the JWT tho
1007
1037
  expiresAt: new Date((/* @__PURE__ */ new Date()).getTime() + 15 * 60 * 1e3)
1008
1038
  };
1009
- if (typeof window !== "undefined" && localStorage)
1010
- localStorage.setItem(_TokenStore.storageKey, JSON.stringify(this.tokens));
1039
+ if (typeof window !== "undefined")
1040
+ this.store.set(_TokenStore.storageKey, this.tokens);
1011
1041
  if (this.refreshTimeoutId != null)
1012
1042
  window.clearTimeout(this.refreshTimeoutId);
1013
1043
  this.refreshTimeoutId = window.setTimeout(() => {
@@ -1040,16 +1070,12 @@ var _TokenStore = class _TokenStore {
1040
1070
  const result = identitySchema.parse(await response.json());
1041
1071
  return this.setUser(result);
1042
1072
  }
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;
1073
+ handleStorageUpdate(updatedTokens) {
1074
+ this.tokens = updatedTokens;
1075
+ if (this.refreshTimeoutId != null)
1076
+ window.clearTimeout(this.refreshTimeoutId);
1077
+ if (this.tokens) {
1078
+ this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1053
1079
  }
1054
1080
  }
1055
1081
  };
@@ -1118,10 +1144,9 @@ __export(schemas_exports3, {
1118
1144
  // src/index.ts
1119
1145
  var MoonbaseClient = class {
1120
1146
  constructor(configuration) {
1121
- var _a;
1122
1147
  this.configuration = configuration;
1123
1148
  this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
1124
- this.tokenStore = (_a = configuration.tokenStore) != null ? _a : new TokenStore(configuration);
1149
+ this.tokenStore = new TokenStore(configuration);
1125
1150
  this.api = new MoonbaseApi(this.configuration.endpoint, this.tokenStore);
1126
1151
  this.storefront = new StorefrontEndpoints(this.api, this.configuration);
1127
1152
  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.21",
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",