@fluxbase/sdk 0.0.1-rc.39 → 0.0.1-rc.40

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
@@ -141,16 +141,60 @@ async function wrapAsyncVoid(operation) {
141
141
 
142
142
  // src/auth.ts
143
143
  var AUTH_STORAGE_KEY = "fluxbase.auth.session";
144
+ var MemoryStorage = class {
145
+ constructor() {
146
+ this.store = /* @__PURE__ */ new Map();
147
+ }
148
+ get length() {
149
+ return this.store.size;
150
+ }
151
+ clear() {
152
+ this.store.clear();
153
+ }
154
+ getItem(key) {
155
+ return this.store.get(key) ?? null;
156
+ }
157
+ setItem(key, value) {
158
+ this.store.set(key, value);
159
+ }
160
+ removeItem(key) {
161
+ this.store.delete(key);
162
+ }
163
+ key(index) {
164
+ return [...this.store.keys()][index] ?? null;
165
+ }
166
+ };
167
+ function isLocalStorageAvailable() {
168
+ try {
169
+ if (typeof localStorage === "undefined") {
170
+ return false;
171
+ }
172
+ const testKey = "__fluxbase_storage_test__";
173
+ localStorage.setItem(testKey, "test");
174
+ localStorage.removeItem(testKey);
175
+ return true;
176
+ } catch {
177
+ return false;
178
+ }
179
+ }
144
180
  var FluxbaseAuth = class {
145
181
  constructor(fetch2, autoRefresh = true, persist = true) {
146
182
  this.session = null;
147
183
  this.refreshTimer = null;
148
184
  this.stateChangeListeners = /* @__PURE__ */ new Set();
185
+ this.storage = null;
149
186
  this.fetch = fetch2;
150
187
  this.persist = persist;
151
188
  this.autoRefresh = autoRefresh;
152
- if (this.persist && typeof localStorage !== "undefined") {
153
- const stored = localStorage.getItem(AUTH_STORAGE_KEY);
189
+ if (this.persist) {
190
+ if (isLocalStorageAvailable()) {
191
+ this.storage = localStorage;
192
+ } else {
193
+ this.storage = new MemoryStorage();
194
+ }
195
+ }
196
+ if (this.storage) {
197
+ const stored = this.storage.getItem(AUTH_STORAGE_KEY);
154
198
  if (stored) {
155
199
  try {
156
200
  this.session = JSON.parse(stored);
@@ -159,7 +203,7 @@ var FluxbaseAuth = class {
159
203
  this.scheduleTokenRefresh();
160
204
  }
161
205
  } catch {
162
- localStorage.removeItem(AUTH_STORAGE_KEY);
206
+ this.storage.removeItem(AUTH_STORAGE_KEY);
163
207
  }
164
208
  }
165
209
  }
@@ -796,8 +840,8 @@ var FluxbaseAuth = class {
796
840
  clearSession() {
797
841
  this.session = null;
798
842
  this.fetch.setAuthToken(null);
799
- if (this.persist && typeof localStorage !== "undefined") {
800
- localStorage.removeItem(AUTH_STORAGE_KEY);
843
+ if (this.storage) {
844
+ this.storage.removeItem(AUTH_STORAGE_KEY);
801
845
  }
802
846
  if (this.refreshTimer) {
803
847
  clearTimeout(this.refreshTimer);
@@ -809,8 +853,8 @@ var FluxbaseAuth = class {
809
853
  * Internal: Save session to storage
810
854
  */
811
855
  saveSession() {
812
- if (this.persist && typeof localStorage !== "undefined" && this.session) {
813
- localStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(this.session));
856
+ if (this.storage && this.session) {
857
+ this.storage.setItem(AUTH_STORAGE_KEY, JSON.stringify(this.session));
814
858
  }
815
859
  }
816
860
  /**