Skip to content

Commit 9a9eef8

Browse files
authored
Merge pull request #26 from nfhipona/release/4.1.0
Generate typescript *.d.ts files
2 parents c8d4522 + 0d24068 commit 9a9eef8

File tree

9 files changed

+216
-7
lines changed

9 files changed

+216
-7
lines changed

lib/class/cryptor.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { CryptorOption, KeyOption, ReturnOption, CryptorModel, VectorIV } from './interface';
2+
export declare const CryptorDefaults: CryptorOption;
3+
export declare class Cryptor implements CryptorModel {
4+
#private;
5+
constructor(options?: CryptorOption, ivHex?: VectorIV);
6+
get settings(): CryptorOption;
7+
get key(): KeyOption;
8+
get ivHex(): KeyOption;
9+
encrypt(subject: string): ReturnOption;
10+
decrypt(encrypted: string): ReturnOption;
11+
}

lib/class/encoded-storage.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { KeyPath, StorageValue, Storage } from './interface';
2+
import { WebStore } from "./storage";
3+
export declare class EncodedWebStore extends WebStore {
4+
#private;
5+
/**
6+
*
7+
* @param storage Storage interface to be used and initialized.
8+
*/
9+
constructor(storage: Storage, delimiter?: string);
10+
getItem(key: KeyPath): StorageValue;
11+
setItem(key: KeyPath, value: StorageValue): boolean | Error;
12+
}

lib/class/encrypted-storage.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { KeyPath, StorageValue, Storage, EncryptedWebStorage } from './interface';
2+
import { WebStore } from "./storage";
3+
import { Cryptor } from "./cryptor";
4+
export declare class EncryptedWebStore extends WebStore implements EncryptedWebStorage {
5+
#private;
6+
/**
7+
*
8+
* @param storage Storage interface to be used and initialized.
9+
*/
10+
constructor(storage: Storage, cryptor: Cryptor, delimiter?: string);
11+
getItem(key: KeyPath): StorageValue;
12+
setItem(key: KeyPath, value: StorageValue): boolean | Error;
13+
getEncryptedRawItem(key: string): any;
14+
setEncryptedRawItem(key: string, value: any): boolean | Error;
15+
}

lib/class/interface.d.ts

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/// <reference types="node" />
2+
export type KeyPath = string;
3+
export type StorageValue = any;
4+
export type StorageItem = {
5+
key: KeyPath;
6+
value: StorageValue;
7+
};
8+
/**
9+
* Attribute compare will work for a collection of items where values match or will replace a value of the matched key for data objects.
10+
*/
11+
export type AttributeCompare = {
12+
name: string;
13+
value: string | number;
14+
};
15+
export interface Storage {
16+
/**
17+
* keypath delimeter. defaults to '.'.
18+
*/
19+
delimiter: string;
20+
/**
21+
* Returns an integer representing the number of data items stored in the Storage object.
22+
*/
23+
get length(): number;
24+
/**
25+
* Returns an integer representing the number of data items stored in the Storage object.
26+
* @param n When passed a number n, this method will return the name of the nth key in the storage.
27+
*/
28+
key(n: number): number;
29+
/**
30+
* When passed a key name, will return that key's value.
31+
* @param {KeyPath} key key name.
32+
*/
33+
getItem(key: KeyPath): StorageValue;
34+
/**
35+
* When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.
36+
* @param {KeyPath} key A string containing the name of the key you want to create/update.
37+
* @param {StorageValue} value A string containing the value you want to give the key you are creating/updating.
38+
*/
39+
setItem(key: KeyPath, value: StorageValue): boolean | Error;
40+
/**
41+
* When passed a key name, will remove that key from the storage.
42+
* @param {KeyPath} key A string containing the name of the key you want to remove.
43+
*/
44+
removeItem(key: KeyPath): void;
45+
/**
46+
* When invoked, will empty all keys out of the storage.
47+
*/
48+
clear(): void;
49+
}
50+
export interface WebStorage extends Storage {
51+
/**
52+
* Add multiple entries of key value pairs to the storage.
53+
* @param {StorageItem[]} items Items to add individually in the storage.
54+
*/
55+
setMultipleItems(items: StorageItem[]): boolean | Error;
56+
/**
57+
* Remove multiple entries found in the specified keypaths.
58+
* Will only work on top level keypaths and will not utilize an `AttributeCompare`.
59+
* Use `removeItemInItem` to utilize an `AttributeCompare`.
60+
* @param {KeyPath[]} keys
61+
*/
62+
removeMultipleItems(keys: KeyPath[]): void;
63+
/**
64+
* Returns multiple entries found in the specified keypaths.
65+
* Will only work on top level keypaths and will not utilize an `AttributeCompare`.
66+
* Use `getItemInItem` to utilize an `AttributeCompare`.
67+
* @param {KeyPath[]} keys
68+
*/
69+
getMultipleItems(keys: KeyPath[]): StorageValue[];
70+
/**
71+
* Append item to an existing item on the storage. Works for object and array type data.
72+
* @param {KeyPath} key keypath of the data you want to append to.
73+
* @param {StorageValue} value data value you want to append to.
74+
*/
75+
appendItemInItem(key: KeyPath, value: StorageValue): boolean | Error;
76+
/**
77+
* Updates an item in the specified keypath.
78+
* @param {KeyPath} key keypath of the data.
79+
* @param {AttributeCompare} attrCompare data key attribute to be updated.
80+
*/
81+
updateItemInItem(key: KeyPath, attrCompare: AttributeCompare | null, newValue: StorageValue): boolean | Error;
82+
/**
83+
* Removes an item in the specified keypath.
84+
* @param {KeyPath} key keypath of the data.
85+
* @param {AttributeCompare} attrCompare data key attribute to be updated.
86+
*/
87+
removeItemInItem(key: KeyPath, attrCompare?: AttributeCompare): boolean | Error;
88+
/**
89+
* Returns data found in the specified keypath.
90+
* @param {KeyPath} key keypath of the data.
91+
* @param {AttributeCompare} attrCompare data key attribute to be updated.
92+
*/
93+
getItemInItem(key: KeyPath, attrCompare?: AttributeCompare): StorageValue;
94+
}
95+
export interface EncryptedWebStorage extends Storage {
96+
/**
97+
* When passed a key name, will return that key's value.
98+
* @param {KeyPath} key key name.
99+
*/
100+
getEncryptedRawItem(key: KeyPath): StorageValue;
101+
/**
102+
* When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.
103+
* @param {KeyPath} key A string containing the name of the key you want to create/update.
104+
* @param {StorageValue} value A string containing the value you want to give the key you are creating/updating.
105+
*/
106+
setEncryptedRawItem(key: KeyPath, value: StorageValue): boolean | Error;
107+
}
108+
/**
109+
* Cryptor interface
110+
*/
111+
export interface CryptorOption {
112+
salt: string | Buffer;
113+
keyLength: number;
114+
algorithm: string;
115+
password: string | Buffer;
116+
byteLength: number;
117+
}
118+
export type KeyOption = string | null;
119+
export type ReturnOption = string | null;
120+
export type VectorIV = string | null;
121+
export interface CryptorModel {
122+
/**
123+
* Returns the cryptor's configs
124+
*/
125+
get settings(): CryptorOption;
126+
/**
127+
* Returns the cryptor's generated encryption key
128+
*/
129+
get key(): KeyOption;
130+
/**
131+
* Returns the cryptor's initialization vector.
132+
* NOTE: This is important for decryption. Make sure you store it somewhere for reuse.
133+
*/
134+
get ivHex(): KeyOption;
135+
/**
136+
* Encrypt the data and save to storage.
137+
*/
138+
encrypt(subject: string): ReturnOption;
139+
/**
140+
* Returns the decrypted data from storage.
141+
*/
142+
decrypt(encrypted: string): ReturnOption;
143+
}

