Kemenkeu.ApiWrapper.Core 1.0.0

dotnet add package Kemenkeu.ApiWrapper.Core --version 1.0.0                
NuGet\Install-Package Kemenkeu.ApiWrapper.Core -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kemenkeu.ApiWrapper.Core" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Kemenkeu.ApiWrapper.Core --version 1.0.0                
#r "nuget: Kemenkeu.ApiWrapper.Core, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Kemenkeu.ApiWrapper.Core as a Cake Addin
#addin nuget:?package=Kemenkeu.ApiWrapper.Core&version=1.0.0

// Install Kemenkeu.ApiWrapper.Core as a Cake Tool
#tool nuget:?package=Kemenkeu.ApiWrapper.Core&version=1.0.0                

Kemenkeu.ApiWrapper.Core

Kemenkeu.ApiWrapper.Core is a library designed to standardize API responses in .NET applications. It provides a consistent response format, error handling, pagination support, and integrates easily with ASP.NET Core.

Features

  • Standardized API Response: Easily wrap your API responses in a consistent format.
  • Custom Exception Handling: Automatically handle exceptions and return structured error messages.
  • Pagination Support: Manage pagination metadata effectively.
  • Global Filters: Automatically apply response formatting to all controllers without additional configuration.

Compatibility

This library supports multiple frameworks:

  • .NET Core 3.1
  • .NET 5.0
  • .NET 6.0 and later

Installation

From NuGet

To install the Kemenkeu.ApiWrapper.Core library, use the following command in your terminal or package manager console:

dotnet add package Kemenkeu.ApiWrapper.Core

Quick Start Guide

1. Configure Services

Add the library to your Startup.cs file in the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddKemenkeuApiWrapper();
}

2. Implement Global Filters

To enable the global response wrapping, simply add the KemenkeuApiWrapper middleware in the Configure method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();
    app.UseAuthorization();
    
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    // Add the ApiWrapper middleware
    app.UseKemenkeuApiWrapper();
}

3. Using the API Response Wrapper

You can now use the response wrapper in your controllers. Here's an example of a simple controller:

[ApiController]
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
    [HttpGet]
    public IActionResult GetSampleData()
    {
        var sampleData = new { Id = 1, Name = "Sample" };
        return Ok(sampleData); // Automatically wrapped in ApiResponse
    }

    [HttpGet("{id}")]
    public IActionResult GetSampleData(int id)
    {
        if (id <= 0)
        {
            return BadRequest(new ApiResponse { IsError = true, StatusCode = 400, Errors = new[] { "Invalid ID" } });
        }

        // Fetch data logic here...

        return Ok(sampleData); // Automatically wrapped in ApiResponse
    }
}

4. Pagination Support

To use pagination, specify pagination metadata on your actions:

[HttpGet]
[PaginatedResponse] // Custom attribute to override response if necessary
public IActionResult GetPaginatedData(int pageNumber = 1, int pageSize = 10)
{
    // Fetch paginated data logic here...

    var data = GetData(pageNumber, pageSize);
    return Ok(data); // Pagination metadata will be included in headers
}

Contributing

If you would like to contribute to Kemenkeu.ApiWrapper.Core, please fork the repository and submit a pull request. We welcome contributions, bug reports, and feature requests!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 76 11/8/2024