| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Web.Spock.Browser
Description
Typed routes shared by native tests and GHC's JavaScript backend. Use Web.Spock.Browser.History to attach a compiled router to browser navigation.
Synopsis
- type Routes = RegistryT IO () () () IO ()
- data Router
- route :: forall (as :: [Type]) (ps :: PathState). Path as ps -> HVectElim as (IO ()) -> Routes
- compileRoutes :: SlashPolicy -> Routes -> IO Router
- dispatch :: Router -> Location -> IO Bool
- data Location = Location {
- locationPathname :: Text
- locationSegments :: [Text]
- locationQuery :: Text
- locationFragment :: Text
- data NavigationError
- parseLocation :: Text -> Either NavigationError Location
- renderPath :: forall (as :: [Type]). AllHave ToHttpApiData as => SlashPolicy -> Path as 'Open -> HVect as -> Either NavigationError Text
- data Path (as :: [Type]) (pathState :: PathState)
- data PathState
- root :: Path ('[] :: [Type]) 'Open
- static :: String -> Path ('[] :: [Type]) 'Open
- var :: (Typeable a, FromHttpApiData a) => Path '[a] 'Open
- wildcard :: Path '[Text] 'Closed
- trailingSlash :: forall (as :: [Type]). Path as 'Open -> Path as 'Open
- (<//>) :: forall (as :: [Type]) (bs :: [Type]) (ps :: PathState). Path as 'Open -> Path bs ps -> Path (Append as bs) ps
- (<.>) :: forall (as :: [Type]) (bs :: [Type]). Path as 'Open -> Path bs 'Open -> Path (Append as bs) 'Open
- data SlashPolicy
Documentation
type Routes = RegistryT IO () () () IO () Source #
A registry of navigation handlers, using the same typed captures as Spock.
route :: forall (as :: [Type]) (ps :: PathState). Path as ps -> HVectElim as (IO ()) -> Routes Source #
Register a route. Handler arguments follow the typed path captures.
compileRoutes :: SlashPolicy -> Routes -> IO Router Source #
Compile definitions once. IgnoreSlashes is Spock's compatibility policy. StrictSlashes preserves empty segments. RedirectTrailingSlashes uses strict matching here; canonical HTTP redirects belong to the server adapter.
dispatch :: Router -> Location -> IO Bool Source #
Run the first match, returning False when no route matched. Handler exceptions propagate; no alternative handler runs after a successful match.
A validated local URL. Query and fragment remain encoded, with their
leading ? and #. Path captures are percent-decoded once, segment by
segment, so an encoded slash remains inside its captured value.
Constructors
| Location | |
Fields
| |
data NavigationError Source #
Failures omit the rejected URL and any sensitive query values.
Instances
parseLocation :: Text -> Either NavigationError Location Source #
Parse an absolute local path with optional query/fragment. Reject external origins, malformed escapes/UTF-8, and dot segments browsers would normalize.
renderPath :: forall (as :: [Type]). AllHave ToHttpApiData as => SlashPolicy -> Path as 'Open -> HVect as -> Either NavigationError Text Source #
Render a safe local href using the same path and slash policy as the registry. Values are encoded as complete segments, including extensions.
data Path (as :: [Type]) (pathState :: PathState) #
Instances
| (a ~ ('[] :: [Type]), pathState ~ 'Open) => IsString (Path a pathState) # | |
Defined in Web.Routing.Combinators Methods fromString :: String -> Path a pathState | |
static :: String -> Path ('[] :: [Type]) 'Open #
A static route piece. One leading slash is optional. Empty internal and
trailing pieces are retained for strict routing; compatibility routing and
renderRoute ignore them. Use renderRouteWith with the application's policy.
wildcard :: Path '[Text] 'Closed #
Matches the rest of the route. Should be the last part of the path.
trailingSlash :: forall (as :: [Type]). Path as 'Open -> Path as 'Open #
Require a trailing slash in strict routing, including after a capture. Root remains root, and a path already ending in a slash is unchanged.
(<//>) :: forall (as :: [Type]) (bs :: [Type]) (ps :: PathState). Path as 'Open -> Path bs ps -> Path (Append as bs) ps infixl 5 Source #
Append path components, using Spock's familiar operator spelling.
(<.>) :: forall (as :: [Type]) (bs :: [Type]). Path as 'Open -> Path bs 'Open -> Path (Append as bs) 'Open infixl 8 #
Join the last segment on the left and the first on the right with a dot.
Both sides may contain typed captures: var . "txt" or var . var.
Matching tries the rightmost dot first and accepts the first split whose
typed parsers and literals succeed. Use a custom capture type to restrict
extensions; a Text capture accepts any text, including an empty extension.
Static routes take precedence over extensions, then plain captures and
wildcards. More literal characters give an extension pattern priority.
data SlashPolicy #
How empty path segments are treated. The compatibility default ignores every empty segment. Strict policies preserve internal and trailing slashes. Redirects are performed by the HTTP adapter, using strict registry matching.
Constructors
| IgnoreSlashes | |
| StrictSlashes | |
| RedirectTrailingSlashes |
Instances
| Read SlashPolicy # | |
Defined in Web.Routing.SafeRouting Methods readsPrec :: Int -> ReadS SlashPolicy readList :: ReadS [SlashPolicy] readPrec :: ReadPrec SlashPolicy readListPrec :: ReadPrec [SlashPolicy] | |
| Show SlashPolicy # | |
Defined in Web.Routing.SafeRouting Methods showsPrec :: Int -> SlashPolicy -> ShowS show :: SlashPolicy -> String showList :: [SlashPolicy] -> ShowS | |
| Eq SlashPolicy # | |
Defined in Web.Routing.SafeRouting | |