FSharp.MinimalApi 0.1.0-beta.41

This is a prerelease version of FSharp.MinimalApi.
There is a newer version of this package available.
See the version list below for details.
dotnet add package FSharp.MinimalApi --version 0.1.0-beta.41                
NuGet\Install-Package FSharp.MinimalApi -Version 0.1.0-beta.41                
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="FSharp.MinimalApi" Version="0.1.0-beta.41" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FSharp.MinimalApi --version 0.1.0-beta.41                
#r "nuget: FSharp.MinimalApi, 0.1.0-beta.41"                
#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 FSharp.MinimalApi as a Cake Addin
#addin nuget:?package=FSharp.MinimalApi&version=0.1.0-beta.41&prerelease

// Install FSharp.MinimalApi as a Cake Tool
#tool nuget:?package=FSharp.MinimalApi&version=0.1.0-beta.41&prerelease                

CI Nuget

FSharp.MinimalApi

Easily define your routes in your ASP.NET Core MinimalAPI with TypedResults support

⚠️ This library is in beta state

Getting started

NuGet package available:

$ dotnet add package FSharp.MinimalApi

💡 You can check a complete sample HERE

Defining Routes

open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Http.HttpResults

open FSharp.MinimalApi
open FSharp.MinimalApi.Builder
open type TypedResults

let routes =
    endpoints {

        get "/hello" (fun () -> "world")

        get "/inc/{v}" (fun (v: int)  -> v + 1)

        get "/secret" (fun () -> "I'm secret") (fun b -> b.ExcludeFromDescription())

        get "/double/{v}" produces<Ok<int>> (fun (v: int) -> Ok(v * 2))

        get "/even/{v}" produces<Ok<string>, BadRequest> (fun (v: int) (logger: ILogger<_>) ->
            (if v % 2 = 0 then
                // the `!>` is necessary for implicit conversions between result types
                 !! Ok("even number!") 
             else
                 logger.LogInformation $"Odd number: {v}"
                 !! BadRequest()))

        // RouteBuilder access
        set (fun b -> b.MapGet("/health", (fun () -> "healthy")))

        mapGroup "user" {
            tags "Users"
            allow_anonymous

            get "/" produces<Ok<User[]>> (fun (db: AppDbContext) ->
                task {
                    let! users = db.Users.ToArrayAsync()
                    return Ok(users)
                })

            get "/{userId}" produces<Ok<User>, NotFound> (fun (userId: Guid) (db: AppDbContext) ->
                task {
                    let! res = db.Users.Where(fun x -> x.Id = UserId userId).TryFirstAsync()

                    match res with
                    | Some user -> return !! Ok(user)
                    | None -> return !! NotFound()
                })

            post "/" produces<Created<User>, Conflict, ValidationProblem>
                (fun (userInfo: NewUser) (db: AppDbContext) ->
                    task {
                        match NewUser.validate userInfo with
                        | Error err -> return !! ValidationProblem(err)
                        | Ok() ->
                            let! exists = db.Users.TryFirstAsync(fun x -> x.Email = userInfo.Email)

                            match exists with
                            | Some _ -> return !! Conflict()
                            | None ->
                                let userId = Guid.NewGuid()

                                let newUser =
                                    { Id = UserId userId
                                        Name = userInfo.Name
                                        Email = userInfo.Email }

                                db.Users.add newUser
                                do! db.saveChangesAsync ()

                                return !! Created($"/user/{userId}", newUser)
                    })

            delete "/{userId}" produces<NoContent, NotFound> (fun (userId: Guid) (db: AppDbContext) ->
                task {
                    let! exists = db.Users.TryFirstAsync(fun x -> x.Id = UserId userId)

                    match exists with
                    | None -> return !! NotFound()
                    | Some user ->
                        db.Users.remove user
                        do! db.saveChangesAsync ()
                        return !! NoContent()
                })
        }
    }

[<EntryPoint>]
let main args =
    let builder = WebApplication.CreateBuilder(args)
    // ... builder configuration ...
    app.MapGroup("api").WithTags("Root") |> routes.Apply |> ignore
    // ... app configuration ...
    app.Run()
    0
Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
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
0.2.1 1,099 12/26/2023
0.2.0 128 12/26/2023
0.2.0-beta.17 120 11/21/2023
0.2.0-beta.16 65 11/21/2023
0.2.0-beta.15 79 11/14/2023
0.2.0-beta.14 68 11/14/2023
0.1.0-beta.54 87 11/14/2023
0.1.0-beta.41 170 11/10/2023
0.1.0-beta.30 70 11/7/2023
0.1.0-beta.20 418 8/17/2023
0.1.0-beta.14 176 7/31/2023
0.1.0-beta.9 512 2/18/2023
0.1.0-beta.5 103 2/18/2023
0.1.0-beta.2 107 2/16/2023
0.1.0-beta.1 106 2/16/2023