38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import * as SecureStore from 'expo-secure-store';
|
|
import { Platform } from 'react-native';
|
|
|
|
type StorageType = typeof Platform.OS;
|
|
type AvailableStorageType = "web"|"android"|"none";
|
|
|
|
interface useStorageFuncs {
|
|
getItemAsync: (key: string) => Promise<string | null>;
|
|
setItemAsync: (key: string, value: string) => Promise<void>;
|
|
removeItemAsync: (key: string) => Promise<void>;
|
|
}
|
|
|
|
const storages: Record<AvailableStorageType, useStorageFuncs> = {
|
|
android: {
|
|
getItemAsync: async (token: string) => await SecureStore.getItemAsync(token),
|
|
setItemAsync: async (token: string, value: string) => await SecureStore.setItemAsync(token, value),
|
|
removeItemAsync: async (token: string) => await SecureStore.deleteItemAsync(token),
|
|
},
|
|
web: {
|
|
getItemAsync: async (token: string) => localStorage.getItem(token),
|
|
setItemAsync: async (token: string, value: string) => localStorage.setItem(token, value),
|
|
removeItemAsync: async (token: string) => localStorage.removeItem(token)
|
|
},
|
|
none: {
|
|
getItemAsync: async (token: string) => "not_realized",
|
|
setItemAsync: async (token: string, value: string) => { },
|
|
removeItemAsync: async (token: string) => { }
|
|
}
|
|
}
|
|
|
|
const getStorage = (type: StorageType = Platform.OS): useStorageFuncs => {
|
|
if(type in storages)
|
|
return storages[type as AvailableStorageType];
|
|
else
|
|
return storages.none;
|
|
}
|
|
|
|
export default getStorage; |