@moonbase.sh/storefront-api 0.3.19 → 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
@@ -381,7 +381,7 @@ var schemas_exports = {};
381
381
  __export(schemas_exports, {
382
382
  addressSchema: () => addressSchema,
383
383
  communicationPreferencesSchema: () => communicationPreferencesSchema,
384
- identityUserSchema: () => identityUserSchema,
384
+ identitySchema: () => identitySchema,
385
385
  userAccountConfirmedSchema: () => userAccountConfirmedSchema,
386
386
  userSchema: () => userSchema
387
387
  });
@@ -410,7 +410,7 @@ var userSchema = import_zod7.z.object({
410
410
  hasProducts: import_zod7.z.boolean().nullish(),
411
411
  hasSubscriptions: import_zod7.z.boolean().nullish()
412
412
  });
413
- var identityUserSchema = userSchema.and(import_zod7.z.object({
413
+ var identitySchema = userSchema.and(import_zod7.z.object({
414
414
  accessToken: import_zod7.z.string(),
415
415
  refreshToken: import_zod7.z.string()
416
416
  }));
@@ -447,7 +447,7 @@ var IdentityEndpoints = class {
447
447
  await handleResponseProblem(response);
448
448
  try {
449
449
  const data = await response.json();
450
- const user = identityUserSchema.parse(data);
450
+ const user = identitySchema.parse(data);
451
451
  this.tokenStore.setUser(user);
452
452
  return userSchema.parse(user);
453
453
  } catch (err) {
@@ -456,7 +456,7 @@ var IdentityEndpoints = class {
456
456
  }
457
457
  }
458
458
  async signUp(name, email, password, address, acceptedPrivacyPolicy, acceptedTermsAndConditions, communicationOptIn) {
459
- const response = await this.api.fetch(`/api/customer/identity/sign-up?scheme=JWT&communicationOptIn=${communicationOptIn ? "true" : "false"}`, identityUserSchema, {
459
+ const response = await this.api.fetch(`/api/customer/identity/sign-up?scheme=JWT&communicationOptIn=${communicationOptIn ? "true" : "false"}`, identitySchema, {
460
460
  method: "POST",
461
461
  body: {
462
462
  name,
@@ -902,7 +902,7 @@ var MoonbaseApi = class {
902
902
  this.tokenStore = tokenStore;
903
903
  }
904
904
  async authenticatedFetch(path, schema, options) {
905
- if (!this.tokenStore.hasAccessToken)
905
+ if (!this.tokenStore.user)
906
906
  throw new NotAuthenticatedError();
907
907
  return await this.fetch(path, schema, options);
908
908
  }
@@ -957,7 +957,7 @@ var MoonbaseApi = class {
957
957
  }
958
958
  async reportParsingProblem(path, err, body) {
959
959
  try {
960
- await (0, import_cross_fetch2.default)(this.baseUrl + "/api/customer/insights/error", {
960
+ await (0, import_cross_fetch2.default)(`${this.baseUrl}/api/customer/insights/error`, {
961
961
  mode: "cors",
962
962
  method: "POST",
963
963
  headers: {
@@ -979,7 +979,7 @@ var MoonbaseApi = class {
979
979
  }
980
980
  async reportRequestProblem(path, request, response, err) {
981
981
  try {
982
- await (0, import_cross_fetch2.default)(this.baseUrl + "/api/customer/insights/warn", {
982
+ await (0, import_cross_fetch2.default)(`${this.baseUrl}/api/customer/insights/warn`, {
983
983
  mode: "cors",
984
984
  method: "POST",
985
985
  headers: {
@@ -1007,29 +1007,56 @@ 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)
1027
1057
  return this.tokens;
1028
1058
  return null;
1029
1059
  }
1030
- get hasAccessToken() {
1031
- return !!this.tokens;
1032
- }
1033
1060
  async getAccessToken() {
1034
1061
  var _a, _b, _c;
1035
1062
  if (this.isExpired) {
@@ -1046,8 +1073,8 @@ var _TokenStore = class _TokenStore {
1046
1073
  setUser(user) {
1047
1074
  if (user === null) {
1048
1075
  this.tokens = null;
1049
- if (typeof window !== "undefined" && localStorage)
1050
- localStorage.removeItem(_TokenStore.storageKey);
1076
+ if (typeof window !== "undefined")
1077
+ this.store.remove(_TokenStore.storageKey);
1051
1078
  if (this.refreshTimeoutId != null)
1052
1079
  window.clearTimeout(this.refreshTimeoutId);
1053
1080
  return null;
@@ -1057,8 +1084,8 @@ var _TokenStore = class _TokenStore {
1057
1084
  // Hardcoded 15 minutes now, might want to check the JWT tho
1058
1085
  expiresAt: new Date((/* @__PURE__ */ new Date()).getTime() + 15 * 60 * 1e3)
1059
1086
  };
1060
- if (typeof window !== "undefined" && localStorage)
1061
- localStorage.setItem(_TokenStore.storageKey, JSON.stringify(this.tokens));
1087
+ if (typeof window !== "undefined")
1088
+ this.store.set(_TokenStore.storageKey, this.tokens);
1062
1089
  if (this.refreshTimeoutId != null)
1063
1090
  window.clearTimeout(this.refreshTimeoutId);
1064
1091
  this.refreshTimeoutId = window.setTimeout(() => {
@@ -1088,19 +1115,15 @@ var _TokenStore = class _TokenStore {
1088
1115
  }
1089
1116
  throw new MoonbaseError("Unexpected result", `Could not refresh access token, status code ${response.status}`, response.status);
1090
1117
  }
1091
- const result = identityUserSchema.parse(await response.json());
1118
+ const result = identitySchema.parse(await response.json());
1092
1119
  return this.setUser(result);
1093
1120
  }
1094
- handleStorageUpdate(event) {
1095
- switch (event.key) {
1096
- case _TokenStore.storageKey:
1097
- this.tokens = event.newValue ? JSON.parse(event.newValue) : null;
1098
- if (this.refreshTimeoutId != null)
1099
- window.clearTimeout(this.refreshTimeoutId);
1100
- if (this.tokens) {
1101
- this.tokens.expiresAt = new Date(this.tokens.expiresAt);
1102
- }
1103
- 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);
1104
1127
  }
1105
1128
  }
1106
1129
  };