forked from emeraldsanto/react-native-encrypted-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptedStorage.ts
More file actions
107 lines (92 loc) · 3.54 KB
/
EncryptedStorage.ts
File metadata and controls
107 lines (92 loc) · 3.54 KB
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
/* eslint-disable no-dupe-class-members */
import { NativeModules } from 'react-native';
const { RNEncryptedStorage } = NativeModules;
if (!RNEncryptedStorage) {
throw new Error('RNEncryptedStorage is undefined');
}
export type StorageErrorCallback = (error?: Error) => void;
export type StorageValueCallback = (error?: Error, value?: string) => void;
export default class EncryptedStorage {
/**
* Writes data to the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that will be associated to the value for later retrieval.
* @param {string} value - The data to store.
*/
static setItem(key: string, value: string): Promise<void>;
/**
* Writes data to the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that will be associated to the value for later retrieval.
* @param {string} value - The data to store.
* @param {Function} cb - The function to call when the operation completes.
*/
static setItem(key: string, value: string, cb: StorageErrorCallback): void;
static setItem(
key: string,
value: string,
cb?: StorageErrorCallback
): void | Promise<void> {
if (cb) {
RNEncryptedStorage.setItem(key, value).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.setItem(key, value);
}
/**
* Retrieves data from the disk, using SharedPreferences or KeyChain, depending on the platform and returns it as the specified type.
* @param {string} key - A string that is associated to a value.
*/
static getItem(key: string): Promise<string | null>;
/**
* Retrieves data from the disk, using SharedPreferences or KeyChain, depending on the platform and returns it as the specified type.
* @param {string} key - A string that is associated to a value.
* @param {Function} cb - The function to call when the operation completes.
*/
static getItem(key: string, cb: StorageValueCallback): void;
static getItem(
key: string,
cb?: StorageValueCallback
): void | Promise<string | null> {
if (cb) {
RNEncryptedStorage.getItem(key).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.getItem(key);
}
/**
* Deletes data from the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that is associated to a value.
*/
static removeItem(key: string): Promise<void>;
/**
* Deletes data from the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that is associated to a value.
* @param {Function} cb - The function to call when the operation completes.
*/
static removeItem(key: string, cb: StorageErrorCallback): void;
static removeItem(
key: string,
cb?: StorageErrorCallback
): void | Promise<void> {
if (cb) {
RNEncryptedStorage.removeItem(key).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.removeItem(key);
}
/**
* Clears all data from disk, using SharedPreferences or KeyChain, depending on the platform.
*/
static clear(): Promise<void>;
/**
* Clears all data from disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {Function} cb - The function to call when the operation completes.
*/
static clear(cb: StorageErrorCallback): void;
static clear(cb?: StorageErrorCallback): void | Promise<void> {
if (cb) {
RNEncryptedStorage.clear().then(cb).catch(cb);
return;
}
return RNEncryptedStorage.clear();
}
}