Skip to content

Commit

Permalink
feat: declare type to encryptStorage util
Browse files Browse the repository at this point in the history
  • Loading branch information
timepresent95 committed Oct 11, 2024
1 parent 393c894 commit 542334f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/utils/encryptStorage.ts
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 };

0 comments on commit 542334f

Please sign in to comment.