Last active
May 8, 2024 13:26
-
-
Save AlbertoDePena/89e25deceea2a222ec0c2510d24d754f to your computer and use it in GitHub Desktop.
Delegate to make minimal APIs FSharp friendly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WebApp.Extensions | |
open System | |
open System.Text | |
open System.Threading.Tasks | |
open Microsoft.AspNetCore.Http | |
open Microsoft.AspNetCore.Antiforgery | |
open Microsoft.Extensions.DependencyInjection | |
open Microsoft.Extensions.Logging | |
open FsToolkit.ErrorHandling | |
[<AutoOpen>] | |
module HttpRequestExtensions = | |
type HttpRequest with | |
member this.TryGetHeaderValues(key: string) : string list = | |
match this.Headers.TryGetValue key |> Option.ofPair with | |
| None -> [] | |
| Some xs -> xs.ToArray() |> List.ofArray | |
member this.TryGetHeaderValue(key: string) : string option = | |
this.TryGetHeaderValues key |> List.tryHead | |
member this.TryGetFormValue(key: string) : string option = | |
match this.HasFormContentType with | |
| false -> None | |
| true -> this.Form.TryGetValue key |> Option.ofPair |> Option.map string | |
member this.TryGetQueryStringValues(key: string) : string list = | |
match this.Query.TryGetValue key |> Option.ofPair with | |
| None -> [] | |
| Some xs -> xs.ToArray() |> List.ofArray | |
member this.TryGetQueryStringValue(key: string) : string option = | |
this.TryGetQueryStringValues key |> List.tryHead | |
member this.TryGetRouteValue(key: string) : string option = | |
this.RouteValues.TryGetValue key |> Option.ofPair |> Option.map string | |
member this.TryGetBearerToken() : string option = | |
this.TryGetHeaderValue "Authorization" | |
|> Option.filter (fun value -> value.Contains("Bearer ")) | |
|> Option.map (fun value -> value.Substring("Bearer ".Length).Trim()) | |
/// Determines if the current HTTP Request was invoked by HTMX on the client. | |
member this.IsHtmx() : bool = | |
this.TryGetHeaderValue "HX-Request" | |
|> Option.exists (String.IsNullOrWhiteSpace >> not) | |
/// Determines if the current HTTP Request was invoked by HTMX on the client with the "boosted" attribute. | |
member this.IsHtmxBoosted() : bool = | |
this.TryGetHeaderValue "HX-Boosted" | |
|> Option.exists (String.IsNullOrWhiteSpace >> not) | |
[<AutoOpen>] | |
module HttpContextExtensions = | |
type HttpContext with | |
member this.GetService<'T>() = | |
this.RequestServices.GetRequiredService<'T>() | |
member this.GetLogger(categoryName: string) = | |
this.GetService<ILoggerFactory>().CreateLogger categoryName | |
member this.GetUserName() : string = this.User.Identity.Name | |
member this.GetAntiforgeryToken() : AntiforgeryTokenSet = | |
this.GetService<IAntiforgery>().GetAndStoreTokens(this) | |
[<AutoOpen>] | |
module ResultsExtensions = | |
type Results with | |
static member Html(content: string) : IResult = | |
Results.Content(content, "text/html; charset=utf-8", Encoding.UTF8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment