Vortex.Api
0.1.0
dotnet add package Vortex.Api --version 0.1.0
NuGet\Install-Package Vortex.Api -Version 0.1.0
<PackageReference Include="Vortex.Api" Version="0.1.0" />
paket add Vortex.Api --version 0.1.0
#r "nuget: Vortex.Api, 0.1.0"
// Install Vortex.Api as a Cake Addin #addin nuget:?package=Vortex.Api&version=0.1.0 // Install Vortex.Api as a Cake Tool #tool nuget:?package=Vortex.Api&version=0.1.0
Vortex.Api - F# Minimal API Wrapper
<div align="center"> <img src="./images/Vortex-logo.png" width="250"> </div>
Overview
Vortex.Api provides a simple and effective way to configure routing for HTTP methods such as GET, POST, PUT, and DELETE within an F# application using the ASP.NET Core Minimal APIs. This module is designed to simplify the process of binding HTTP methods to specific request handlers.
Installation
To use the Vortex.Api
module, include it in your F# project by copying the source files directly or referencing the library if it's packaged. Ensure that your project references the Microsoft.AspNetCore.App
framework.
Usage
Configuring the Web Application
Here's how to use the Vortex.Api
to map different HTTP methods:
open Vortex.Api
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Http
let handler (context: HttpContext) =
async {
// Logic to handle the request
return "Hello World!"
} |> Async.StartAsTask
let configureApp (app: WebApplication) =
app |> mapGet "/api/hello" handler
|> mapPost "/api/post" handler
|> mapPut "/api/put" handler
|> mapDelete "/api/delete" handler
app.Run()
Vortex.Http - HttpContext Extensions
Overview
Vortex.Http enhances the HttpContext by providing extension methods that facilitate easier access to services, request headers, cookies, and query parameters. It simplifies operations such as retrieving and deserializing request bodies and extracting service instances or loggers from the dependency injection container.
Extension Methods
Logging and Services
GetLogger<'service>()
: Retrieves a logger service for the specified type, enabling easy logging within the request context.GetService<'service>()
: Retrieves a service instance from the ASP.NET Core dependency injection container, allowing for decoupled and testable code.
Request Data
GetHeader(name: string)
: Returns the value of a specified request header, or an empty string if the header is not found.GetCookie(key: string)
: Retrieves the value of a specified cookie, ornull
if the cookie does not exist.GetBody<'body>()
: Deserializes the request body into a specified type. It throws an exception if deserialization fails.TryGetBody<'body>()
: Tries to deserialize the request body into a specified type safely, returningNone
if unsuccessful to avoid exceptions.
Query Parameters
GetQuery(name: string)
: Retrieves the first value of a specified query parameter as a string, or an empty string if not present.GetQueryInt(name: string)
: Retrieves the first value of a specified query parameter, converted to an integer. Throws aFormatException
if the conversion fails.GetQueryFloat(name: string)
: Retrieves the first value of a specified query parameter, converted to a float. Includes error handling for conversion failures.GetQueryDecimal(name: string)
: Retrieves the first value of a specified query parameter, converted to a decimal. Error handling for conversion issues is included.GetQueryDateTime(name: string)
: Converts the first value of a specified query parameter to aDateTime
object. It throws aFormatException
if the conversion is not possible.GetQueryDateTimeOffset(name: string)
: Retrieves the first value of a specified query parameter and converts it to aDateTimeOffset
object, with error handling for conversion failures.GetQueryGuid(name: string)
: Retrieves and converts the first value of a specified query parameter to aGuid
.GetQueryUri(name: string)
: Converts the first value of a specified query parameter to aUri
object. Includes handling forUriFormatException
.GetQueryList(name: string)
: Retrieves all values of a specified query parameter as a list of strings, allowing for multiple values per query parameter.
Usage
Here are a few examples of how Vortex.Http can be used within your ASP.NET Core handlers:
open Vortex.Http
open Microsoft.AspNetCore.Http
let exampleHandler (context: HttpContext) =
async {
let logger = context.GetLogger<YourServiceType>()
let service = context.GetService<YourServiceType>()
let headerValue = context.GetHeader "User-Agent"
// Log and return the header value
logger.LogInformation("Received user-agent: {HeaderValue}", headerValue)
return headerValue
} |> Async.StartAsTask
Installation
dotnet add package Vortex
Contributing
Contributions are welcome! Please refer to the TODO.md or create an issue to get started.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net8.0
- FSharp.Core (>= 8.0.200)
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 |
---|---|---|
0.1.0 | 70 | 5/2/2024 |