Init
This commit is contained in:
commit
e21c8a23de
45
.gitignore
vendored
Normal file
45
.gitignore
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# compiled output
|
||||
/tmp
|
||||
/out-tsc
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
|
||||
# IDEs and editors
|
||||
/.idea
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
|
||||
# misc
|
||||
/.sass-cache
|
||||
/connect.lock
|
||||
/coverage
|
||||
/libpeerconnection.log
|
||||
npm-debug.log
|
||||
testem.log
|
||||
/typings
|
||||
|
||||
# e2e
|
||||
/e2e/*.js
|
||||
/e2e/*.map
|
||||
|
||||
# System Files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
/dist
|
||||
dist.tgz
|
||||
/.ng_pkg_build
|
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));
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
24
package-lock.json
generated
Normal file
24
package-lock.json
generated
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "WebStorage",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"@types/crypto-js": {
|
||||
"version": "3.1.39",
|
||||
"resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-3.1.39.tgz",
|
||||
"integrity": "sha512-LNPf60hAYUnjqeUwWxFrJwjPwOFIwQNy/xdVRQMWnsly38M23lLjhG3osdAJQ74jOplIUCjOhibpqOos0l0FHA=="
|
||||
},
|
||||
"crypto-js": {
|
||||
"version": "3.1.9-1",
|
||||
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-3.1.9-1.tgz",
|
||||
"integrity": "sha1-/aGedh/Ad+Af+/3G6f38WeiAbNg="
|
||||
},
|
||||
"typescript": {
|
||||
"version": "2.8.3",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-2.8.3.tgz",
|
||||
"integrity": "sha512-K7g15Bb6Ra4lKf7Iq2l/I5/En+hLIHmxWZGq3D4DIRNFxMNV6j2SHSvDOqs2tGd4UvD/fJvrwopzQXjLrT7Itw==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
}
|
28
package.json
Normal file
28
package.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "webstorage-decorators",
|
||||
"version": "1.0.0",
|
||||
"description": "Decorators to sync variable to Local/Session storage",
|
||||
"main": "./lib/index.js",
|
||||
"typings": "./lib/index.d.ts",
|
||||
"scripts": {
|
||||
"prepare": "npm run build",
|
||||
"build": "tsc"
|
||||
},
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"keywords": [
|
||||
"LocalStorage",
|
||||
"SessionStorage",
|
||||
"WebStorage"
|
||||
],
|
||||
"author": "Zak Timson",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@types/crypto-js": "^3.1.39",
|
||||
"crypto-js": "^3.1.9-1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^2.8.3"
|
||||
}
|
||||
}
|
1
src/index.ts
Normal file
1
src/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './webStorage';
|
34
src/webStorage.ts
Normal file
34
src/webStorage.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import {AES, enc} from 'crypto-js';
|
||||
|
||||
export interface WebStorageOptions {
|
||||
fieldName?: string;
|
||||
encryptionKey?: string;
|
||||
defaultValue?: any;
|
||||
}
|
||||
|
||||
export function LocalStorage(opts: WebStorageOptions = {}) {
|
||||
return storage(localStorage, opts);
|
||||
}
|
||||
|
||||
export function SessionStorage(opts: WebStorageOptions = {}) {
|
||||
return storage(sessionStorage, opts);
|
||||
}
|
||||
|
||||
function storage(storageType: Storage, opts: WebStorageOptions = {}) {
|
||||
return function(target: object, key: string) {
|
||||
if(!opts.fieldName) opts.fieldName = key;
|
||||
|
||||
Object.defineProperty(target, key, {
|
||||
get: function() {
|
||||
let value = storageType.getItem(<string>opts.fieldName);
|
||||
if(!value && opts.defaultValue != null) return opts.defaultValue;
|
||||
if(value != null && opts.encryptionKey) value = AES.decrypt(JSON.parse(value), opts.encryptionKey).toString(enc.Utf8);
|
||||
return JSON.parse(<string>value);
|
||||
},
|
||||
set: function(value) {
|
||||
if(value != null && opts.encryptionKey) value = AES.encrypt(JSON.stringify(value), opts.encryptionKey).toString();
|
||||
storageType.setItem(<string>opts.fieldName, JSON.stringify(value));
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
13
tsconfig.json
Normal file
13
tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2015",
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"outDir": "lib",
|
||||
"strict": true
|
||||
},
|
||||
"include": [
|
||||
"index.ts",
|
||||
"src/**/*"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user