lib/class/storage.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { AttributeCompare, KeyPath, Storage, StorageItem, StorageValue, WebStorage } from './interface';
2+
export declare class WebStore implements WebStorage {
3+
#private;
4+
delimiter: string;
5+
/**
6+
*
7+
* @param storage Storage interface to be used and initialized.
8+
*/
9+
constructor(storage: Storage, delimiter?: string);
10+
get length(): number;
11+
key(n: number): number;
12+
getItem(key: KeyPath): StorageValue;
13+
setItem(key: KeyPath, value: StorageValue): boolean | Error;
14+
removeItem(key: KeyPath): void;
15+
clear(): void;
16+
setMultipleItems(items: StorageItem[]): boolean | Error;
17+
removeMultipleItems(keys: KeyPath[]): void;
18+
getMultipleItems(keys: KeyPath[]): StorageValue[];
19+
appendItemInItem(key: KeyPath, value: any): boolean | Error;
20+
updateItemInItem(key: KeyPath, attrCompare: AttributeCompare | null, newValue: StorageValue): boolean | Error;
21+
removeItemInItem(key: KeyPath, attrCompare?: AttributeCompare): boolean | Error;
22+
getItemInItem(key: KeyPath, attrCompare?: AttributeCompare): StorageValue;
23+
}

lib/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Options {
2+
delimiter?: string;
3+
isEncoded: boolean;
4+
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
{
22
"name": "web-storage-manager",
3-
"version": "4.0.4",
3+
"version": "4.1.0",
44
"description": "Web utility storage manager for handling data encryption, save and persist, update and data purge in your local and session storage",
55
"main": "lib/index.js",
66
"type": "commonjs",
7+
"types": "lib/index.d.ts",
78
"directories": {
89
"lib": "lib"
910
},
1011
"scripts": {
1112
"test": "jest",
12-
"test:build": "rm -rf lib && npm run build:js && jest",
13-
"test:silent-build": "rm -rf lib && npm run build:js && jest --verbose false",
14-
"build": "rm -rf lib && npm run build:js",
13+
"test:build": "rm -rf lib && npm run build && jest",
14+
"test:silent-build": "rm -rf lib && npm run build && jest --verbose false",
15+
"build": "rm -rf lib && npm run build:js && npm run build:types",
1516
"build:types": "tsc --emitDeclarationOnly",
1617
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
1718
"publish": "npm run build && npm publish"

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"noEmit": true,
3+
"noEmit": false,
44
"allowJs": true,
55
"alwaysStrict": true,
66
"module": "ESNext",

0 commit comments

Comments
 (0)