Init
This commit is contained in:
1
lib/index.d.ts
vendored
Normal file
1
lib/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export * from './src/index';
|
6
lib/index.js
Normal file
6
lib/index.js
Normal 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
1
lib/src/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export * from './webStorage';
|
6
lib/src/index.js
Normal file
6
lib/src/index.js
Normal 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
7
lib/src/webStorage.d.ts
vendored
Normal 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
32
lib/src/webStorage.js
Normal 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));
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user