Storage Manager
Overview
The Storage Manager provides two specialized classes for working with browser storage (localStorage or sessionStorage):
StringStorageManager
- For simple string valuesJsonStorageManager<T>
- For complex objects with type safety
Both classes provide a clean, consistent API and handle storage backend selection automatically.
Location
1 |
|
Configuration
The storage backend (localStorage or sessionStorage) is configured via the environment variable:
1 |
|
The system defaults to localStorage
if not specified.
Basic Usage
Predefined Storage Instances
Several storage instances are pre-configured and exported from StorageManager.ts
:
1 2 3 4 5 6 7 8 9 |
|
String Storage
For simple string values using StringStorageManager
:
1 2 3 4 5 6 7 8 |
|
JSON Storage
For complex objects using JsonStorageManager
(strongly typed):
1 2 3 4 5 6 7 8 9 10 11 |
|
Creating New Storage Instances
For String Values
Use StringStorageManager
for simple string storage:
1 2 3 4 5 6 |
|
For Typed JSON Values
Use JsonStorageManager
with a type parameter for complex objects:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
API Reference
StringStorageManager
set(value: string): void
Stores a string value in storage.
1 |
|
get(): string | null
Retrieves a string value from storage. Returns null
if the key doesn't exist.
1 2 3 4 |
|
remove(): void
Removes the value from storage.
1 |
|
JsonStorageManager\<T>
set(value: T): void
Serializes and stores a JSON object.
1 2 3 4 |
|
get(): T | null
Retrieves and deserializes a JSON object. Returns null
if:
- The key doesn't exist
- The stored value is not valid JSON
- Parsing fails for any reason
1 2 3 4 |
|
remove(): void
Removes the value from storage.
1 |
|
Best Practices
1. Choose the Right Manager Class
Use the appropriate manager for your data type:
1 2 3 4 5 6 7 8 |
|
2. Always Export Storage Instances
Create storage instances in StorageManager.ts
and export them:
1 2 3 4 5 |
|
3. Use Type Parameters for JSON Storage
Always specify types when storing complex objects:
1 2 3 4 5 6 7 8 |
|
4. Check for Null Returns
Always handle the possibility of null
returns:
1 2 3 4 5 6 7 8 9 |
|
5. Use Consistent Key Naming
Follow the established pattern for storage keys:
1 2 3 4 5 6 |
|
Available Storage Instances
Instance | Type | Key | Purpose |
---|---|---|---|
AuthTokenStorage |
String | waldur/auth/token |
Authentication token |
AuthMethodStorage |
String | waldur/auth/method |
Authentication method (local, oauth, etc.) |
RedirectStorage |
JSON | waldur/auth/redirect |
Post-login redirect state |
ImpersonationStorage |
String | waldur/auth/impersonation |
Impersonated user UUID |
InvitationTokenStorage |
String | waldur/invitation/token |
Invitation acceptance token |
GroupInvitationTokenStorage |
String | waldur/group-invitation/token |
Group invitation token |
LanguageStorage |
String | waldur/i18n/lang |
Current language code |
Differences Between Manager Classes
StringStorageManager
- Purpose: Store and retrieve plain string values
- Methods:
set(string)
,get()
,remove()
- Use for: Tokens, IDs, language codes, simple flags
1 2 3 |
|
JsonStorageManager\<T>
- Purpose: Store and retrieve typed objects with automatic serialization
- Methods:
set(T)
,get()
,remove()
- Use for: Configuration objects, state data, complex structures
- Type parameter: Provides compile-time type checking
1 2 3 4 5 6 7 8 |
|
Error Handling
The JsonStorageManager.get()
method handles parsing errors gracefully:
1 2 3 |
|
This ensures your application doesn't crash due to corrupted storage data.
Testing
When testing code that uses Storage Managers:
1 2 3 4 5 6 7 8 |
|
For JSON storage:
1 2 3 4 5 6 7 8 9 |
|
Technical Details
Storage Backend Selection
The actual storage backend is determined at runtime by the getStorage()
function:
1 2 3 4 5 6 7 8 |
|
This allows the application to switch between:
- localStorage - Persists across browser sessions
- sessionStorage - Cleared when the tab/browser closes
Type Safety
The JsonStorageManager
type parameter ensures type safety:
1 2 3 4 5 6 |
|
Separation of Concerns
The two manager classes follow the Single Responsibility Principle:
StringStorageManager
focuses only on string operationsJsonStorageManager
handles serialization/deserialization- No unnecessary methods on either class
- Clear intent when reading code
This design makes it immediately obvious whether a storage instance contains strings or complex objects:
1 2 3 4 5 |
|