HashIds extensions and bindings for Minimal API.
$ dotnet add package MinApiLib.HashedIdsThis package contains extensions to use hashed ids in your endpoints. It uses the Hashids library.
You can install this package using the NuGet package manager:
Install-Package MinApiLib.HashedIds
Or using the .NET CLI:
dotnet add package MinApiLib.HashedIds
To use hashed ids, you can add the AddHashedIds method in your Program.cs file:
global using MinApiLib.HashedIds;
// ...
builder.Services.AddHashedIds(op => op.Passphrase = "my secret passphrase");
Then, you can use the HashedId type in your endpoints:
public record struct Response(HashedId Id, string Name);
public record Get() : GetEndpoint("/things")
{
public IResult Handle()
=> Results.Ok(new Response(1, "John Doe"));
}
And test it with curl:
$ curl "http://localhost:5000/things"
{"id":"Wd","name":"John Doe"}%
You can also use the HashedId type in your requests and responses:
public record struct Request(HashedId Id, CancellationToken CancellationToken);
public record struct Response(HashedId Id, string Name);
public record GetThing() : GetEndpoint<Request>("/things/{id}")
{
protected override Task<IResult> OnHandleAsync(Request request, CancellationToken cancellationToken)
{
int id = request.Id;
var response = new Response(id, "John Doe");
return Task.FromResult(Results.Ok(response));
}
}
And test it:
$ curl "http://localhost:5000/things/Wd"
{"id":"Wd","name":"John Doe"}%