|
| 1 | +-- | |
| 2 | +-- Module : Hypermedia.Datastar |
| 3 | +-- Description : Haskell SDK for building real-time hypermedia apps with Datastar |
| 4 | +-- |
| 5 | +-- <https://data-star.dev/ Datastar> is a hypermedia framework: instead of |
| 6 | +-- building a JSON API and a JavaScript SPA, you write HTML on the server and |
| 7 | +-- let Datastar handle the interactivity. The browser sends requests, the |
| 8 | +-- server holds the connection open as a server-sent event (SSE) stream, and |
| 9 | +-- pushes HTML fragments, signal updates, or scripts back to the browser as |
| 10 | +-- things change. |
| 11 | +-- |
| 12 | +-- This SDK provides the server-side Haskell API. It builds on |
| 13 | +-- <https://hackage.haskell.org/package/wai WAI> so it works with Warp, Scotty, |
| 14 | +-- Servant, Yesod, or any other WAI-compatible framework. |
| 15 | +-- |
| 16 | +-- === Minimal example |
| 17 | +-- |
| 18 | +-- @ |
| 19 | +-- import Hypermedia.Datastar |
| 20 | +-- import Network.Wai (Application, responseLBS, requestMethod, pathInfo) |
| 21 | +-- import Network.Wai.Handler.Warp qualified as Warp |
| 22 | +-- import Network.HTTP.Types (status404) |
| 23 | +-- |
| 24 | +-- app :: Application |
| 25 | +-- app req respond = |
| 26 | +-- case (requestMethod req, pathInfo req) of |
| 27 | +-- (\"GET\", [\"hello\"]) -> |
| 28 | +-- respond $ sseResponse $ \\gen -> |
| 29 | +-- sendPatchElements gen (patchElements \"\<div id=\\\"msg\\\"\>Hello!\<\/div\>\") |
| 30 | +-- _ -> |
| 31 | +-- respond $ responseLBS status404 [] \"Not found\" |
| 32 | +-- |
| 33 | +-- main :: IO () |
| 34 | +-- main = Warp.run 3000 app |
| 35 | +-- @ |
| 36 | +-- |
| 37 | +-- === Module guide |
| 38 | +-- |
| 39 | +-- * "Hypermedia.Datastar.PatchElements" — send HTML to morph into the DOM |
| 40 | +-- * "Hypermedia.Datastar.PatchSignals" — update the browser's reactive signals |
| 41 | +-- * "Hypermedia.Datastar.ExecuteScript" — run JavaScript in the browser |
| 42 | +-- * "Hypermedia.Datastar.WAI" — SSE streaming, signal decoding, request helpers |
| 43 | +-- * "Hypermedia.Datastar.Types" — protocol types and defaults |
| 44 | +-- |
| 45 | +-- === Further reading |
| 46 | +-- |
| 47 | +-- * <https://data-star.dev/ Datastar homepage> — guides, reference, and examples |
| 48 | +-- * <https://github.com/carlohamalainen/datastar-hs-examples Examples repository> — full working Haskell examples |
| 49 | +-- * <https://cljdoc.org/d/dev.data-star.clojure/http-kit/1.0.0-RC7/doc/sdk-docs/using-datastar Clojure SDK docs> — excellent Datastar walkthrough that applies across SDKs |
| 50 | +module Hypermedia.Datastar |
| 51 | + ( -- * Types |
| 52 | + EventType (..) |
| 53 | + , ElementPatchMode (..) |
| 54 | + , ElementNamespace (..) |
| 55 | + |
| 56 | + -- * Patch Elements |
| 57 | + , PatchElements (..) |
| 58 | + , patchElements |
| 59 | + , removeElements |
| 60 | + |
| 61 | + -- * Patch Signals |
| 62 | + , PatchSignals (..) |
| 63 | + , patchSignals |
| 64 | + |
| 65 | + -- * Execute Script |
| 66 | + , ExecuteScript (..) |
| 67 | + , executeScript |
| 68 | + |
| 69 | + -- * WAI |
| 70 | + , ServerSentEventGenerator |
| 71 | + , sseResponse |
| 72 | + , sendPatchElements |
| 73 | + , sendPatchSignals |
| 74 | + , sendExecuteScript |
| 75 | + , readSignals |
| 76 | + , isDatastarRequest |
| 77 | + ) |
| 78 | +where |
| 79 | + |
| 80 | +import Hypermedia.Datastar.ExecuteScript (ExecuteScript (..), executeScript) |
| 81 | +import Hypermedia.Datastar.PatchElements (PatchElements (..), patchElements, removeElements) |
| 82 | +import Hypermedia.Datastar.PatchSignals (PatchSignals (..), patchSignals) |
| 83 | +import Hypermedia.Datastar.Types (ElementNamespace (..), ElementPatchMode (..), EventType (..)) |
| 84 | +import Hypermedia.Datastar.WAI |
| 85 | + ( ServerSentEventGenerator |
| 86 | + , isDatastarRequest |
| 87 | + , readSignals |
| 88 | + , sendExecuteScript |
| 89 | + , sendPatchElements |
| 90 | + , sendPatchSignals |
| 91 | + , sseResponse |
| 92 | + ) |
0 commit comments