UNPKG

browser-storage-utilities

Version:

A front-end library that provides utility methods to facilitate CRUD operations to data stored in the browser, and more.

190 lines 8.74 kB
Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const rxjs_1 = require("rxjs"); const storage_utilities_base_1 = require("./storage-utilities-base"); /** * Provides Core functionality for StorageUtilities, * including getItem(), addItem(), removeItem(), removeItems(), * as well as additional functionality. */ class StorageUtilities { constructor(settings) { StorageUtilities._settings = Object.assign(Object.assign({}, storage_utilities_base_1.default.defaultSettings), settings); if (settings) { storage_utilities_base_1.default.setCustomSettings(StorageUtilities._settings); } if (StorageUtilities._settings.notifiedOfStateChanges) { this.storageStateChanged = this.getStorageState(); } } /** * Get default settings applied to StorageUtilities. * @returns default settings */ static get defaultSettings() { return storage_utilities_base_1.default.defaultSettings; } /** * Get current settings applied to StorageUtilities. * These can be either the default, custom settings, or both. * @returns currently applied settings */ static get currentSettings() { return this._settings; } /** * Set custom settings to be applied to StorageUtilities, * they always overwrite default settings. * @param settings the custom settings to be applied to StorageUtilities */ static set customSettings(settings) { if (!settings || _.isEmpty(settings)) { throw new Error(`The settings ${settings} are invalid!`); } StorageUtilities._settings = settings; storage_utilities_base_1.default.setCustomSettings(settings); } /** * Reset to default settings applied to StorageUtilities. */ static resetSettings() { StorageUtilities._settings = storage_utilities_base_1.defaultSettings; storage_utilities_base_1.default.resetSettings(); } /** * Returns the item stored in Storage by its key. * If item is not found, or it has an expiry time which has elapsed, null will be returned. * The item can be returned as a Promise or Observable if @param returnType is specified. * The item is returned from localStorage by default, or sessionStorage as specified by @param storageType. * @param key the item's key * @param storageType (optional) localStorage (default) / sessionStorage * @param returnType (optional) Promise / Observable * @returns the stored item, or null if item is not found or has expired */ getItem(key, storageType, returnType) { return this._getDataBasedOnReturnType(storage_utilities_base_1.default.getItem(key, storageType), returnType); } /** * Adds the item to Storage with its key. * The item is added to localStorage by default, or sessionStorage if specified by @param storageType. * The item can be added with an expiry time (in milliseconds). * This is to add a TTL (Time to live) to invalidate item after the expiry time elapses. * @param key the item's key * @param item the item to be added to Storage * @param expiry (optional) the expiry time (in milliseconds) * @param storageType (optional) localStorage (default) / sessionStorage */ addItem(key, item, expiry, storageType) { storage_utilities_base_1.default.addItem(key, item, expiry, storageType); } /** * Updates the value of a specific property for the stored item. * The property can be of any type. If the property doesn't exist, a new property will be created. * If the item is not found by key, or has expired, null will be returned, or updated item otherwise. * @param key the item's key * @param propName the item's property to be updated * @param newValue the item's property's new value * @param storageType (optional) localStorage (default) / sessionStorage * @returns the updated item, or null if item is not found or has expired */ updateItemProperty(key, propName, newValue, storageType) { return storage_utilities_base_1.default.updateItemProp(key, propName, newValue, storageType); } /** * Removes the item's specified property. * If the item is not found by key, or has expired, null will be returned, or updated item otherwise. * @param key the item's key * @param propName the item's property to be removed * @param storageType (optional) localStorage (default) / sessionStorage * @returns the updated item, or null if item is not found or has expired */ removeItemProperty(key, propName, storageType) { return storage_utilities_base_1.default.removeItemProp(key, propName, storageType); } /** * Removes item from Storage by its key. * The item is removed from localStorage by default, or sessionStorage if specified by @param storageType. * @param key the item's key * @param storageType (optional) localStorage (default) / sessionStorage */ removeItem(key, storageType) { storage_utilities_base_1.default.removeItem(key, storageType); } /** * Removes items from Storage by their keys. * The items are removed from localStorage by default, or sessionStorage if specified by @param storageType. * @param keys the items' keys * @param storageType (optional) localStorage (default) / sessionStorage */ removeItems(keys, storageType) { storage_utilities_base_1.default.removeItems(keys, storageType); } /** * Returns storage state as an Observable of type IStorageNotifier. * All subscribers will be notified when state changes. * @returns storage state of Observable type */ getStorageState() { return storage_utilities_base_1.default.getStateObservable(); } /** * Returns an Array of all storage items. * The items are returned from localStorage by default, or sessionStorage if specified by @param storageType. * The items can be returned as a Promise or Observable if @param returnType is specified. * @param storageType (optional) localStorage (default) / sessionStorage * @param returnType (optional) Promise or Observable * @returns an Array of all storage items */ getStorageItems(storageType, returnType) { return this._getDataBasedOnReturnType(storage_utilities_base_1.default.getItems(storageType), returnType); } /** * Returns an Array of all storage values. * The values are returned from localStorage by default, or sessionStorage if specified by @param storageType. * The values can be returned as a Promise or Observable if @param returnType is specified. * @param storageType (optional) localStorage (default) / sessionStorage * @param returnType (optional) Promise or Observable * @returns an Array of all storage values */ getStorageValues(storageType, returnType) { return this._getDataBasedOnReturnType(storage_utilities_base_1.default.getValues(storageType), returnType); } /** * Returns an Array of all storage keys. * The keys are returned from localStorage by default, or sessionStorage if specified by @param storageType. * The keys can be returned as a Promise or Observable if @param returnType is specified. * @param storageType (optional) localStorage (default) / sessionStorage * @param returnType (optional) Promise or Observable * @returns an Array of all storage keys */ getStorageKeys(storageType, returnType) { return this._getDataBasedOnReturnType(storage_utilities_base_1.default.getKeys(storageType), returnType); } /** * Removes all items from storage. * The items are removed from localStorage by default, or sessionStorage if specified by @param storageType. * @param storageType (optional) localStorage (default) / sessionStorage */ clearStorage(storageType) { storage_utilities_base_1.default.clearAll(storageType); } _getDataBasedOnReturnType(data, returnType) { const type = returnType || StorageUtilities._settings.setReturnType; if (type) { if (type === 'promise') { return Promise.resolve(data); } else if (type === 'observable') { return rxjs_1.of(data); } else { throw new Error(`Return type ${type} is not supported!`); } } else { return data; } } } exports.StorageUtilities = StorageUtilities; //# sourceMappingURL=storage-utilities.js.map