Install
| Product | Versions |
|---|---|
| .NET |
README
Danom.MinimalApi
Danom.MinimalApi is a library that provides a set of utilities to help integrate the Danom library with common tasks in ASP.NET Core Minimal API applications.
Key Features
- Extension methods for converting
Option<T>andResult<T, TError>types toIResultresponses. - Support for both standard and typed results in Minimal APIs.
Design Goals
- Simplify the handling of optional and result-based data in Minimal APIs.
- Provide a clean and intuitive API for developers familiar with Danom and ASP.NET Core.
Getting Started
Install the Danom.MinimalApi NuGet package:
PM> Install-Package Danom.MinimalApi
Or using the dotnet CLI
dotnet add package Danom.MinimalApi
Basic Usage
Returning Option
- Some: Returns
200 OKwith the value. - None: Return
404 Not Found(or a custom result, if provided).
app.MapGet("/user/{id}", (int id) => {
var user = FindUser(id);
return Results.Extensions.Option(user);
});
Or, with a custom result:
app.MapGet("/user/{id}", (int id) => {
var user = FindUser(id);
return Results.Extensions.Option(user, () => Results.Conflict());
});
Returning Result
- Ok: Returns
200 OKwith the value. - Error: Returns
400 Bad Request(or a customIResult, if provided).
app.MapPost("/user", (User user) => {
var result = TryCreateUser(user);
return Results.Extensions.Result(result);
});
Or, with a custom result:
app.MapPost("/user", (User user) => {
var result = TryCreateUser(user);
return Results.Extensions.Result(result, error => Results.UnprocessableEntity());
});
Typed Results
If you want to use ASP.NET Core's typed results, Danom.MinimalApi provides additional extension methods.
Returning Typed Option
- Some: Returns
OptionHttpResult<T>and200 OK. - None: Returns
OptionHttpResult<T>and404 Not Found(or a customIResult, if provided).
using Danom;
using Danom.MinimalApi;
app.MapGet("/user/{id}", (int id) => {
var user = FindUser(id);
return DanomTypedResults.Option(user);
});
// or, with a custom error handler
app.MapGet("/user/{id}/custom", (int id) => {
var user = FindUser(id);
return DanomTypedResults.Option(user,
noneResult: () => Results.NotFound("Custom not found!"));
});
Returning Typed Result
- Ok: Returns
ResultHttpResult<T, TError>and200 OK. - Error: Returns
ResultHttpResult<T, TError>and400 Bad Request
using Danom;
using Danom.MinimalApi;
app.MapPost("/user", (User user) => {
var result = TryCreateUser(user);
return DanomTypedResults.Result(result);
});
// or, with a custom error handler
app.MapPost("/user/custom", (User user) => {
var result = TryCreateUser(user);
return DanomTypedResults.Result(result,
errorResult: error => Results.Ok(new { Message = "There was a problem", error }));
});
// or, return a problem details response
app.MapPost("/user/problem", (User user) => {
var result = TryCreateUser(user);
return DanomTypedResults.ResultProblem(result);
});
Contributing
I kindly ask that before submitting a pull request, you first submit an issue.
If functionality is added to the API, or changed, please kindly update the relevant documentation. Unit tests must also be added and/or updated before a pull request can be successfully merged.
Only pull requests which pass all build checks and comply with the general coding standard can be approved.
Find a bug?
There's an issue for that.
License
Licensed under MIT.
Changes from Previous Version
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.