@fluid-app/storage-manager
Version:
Storage management utilities for Fluid Commerce applications
178 lines (174 loc) • 4.52 kB
JavaScript
// src/StorageKeys.ts
var STORAGE_KEYS = {
CART: "fs_cart",
EVENT_QUEUE: "fs_event_queue",
SESSION: "fs_session",
FINGERPRINT: "fs_fingerprint",
COUNTRY: "fs_country",
LOCALE: "fs_locale",
SERVER_SETTINGS: "fs_server_settings",
SETTINGS_OVERRIDE: "fs_settings_override",
ATTRIBUTION: "fs_attribution",
ATTRIBUTION_CACHE: "fs_attribution_cache"
};
// src/utils/environment.ts
var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
var getCookie = (name) => {
if (!isBrowser) return null;
try {
const cookies = document.cookie.split(";");
for (const cookieStr of cookies) {
const cookie = cookieStr.trim();
if (cookie.startsWith(name + "=")) {
return cookie.substring(name.length + 1);
}
}
return null;
} catch (error) {
console.error("Error reading cookie:", error);
return null;
}
};
var getLocalStorage = () => {
return {
/**
* Get an item from localStorage
* @param key The key to retrieve
* @returns The value or null if not found or an error occurred
*/
get: (key) => {
if (!isBrowser) return null;
try {
return localStorage.getItem(key);
} catch (error) {
console.error("Error accessing localStorage:", error);
return null;
}
},
/**
* Set an item in localStorage
* @param key The key to set
* @param value The value to store
*/
set: (key, value) => {
if (!isBrowser) return;
try {
localStorage.setItem(key, value);
} catch (error) {
console.error("Error writing to localStorage:", error);
}
},
/**
* Remove an item from localStorage
* @param key The key to remove
*/
remove: (key) => {
if (!isBrowser) return;
try {
localStorage.removeItem(key);
} catch (error) {
console.error("Error removing from localStorage:", error);
}
}
};
};
// src/StorageManager.ts
var StorageManager = class {
storage = getLocalStorage();
/**
* Set an item in storage
* @param key Storage key
* @param value Storage value
*/
setItem(key, value) {
this.storage.set(key, value);
}
/**
* Get an item from storage
* @param key Storage key
* @returns The stored value or null if not found
*/
getItem(key) {
return this.storage.get(key);
}
/**
* Remove an item from storage
* @param key Storage key
*/
removeItem(key) {
this.storage.remove(key);
}
/**
* Set an object in storage (serialized as JSON)
* @param key Storage key
* @param value Object to store
*/
setObject(key, value) {
this.storage.set(key, JSON.stringify(value));
}
/**
* Get an object from storage (deserialized from JSON)
* @param key Storage key
* @returns The stored object or null if not found or invalid
*/
getObject(key) {
const value = this.storage.get(key);
if (!value) return null;
try {
return JSON.parse(value);
} catch (error) {
console.error("Error parsing stored object:", error);
return null;
}
}
/**
* Gets country code from storage or cookies
* @returns The country code or null if not found
*/
getCountryCode() {
const storedCountry = this.getItem(STORAGE_KEYS.COUNTRY);
if (storedCountry) return storedCountry;
const cookieCountry = getCookie("country");
if (cookieCountry) {
this.setItem(STORAGE_KEYS.COUNTRY, cookieCountry);
return cookieCountry;
}
return null;
}
/**
* Sets the country code in storage
* @param countryCode The country code to store
*/
setCountryCode(countryCode) {
this.setItem(STORAGE_KEYS.COUNTRY, countryCode);
}
/**
* Gets locale from storage or cookies
* @returns The locale or null if not found
*/
getLocale() {
const storedLocale = this.getItem(STORAGE_KEYS.LOCALE);
if (storedLocale) return storedLocale;
const cookieLocale = getCookie("locale");
if (cookieLocale) {
this.setItem(STORAGE_KEYS.LOCALE, cookieLocale);
return cookieLocale;
}
return "EN";
}
/**
* Sets the locale in storage
* @param locale The locale to store
*/
setLocale(locale) {
this.setItem(STORAGE_KEYS.LOCALE, locale);
}
};
// src/index.ts
var COOKIE_KEYS = {
COUNTRY: "country",
LOCALE: "locale"
};
export { COOKIE_KEYS, STORAGE_KEYS, StorageManager, getCookie, isBrowser };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map