From 5e4d8254ddd68285da9ec4ccf27c6c0bd01e27f2 Mon Sep 17 00:00:00 2001 From: Sik Date: Sun, 1 Nov 2026 16:43:32 +0100 Subject: [PATCH 1/2] feat: add Axios.Net.Extensions.DependencyInjection package --- .github/workflows/release.yml | 2 +- Axios.Net.slnx | 2 + README.md | 32 ++++++ ....Net.Extensions.DependencyInjection.csproj | 48 +++++++++ .../ServiceCollectionExtensions.cs | 24 +++++ .../AddAxiosTests.cs | 99 +++++++++++++++++++ ...xtensions.DependencyInjection.Tests.csproj | 19 ++++ tests/Axios.Net.Tests/Axios.Net.Tests.csproj | 2 +- 8 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj create mode 100644 src/Axios.Net.Extensions.DependencyInjection/ServiceCollectionExtensions.cs create mode 100644 tests/Axios.Net.Extensions.DependencyInjection.Tests/AddAxiosTests.cs create mode 100644 tests/Axios.Net.Extensions.DependencyInjection.Tests/Axios.Net.Extensions.DependencyInjection.Tests.csproj diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a668b26..c689848 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,7 +40,7 @@ jobs: - name: Pack run: > - dotnet pack src/Axios.Net/Axios.Net.csproj + dotnet pack Axios.Net.slnx --configuration Release --no-build --output artifacts diff --git a/Axios.Net.slnx b/Axios.Net.slnx index c8abf87..14e784d 100644 --- a/Axios.Net.slnx +++ b/Axios.Net.slnx @@ -1,8 +1,10 @@ + + diff --git a/README.md b/README.md index e2acebb..3bdca33 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,38 @@ api.Interceptors.Response.Clear(); Request interceptors run last registered first, response interceptors first registered first. +## Dependency injection + +```bash +dotnet add package Axios.Net.Extensions.DependencyInjection +``` + +```csharp +using AxiosNet.Extensions.DependencyInjection; + +services.AddAxios(c => c.BaseUrl = "https://api.test/v1/"); +``` + +`AxiosClient` is registered as a singleton, so inject it where you need it: + +```csharp +public class UserService(AxiosClient api) +{ + public Task> Get(int id) => api.Get($"/users/{id}"); +} +``` + +The client sends its requests through an [`IHttpClientFactory`](https://learn.microsoft.com/dotnet/core/extensions/httpclient-factory) named client called `Axios.Net`. Chain on it to add handlers, such as logging or resilience: + +```csharp +services.AddHttpClient("Axios.Net") + .AddHttpMessageHandler(); +``` + +Because the handler comes from the factory, `MaxRedirects`, `Decompress` and `Proxy` are no longer read from the request config. Configure them on the primary handler instead (`ConfigurePrimaryHttpMessageHandler`). + +The singleton keeps the handler it was created with, so it is not recycled and DNS changes are not picked up for the lifetime of the app. + ## Not applicable in .NET These axios options are tied to browsers or Node.js and have no equivalent: `adapter` (pass an diff --git a/src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj b/src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj new file mode 100644 index 0000000..9fdc9fe --- /dev/null +++ b/src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj @@ -0,0 +1,48 @@ + + + + net8.0;net10.0 + enable + enable + AxiosNet.Extensions.DependencyInjection + + + + Axios.Net.Extensions.DependencyInjection + Axios.Net.Extensions.DependencyInjection + 0.1.0 + FrenchyApps42, Sikelio + Copyright (C) 2026 Frenchy Apps 42 + An axios-like HTTP client for .NET: Axios.Get<T>(url), Axios.Create(config), interceptors, request config merging, params and form serialization, progress, cancellation and typed errors. + axios;http;httpclient;rest;api;interceptors;client + AGPL-3.0-or-later + README.md + Initial release. + https://github.com/FrApp42/Axios.Net + https://github.com/FrApp42/Axios.Net.git + git + + + + true + $(NoWarn);CS1591 + true + snupkg + true + true + + + + + + + + + + + + + + + + diff --git a/src/Axios.Net.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/Axios.Net.Extensions.DependencyInjection/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..94897af --- /dev/null +++ b/src/Axios.Net.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -0,0 +1,24 @@ +using AxiosNet.Client; +using AxiosNet.Config; +using Microsoft.Extensions.DependencyInjection; + +namespace AxiosNet.Extensions.DependencyInjection; + +public static class ServiceCollectionExtensions +{ + public static IServiceCollection AddAxios(this IServiceCollection services, Action? configure = null) + { + services.AddHttpClient("Axios.Net"); + services.AddSingleton(sp => + { + AxiosRequestConfig axiosRequestConfig = new(); + HttpMessageHandler handler = sp.GetRequiredService().CreateHandler("Axios.Net"); + + configure?.Invoke(axiosRequestConfig); + + return new AxiosClient(axiosRequestConfig, handler); + }); + + return services; + } +} diff --git a/tests/Axios.Net.Extensions.DependencyInjection.Tests/AddAxiosTests.cs b/tests/Axios.Net.Extensions.DependencyInjection.Tests/AddAxiosTests.cs new file mode 100644 index 0000000..a17771e --- /dev/null +++ b/tests/Axios.Net.Extensions.DependencyInjection.Tests/AddAxiosTests.cs @@ -0,0 +1,99 @@ +using System.Net; +using AxiosNet.Client; +using Microsoft.Extensions.DependencyInjection; + +namespace AxiosNet.Extensions.DependencyInjection.Tests; + +[TestClass] +public class AddAxiosTests +{ + private sealed class CountingHandler : DelegatingHandler + { + public int Calls { get; private set; } + public Uri? LastUri { get; private set; } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken ct) + { + Calls++; + LastUri = request.RequestUri; + + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent("\"ok\"", System.Text.Encoding.UTF8, "application/json") + }); + } + } + + [TestMethod] + public void AddAxios_RegistersResolvableClient() + { + ServiceCollection services = new(); + services.AddAxios(); + + using ServiceProvider provider = services.BuildServiceProvider(); + + Assert.IsNotNull(provider.GetRequiredService()); + } + + [TestMethod] + public void AddAxios_AppliesConfiguredDefaults() + { + ServiceCollection services = new(); + services.AddAxios(c => c.BaseUrl = "https://api.test/v1/"); + + using ServiceProvider provider = services.BuildServiceProvider(); + + AxiosClient client = provider.GetRequiredService(); + + Assert.AreEqual("https://api.test/v1/", client.Defaults.BaseUrl); + } + + [TestMethod] + public void AddAxios_ClientIsSingleton() + { + ServiceCollection services = new(); + services.AddAxios(); + + using ServiceProvider provider = services.BuildServiceProvider(); + + Assert.AreSame(provider.GetRequiredService(), provider.GetRequiredService()); + } + + [TestMethod] + public async Task AddAxios_UsesHandlersFromHttpClientFactory() + { + CountingHandler handler = new(); + ServiceCollection services = new(); + + services.AddAxios(c => c.BaseUrl = "http://test"); + services.AddHttpClient("Axios.Net") + .ConfigurePrimaryHttpMessageHandler(() => handler); + + using ServiceProvider provider = services.BuildServiceProvider(); + + AxiosClient client = provider.GetRequiredService(); + + await client.Get("/x"); + + Assert.AreEqual(1, handler.Calls); + Assert.AreEqual("http://test/x", handler.LastUri!.AbsoluteUri); + } + + [TestMethod] + public async Task AddAxios_RunsDelegatingHandlersAddedToTheNamedClient() + { + CountingHandler counting = new(); + ServiceCollection services = new(); + + services.AddAxios(c => c.BaseUrl = "http://test"); + services.AddHttpClient("Axios.Net") + .AddHttpMessageHandler(() => counting) + .ConfigurePrimaryHttpMessageHandler(() => new CountingHandler()); + + using ServiceProvider provider = services.BuildServiceProvider(); + + await provider.GetRequiredService().Get("/x"); + + Assert.AreEqual(1, counting.Calls); + } +} diff --git a/tests/Axios.Net.Extensions.DependencyInjection.Tests/Axios.Net.Extensions.DependencyInjection.Tests.csproj b/tests/Axios.Net.Extensions.DependencyInjection.Tests/Axios.Net.Extensions.DependencyInjection.Tests.csproj new file mode 100644 index 0000000..dc5a7ca --- /dev/null +++ b/tests/Axios.Net.Extensions.DependencyInjection.Tests/Axios.Net.Extensions.DependencyInjection.Tests.csproj @@ -0,0 +1,19 @@ + + + + net10.0 + enable + enable + false + + + + + + + + + + + + diff --git a/tests/Axios.Net.Tests/Axios.Net.Tests.csproj b/tests/Axios.Net.Tests/Axios.Net.Tests.csproj index d32d481..473c4e3 100644 --- a/tests/Axios.Net.Tests/Axios.Net.Tests.csproj +++ b/tests/Axios.Net.Tests/Axios.Net.Tests.csproj @@ -1,4 +1,4 @@ - + net10.0 From fc006dc077acac3de66a40db2dab14ff62e558a1 Mon Sep 17 00:00:00 2001 From: Sik Date: Sun, 1 Nov 2026 17:15:41 +0100 Subject: [PATCH 2/2] ci: version the DI package independently with di-v tags --- .github/workflows/release.yml | 31 ++++++++----- ....Net.Extensions.DependencyInjection.csproj | 45 ++++++++++--------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c689848..86e9146 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,10 +4,11 @@ on: push: tags: - "v*.*.*" + - "di-v*.*.*" permissions: contents: write - id-token: write # GitHub OIDC token used by NuGet Trusted Publishing + id-token: write jobs: release: @@ -25,29 +26,39 @@ jobs: 8.0.x 10.0.x - - name: Get version from tag - id: version - run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT" + - name: Resolve project and version + id: target + run: | + case "$GITHUB_REF_NAME" in + di-v*) + echo "project=src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj" >> "$GITHUB_OUTPUT" + echo "version=${GITHUB_REF_NAME#di-v}" >> "$GITHUB_OUTPUT" + echo "prop=DiVersion" >> "$GITHUB_OUTPUT" + ;; + *) + echo "project=src/Axios.Net/Axios.Net.csproj" >> "$GITHUB_OUTPUT" + echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT" + echo "prop=Version" >> "$GITHUB_OUTPUT" + ;; + esac - name: Restore run: dotnet restore Axios.Net.slnx - name: Build - run: dotnet build Axios.Net.slnx --configuration Release --no-restore -p:Version=${{ steps.version.outputs.version }} + run: dotnet build Axios.Net.slnx --configuration Release --no-restore -p:${{ steps.target.outputs.prop }}=${{ steps.target.outputs.version }} - name: Test run: dotnet test Axios.Net.slnx --configuration Release --no-build --verbosity normal - name: Pack run: > - dotnet pack Axios.Net.slnx + dotnet pack ${{ steps.target.outputs.project }} --configuration Release --no-build --output artifacts - -p:Version=${{ steps.version.outputs.version }} + -p:${{ steps.target.outputs.prop }}=${{ steps.target.outputs.version }} - # Trusted Publishing: exchanges the GitHub OIDC token for a short-lived (1 hour) NuGet API key, - # so it must run right before the push. - name: NuGet login id: login uses: NuGet/login@v1 @@ -73,7 +84,7 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | prerelease="" - if [[ "$GITHUB_REF_NAME" == *-* ]]; then + if [[ "${{ steps.target.outputs.version }}" == *-* ]]; then prerelease="--prerelease" fi diff --git a/src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj b/src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj index 9fdc9fe..8c69382 100644 --- a/src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj +++ b/src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj @@ -4,37 +4,38 @@ net8.0;net10.0 enable enable - AxiosNet.Extensions.DependencyInjection + AxiosNet.Extensions.DependencyInjection - Axios.Net.Extensions.DependencyInjection - Axios.Net.Extensions.DependencyInjection - 0.1.0 - FrenchyApps42, Sikelio - Copyright (C) 2026 Frenchy Apps 42 - An axios-like HTTP client for .NET: Axios.Get<T>(url), Axios.Create(config), interceptors, request config merging, params and form serialization, progress, cancellation and typed errors. - axios;http;httpclient;rest;api;interceptors;client - AGPL-3.0-or-later - README.md - Initial release. - https://github.com/FrApp42/Axios.Net - https://github.com/FrApp42/Axios.Net.git - git + Axios.Net.Extensions.DependencyInjection + Axios.Net.Extensions.DependencyInjection + 0.1.0 + $(DiVersion) + FrenchyApps42, Sikelio + Copyright (C) 2026 Frenchy Apps 42 + An axios-like HTTP client for .NET: Axios.Get<T>(url), Axios.Create(config), interceptors, request config merging, params and form serialization, progress, cancellation and typed errors. + axios;http;httpclient;rest;api;interceptors;client + AGPL-3.0-or-later + README.md + Initial release. + https://github.com/FrApp42/Axios.Net + https://github.com/FrApp42/Axios.Net.git + git - true - $(NoWarn);CS1591 - true - snupkg - true - true + true + $(NoWarn);CS1591 + true + snupkg + true + true - - + +