|
1 | | -# manuhub-blazor-utilities |
| 1 | +# BrowserStorage.Wasm |
| 2 | + |
| 3 | + |
| 4 | +` BrowserStorage.Wasm` is a Razor library for managing browser storage in WebAssembly (WASM) applications. This package includes interfaces for managing data across cookies, session, and local storage with asynchronous methods. It is recommended to access these storage options only via the provided interfaces. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +- **ICookieStore**: Interface for managing data in browser cookies with async operations. |
| 9 | +- **ISessionStore**: Interface for managing session data that is cleared when the session ends. |
| 10 | +- **ILocalStore**: Interface for managing persistent data in local storage. |
| 11 | + |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +To use the package, make sure to access storage via the provided interfaces (`ICookieStore`, `ISessionStore`, `ILocalStore`). |
| 16 | + |
| 17 | +### ICookieStore Example |
| 18 | + |
| 19 | +```csharp |
| 20 | +// Inject ICookieStore in your Razor component or service |
| 21 | +public class ExampleService |
| 22 | +{ |
| 23 | + private readonly ICookieStore _cookieStore; |
| 24 | + |
| 25 | + public ExampleService(ICookieStore cookieStore) |
| 26 | + { |
| 27 | + _cookieStore = cookieStore; |
| 28 | + } |
| 29 | + |
| 30 | + public async Task ManageCookies() |
| 31 | + { |
| 32 | + await _cookieStore.SetAsync("user", "JohnDoe", expires: 24); // Set cookie with expiration |
| 33 | + var user = await _cookieStore.GetAsync<string>("user"); // Get cookie |
| 34 | + await _cookieStore.DeleteAsync("user"); // Remove cookie |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### ISessionStore Example |
| 40 | + |
| 41 | +```csharp |
| 42 | +// Inject ISessionStore in your Razor component or service |
| 43 | +public class ExampleService |
| 44 | +{ |
| 45 | + private readonly ISessionStore _sessionStore; |
| 46 | + |
| 47 | + public ExampleService(ISessionStore sessionStore) |
| 48 | + { |
| 49 | + _sessionStore = sessionStore; |
| 50 | + } |
| 51 | + |
| 52 | + public async Task ManageSession() |
| 53 | + { |
| 54 | + await _sessionStore.SetAsync("sessionData", "value"); // Store data for session |
| 55 | + var sessionData = await _sessionStore.GetAsync<string>("sessionData"); // Get session data |
| 56 | + await _sessionStore.RemoveItemAsync("sessionData"); // Remove session data |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### ILocalStore Example |
| 62 | + |
| 63 | +```csharp |
| 64 | +// Inject ILocalStore in your Razor component or service |
| 65 | +public class ExampleService |
| 66 | +{ |
| 67 | + private readonly ILocalStore _localStore; |
| 68 | + |
| 69 | + public ExampleService(ILocalStore localStore) |
| 70 | + { |
| 71 | + _localStore = localStore; |
| 72 | + } |
| 73 | + |
| 74 | + public async Task ManageLocalStorage() |
| 75 | + { |
| 76 | + await _localStore.SetAsync("persistentData", "value"); // Store data persistently |
| 77 | + var persistentData = await _localStore.GetAsync<string>("persistentData"); // Get persistent data |
| 78 | + await _localStore.RemoveItemAsync("persistentData"); // Remove persistent data |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Register services in your Blazor app: |
| 84 | + |
| 85 | +```csharp |
| 86 | +builder.Services.AddWasmBrowserStorage(); |
| 87 | +``` |
| 88 | + |
| 89 | +## Contributing |
| 90 | + |
| 91 | +We welcome contributions to the project! If you'd like to contribute, please fork the repository and submit a pull request with your changes. |
| 92 | + |
| 93 | +## License |
| 94 | + |
| 95 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE.txt) file for details. |
0 commit comments