Updated to 3.0
This commit is contained in:
2
lib/src/webstorage.d.ts
vendored
2
lib/src/webstorage.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user