diff --git a/.github/workflows/publish-nuget.yml b/.github/workflows/publish-nuget.yml index e90f8a7..785e60c 100644 --- a/.github/workflows/publish-nuget.yml +++ b/.github/workflows/publish-nuget.yml @@ -31,18 +31,22 @@ jobs: - name: Restore dependencies run: | dotnet restore ./src/Blazor.SourceGenerators/Blazor.SourceGenerators.csproj + dotnet restore ./src/Blazor.Serialization/Blazor.Serialization.csproj dotnet restore ./src/Blazor.LocalStorage.WebAssembly/Blazor.LocalStorage.WebAssembly.csproj dotnet restore ./src/Blazor.LocalStorage.Server/Blazor.LocalStorage.Server.csproj - dotnet restore ./src/Blazor.Serialization/Blazor.Serialization.csproj + dotnet restore ./src/Blazor.SessionStorage.WebAssembly/Blazor.LocalStorage.WebAssembly.csproj + dotnet restore ./src/Blazor.SessionStorage.Server/Blazor.LocalStorage.Server.csproj dotnet restore ./src/Blazor.Geolocation.WebAssembly/Blazor.Geolocation.WebAssembly.csproj dotnet restore ./src/Blazor.Geolocation.Server/Blazor.Geolocation.Server.csproj - name: Build run: | dotnet build ./src/Blazor.SourceGenerators/Blazor.SourceGenerators.csproj --configuration Release --no-restore + dotnet build ./src/Blazor.Serialization/Blazor.Serialization.csproj --configuration Release --no-restore dotnet build ./src/Blazor.LocalStorage.WebAssembly/Blazor.LocalStorage.WebAssembly.csproj --configuration Release --no-restore dotnet build ./src/Blazor.LocalStorage.Server/Blazor.LocalStorage.Server.csproj --configuration Release --no-restore - dotnet build ./src/Blazor.Serialization/Blazor.Serialization.csproj --configuration Release --no-restore + dotnet build ./src/Blazor.SessionStorage.WebAssembly/Blazor.LocalStorage.WebAssembly.csproj --configuration Release --no-restore + dotnet build ./src/Blazor.SessionStorage.Server/Blazor.LocalStorage.Server.csproj --configuration Release --no-restore dotnet build ./src/Blazor.Geolocation.WebAssembly/Blazor.Geolocation.WebAssembly.csproj --configuration Release --no-restore dotnet build ./src/Blazor.Geolocation.Server/Blazor.Geolocation.Server.csproj --configuration Release --no-restore @@ -52,9 +56,11 @@ jobs: - name: Pack run: | dotnet pack ./src/Blazor.SourceGenerators/Blazor.SourceGenerators.csproj --output packages + dotnet pack ./src/Blazor.Serialization/Blazor.Serialization.csproj --output packages dotnet pack ./src/Blazor.LocalStorage.WebAssembly/Blazor.LocalStorage.WebAssembly.csproj --output packages dotnet pack ./src/Blazor.LocalStorage.Server/Blazor.LocalStorage.Server.csproj --output packages - dotnet pack ./src/Blazor.Serialization/Blazor.Serialization.csproj --output packages + dotnet pack ./src/Blazor.SessionStorage.WebAssembly/Blazor.LocalStorage.WebAssembly.csproj --output packages + dotnet pack ./src/Blazor.SessionStorage.Server/Blazor.LocalStorage.Server.csproj --output packages dotnet pack ./src/Blazor.Geolocation.WebAssembly/Blazor.Geolocation.WebAssembly.csproj --output packages dotnet pack ./src/Blazor.Geolocation.Server/Blazor.Geolocation.Server.csproj --output packages @@ -68,6 +74,12 @@ jobs: name: Blazor.SourceGenerators.${{ steps.regex-match.outputs.match }}.nupkg path: packages/ + - name: Upload serialization package + uses: actions/upload-artifact@v2 + with: + name: Blazor.Serialization.${{ steps.regex-match.outputs.match }}.nupkg + path: packages/ + - name: Upload localStorage WebAssembly package uses: actions/upload-artifact@v2 with: @@ -80,10 +92,16 @@ jobs: name: Blazor.LocalStorage.Server.${{ steps.regex-match.outputs.match }}.nupkg path: packages/ - - name: Upload serialization package + - name: Upload sessionStorage WebAssembly package uses: actions/upload-artifact@v2 with: - name: Blazor.Serialization.${{ steps.regex-match.outputs.match }}.nupkg + name: Blazor.SessionStorage.WebAssembly.${{ steps.regex-match.outputs.match }}.nupkg + path: packages/ + + - name: Upload sessionStorage Server package + uses: actions/upload-artifact@v2 + with: + name: Blazor.SessionStorage.Server.${{ steps.regex-match.outputs.match }}.nupkg path: packages/ - name: Upload geolocation WebAssembly package diff --git a/README.md b/README.md index 547e94f..d779cec 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,18 @@ A C# source generator that creates extensions methods on the Blazor WebAssembly |--|--|--| | [`Blazor.SourceGenerators`](https://www.nuget.org/packages/Blazor.SourceGenerators) | [![NuGet](https://img.shields.io/nuget/v/Blazor.SourceGenerators.svg?style=flat)](https://www.nuget.org/packages/Blazor.SourceGenerators) | Core source generator library. | | [`Blazor.Serialization`](https://www.nuget.org/packages/Blazor.Serialization) | [![NuGet](https://img.shields.io/nuget/v/Blazor.Serialization.svg?style=flat)](https://www.nuget.org/packages/Blazor.Serialization) | Common serialization library, required in some scenarios when using generics. | -| [`Blazor.LocalStorage.WebAssembly`](https://www.nuget.org/packages/Blazor.LocalStorage.WebAssembly) | [![NuGet](https://img.shields.io/nuget/v/Blazor.LocalStorage.WebAssembly.svg?style=flat)](https://www.nuget.org/packages/Blazor.LocalStorage.WebAssembly) | Blazor WebAssembly class library exposing DI-ready `IStorage` type for the `localStorage` implementation (relies on `IJSInProcessRuntime`). | -| [`Blazor.LocalStorage.Server`](https://www.nuget.org/packages/Blazor.LocalStorage.Server) | [![NuGet](https://img.shields.io/nuget/v/Blazor.LocalStorage.Server.svg?style=flat)](https://www.nuget.org/packages/Blazor.LocalStorage.Server) | Blazor Server class library exposing DI-ready `IStorage` type for the `localStorage` implementation (relies on `IJSRuntime`) | -| [`Blazor.Geolocation.WebAssembly`](https://www.nuget.org/packages/Blazor.Geolocation.WebAssembly) | [![NuGet](https://img.shields.io/nuget/v/Blazor.Geolocation.WebAssembly.svg?style=flat)](https://www.nuget.org/packages/Blazor.Geolocation.WebAssembly) | Razor class library exposing DI-ready `IGeolocation` type (and dependent callback types) for the `geolocation` implementation (relies on `IJSInProcessRuntime`). | -| [`Blazor.Geolocation.Server`](https://www.nuget.org/packages/Blazor.Geolocation.Server) | [![NuGet](https://img.shields.io/nuget/v/Blazor.Geolocation.Server.svg?style=flat)](https://www.nuget.org/packages/Blazor.Geolocation.Server) | Razor class library exposing DI-ready `IGeolocation` type (and dependent callback types) for the `geolocation` implementation (relies on `IJSRuntime`). | +| [`Blazor.LocalStorage.WebAssembly`](https://www.nuget.org/packages/Blazor.LocalStorage.WebAssembly) | [![NuGet](https://img.shields.io/nuget/v/Blazor.LocalStorage.WebAssembly.svg?style=flat)](https://www.nuget.org/packages/Blazor.LocalStorage.WebAssembly) | Blazor WebAssembly class library exposing DI-ready `IStorageService` type for the `localStorage` implementation (relies on `IJSInProcessRuntime`). | +| [`Blazor.LocalStorage.Server`](https://www.nuget.org/packages/Blazor.LocalStorage.Server) | [![NuGet](https://img.shields.io/nuget/v/Blazor.LocalStorage.Server.svg?style=flat)](https://www.nuget.org/packages/Blazor.LocalStorage.Server) | Blazor Server class library exposing DI-ready `IStorageService` type for the `localStorage` implementation (relies on `IJSRuntime`) | +| [`Blazor.SessionStorage.WebAssembly`](https://www.nuget.org/packages/Blazor.SessionStorage.WebAssembly) | [![NuGet](https://img.shields.io/nuget/v/Blazor.SessionStorage.WebAssembly.svg?style=flat)](https://www.nuget.org/packages/Blazor.SessionStorage.WebAssembly) | Blazor WebAssembly class library exposing DI-ready `IStorageService` type for the `sessionStorage` implementation (relies on `IJSInProcessRuntime`). | +| [`Blazor.SessionStorage.Server`](https://www.nuget.org/packages/Blazor.SessionStorage.Server) | [![NuGet](https://img.shields.io/nuget/v/Blazor.SessionStorage.Server.svg?style=flat)](https://www.nuget.org/packages/Blazor.SessionStorage.Server) | Blazor Server class library exposing DI-ready `IStorageService` type for the `sessionStorage` implementation (relies on `IJSRuntime`) | +| [`Blazor.Geolocation.WebAssembly`](https://www.nuget.org/packages/Blazor.Geolocation.WebAssembly) | [![NuGet](https://img.shields.io/nuget/v/Blazor.Geolocation.WebAssembly.svg?style=flat)](https://www.nuget.org/packages/Blazor.Geolocation.WebAssembly) | Razor class library exposing DI-ready `IGeolocationService` type (and dependent callback types) for the `geolocation` implementation (relies on `IJSInProcessRuntime`). | +| [`Blazor.Geolocation.Server`](https://www.nuget.org/packages/Blazor.Geolocation.Server) | [![NuGet](https://img.shields.io/nuget/v/Blazor.Geolocation.Server.svg?style=flat)](https://www.nuget.org/packages/Blazor.Geolocation.Server) | Razor class library exposing DI-ready `IGeolocationService` type (and dependent callback types) for the `geolocation` implementation (relies on `IJSRuntime`). | ## Using the `Blazor.SourceGenerators` package 📦 As an example, the official [`Blazor.LocalStorage.WebAssembly`](https://www.nuget.org/packages/Blazor.LocalStorage.WebAssembly) package consumes the [`Blazor.SourceGenerators`](https://www.nuget.org/packages/Blazor.SourceGenerators) package. It exposes extension methods specific to Blazor WebAssembly and the [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) Web API. -Consider the _SynchronousLocalStorageExtensions.cs_ C# file: +Consider the IStorageService.cs_ C# file: ```csharp // Copyright (c) David Pine. All rights reserved. @@ -43,9 +45,7 @@ namespace Microsoft.JSInterop; "getItem", "setItem:value" })] -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public partial interface IStorageService -#pragma warning restore CS1591 // The XML comments are source generated { } ``` @@ -60,7 +60,7 @@ This code designates itself into the `Microsoft.JSInterop` namespace, making the > The generic method descriptors syntax is: > `"methodName"` for generic return type and `"methodName:parameterName"` for generic parameter type. -The file needs to define an interface and it needs to be `partial`, for example; `public partial interface`. Decorating the class with the `JSAutoInterop` (or `JSAutoGenericInterop) attribute will source generate the following C# code, as shown in the source generated `IStorageService.g.cs`: +The file needs to define an interface and it needs to be `partial`, for example; `public partial interface`. Decorating the class with the `JSAutoInterop` (or `JSAutoGenericInterop) attribute will source generate the following C# code, as shown in the source generated `IStorageServiceService.g.cs`: ```csharp using Blazor.Serialization.Extensions; @@ -72,7 +72,7 @@ namespace Microsoft.JSInterop; /// /// Source generated interface definition of the Storage type. /// -public partial interface IStorageService +public partial interface IStorageServiceService { /// /// Source generated implementation of window.localStorage.length. @@ -119,7 +119,7 @@ These internal extension methods rely on the `IJSInProcessRuntime` to perform Ja - `IStorageService.g.cs`: The interface for the corresponding `Storage` Web API surface area. - `LocalStorgeService.g.cs`: The `internal` implementation of the `IStorageService` interface. -- `LocalStorageServiceCollectionExtensions.g.cs`: Extension methods to add the `IStorage` service to the dependency injection `IServiceCollection`. +- `LocalStorageServiceCollectionExtensions.g.cs`: Extension methods to add the `IStorageService` service to the dependency injection `IServiceCollection`. Here is the source generated `LocalStorageService` implementation: @@ -202,17 +202,17 @@ namespace Microsoft.Extensions.DependencyInjection; public static class LocalStorageServiceCollectionExtensions { /// - /// Adds the service to the service collection. + /// Adds the service to the service collection. /// public static IServiceCollection AddLocalStorageServices( this IServiceCollection services) => services.AddSingleton(serviceProvider => (IJSInProcessRuntime)serviceProvider.GetRequiredService()) - .AddSingleton(); + .AddSingleton(); } ``` -Putting this all together, the `Blazor.LocalStorage.WebAssembly` NuGet package is actually only 20 lines of code, and it generates full DI-ready services with JavaScript interop. +Putting this all together, the `Blazor.LocalStorage.WebAssembly` NuGet package is actually less than 15 lines of code, and it generates full DI-ready services with JavaScript interop. The `Blazor.LocalStorage.Server` package, generates extensions on the `IJSRuntime` type. @@ -228,9 +228,7 @@ namespace Microsoft.JSInterop; HostingModel = BlazorHostingModel.Server, OnlyGeneratePureJS = true, Url = "https://developer.mozilla.org/docs/Web/API/Window/localStorage")] -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member -public partial interface IStorageService -#pragma warning restore CS1591 // The XML comments are source generated +public partial interface IStorageServiceService { } ``` @@ -248,7 +246,7 @@ using System.Threading.Tasks; #nullable enable namespace Microsoft.JSInterop; -public partial interface IStorageService +public partial interface IStorageServiceService { /// /// Source generated implementation of window.localStorage.length. @@ -339,9 +337,7 @@ namespace Microsoft.JSInterop; TypeName = "Geolocation", Implementation = "window.navigator.geolocation", Url = "https://developer.mozilla.org/docs/Web/API/Geolocation")] -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public partial interface IGeolocationService -#pragma warning restore CS1591 // The XML comments are source generated { } ``` diff --git a/blazorators.sln b/blazorators.sln index fddfafd..5f9312d 100644 --- a/blazorators.sln +++ b/blazorators.sln @@ -36,6 +36,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Geolocation.WebAssem EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Geolocation.Server", "src\Blazor.Geolocation.Server\Blazor.Geolocation.Server.csproj", "{A9F3FE17-EF4C-44B8-B265-7D7DBAA42F84}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazor.SessionStorage.WebAssembly", "src\Blazor.SessionStorage.WebAssembly\Blazor.SessionStorage.WebAssembly.csproj", "{B8FF9EFA-D8DA-43AB-8056-991FFDB609D8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazor.SessionStorage.Server", "src\Blazor.SessionStorage.Server\Blazor.SessionStorage.Server.csproj", "{F54C9271-5C28-498D-9081-59EAA22FEC69}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -74,6 +78,14 @@ Global {A9F3FE17-EF4C-44B8-B265-7D7DBAA42F84}.Debug|Any CPU.Build.0 = Debug|Any CPU {A9F3FE17-EF4C-44B8-B265-7D7DBAA42F84}.Release|Any CPU.ActiveCfg = Release|Any CPU {A9F3FE17-EF4C-44B8-B265-7D7DBAA42F84}.Release|Any CPU.Build.0 = Release|Any CPU + {B8FF9EFA-D8DA-43AB-8056-991FFDB609D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B8FF9EFA-D8DA-43AB-8056-991FFDB609D8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B8FF9EFA-D8DA-43AB-8056-991FFDB609D8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B8FF9EFA-D8DA-43AB-8056-991FFDB609D8}.Release|Any CPU.Build.0 = Release|Any CPU + {F54C9271-5C28-498D-9081-59EAA22FEC69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F54C9271-5C28-498D-9081-59EAA22FEC69}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F54C9271-5C28-498D-9081-59EAA22FEC69}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F54C9271-5C28-498D-9081-59EAA22FEC69}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -87,6 +99,8 @@ Global {5E4658A7-4751-4F85-A558-12B8EBCA2BCA} = {537EB83C-6982-40B0-801A-479DF3B17DBE} {0BAC9703-45EE-4E9E-A0F0-556F02FCB901} = {537EB83C-6982-40B0-801A-479DF3B17DBE} {A9F3FE17-EF4C-44B8-B265-7D7DBAA42F84} = {537EB83C-6982-40B0-801A-479DF3B17DBE} + {B8FF9EFA-D8DA-43AB-8056-991FFDB609D8} = {537EB83C-6982-40B0-801A-479DF3B17DBE} + {F54C9271-5C28-498D-9081-59EAA22FEC69} = {537EB83C-6982-40B0-801A-479DF3B17DBE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3F86284A-32D2-4F79-B23C-7A0CB8775971} diff --git a/src/Blazor.Geolocation.Server/IGeolocationService.cs b/src/Blazor.Geolocation.Server/IGeolocationService.cs index 0672cc0..7d10a57 100644 --- a/src/Blazor.Geolocation.Server/IGeolocationService.cs +++ b/src/Blazor.Geolocation.Server/IGeolocationService.cs @@ -8,8 +8,6 @@ namespace Microsoft.JSInterop; Implementation = "window.navigator.geolocation", HostingModel = BlazorHostingModel.Server, Url = "https://developer.mozilla.org/docs/Web/API/Geolocation")] -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public partial interface IGeolocationService -#pragma warning restore CS1591 // The XML comments are source generated { } \ No newline at end of file diff --git a/src/Blazor.Geolocation.WebAssembly/IGeolocationService.cs b/src/Blazor.Geolocation.WebAssembly/IGeolocationService.cs index 4be9c60..ac3203a 100644 --- a/src/Blazor.Geolocation.WebAssembly/IGeolocationService.cs +++ b/src/Blazor.Geolocation.WebAssembly/IGeolocationService.cs @@ -7,8 +7,6 @@ namespace Microsoft.JSInterop; TypeName = "Geolocation", Implementation = "window.navigator.geolocation", Url = "https://developer.mozilla.org/docs/Web/API/Geolocation")] -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public partial interface IGeolocationService -#pragma warning restore CS1591 // The XML comments are source generated { } \ No newline at end of file diff --git a/src/Blazor.LocalStorage.Server/IStorageService.cs b/src/Blazor.LocalStorage.Server/IStorageService.cs index 2f42204..de3a09e 100644 --- a/src/Blazor.LocalStorage.Server/IStorageService.cs +++ b/src/Blazor.LocalStorage.Server/IStorageService.cs @@ -9,8 +9,6 @@ namespace Microsoft.JSInterop; HostingModel = BlazorHostingModel.Server, OnlyGeneratePureJS = true, Url = "https://developer.mozilla.org/docs/Web/API/Window/localStorage")] -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public partial interface IStorageService -#pragma warning restore CS1591 // The XML comments are source generated { } \ No newline at end of file diff --git a/src/Blazor.LocalStorage.WebAssembly/IStorageService.cs b/src/Blazor.LocalStorage.WebAssembly/IStorageService.cs index 8fce66d..8db577b 100644 --- a/src/Blazor.LocalStorage.WebAssembly/IStorageService.cs +++ b/src/Blazor.LocalStorage.WebAssembly/IStorageService.cs @@ -12,8 +12,6 @@ namespace Microsoft.JSInterop; "getItem", "setItem:value" })] -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public partial interface IStorageService -#pragma warning restore CS1591 // The XML comments are source generated { } \ No newline at end of file diff --git a/src/Blazor.SessionStorage.Server/Blazor.SessionStorage.Server.csproj b/src/Blazor.SessionStorage.Server/Blazor.SessionStorage.Server.csproj new file mode 100644 index 0000000..56f7bba --- /dev/null +++ b/src/Blazor.SessionStorage.Server/Blazor.SessionStorage.Server.csproj @@ -0,0 +1,77 @@ + + + + net6.0 + enable + true + Source generated JavaScript interop for the browser's sessionStorage API compatible with Blazor Server. + Copyright © David Pine. All rights reserved. Licensed under the MIT License. + en-US + $([System.DateTime]::Now.ToString(yyyyMMdd)) + $(ClientOfficialVersion) + $(ClientPreviewVersion) + nightly-$(CurrentDate) + preview + $(ClientVersion) + $(ClientVersion)-$(VersionSuffix) + $(ClientVersion) + David Pine + true + Blazor.SessionStorage.Server + A C# source-generated class library implementation of the native browser's sessionStorage API available as IJSRuntime extension methods. + Blazor.SessionStorage.Server + dotnet;dotnetcore;csharp;blazor;generators;sourcegen;roslyn; + + https://github.com/IEvangelist/blazorators + true + true + false + true + AnyCPU + External + Product + embedded + false + false + Blazor.SessionStorage.Server + NU5125;NU5039 + true + https://github.com/IEvangelist/blazorators + LICENSE + git + true + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb + true + README.md + true + logo.png + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + + + + diff --git a/src/Blazor.SessionStorage.Server/IStorageService.cs b/src/Blazor.SessionStorage.Server/IStorageService.cs new file mode 100644 index 0000000..3caae4f --- /dev/null +++ b/src/Blazor.SessionStorage.Server/IStorageService.cs @@ -0,0 +1,14 @@ +// Copyright (c) David Pine. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.JSInterop; + +[JSAutoInterop( + TypeName = "Storage", + Implementation = "window.sessionStorage", + HostingModel = BlazorHostingModel.Server, + OnlyGeneratePureJS = true, + Url = "https://developer.mozilla.org/docs/Web/API/Window/sessionStorage")] +public partial interface IStorageService +{ +} \ No newline at end of file diff --git a/src/Blazor.SessionStorage.Server/README.md b/src/Blazor.SessionStorage.Server/README.md new file mode 100644 index 0000000..85484ec --- /dev/null +++ b/src/Blazor.SessionStorage.Server/README.md @@ -0,0 +1,80 @@ +# Blazorators: The Source Generated `sessionStorage` JavaScript Interop library for Blazor Server + +The [`Blazor.SessionStorage.Server`](https://www.nuget.org/packages/Blazor.SessionStorage.Server) package consumes the [`Blazor.SourceGenerators`](https://www.nuget.org/packages/Blazor.SourceGenerators) package. It exposes a source generated `ISessionStorage` interface specific to Blazor WebAssembly and the [`sessionStorage`](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) Web API. + +## Get started + +After the NuGet package is added as a reference, call the `AddSessionStorageServices` method to register the `IStorageService` service type. + +```csharp +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddSessionStorageServices(); +builder.Services.AddRazorPages(); +builder.Services.AddServerSideBlazor(); + +var app = builder.Build(); + +app.UseHttpsRedirection(); +app.UseStaticFiles(); +app.UseRouting(); + +app.MapControllers(); +app.MapBlazorHub(); +app.MapFallbackToPage("/_Host"); + +app.Run(); +``` + +Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IStorageService` type. The interface takes the following shape: + +```csharp +using Blazor.Serialization.Extensions; +using System.Text.Json; + +#nullable enable +namespace Microsoft.JSInterop; + +/// +/// Source generated interface definition of the Storage type. +/// +public interface IStorageService +{ + /// + /// Source generated implementation of window.sessionStorage.clear. + /// + /// + ValueTask ClearAsync(); + + /// + /// Source generated implementation of window.sessionStorage.getItem. + /// + /// + ValueTask GetItemAsync(string key); + + /// + /// Source generated implementation of window.sessionStorage.key. + /// + /// + ValueTask KeyAsync(double index); + + /// + /// Source generated implementation of window.sessionStorage.removeItem. + /// + /// + ValueTask RemoveItemAsync(string key); + + /// + /// Source generated implementation of window.sessionStorage.setItem. + /// + /// + ValueTask SetItemAsync(string key, string value); + + /// + /// Source generated implementation of window.sessionStorage.length. + /// + /// + ValueTask Length { get; } +} +``` diff --git a/src/Blazor.SessionStorage.WebAssembly/Blazor.SessionStorage.WebAssembly.csproj b/src/Blazor.SessionStorage.WebAssembly/Blazor.SessionStorage.WebAssembly.csproj new file mode 100644 index 0000000..395e316 --- /dev/null +++ b/src/Blazor.SessionStorage.WebAssembly/Blazor.SessionStorage.WebAssembly.csproj @@ -0,0 +1,78 @@ + + + + net6.0 + enable + true + Source generated JavaScript interop for the browser's sessionStorage API compatible with Blazor WebAssembly. + Copyright © David Pine. All rights reserved. Licensed under the MIT License. + en-US + $([System.DateTime]::Now.ToString(yyyyMMdd)) + $(ClientOfficialVersion) + $(ClientPreviewVersion) + nightly-$(CurrentDate) + preview + $(ClientVersion) + $(ClientVersion)-$(VersionSuffix) + $(ClientVersion) + David Pine + true + Blazor.SessionStorage.WebAssembly + A C# source-generated class library implementation of the native browser's sessionStorage API available as IJSInProcessRuntime extension methods. + Blazor.SessionStorage.WebAssembly + dotnet;dotnetcore;csharp;blazor;webassembly;wasm;generators;sourcegen;roslyn; + + https://github.com/IEvangelist/blazorators + true + true + false + true + AnyCPU + External + Product + embedded + false + false + Blazor.SessionStorage.WebAssembly + NU5125;NU5039; + true + https://github.com/IEvangelist/blazorators + LICENSE + git + true + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb + true + README.md + true + logo.png + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + + + + + diff --git a/src/Blazor.SessionStorage.WebAssembly/IStorageService.cs b/src/Blazor.SessionStorage.WebAssembly/IStorageService.cs new file mode 100644 index 0000000..f3c6efc --- /dev/null +++ b/src/Blazor.SessionStorage.WebAssembly/IStorageService.cs @@ -0,0 +1,17 @@ +// Copyright (c) David Pine. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.JSInterop; + +[JSAutoGenericInterop( + TypeName = "Storage", + Implementation = "window.sessionStorage", + Url = "https://developer.mozilla.org/docs/Web/API/Window/sessionStorage", + GenericMethodDescriptors = new[] + { + "getItem", + "setItem:value" + })] +public partial interface IStorageService +{ +} \ No newline at end of file diff --git a/src/Blazor.SessionStorage.WebAssembly/README.md b/src/Blazor.SessionStorage.WebAssembly/README.md new file mode 100644 index 0000000..fecc9eb --- /dev/null +++ b/src/Blazor.SessionStorage.WebAssembly/README.md @@ -0,0 +1,78 @@ +# Blazorators: The Source Generated `sessionStorage` JavaScript Interop library for Blazor WebAssembly + +The [`Blazor.SessionStorage.WebAssembly`](https://www.nuget.org/packages/Blazor.SessionStorage.WebAssembly) package consumes the [`Blazor.SourceGenerators`](https://www.nuget.org/packages/Blazor.SourceGenerators) package. It exposes a source generated `ISessionStorage` interface specific to Blazor WebAssembly and the [`sessionStorage`](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) Web API. + +## Get started + +After the NuGet package is added as a reference, call the `AddSessionStorageServices` method to register the `IStorageService` service type. + +```csharp +using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + +var builder = WebAssemblyHostBuilder.CreateDefault(args); +builder.RootComponents.Add("#app"); +builder.RootComponents.Add("head::after"); + +builder.Services.AddScoped( + sp => new HttpClient + { + BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) + }); + +builder.Services.AddSessionStorageServices(); + +await builder.Build().RunAsync(); +``` + +Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IStorageService` type. The interface takes the following shape: + +```csharp +using Blazor.Serialization.Extensions; +using System.Text.Json; + +#nullable enable +namespace Microsoft.JSInterop; + +/// +/// Source generated interface definition of the Storage type. +/// +public interface IStorageService +{ + /// + /// Source generated implementation of window.sessionStorage.clear. + /// + /// + void Clear(); + + /// + /// Source generated implementation of window.sessionStorage.getItem. + /// + /// + TResult? GetItem(string key, JsonSerializerOptions? options = null); + + /// + /// Source generated implementation of window.sessionStorage.key. + /// + /// + string? Key(double index); + + /// + /// Source generated implementation of window.sessionStorage.removeItem. + /// + /// + void RemoveItem(string key); + + /// + /// Source generated implementation of window.sessionStorage.setItem. + /// + /// + void SetItem(string key, TArg value, JsonSerializerOptions? options = null); + + /// + /// Source generated implementation of window.sessionStorage.length. + /// + /// + double Length { get; } +} +```