Updated to 2.0
This commit is contained in:
		@@ -1 +1 @@
 | 
			
		||||
export * from './webStorage';
 | 
			
		||||
export * from './webstorage';
 | 
			
		||||
 
 | 
			
		||||
@@ -1,80 +0,0 @@
 | 
			
		||||
import {LocalStorage, SessionStorage} from './index'
 | 
			
		||||
 | 
			
		||||
class TestClass {
 | 
			
		||||
    @LocalStorage() localStorage: any;
 | 
			
		||||
    @SessionStorage() sessionStorage: any;
 | 
			
		||||
    @LocalStorage({fieldName: 'customKey'}) customKey: any;
 | 
			
		||||
    @LocalStorage({fieldName: 'encrypted', encryptionKey: 'ENCRYPTION_KEY'}) encrypted: any;
 | 
			
		||||
    @LocalStorage({defaultValue: 'test'}) defaultedStorage: any;
 | 
			
		||||
    @LocalStorage({defaultValue: {a: true, b: 'test', c: 3.14}}) objectDefault: any;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
describe('LocalStorage Tests', () => {
 | 
			
		||||
    it('LocalStorage', () => {
 | 
			
		||||
        let testValue = Math.random().toString(36).substring(7);
 | 
			
		||||
        new TestClass().localStorage = testValue;
 | 
			
		||||
        expect(JSON.parse(<string>localStorage.getItem('_localStorage'))).toEqual(testValue);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('SessionStorage', () => {
 | 
			
		||||
        let testValue = Math.random().toString(36).substring(7);
 | 
			
		||||
        new TestClass().sessionStorage = testValue;
 | 
			
		||||
        expect(JSON.parse(<string>sessionStorage.getItem('_sessionStorage'))).toEqual(testValue);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('Custom Key', () => {
 | 
			
		||||
        let testValue = Math.random().toString(36).substring(7);
 | 
			
		||||
        new TestClass().customKey = testValue;
 | 
			
		||||
        expect(JSON.parse(<string>localStorage.getItem('customKey'))).toEqual(testValue);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('Maintain Object Structure', () => {
 | 
			
		||||
        let testObject = new TestClass();
 | 
			
		||||
        let testValue = {a: Math.random().toString(36).substring(7), b: Math.random(), c: true, d: [{a: false}, {a: true}]};
 | 
			
		||||
        testObject.localStorage = testValue;
 | 
			
		||||
        expect(testObject.localStorage).toEqual(testValue);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    describe('Default', () => {
 | 
			
		||||
        it('Default Value', () => {
 | 
			
		||||
            localStorage.removeItem('_defaultedStorage');
 | 
			
		||||
            let testObject = new TestClass();
 | 
			
		||||
            expect(testObject.defaultedStorage).toEqual('test');
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        it('Key Already Has Value', () => {
 | 
			
		||||
            let testValue = {a: Math.random().toString(36).substring(7), b: Math.random(), c: true, d: [{a: false}, {a: true}]};
 | 
			
		||||
            localStorage.setItem('_defaultedStorage', JSON.stringify(testValue));
 | 
			
		||||
            let testObject = new TestClass();
 | 
			
		||||
            expect(testObject.defaultedStorage).toEqual(testValue);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        it('Using Object As Default', () => {
 | 
			
		||||
            let testObject = new TestClass();
 | 
			
		||||
            expect(testObject.objectDefault).toEqual({a: true, b: 'test', c: 3.14});
 | 
			
		||||
        })
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    describe('Encryption Tests', () => {
 | 
			
		||||
        it('Encrypt', () => {
 | 
			
		||||
            let testObject = new TestClass();
 | 
			
		||||
            let testValue = Math.random().toString(36).substring(7);
 | 
			
		||||
            testObject.encrypted = testValue;
 | 
			
		||||
            expect(JSON.parse(<string>localStorage.getItem('encrypted'))).not.toEqual(testValue);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        it('Decrypt', () => {
 | 
			
		||||
            let testObject = new TestClass();
 | 
			
		||||
            let testValue = Math.random().toString(36).substring(7);
 | 
			
		||||
            testObject.encrypted = testValue;
 | 
			
		||||
            expect(testObject.encrypted).toEqual(testValue);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        it('Maintain Object Structure', () => {
 | 
			
		||||
            let testObject = new TestClass();
 | 
			
		||||
            let testValue = {a: Math.random().toString(36).substring(7), b: Math.random(), c: true, d: [{a: false}, {a: true}]};
 | 
			
		||||
            testObject.encrypted = testValue;
 | 
			
		||||
            expect(testObject.encrypted).toEqual(testValue);
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
@@ -1,36 +0,0 @@
 | 
			
		||||
import {AES, enc} from 'crypto-js';
 | 
			
		||||
 | 
			
		||||
export interface WebStorageOptions {
 | 
			
		||||
    fieldName?: string;
 | 
			
		||||
    encryptionKey?: string;
 | 
			
		||||
    defaultValue?: any;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function LocalStorage(opts: WebStorageOptions) {
 | 
			
		||||
    if(!opts) opts = {};
 | 
			
		||||
    return storage(localStorage, opts);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function SessionStorage(opts: WebStorageOptions) {
 | 
			
		||||
    if(!opts) opts = {};
 | 
			
		||||
    return storage(sessionStorage, opts);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function storage(storageType: Storage, opts: WebStorageOptions) {
 | 
			
		||||
    return function(target: object, key: string) {
 | 
			
		||||
        if(!opts.fieldName) opts.fieldName = key;
 | 
			
		||||
 | 
			
		||||
        Object.defineProperty(target, key, {
 | 
			
		||||
            get: function() {
 | 
			
		||||
                let value = storageType.getItem(<string>opts.fieldName);
 | 
			
		||||
                if(!value && opts.defaultValue != null) return opts.defaultValue;
 | 
			
		||||
                if(value != null && opts.encryptionKey) value = AES.decrypt(JSON.parse(value), opts.encryptionKey).toString(enc.Utf8);
 | 
			
		||||
                return JSON.parse(<string>value);
 | 
			
		||||
            },
 | 
			
		||||
            set: function(value) {
 | 
			
		||||
                if(value != null && opts.encryptionKey) value = AES.encrypt(JSON.stringify(value), opts.encryptionKey).toString();
 | 
			
		||||
                storageType.setItem(<string>opts.fieldName, JSON.stringify(value));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										77
									
								
								src/webstorage.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/webstorage.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,77 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Options to be used with WebStorage decorators
 | 
			
		||||
 * @category WebStorage
 | 
			
		||||
 */
 | 
			
		||||
export interface WebStorageOptions {
 | 
			
		||||
    /** Default value to provide if storage is empty */
 | 
			
		||||
    default?: any
 | 
			
		||||
    /** Key to save under */
 | 
			
		||||
    key?: string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Automatically syncs localStorage with the decorated property.
 | 
			
		||||
 *
 | 
			
		||||
 * **Example**
 | 
			
		||||
 * ```
 | 
			
		||||
 * class Example {
 | 
			
		||||
 *     @LocalStorage() lastLogin: string;
 | 
			
		||||
 *     @LocalStorage(false, {key: '_hideMenu'}) hideMenu: boolean;
 | 
			
		||||
 * }
 | 
			
		||||
 * ```
 | 
			
		||||
 *
 | 
			
		||||
 * @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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Automatically syncs sessionStorage with the decorated property.
 | 
			
		||||
 *
 | 
			
		||||
 * **Example**
 | 
			
		||||
 * ```
 | 
			
		||||
 * class Example {
 | 
			
		||||
 *     @SessionStorage() lastLogin: string;
 | 
			
		||||
 *     @SessionStorage(false, {key: '_hideMenu'}) hideMenu: boolean;
 | 
			
		||||
 * }
 | 
			
		||||
 * ```
 | 
			
		||||
 *
 | 
			
		||||
 * @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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * **Internal use only**
 | 
			
		||||
 *
 | 
			
		||||
 * 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) {
 | 
			
		||||
    return function(target: object, key: string) {
 | 
			
		||||
        if(!opts.key) opts.key = key;
 | 
			
		||||
        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);
 | 
			
		||||
            },
 | 
			
		||||
            set: function(value) {
 | 
			
		||||
                if(value == null) storage.removeItem(<string>opts.key);
 | 
			
		||||
                storage.setItem(<string>opts.key, JSON.stringify(value));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user