@hackage Shpadoinkle-router0.2.0.0

A single page application rounter for Shpadoinkle based on Servant.

Shpadoinkle Servant Router

Goldwater Haddock BSD-3 built with nix Hackage Hackage Deps Hackage CI

A Servant combinator-based router for Shpadoinkle single-page applications. Consuming this router requires that you provide two types:

  • Type alias for the recognized URIs
  • ADT representing views that can be rendered

The relationship between these two types is surjective. meaning more than one URI may result in the same route. This is important for backward compatibility, so the routing schema can evolve while still supporting older schemas.

Because interactions are done through the ADT, application code should be type-safe, and route canonically.

-- The accepted URIs
type SPA
  =            "echo" :> QueryParam "echo" Text :> Raw
  :<|> "v2" :> "echo" :> QueryParam "echo" Text :> Raw
  :<|> "home" :> Raw

-- The routes that can be rendered
data Route
  = Echo (Maybe Text)
  | Home

-- Surjection from URIs to routes
routes :: SPA :>> Route
routes
  =    REcho
  :<|> REcho
  :<|> Home

-- Canonical URI for each route
instance Routed SPA Route where
  redirect = \case
    REcho t -> Redirect (Proxy @("v2" :> "echo" :> QueryParam "echo" Text :> Raw)) ($ t)
    Home    -> Redirect (Proxy @("home" :> Raw)) id

The above specification can be used on both the client and the server. See the servant-crud example for more on how to use this technique.