Updated to 3.0
This commit is contained in:
		@@ -1,12 +1,15 @@
 | 
			
		||||
import * as crypto from 'crypto-js';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Options to be used with WebStorage decorators
 | 
			
		||||
 * @category WebStorage
 | 
			
		||||
 */
 | 
			
		||||
export interface WebStorageOptions {
 | 
			
		||||
    /** Default value to provide if storage is empty */
 | 
			
		||||
    default?: any
 | 
			
		||||
    default?: any;
 | 
			
		||||
    /** Key to prevent plain text storage **/
 | 
			
		||||
    encryptWith?: string;
 | 
			
		||||
    /** Key to save under */
 | 
			
		||||
    key?: string
 | 
			
		||||
    key?: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -20,13 +23,12 @@ export interface WebStorageOptions {
 | 
			
		||||
 * }
 | 
			
		||||
 * ```
 | 
			
		||||
 *
 | 
			
		||||
 * @category WebStorage
 | 
			
		||||
 * @param defaultValue Default value to return if property does no exist inside localStorage.
 | 
			
		||||
 * @param opts Any additional options
 | 
			
		||||
 */
 | 
			
		||||
export function LocalStorage(defaultValue?: any, opts: WebStorageOptions = {}) {
 | 
			
		||||
    opts.default = defaultValue;
 | 
			
		||||
    return storage(localStorage, opts);
 | 
			
		||||
    return decoratorBuilder(localStorage, opts);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -40,13 +42,27 @@ export function LocalStorage(defaultValue?: any, opts: WebStorageOptions = {}) {
 | 
			
		||||
 * }
 | 
			
		||||
 * ```
 | 
			
		||||
 *
 | 
			
		||||
 * @category WebStorage
 | 
			
		||||
 * @param defaultValue Default value to return if property does no exist inside sessionStorage.
 | 
			
		||||
 * @param opts Any additional options
 | 
			
		||||
 */
 | 
			
		||||
export function SessionStorage(defaultValue?, opts: WebStorageOptions = {}) {
 | 
			
		||||
    opts.default = defaultValue;
 | 
			
		||||
    return storage(sessionStorage, opts);
 | 
			
		||||
    return decoratorBuilder(sessionStorage, opts);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * **Internal use only**
 | 
			
		||||
 *
 | 
			
		||||
 * Fetch variable from storage & take care of any defaults, object definitions, encryption & serialization
 | 
			
		||||
 *
 | 
			
		||||
 * @param storage Web Storage API
 | 
			
		||||
 * @param opts Any additional options
 | 
			
		||||
 */
 | 
			
		||||
function fromStorage(storage: Storage, opts: WebStorageOptions) {
 | 
			
		||||
    let storedVal = storage.getItem(<string>opts.key);
 | 
			
		||||
    if(storedVal == null) return opts.default != null ? opts.default : null;
 | 
			
		||||
    if(opts.encryptWith != null) storedVal = JSON.parse(crypto.AES.decrypt(JSON.parse(storedVal), opts.encryptWith).toString(crypto.enc.Utf8));
 | 
			
		||||
    return typeof storedVal == 'object' && !Array.isArray(storedVal) ? Object.assign(opts.default, storedVal) : storedVal;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -54,22 +70,22 @@ export function SessionStorage(defaultValue?, opts: WebStorageOptions = {}) {
 | 
			
		||||
 *
 | 
			
		||||
 * Overrides the properties getter/setter methods to read/write from the provided storage endpoint.
 | 
			
		||||
 *
 | 
			
		||||
 * @hidden
 | 
			
		||||
 * @category WebStorage
 | 
			
		||||
 * @param storage Web Storage API
 | 
			
		||||
 * @param opts Any additional options
 | 
			
		||||
 */
 | 
			
		||||
function storage(storage: Storage, opts: WebStorageOptions) {
 | 
			
		||||
function decoratorBuilder(storage: Storage, opts: WebStorageOptions) {
 | 
			
		||||
    return function(target: object, key: string) {
 | 
			
		||||
        if(!opts.key) opts.key = key;
 | 
			
		||||
        let field = fromStorage(storage, opts);
 | 
			
		||||
        Object.defineProperty(target, key, {
 | 
			
		||||
            get: function() {
 | 
			
		||||
                const storageVal = storage.getItem(<string>opts.key);
 | 
			
		||||
                if(storageVal == null || storageVal == 'null' || storageVal == 'undefined') return opts.default || null;
 | 
			
		||||
                return JSON.parse(storageVal);
 | 
			
		||||
                if(field != fromStorage(storage, opts)) target[key] = field;
 | 
			
		||||
                return field;
 | 
			
		||||
            },
 | 
			
		||||
            set: function(value) {
 | 
			
		||||
            set: function(value?) {
 | 
			
		||||
                field = value;
 | 
			
		||||
                if(value == null) storage.removeItem(<string>opts.key);
 | 
			
		||||
                if(opts.encryptWith != null) value = <any>crypto.AES.encrypt(JSON.stringify(value), opts.encryptWith).toString();
 | 
			
		||||
                storage.setItem(<string>opts.key, JSON.stringify(value));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user