Updated to 3.0

This commit is contained in:
2021-01-28 13:15:46 -05:00
parent 311a4311ac
commit 954049d992
6 changed files with 128 additions and 49 deletions

View File

@ -5,6 +5,8 @@
export interface WebStorageOptions {
/** Default value to provide if storage is empty */
default?: any;
/** Key to prevent plain text storage **/
encryptWith?: string;
/** Key to save under */
key?: string;
}

View File

@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionStorage = exports.LocalStorage = void 0;
const crypto = require("crypto-js");
/**
* Automatically syncs localStorage with the decorated property.
*
@ -57,14 +58,18 @@ function storage(storage, opts) {
opts.key = key;
Object.defineProperty(target, key, {
get: function () {
const storageVal = storage.getItem(opts.key);
let storageVal = storage.getItem(opts.key);
if (storageVal == null || storageVal == 'null' || storageVal == 'undefined')
return opts.default || null;
if (opts.encryptWith != null)
storageVal = crypto.AES.decrypt(JSON.parse(storageVal), opts.encryptWith).toString(crypto.enc.Utf8);
return JSON.parse(storageVal);
},
set: function (value) {
if (value == null)
storage.removeItem(opts.key);
if (opts.encryptWith != null)
value = crypto.AES.encrypt(JSON.stringify(value), opts.encryptWith).toString();
storage.setItem(opts.key, JSON.stringify(value));
}
});