Phetch.Blazor 0.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Phetch.Blazor --version 0.0.3                
NuGet\Install-Package Phetch.Blazor -Version 0.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Phetch.Blazor" Version="0.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Phetch.Blazor --version 0.0.3                
#r "nuget: Phetch.Blazor, 0.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Phetch.Blazor as a Cake Addin
#addin nuget:?package=Phetch.Blazor&version=0.0.3

// Install Phetch.Blazor as a Cake Tool
#tool nuget:?package=Phetch.Blazor&version=0.0.3                

Phetch

Phetch is a small Blazor library for handling async query state, in the style of React Query or SWR.

Currently, Phetch is only designed for use with Blazor WebAssembly. However, there are no dependencies on Blazor or ASP.NET Core, so in theory it can be used anywhere that supports .NET Standard 2.1.

⚠️ Note: Phetch is in early development and likely to change.

Features

  • Automatically handles loading and error states, and updates your components whenever the state changes
  • Use any async method as a query or mutation (not restricted just to HTTP requests)
  • Built-in support for CancellationTokens
  • Built-in debouncing, to (optionally) limit the rate of queries being sent
  • Supports mutations, dependent queries, and pagination
  • 100% strongly typed, with nullability annotations
  • Super lightweight and easy to mix-and-match with other state management methods
  • No Javascript whatsoever!

But why?

You're probably familiar with the "normal" way of performing async actions in client-side Blazor. You start a request in OnInitializedAsync, and then update a field when it returns.

But then you need to add error handling, so you add another variable to keep track of whether there was an error. Then once your component gets more complicated, you need to add another variable to explicitly track whether the query is loading. Then you realise that your query depends on values from your parameters, so you need to add custom logic in OnParametersSetAsync to manually re-start the query, but only when the parameters change. Then you need to be able to cancel queries after they've started, so you add a CancellationTokenSource and all the corresponding logic. If you've been paying attention, you might notice that your queries sometimes return in the wrong order because your server doesn't always take the same amount of time. And then you get to the next component and do it all over again.

Phetch aims to solve all of these problems.

Show me some code!

Click here to view the source code for the sample project, with more detailed examples.

Below is the code for a super-basic component that runs a query when the component is first loaded. Phetch can do a whole lot more than that though, so make sure to check out the samples project and full documentation!

@inject HttpClient Http

@if (forecastsQuery.IsError)
{
    <p><em>Error!</em></p>
}
else if (forecastsQuery.IsLoading)
{
    <p><em>Loading...</em></p>
}
else
{
    // Render data from forecastsQuery.Data
}

@code {
    private Query<WeatherForecast[]> forecastsQuery = null!;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        forecastsQuery = new(
            _ => Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json")!,
            onStateChanged: StateHasChanged
        );
    }
}

Phetch will also come with some useful extension methods to do things like this:

<p>
    This number is: @isEvenQuery.Match(
        fetching: () => @<text>...</text>,
        error: ex => @<em>Something went wrong!</em>,
        success: isEven => @<b>@(isEven ? "even" : "odd")</b>
    )
</p>

Installing

Note: Because many features have not been finalized, I won't yet be updating the NuGet version on a regular basis. If you want to try out Phetch in the meantime, I would recommend downloading the source code instead.

You can install Phetch via the .NET CLI with the following command:

dotnet add package Phetch.Blazor

If you're using Visual Studio, you can also install via the built in NuGet package manager.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 755 7/13/2024
0.7.1 1,544 6/8/2024
0.7.0 615 5/4/2024
0.6.0 696 12/1/2023
0.5.1 343 7/14/2023
0.5.0 229 6/19/2023
0.4.0 283 3/6/2023
0.3.1 372 11/19/2022
0.3.0 376 11/13/2022
0.2.0 430 8/18/2022
0.1.2 475 8/6/2022
0.1.1 424 8/1/2022
0.1.0 470 7/26/2022
0.0.3 489 7/9/2022