Updated to 2.0
This commit is contained in:
15
lib/index.js
15
lib/index.js
@ -1,6 +1,13 @@
|
||||
"use strict";
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__export(require("./src/index"));
|
||||
__exportStar(require("./src/index"), exports);
|
||||
|
2
lib/src/index.d.ts
vendored
2
lib/src/index.d.ts
vendored
@ -1 +1 @@
|
||||
export * from './webStorage';
|
||||
export * from './webstorage';
|
||||
|
@ -1,6 +1,13 @@
|
||||
"use strict";
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__export(require("./webStorage"));
|
||||
__exportStar(require("./webstorage"), exports);
|
||||
|
7
lib/src/webStorage.d.ts
vendored
7
lib/src/webStorage.d.ts
vendored
@ -1,7 +0,0 @@
|
||||
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;
|
@ -1,32 +0,0 @@
|
||||
"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));
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
42
lib/src/webstorage.d.ts
vendored
Normal file
42
lib/src/webstorage.d.ts
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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 declare function LocalStorage(defaultValue?: any, opts?: WebStorageOptions): (target: object, key: string) => void;
|
||||
/**
|
||||
* 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 declare function SessionStorage(defaultValue?: any, opts?: WebStorageOptions): (target: object, key: string) => void;
|
72
lib/src/webstorage.js
Normal file
72
lib/src/webstorage.js
Normal file
@ -0,0 +1,72 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.SessionStorage = exports.LocalStorage = void 0;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function LocalStorage(defaultValue, opts = {}) {
|
||||
opts.default = defaultValue;
|
||||
return storage(localStorage, opts);
|
||||
}
|
||||
exports.LocalStorage = LocalStorage;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function SessionStorage(defaultValue, opts = {}) {
|
||||
opts.default = defaultValue;
|
||||
return storage(sessionStorage, opts);
|
||||
}
|
||||
exports.SessionStorage = SessionStorage;
|
||||
/**
|
||||
* **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, opts) {
|
||||
return function (target, key) {
|
||||
if (!opts.key)
|
||||
opts.key = key;
|
||||
Object.defineProperty(target, key, {
|
||||
get: function () {
|
||||
const storageVal = storage.getItem(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(opts.key);
|
||||
storage.setItem(opts.key, JSON.stringify(value));
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user