Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 src/Axios.Net/Axios.Net.csproj
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
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Axios.Net.slnx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<Solution>
<Folder Name="/src/">
<Project Path="src/Axios.Net.Extensions.DependencyInjection/Axios.Net.Extensions.DependencyInjection.csproj" Id="15241509-07d4-46b5-960a-9c990cca2f3f" />
<Project Path="src/Axios.Net/Axios.Net.csproj" />
</Folder>
<Folder Name="/tests/">
<Project Path="tests/Axios.Net.Extensions.DependencyInjection.Tests/Axios.Net.Extensions.DependencyInjection.Tests.csproj" Id="530691f0-0edc-4ef5-ac66-a7214df8ba77" />
<Project Path="tests/Axios.Net.Tests/Axios.Net.Tests.csproj" />
</Folder>
</Solution>
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AxiosResponse<User>> Get(int id) => api.Get<User>($"/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<MyLoggingHandler>();
```

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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>AxiosNet.Extensions.DependencyInjection</RootNamespace>
</PropertyGroup>

<PropertyGroup Label="Package">
<PackageId>Axios.Net.Extensions.DependencyInjection</PackageId>
<Title>Axios.Net.Extensions.DependencyInjection</Title>
<Version>0.1.0</Version>
<Version Condition="'$(DiVersion)' != ''">$(DiVersion)</Version>
<Authors>FrenchyApps42, Sikelio</Authors>
<Copyright>Copyright (C) 2026 Frenchy Apps 42</Copyright>
<Description>An axios-like HTTP client for .NET: Axios.Get&lt;T&gt;(url), Axios.Create(config), interceptors, request config merging, params and form serialization, progress, cancellation and typed errors.</Description>
<PackageTags>axios;http;httpclient;rest;api;interceptors;client</PackageTags>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>Initial release.</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/FrApp42/Axios.Net</PackageProjectUrl>
<RepositoryUrl>https://github.com/FrApp42/Axios.Net.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<PropertyGroup Label="Build">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Deterministic>true</Deterministic>
<ContinuousIntegrationBuild Condition="'$(CI)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Axios.Net\Axios.Net.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<AxiosRequestConfig>? configure = null)
{
services.AddHttpClient("Axios.Net");
services.AddSingleton(sp =>
{
AxiosRequestConfig axiosRequestConfig = new();
HttpMessageHandler handler = sp.GetRequiredService<IHttpMessageHandlerFactory>().CreateHandler("Axios.Net");

configure?.Invoke(axiosRequestConfig);

return new AxiosClient(axiosRequestConfig, handler);
});

return services;
}
}
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> 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<AxiosClient>());
}

[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<AxiosClient>();

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<AxiosClient>(), provider.GetRequiredService<AxiosClient>());
}

[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<AxiosClient>();

await client.Get<string>("/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<AxiosClient>().Get<string>("/x");

Assert.AreEqual(1, counting.Calls);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest" Version="4.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Axios.Net.Extensions.DependencyInjection\Axios.Net.Extensions.DependencyInjection.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion tests/Axios.Net.Tests/Axios.Net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
Expand Down
Loading