{"id":15011535,"url":"https://github.com/co-it/csharpfunctionalextensions.httpresults","last_synced_at":"2025-10-30T05:50:07.129Z","repository":{"id":244798770,"uuid":"814730819","full_name":"co-IT/CSharpFunctionalExtensions.HttpResults","owner":"co-IT","description":"Extensions for CSharpFunctionalExtensions to map Results to HttpResults in your Web-API","archived":false,"fork":false,"pushed_at":"2025-03-19T13:28:43.000Z","size":149,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T13:47:30.133Z","etag":null,"topics":["actionresult","api","csharp","csharpfunctionalextensions","dotnet","http","minimal","nuget","result"],"latest_commit_sha":null,"homepage":"https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/co-IT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-13T15:29:56.000Z","updated_at":"2025-03-19T13:28:09.000Z","dependencies_parsed_at":"2024-09-30T04:00:37.129Z","dependency_job_id":"1080b01b-886d-4368-aece-c30b8f9f3ea6","html_url":"https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults","commit_stats":{"total_commits":33,"total_committers":1,"mean_commits":33.0,"dds":0.0,"last_synced_commit":"fe0533ef50c1f2096748f398f6d0d511af652b87"},"previous_names":["co-it/csharpfunctionalextensions.httpresults"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co-IT%2FCSharpFunctionalExtensions.HttpResults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co-IT%2FCSharpFunctionalExtensions.HttpResults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co-IT%2FCSharpFunctionalExtensions.HttpResults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co-IT%2FCSharpFunctionalExtensions.HttpResults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/co-IT","download_url":"https://codeload.github.com/co-IT/CSharpFunctionalExtensions.HttpResults/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248512707,"owners_count":21116664,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actionresult","api","csharp","csharpfunctionalextensions","dotnet","http","minimal","nuget","result"],"created_at":"2024-09-24T19:41:13.172Z","updated_at":"2025-10-30T05:50:07.123Z","avatar_url":"https://github.com/co-IT.png","language":"C#","readme":"# CSharpFunctionalExtensions.HttpResults\n\n[![dotnet](https://img.shields.io/badge/platform-.NET-blue)](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/)\n[![nuget version](https://img.shields.io/nuget/v/CSharpFunctionalExtensions.HttpResults)](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/)\n[![nuget downloads](https://img.shields.io/nuget/dt/CSharpFunctionalExtensions.HttpResults)](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/)\n[![GitHub license](https://img.shields.io/github/license/co-IT/CSharpFunctionalExtensions.HttpResults)](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/LICENSE.md)\n\nSeamlessly map Results from [CSharpFunctionalExtensions](https://github.com/vkhorikov/CSharpFunctionalExtensions) to HttpResults for cleaner, more fluent Web APIs\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003eTable of Contents\u003c/b\u003e\u003c/summary\u003e\n\n1. [Overview](#overview)\n2. [Installation](#installation)\n3. [Usage](#usage)\n    1. [Available methods](#available-methods)\n    2. [Default mapping](#default-mapping)\n    3. [Custom error mapping](#custom-error-mapping)\n4. [Analyzers](#analyzers)\n5. [Examples](#examples)\n6. [Development](#development)\n\u003c/details\u003e\n\n## Overview\n\nThis library provides convenient extension methods to seamlessly map Results from [CSharpFunctionalExtensions](https://github.com/vkhorikov/CSharpFunctionalExtensions) to HttpResults. With this, it streamlines your Web API resulting in cleaner, more fluent code.\n\n### Key Benefits\n\n- ⚙️ **Zero Configuration:** Get started immediately — the mapping works out of the box without any configuration.\n- 🛠️ **Customizable Mappings:** Tailor default mappings or define custom mappings for specific use cases.\n- 🔗 **Fluent API:** Maintain a smooth, railway-oriented flow by chaining HttpResult mappings at the end of your Result chain.\n- 🧱 **Separation of Domain and HTTP Errors:** Keeps domain errors distinct from HTTP errors, improving maintainability and clarity between business logic and web API concerns.\n- ⚡ **Minimal APIs \u0026 Controllers Support:** Works with both Minimal APIs and traditional controllers in ASP.NET.\n- 📦 **Full Support for ASP.NET Results:** Supports all built-in HTTP response types in ASP.NET, including `Ok`, `Created`, `NoContent`, `Accepted`, `FileStream`, and more.\n- 🦺 **Typed Results:** Utilizes `TypedResults` for consistent, type-safe API responses.\n- 📑 **OpenAPI Ready:** Ensures accurate OpenAPI generation for clear and reliable API documentation.\n- 🛡️ **RFC Compliance:** Default mappings adhere to the RFC 9457 standard (`ProblemDetails`), ensuring your API errors are standardized and interoperable.\n- 🧑‍💻 **Developer-Friendly:** Includes built-in analyzers and source generators to speed up development and reduce errors.\n\n## Installation\n\nAvailable on [NuGet](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/).\n\n```bash\ndotnet add package CSharpFunctionalExtensions.HttpResults\n```\n\nor\n\n```powershell\nPM\u003e Install-Package CSharpFunctionalExtensions.HttpResults\n```\n\n\u003e [!NOTE]\n\u003e This library references an older version of CSharpFunctionalExtensions for wider compatibility.\n\u003e It's recommended to additionally install the latest version of CSharpFunctionalExtensions in your project to get the latest features and fixes.\n\n## Usage\n\nThis library provides you extension methods to map the following `Result` types to `HttpResults` at the end of our result chain:\n\n- `Result`\n- `Result\u003cT\u003e`\n- `Result\u003cT,E\u003e`\n- `UnitResult\u003cE\u003e`\n\n*Example:*\n```csharp\napp.MapGet(\"/books\", (BookService service) =\u003e\n    service.Get()       //Result\u003cBook[]\u003e\n      .ToOkHttpResult() //Results\u003cOk\u003cBook[]\u003e, ProblemHttpResult\u003e\n);\n```\n\n### Available methods\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u003cb\u003eClick here\u003c/b\u003e to view all available methods.\u003c/summary\u003e\n\n| Method                                | Short Description                                                            |\n|---------------------------------------|------------------------------------------------------------------------------|\n| `.ToStatusCodeHttpResult()`           | Returns `StatusCodeHttpResult` or `ProblemHttpResult`                        |\n| `.ToStatusCodeHttpResult\u003cT\u003e()`        | Returns `StatusCodeHttpResult` or `ProblemHttpResult`                        |\n| `.ToStatusCodeHttpResult\u003cT,E\u003e()`      | Returns `StatusCodeHttpResult` or custom error                               |\n| `.ToStatusCodeHttpResult\u003cE\u003e()`        | Returns `StatusCodeHttpResult` or custom error                               |\n| `.ToJsonHttpResult\u003cT\u003e()`              | Returns `JsonHttpResult\u003cT\u003e` or `ProblemHttpResult`                           |\n| `.ToJsonHttpResult\u003cT,E\u003e()`            | Returns `JsonHttpResult\u003cT\u003e` or custom error                                  |\n| `.ToOkHttpResult\u003cT\u003e()`                | Returns `Ok\u003cT\u003e` or `ProblemHttpResult`                                       |\n| `.ToOkHttpResult\u003cT,E\u003e()`              | Returns `Ok\u003cT\u003e` or custom error                                              |\n| `.ToNoContentHttpResult()`            | Returns `NoContent` or `ProblemHttpResult`                                   |\n| `.ToNoContentHttpResult\u003cT\u003e()`         | Discards value of `Result\u003cT\u003e` and returns `NoContent` or `ProblemHttpResult` |\n| `.ToNoContentHttpResult\u003cT,E\u003e()`       | Discards value of `Result\u003cT\u003e` and returns `NoContent` or custom error        |\n| `.ToNoContentHttpResult\u003cE\u003e()`         | Returns `NoContent` or custom error                                          |\n| `.ToCreatedHttpResult\u003cT\u003e()`           | Returns `Created\u003cT\u003e` or `ProblemHttpResult`                                  |\n| `.ToCreatedHttpResult\u003cT,E\u003e()`         | Returns `Created\u003cT\u003e` or custom error                                         |\n| `.ToCreatedAtRouteHttpResult\u003cT\u003e()`    | Returns `CreatedAtRoute\u003cT\u003e` or `ProblemHttpResult`                           |\n| `.ToCreatedAtRouteHttpResult\u003cT,E\u003e()`  | Returns `CreatedAtRoute\u003cT\u003e` or custom error                                  |\n| `.ToAcceptedHttpResult\u003cT\u003e()`          | Returns `Accepted\u003cT\u003e` or `ProblemHttpResult`                                 |\n| `.ToAcceptedHttpResult\u003cT,E\u003e()`        | Returns `Accepted\u003cT\u003e` or custom error                                        |\n| `.ToAcceptedAtRouteHttpResult\u003cT\u003e()`   | Returns `AcceptedAtRoute\u003cT\u003e` or `ProblemHttpResult`                          |\n| `.ToAcceptedAtRouteHttpResult\u003cT,E\u003e()` | Returns `AcceptedAtRoute\u003cT\u003e` or custom error                                 |\n| `.ToFileHttpResult\u003cbyte[]\u003e()`         | Returns `FileContentHttpResult` or `ProblemHttpResult`                       |\n| `.ToFileHttpResult\u003cbyte[],E\u003e()`       | Returns `FileContentHttpResult` or custom error                              |\n| `.ToFileStreamHttpResult\u003cStream\u003e()`   | Returns `FileStreamHttpResult` or `ProblemHttpResult`                        |\n| `.ToFileStreamHttpResult\u003cStream,E\u003e()` | Returns `FileStreamHttpResult` or custom error                               |\n| `.ToContentHttpResult\u003cstring\u003e()`      | Returns `ContentHttpResult` or `ProblemHttpResult`                           |\n| `.ToContentHttpResult\u003cstring,E\u003e()`    | Returns `ContentHttpResult` or custom error                                  |\n\u003c/details\u003e\n\nAll methods are available in sync and async variants.\n\n### Default mapping\n\nBy default, `Result` and `Result\u003cT\u003e` failures are mapped to a `ProblemHttpResult` based on [RFC9457](https://www.rfc-editor.org/rfc/rfc9457).\n\n- The `status` property contains the status code of the HTTP response. Note: For almost every method you can override the default status codes for Success/Failure case.\n- The `type` property contains a URI to the corresponding [RFC9110](https://tools.ietf.org/html/rfc9110) entry based on the status code.\n- The `title` property contains a generic short messages based on the status code.\n- The `detail` property contains the error string of the `Result`.\n\nThis default mapping behaviour is configured inside the [`ProblemDetailsMappingProvider`](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults/ProblemDetailsMappingProvider.cs).\n\n#### Override default mapping\n\nYou can override this behavior by providing your own dictionary that maps status codes to their corresponding `title` and `type` of the resulting `ProblemDetails` object.\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003eClick here\u003c/b\u003e to see an example of changing the default mapping for German localization.\u003c/summary\u003e\n\n```csharp\nProblemDetailsMappingProvider.DefaultMappings = new Dictionary\u003cint, (string? Title, string? Type)\u003e\n{\n  { 400, (\"Ungültige Anfrage\", \"https://tools.ietf.org/html/rfc9110#section-15.5.1\") },\n  { 401, (\"Nicht autorisiert\", \"https://tools.ietf.org/html/rfc9110#section-15.5.2\") },\n  { 403, (\"Verboten\", \"https://tools.ietf.org/html/rfc9110#section-15.5.4\") },\n  { 404, (\"Nicht gefunden\", \"https://tools.ietf.org/html/rfc9110#section-15.5.5\") },\n  { 405, (\"Methode nicht erlaubt\", \"https://tools.ietf.org/html/rfc9110#section-15.5.6\") },\n  { 406, (\"Nicht akzeptabel\", \"https://tools.ietf.org/html/rfc9110#section-15.5.7\") },\n  { 408, (\"Zeitüberschreitung der Anfrage\", \"https://tools.ietf.org/html/rfc9110#section-15.5.9\") },\n  { 409, (\"Konflikt\", \"https://tools.ietf.org/html/rfc9110#section-15.5.10\") },\n  { 412, (\"Vorbedingung fehlgeschlagen\", \"https://tools.ietf.org/html/rfc9110#section-15.5.13\") },\n  { 415, (\"Nicht unterstützter Medientyp\", \"https://tools.ietf.org/html/rfc9110#section-15.5.16\") },\n  { 422, (\"Nicht verarbeitbare Entität\", \"https://tools.ietf.org/html/rfc4918#section-11.2\") },\n  { 426, (\"Upgrade erforderlich\", \"https://tools.ietf.org/html/rfc9110#section-15.5.22\") },\n  { 500, (\"Ein Fehler ist bei der Verarbeitung Ihrer Anfrage aufgetreten.\", \"https://tools.ietf.org/html/rfc9110#section-15.6.1\") },\n  { 502, (\"Schlechtes Gateway\", \"https://tools.ietf.org/html/rfc9110#section-15.6.3\") },\n  { 503, (\"Dienst nicht verfügbar\", \"https://tools.ietf.org/html/rfc9110#section-15.6.4\") },\n  { 504, (\"Gateway-Zeitüberschreitung\", \"https://tools.ietf.org/html/rfc9110#section-15.6.5\") },\n};\n```\n\n\u003e Example from [here](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Program.cs#L9-L34)\n\n\u003c/details\u003e\n\nYou don't have to provide the whole dictionary; you can also override or add mappings for specific status codes like this:\n\n```csharp\nProblemDetailsMappingProvider.AddOrUpdateMapping(420, \"Enhance Your Calm\", \"https://http-status-code.de/420/\");\n```\n\nIt's recommended to override the mappings during startup e.g. in `Program.cs`.\n\n#### Override mapping for single use case\n\nIf you need to override the mapping for a specific use case in a single location, you can provide an `Action\u003cProblemDetails\u003e` to fully customize the `ProblemDetails`. This is particularly useful when you want to add extensions or tailor the `ProblemDetails` specifically for that use case.\n\n```csharp\n...\n.ToOkHttpResult(customizeProblemDetails: problemDetails =\u003e\n{\n  problemDetails.Title = \"Custom Title\";\n  problemDetails.Extensions.Add(\"custom\", \"value\");\n});\n```\n\n### Custom error mapping\n\nWhen using `Result\u003cT,E\u003e` or `UnitResult\u003cE\u003e`, this library uses a Source Generator to generate extension methods for your own custom error types.\n\n1. Create a custom error type\n    ```csharp\n    public record UserNotFoundError(string UserId);\n    ```\n2. Create a mapper that implements `IResultErrorMapper` which maps this custom error type to an HttpResult / `Microsoft.AspNetCore.Http.IResult` that you want to return in your Web API:\n    ```csharp\n    public class UserNotFoundErrorMapper : IResultErrorMapper\u003cUserNotFoundError, ProblemHttpResult\u003e\n    {\n        public ProblemHttpResult Map(UserNotFoundError error)\n        {\n            var problemDetails = new ProblemDetails\n            {\n                Status = 404,\n                Title = \"User not found\",\n                Type = \"https://tools.ietf.org/html/rfc9110#section-15.5.5\",\n                Detail = $\"The user with ID {error.UserId} couldn't be found.\n            };\n            \n            return TypedResults.Problem(problemDetails);  \n        };\n    }\n    ```\n3. Use the auto-generated extension method:\n    ```csharp\n    app.MapGet(\"/users/{id}\", (string id, UserRepository repo) =\u003e\n        repo.Find(id)       //Result\u003cUser,UserNotFoundError\u003e\n          .ToOkHttpResult() //Results\u003cOk\u003cUser\u003e,ProblemHttpResult\u003e\n    );\n    ```\n\n\u003e [!IMPORTANT]  \n\u003e Make sure that each custom error type has exactly one corresponding `IResultMapper` implementation.\n\n\u003e [!TIP]\n\u003e You can use the `ProblemDetailsMappingProvider.FindMapping()` method to find a suitable title and type for a status code based on [RFC9110](https://tools.ietf.org/html/rfc9110).\n\n\u003e [!NOTE]\n\u003e If extension methods for custom errors are missing, rebuild the project to trigger Source Generation.\n\n## Analyzers\n\nThis library includes analyzers to help you use it correctly.\n\nFor example, they will notify you if you have multiple mappers for the same custom error type or if your mapper class doesn't have a parameterless constructor.\n\nYou can find a complete list of all analyzers [here](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Generators/AnalyzerReleases.Shipped.md).\n\n## Examples\n\nThe [`CSharpFunctionalExtensions.HttpResults.Examples`](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples) project contains various examples demonstrating how to use this library in different scenarios, including:\n\n- **[Basic CRUD operations](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/CRUD)** – Handling `GET`, `POST`, `PUT`, and `DELETE` requests\n- **[File handling](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/FileStream)** – Returning files from your Web API\n- **[Custom error mapping](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/CustomError)** – Defining and mapping custom error types to meaningful HTTP responses\n- **[Multiple errors in chain](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/MultipleErrorChain)** – Using different kind of custom errors in the same result chain\n- **[Customizing default mapping](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Program.cs)** – Overriding default mappings for localization or specific use cases\n\nCheck out the example project for hands-on implementation details!\n\n## Development\n\nContributions are welcome! Please keep the following rules in mind:\n\n- add documentation in the form of summary comments\n- add tests for your additions\n- add sync and async variants where possible\n- refer to existing code files and the folder structure when adding something\n\nThis project uses [CSharpier](https://csharpier.com) for code formatting. You can format your code with `dotnet csharpier .`.\n\n### Add new extension methods\n\nTo add new methods follow these steps:\n\n1. Add methods for `Result` and `Result\u003cT\u003e` to `CSharpFunctionalExtensions.HttpResults.ResultExtensions`\n2. Add methods for `Result\u003cT,E\u003e` to `CSharpFunctionalExtensions.HttpResults.Generators.ResultExtensions` and add the class to `ResultExtensionsClassBuilder`\n3. Add methods for `UnitResult\u003cE\u003e` to `CSharpFunctionalExtensions.HttpResults.Generators.UnitResultExtensions` and add the class to `UnitResultExtensionsClassBuilder`\n4. Add tests for **all** new methods to `CSharpFunctionalExtensions.HttpResults.Tests`\n5. Add methods to [README](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/README.md)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fco-it%2Fcsharpfunctionalextensions.httpresults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fco-it%2Fcsharpfunctionalextensions.httpresults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fco-it%2Fcsharpfunctionalextensions.httpresults/lists"}