@xompass/sdk-cloud-api
Version:
Xompass Client for cloud-api
62 lines • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrowserStorage = void 0;
var BrowserStorage = /** @class */ (function () {
function BrowserStorage() {
}
/**
* @method get
* @param key Storage key name
* @description
* The getter will return any type of data persisted in localStorage.
*/
BrowserStorage.prototype.get = function (key) {
var data = BrowserStorage.parse(localStorage.getItem(key));
if (!data || data.e && new Date(data.e) < new Date()) {
return null;
}
return data.v;
};
/**
* @method set
* @param key Storage key name
* @param value Any value
* @param [expires] The date of expiration (Optional)
* @description
* The setter will return any type of data persisted in localStorage.
*/
BrowserStorage.prototype.set = function (key, value, expires) {
var data = { v: value, e: expires ? expires.getTime() : undefined };
localStorage.setItem(key, JSON.stringify(data));
};
/**
* @method remove
* @param key Storage key name
* @description
* This method will remove a localStorage item from the client.
*/
BrowserStorage.prototype.remove = function (key) {
localStorage.removeItem(key);
};
/**
* @method parse
* @param value Input data expected to be JSON
* @description
* This method will parse the string as JSON if possible, otherwise will
* return the value itself.
*/
BrowserStorage.parse = function (value) {
if (!value) {
return value;
}
try {
return JSON.parse(value);
}
catch (e) {
return value;
}
};
return BrowserStorage;
}());
exports.BrowserStorage = BrowserStorage;
//# sourceMappingURL=BrowserStorage.js.map