-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: declare type to encryptStorage util
- Loading branch information
1 parent
393c894
commit 542334f
Showing
1 changed file
with
12 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import EncryptedStorage from 'react-native-encrypted-storage'; | ||
|
||
const setEncryptStorage = async <T>(key: string, data: T) => { | ||
export type EncryptedStorageRecord = {}; | ||
|
||
type EncryptedStorageKey = keyof EncryptedStorageRecord; | ||
|
||
const setEncryptStorage = async <T extends EncryptedStorageKey>( | ||
key: T, | ||
data: EncryptedStorageRecord[T], | ||
) => { | ||
await EncryptedStorage.setItem(key, JSON.stringify(data)); | ||
}; | ||
|
||
const getEncryptStorage = async (key: string) => { | ||
const getEncryptStorage = async (key: EncryptedStorageKey) => { | ||
const storedData = await EncryptedStorage.getItem(key); | ||
|
||
return storedData ? JSON.parse(storedData) : null; | ||
return storedData ? JSON.parse(storedData) : undefined; | ||
}; | ||
|
||
const removeEncryptStorage = async (key: string) => { | ||
const data = await getEncryptStorage(key); | ||
if (data) { | ||
await EncryptedStorage.removeItem(key); | ||
} | ||
const removeEncryptStorage = async (key: EncryptedStorageKey) => { | ||
await EncryptedStorage.removeItem(key); | ||
}; | ||
|
||
export { setEncryptStorage, getEncryptStorage, removeEncryptStorage }; |