This commit is contained in:
2018-05-03 15:07:59 -04:00
commit e21c8a23de
14 changed files with 200 additions and 0 deletions

1
lib/index.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from './src/index';

6
lib/index.js Normal file
View File

@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./src/index"));

1
lib/src/index.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from './webStorage';

6
lib/src/index.js Normal file
View File

@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./webStorage"));

7
lib/src/webStorage.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
export interface WebStorageOptions {
fieldName?: string;
encryptionKey?: string;
defaultValue?: any;
}
export declare function LocalStorage(opts?: WebStorageOptions): (target: object, key: string) => void;
export declare function SessionStorage(opts?: WebStorageOptions): (target: object, key: string) => void;

32
lib/src/webStorage.js Normal file
View File

@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_js_1 = require("crypto-js");
function LocalStorage(opts = {}) {
return storage(localStorage, opts);
}
exports.LocalStorage = LocalStorage;
function SessionStorage(opts = {}) {
return storage(sessionStorage, opts);
}
exports.SessionStorage = SessionStorage;
function storage(storageType, opts = {}) {
return function (target, key) {
if (!opts.fieldName)
opts.fieldName = key;
Object.defineProperty(target, key, {
get: function () {
let value = storageType.getItem(opts.fieldName);
if (!value && opts.defaultValue != null)
return opts.defaultValue;
if (value != null && opts.encryptionKey)
value = crypto_js_1.AES.decrypt(JSON.parse(value), opts.encryptionKey).toString(crypto_js_1.enc.Utf8);
return JSON.parse(value);
},
set: function (value) {
if (value != null && opts.encryptionKey)
value = crypto_js_1.AES.encrypt(JSON.stringify(value), opts.encryptionKey).toString();
storageType.setItem(opts.fieldName, JSON.stringify(value));
}
});
};
}