Use setHttpHeader handler

This commit is contained in:
Daniel J. Summers 2021-10-05 10:47:21 -04:00
parent 98175b9b31
commit 2dd45cefe0

View File

@ -23,7 +23,7 @@ type IHeaderDictionary with
/// The user response to an `hx-prompt` /// The user response to an `hx-prompt`
member this.HxPrompt with get () = hdr this "HX-Prompt" member this.HxPrompt with get () = hdr this "HX-Prompt"
/// Always `true` /// `true` if the request came from HTMX
member this.HxRequest with get () = hdr this "HX-Request" |> Option.map bool.Parse member this.HxRequest with get () = hdr this "HX-Request" |> Option.map bool.Parse
/// The `id` of the target element if it exists /// The `id` of the target element if it exists
@ -51,51 +51,49 @@ type HttpRequest with
[<AutoOpen>] [<AutoOpen>]
module Handlers = module Handlers =
/// Convert a boolean to lowercase `true` or `false`
let private toLowerBool (trueOrFalse : bool) =
(string trueOrFalse).ToLowerInvariant ()
/// Serialize a list of key/value pairs to JSON (very rudimentary) /// Serialize a list of key/value pairs to JSON (very rudimentary)
let private toJson (evts : (string * string) list) = let private toJson (evts : (string * string) list) =
evts evts
|> List.map (fun evt -> sprintf "\"%s\": \"%s\"" (fst evt) ((snd evt).Replace ("\"", "\\\""))) |> List.map (fun evt -> sprintf "\"%s\": \"%s\"" (fst evt) ((snd evt).Replace ("\"", "\\\"")))
|> String.concat ", " |> String.concat ", "
|> (sprintf "{ %s }" >> StringValues) |> sprintf "{ %s }"
/// Set a header
let private setHeader item value : HttpHandler =
fun next ctx ->
ctx.Response.Headers.[item] <- value
next ctx
// Pushes a new url into the history stack // Pushes a new url into the history stack
let withHxPush (push : bool) : HttpHandler = let withHxPush : bool -> HttpHandler =
push |> (string >> StringValues >> setHeader "HX-Push") toLowerBool >> setHttpHeader "HX-Push"
/// Can be used to do a client-side redirect to a new location /// Can be used to do a client-side redirect to a new location
let withHxRedirect (url : string) : HttpHandler = let withHxRedirect : string -> HttpHandler =
StringValues url |> setHeader "HX-Redirect" setHttpHeader "HX-Redirect"
/// If set to `true` the client side will do a a full refresh of the page /// If set to `true` the client side will do a a full refresh of the page
let withHxRefresh (refresh : bool) : HttpHandler = let withHxRefresh : bool -> HttpHandler =
refresh |> (string >> StringValues >> setHeader "HX-Refresh") toLowerBool >> setHttpHeader "HX-Refresh"
/// Allows you to trigger a single client side event /// Allows you to trigger a single client side event
let withHxTrigger (evt : string) : HttpHandler = let withHxTrigger : string -> HttpHandler =
StringValues evt |> setHeader "HX-Trigger" setHttpHeader "HX-Trigger"
/// Allows you to trigger multiple client side events /// Allows you to trigger multiple client side events
let withHxTriggerMany evts : HttpHandler = let withHxTriggerMany evts : HttpHandler =
toJson evts |> setHeader "HX-Trigger" toJson evts |> setHttpHeader "HX-Trigger"
/// Allows you to trigger a single client side event after changes have settled /// Allows you to trigger a single client side event after changes have settled
let withHxTriggerAfterSettle (evt : string) : HttpHandler = let withHxTriggerAfterSettle : string -> HttpHandler =
StringValues evt |> setHeader "HX-Trigger-After-Settle" setHttpHeader "HX-Trigger-After-Settle"
/// Allows you to trigger multiple client side events after changes have settled /// Allows you to trigger multiple client side events after changes have settled
let withHxTriggerManyAfterSettle evts : HttpHandler = let withHxTriggerManyAfterSettle evts : HttpHandler =
toJson evts |> setHeader "HX-Trigger-After-Settle" toJson evts |> setHttpHeader "HX-Trigger-After-Settle"
/// Allows you to trigger a single client side event after DOM swapping occurs /// Allows you to trigger a single client side event after DOM swapping occurs
let withHxTriggerAfterSwap (evt : string) : HttpHandler = let withHxTriggerAfterSwap : string -> HttpHandler =
StringValues evt |> setHeader "HX-Trigger-After-Swap" setHttpHeader "HX-Trigger-After-Swap"
/// Allows you to trigger multiple client side events after DOM swapping occurs /// Allows you to trigger multiple client side events after DOM swapping occurs
let withHxTriggerManyAfterSwap evts : HttpHandler = let withHxTriggerManyAfterSwap evts : HttpHandler =
toJson evts |> setHeader "HX-Trigger-After-Swap" toJson evts |> setHttpHeader "HX-Trigger-After-Swap"