giraffe-fsharp / Giraffe

A native functional ASP.NET Core web framework for F# developers.
https://giraffe.wiki
Apache License 2.0
2.13k stars 266 forks source link

EndpointRouting - Create endpoint for multiple http verbs #589

Open jsvilling opened 7 months ago

jsvilling commented 7 months ago

Issue

With Giraffe default routing it was easy to compose routes to listen to multiple http verbs. One could do someting like:

let private GET_HEAD_OPTIONS: HttpHandler = choose [ GET_HEAD; OPTIONS ]

With endpoint routing this is no longer possible as Endpoints cannot be composed in this way. It is possible to "just" create an endpoint for each needed http verb. However, this can quickly become cumbersome.

Suggestion

It would be very convenient to have a function in the Giraffe Routers module to create an endpoint for multiple http verbs. The Routers module already has a private function to create such endpoints.

To make it easier to create endpoints for multiple verbs one could add a helper function in the style of:

    let forVerbs (verbs: HttpVerb list) =
        verbs
        |> List.distinct
        |> applyHttpVerbsToEndpoints 

This would make it possible to create endpoints for multile http verbs neatly like

    let api = [
        forVerbs [ GET; HEAD; OPTIONS ] [
            route "/api/subpath" handler
        ]
    ]