servant-htmx-0.1.0.1: A library for using htmx with servant
Safe HaskellNone
LanguageHaskell2010

Servant.Htmx

Description

See https://htmx.org/reference/#request_headers and https://htmx.org/reference/#response_headers to see all htmx request and response headers, and how to use them.

Example Servant API:

module MyServer where

import Servant.API
import Servant.Server

-- | Example usage of htmx header types
type ExampleAPI = HXRequest :> Get '[JSON] Text
               :| HXTriggerId :> Post '[JSON] Text
               :| "somePath" :> Get '[JSON] (Headers '[HXPush, HXRedirect] Text)

exampleServer :: Server ExampleAPI
exampleServer = exampleGetHandler
             :| examplePostHandler
             :| exampleSomePathHandler
 where
   exampleGetHandler :: Maybe Text -> Handler Text
   exampleGetHandler mb = case mb of
     Just "true" -> pure "The request was sent to the server by htmx"
     _ -> pure "The request wasn't sent to the server by htmx"

   examplePostHandler :: Maybe Text -> Handler Text
   examplePostHandler mb = case mb of
     Just "adminPanel" -> pure "The request was triggered by the admin panel"
     _ -> pure "The request wasn't triggered by the admin panel"

   exampleSomePathHandler :: Handler (Headers '[HXPush, HXRedirect] Text)
   exampleSomePathHandler = pure $ noHeader $ addHeader "someURLForRedirect" "This response has htmx headers"

exampleApp :: Application
exampleApp = serve (Proxy :: Proxy ExampleAPI) exampleServer

Documentation

type HXRequest = Header "HX-Request" Text Source #

type HXTriggerId = Header "HX-Trigger" Text Source #

type HXTriggerName = Header "HX-Trigger-Name" Text Source #

type HXTarget = Header "HX-Target" Text Source #

type HXPrompt = Header "HX-Prompt" Text Source #

type HXPush = Header "HX-Push" Text Source #

type HXRedirect = Header "HX-Redirect" Text Source #

type HXRefresh = Header "HX-Refresh" Text Source #

type HXTrigger = Header "HX-Trigger" Text Source #

type HXTriggerAfterSwap = Header "HX-Trigger-After-Swap" Text Source #

type HXTriggerAfterSettle = Header "HX-Trigger-After-Settle" Text Source #