-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
types.ts
214 lines (190 loc) · 6.78 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
export type EncAlgorithm = 'AES' | 'Rabbit' | 'RC4' | 'RC4Drop';
type StorageType = 'localStorage' | 'sessionStorage';
type ChangeType =
| 'set'
| 'get'
| 'getMultiple'
| 'setMultiple'
| 'remove'
| 'removeMultiple'
| 'clear'
| 'length'
| 'key';
export interface NotifyHandlerParams {
type: ChangeType;
key?: string | string[];
value?: any;
index?: number;
}
export type NotifyHandler = (params: NotifyHandlerParams) => void;
export interface Encryptation {
encrypt(value: string): string;
decrypt(value: string): string;
}
export interface EncryptStorageOptions {
prefix?: string;
stateManagementUse?: boolean;
storageType?: StorageType;
encAlgorithm?: EncAlgorithm;
doNotEncryptValues?: boolean;
doNotParseValues?: boolean;
notifyHandler?: NotifyHandler;
}
export interface RemoveFromPatternOptions {
exact?: boolean;
}
export interface GetFromPatternOptions extends RemoveFromPatternOptions {
multiple?: boolean;
doNotDecrypt?: boolean;
}
export interface EncryptStorageInterface extends Storage {
/**
* `setItem` - Is the function to be set `safeItem` in `selected storage`
* @param {string} key - Is the key of `data` in `selected storage`.
* @param {any} value - Value to be `encrypted`, the same being a `string` or `object`.
* @return {void} `void`
* @usage
* setItem('any_key', {key: 'value', another_key: 2})
* setItem('any_key', 'any value')
*/
setItem(key: string, value: any, doNotEncrypt?: boolean): void;
/**
* `setMultipeItems` - Is the function to be set `safeItem` in `selected storage`
* @param {[string, any][]} param - .
* @param {any} value - It's an `array` of `tuples` to be set at once...
* @return {void} `void`
* @usage
* setItem(['any_key', {key: 'value', another_key: 2}])
* setItem(['any_key', 'any value'])
*/
setMultipleItems(param: [string, any][], doNotEncrypt?: boolean): void;
/**
* `hash` - Is the function to be `hash` value width SHA256 encryptation
* @param {any} value - Value to be `hashed`, the same being a `string`.
* @return {string} `hashed string`
* @usage
* hash('any_string')
*/
hash(value: string): string;
/**
* `md5Hash` - Is the function to be `hash` value width MD5 encryptation
* @param {any} value - Value to be `hashed`, the same being a `string`.
* @return {string} `hashed string`
* @usage
* md5Hash('any_string')
*/
md5Hash(value: string): string;
/**
* `getItem` - Is the function to be get `safeItem` in `selected storage`
* @param {string} key - Is the key of `data` in `selected storage`.
* @return {string | any | undefined} - Returns a formatted value when the same is an object or string when not.
* Returns `undefined` when value not exists.
* @usage
* getItem('any_key') -> `{key: 'value', another_key: 2}`
* getItem('any_key') -> `'any value'`
*/
getItem(key: string, doNotDecrypt?: boolean): string | any | undefined;
/**
* `getMulpleItems` - Is the function to be get `safeItems` in `selected storage`
* @param {string[]} keys - Is the keys of `data` in `selected storage`.
* @return {Record<string, any> | undefined} - Returns a formatted value when the same is an object or string when not.
* Returns `undefined` when value not exists.
* @usage
* getMultipleItems(['any_key']) -> `{any_key: {key: 'value', another_key: 2}}`
* getMultipleItems(['any_key']) -> `{any_key: 'any value'}`
*/
getMultipleItems(keys: string[], doNotDecrypt?: boolean): Record<string, any>;
/**
* `removeItem` - Is the function to be remove `safeItem` in `selected storage`
* @param {string} key - Is the key of `data` in `selected storage`.
* @return {void}
* Returns `void`.
* @usage
* removeItem('any_key')
*/
removeItem(key: string): void;
/**
* `removeMultipleItems` - Is the function to be remove `safeItems` in `selected storage`
* @param {string[]} keys - Is the keys of `data` in `selected storage`.
* @return {void}
* Returns `void`.
* @usage
* removeMultipleItems(['any_key_1'm 'any_key_2'])
*/
removeMultipleItems(keys: string[]): void;
/**
* `getItemFromPattern` - Is the function to be get `safeItem` in `selected storage` from `pattern` based
* @param {string} pattern - Is the pattern existent in keys of `selected storage`.
* @return {any | Record<string, any> | undefined}
* Returns `void`.
* @usage
* // itemKey = '12345678:user'
* // another itemKey = '12345678:item'
* getItemFromPattern('12345678') -> {'12345678:user': 'value', '12345678:item': 'otherValue'}
*/
getItemFromPattern(
pattern: string,
options?: GetFromPatternOptions,
): Record<string, any> | any | undefined;
/**
* `removeItemFromPattern` - Is the function to be remove `safeItem` in `selected storage` from `pattern` based
* @param {string} pattern - Is the pattern existent in keys of `selected storage`.
* @return {void}
* Returns `void`.
* @usage
* // itemKey = '12345678:user'
* // another itemKey = '12345678:item'
* removeItem('12345678') -> item removed from `selected storage`
*/
removeItemFromPattern(
pattern: string,
options?: RemoveFromPatternOptions,
): void;
/**
* `clear` - Clear all selected storage
*/
clear(): void;
/**
* `key` - Return a `key` in selected storage index or `null`
* @param {number} index - Index of `key` in `selected storage`
*/
key(index: number): string | null;
/**
*
* `encryptString` - Is the function to be `encrypt` any string and return encrypted value
* @param {string} str - A `string` to be encrypted.
* @return {string} result
* Returns `string`.
* @usage
* encryptString('any_string') -> 'encrypted value'
*/
encryptString(str: string): string;
/**
*
* `decryptString` - Is the function to be `decrypt` any string encrypted by `encryptString` and return decrypted value
* @param {string} str - A `string` to be decrypted.
* @return {string} result
* Returns `string`.
* @usage
* decryptString('any_string') -> 'decrypted value'
*/
decryptString(str: string): string;
/**
* `encryptValue` - Is the function to be `encrypt` any value and return encrypted value
* @param {string} value - A `value|object|array` to be encrypted.
* @return {string} result
* Returns `string`.
* @usage
* encryptString('any_string') -> 'encrypted value'
*/
encryptValue(value: any): string;
/**
* `decryptString` - Is the function to be `decrypt` any value encrypted by `encryptValue` and return decrypted value
* @param {string} value - A `value|object|array` to be decrypted.
* @return {T|any} result
* Returns `string`.
* @usage
* decryptString('any_value') -> '{value: "decrypted value"}'
*/
decryptValue<T = any>(key: string): T;
}