1.0.3 • Published 1 year ago
@shaaz1000/rn-storage v1.0.3
@shaaz1000/rn-storage
A powerful and flexible storage solution for React Native applications with built-in support for encryption, caching, and offline synchronization.
Features
- 🔒 Secure data encryption
- 📦 Efficient caching system
- 🔄 Offline data synchronization
- ⚡ High-performance storage
- 🎯 TypeScript support
- ✨ React Hooks for easy integration
Installation
npm install @shaaz1000/rn-storage
# Required peer dependencies
npm install @react-native-async-storage/async-storage @react-native-community/netinfoBasic Usage
import { StorageManager, useStorage } from '@shaaz1000/rn-storage';
// Initialize the storage (do this in your app's entry point)
StorageManager.initialize('your-secure-key');
// Use in your components
function MyComponent() {
const { value, setValue, loading } = useStorage('my-key', initialValue);
return (
<View>
{loading ? <ActivityIndicator /> : <Text>{value}</Text>}
</View>
);
}Available Hooks
1. useStorage
General-purpose storage hook with all features.
const {
value,
setValue,
remove,
loading,
error
} = useStorage(key, initialValue, {
encrypt: true,
cache: true,
sync: true,
expiryTime: 3600000 // 1 hour
});2. useCache
Specialized hook for caching with automatic cleanup.
const {
value,
setCache,
removeFromCache,
clearCache
} = useCache(key, initialValue, {
expiryTime: 3600000,
encryptData: false
});3. useEncryptedStorage
Hook specifically for handling encrypted data.
const {
value,
setValue,
remove
} = useEncryptedStorage(key, initialValue);4. useOfflineSync
Hook for managing offline data synchronization.
const {
syncStatus,
lastSyncTime,
pendingOperations,
queueOperation,
forceSync
} = useOfflineSync({
syncInterval: 5000,
retryAttempts: 3,
onSyncComplete: (success) => console.log('Sync completed:', success),
onSyncError: (error) => console.error('Sync failed:', error)
});Advanced Usage
Custom Encryption
import { StorageManager, Encryption } from '@shaaz1000/rn-storage';
// Initialize with a strong encryption key
StorageManager.initialize('your-secure-encryption-key');
// Store encrypted data
await StorageManager.getInstance().setItem('secure-key', sensitiveData, true);Caching with Size Limits
import { CacheManager } from '@shaaz1000/rn-storage';
const cache = CacheManager.getInstance({
maxSize: 100, // Maximum items in cache
expiryTime: 1000 * 60 * 60, // 1 hour
encryptData: true
});
await cache.set('key', data);Offline Sync Configuration
import { OfflineSync } from '@shaaz1000/rn-storage';
const sync = OfflineSync.getInstance();
sync.configure({
syncInterval: 5000, // Sync every 5 seconds
retryAttempts: 3,
onSyncComplete: (success) => {
console.log('Sync status:', success);
},
onSyncError: (error) => {
console.error('Sync failed:', error);
}
});API Reference
StorageManager
initialize(key: string): Initialize storage with encryption keygetInstance(): Get storage instancesetItem(key: string, value: any, encrypt?: boolean): Store datagetItem(key: string, decrypt?: boolean): Retrieve dataremoveItem(key: string): Remove dataclear(): Clear all data
CacheManager
getInstance(config?: CacheConfig): Get cache instanceset(key: string, value: any, expiryTime?: number): Cache dataget(key: string): Get cached dataremove(key: string): Remove from cacheclear(): Clear cache
OfflineSync
getInstance(): Get sync instanceconfigure(config: SyncConfig): Configure syncqueueOperation(operation: QueueItem): Queue operation for syncsyncQueuedItems(): Force syncstopSync(): Stop sync process
TypeScript Support
The package includes full TypeScript definitions. Example with custom types:
interface UserData {
id: number;
name: string;
}
const { value, setValue } = useStorage<UserData>('user', {
id: 0,
name: ''
});Error Handling
All hooks provide error states and handling:
const { error, value, setValue } = useStorage('key', initialValue);
if (error) {
console.error('Storage error:', error.message);
}Best Practices
- Initialize early in your app:
// App.tsx or index.js
StorageManager.initialize('your-secure-key');- Handle loading states:
const { value, loading } = useStorage('key', null);
if (loading) {
return <LoadingSpinner />;
}- Use encryption for sensitive data:
const { value } = useEncryptedStorage('api-token', null);- Configure cache expiry appropriately:
const { value } = useCache('user-preferences', null, {
expiryTime: 1000 * 60 * 60 * 24 // 24 hours
});License
MIT