@empathyco/x-storage-service
Version:
Storage service with TTL
120 lines • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrowserStorageService = void 0;
var tslib_1 = require("tslib");
/**
* In browser implementation of the storage service.
*
* @public
*/
var BrowserStorageService = /** @class */ (function () {
function BrowserStorageService(storage, prefix) {
if (storage === void 0) { storage = localStorage; }
if (prefix === void 0) { prefix = 'empathy'; }
this.storage = storage;
this.prefix = prefix;
}
/**
* Adds a new item in the browser storage.
*
* @param key - The key of the item.
* @param item - The item to save.
* @param ttlInMs - The TTL in ms of the item in the browser storage.
*
* @public
*/
BrowserStorageService.prototype.setItem = function (key, item, ttlInMs) {
if (item === undefined) {
console.warn("[StorageService][".concat(this.prefix, "] Tried to store an undefined object with key ").concat(key));
}
else {
var prefixedKey = this.prefixKey(key);
var expirableItem = this.createExpirableItem(item, ttlInMs);
var serializedItem = JSON.stringify(expirableItem);
this.storage.setItem(prefixedKey, serializedItem);
}
};
/**
* Retrieves an item by its key.
*
* @param key - The key of the item.
* @returns The founded item or null.
*
* @public
*/
BrowserStorageService.prototype.getItem = function (key) {
this.removeExpiredItems();
var prefixedKey = this.prefixKey(key);
var serializedItem = this.storage.getItem(prefixedKey);
if (serializedItem) {
var item = JSON.parse(serializedItem);
return this.getItemValue(item);
}
return null;
};
/**
* Removes an item by its key.
*
* @param key - The key of the item.
* @returns The removed item or null.
*
* @public
*/
BrowserStorageService.prototype.removeItem = function (key) {
var item = this.getItem(key);
var prefixedKey = this.prefixKey(key);
this.storage.removeItem(prefixedKey);
return item;
};
/**
* Clears the storage..
*
* @returns The number of removed items.
*
* @public
*/
BrowserStorageService.prototype.clear = function () {
var _this = this;
return this.getOwnKeys().reduce(function (removedCount, key) {
_this.storage.removeItem(key);
return ++removedCount;
}, 0);
};
BrowserStorageService.prototype.prefixKey = function (key) {
return "".concat(this.prefix, "-").concat(key);
};
BrowserStorageService.prototype.createExpirableItem = function (item, ttlInMs) {
return tslib_1.__assign(tslib_1.__assign({}, (!!ttlInMs && { ttl: ttlInMs + this.currentTimestamp() })), { value: item });
};
BrowserStorageService.prototype.currentTimestamp = function () {
return Date.now();
};
BrowserStorageService.prototype.getItemValue = function (item) {
return item.value;
};
BrowserStorageService.prototype.getOwnKeys = function () {
var _this = this;
return Object.keys(this.storage).filter(function (key) { return key.startsWith("".concat(_this.prefix, "-")); });
};
BrowserStorageService.prototype.removeExpiredItems = function () {
var _this = this;
this.getOwnKeys().forEach(function (key) {
var serializedItem = _this.storage.getItem(key);
if (serializedItem) {
try {
var item = JSON.parse(serializedItem);
if (item.ttl && item.ttl <= _this.currentTimestamp()) {
_this.storage.removeItem(key);
}
}
catch (_a) {
console.warn("[StorageService][".concat(_this.prefix, "] Item for key ").concat(key, " has been removed from storage because it had an invalid JSON value: \"").concat(serializedItem, "\""));
_this.storage.removeItem(key);
}
}
});
};
return BrowserStorageService;
}());
exports.BrowserStorageService = BrowserStorageService;
//# sourceMappingURL=browser-storage-service.js.map