diff --git a/lib/Hikaru/Dispatch.hs b/lib/Hikaru/Dispatch.hs index 4d68d6ca035afb18d2395519795938404c69864f..000cb5fcc82247bcc973137caf3b50dd5d9a2766 100644 --- a/lib/Hikaru/Dispatch.hs +++ b/lib/Hikaru/Dispatch.hs @@ -16,7 +16,9 @@ module Hikaru.Dispatch -- ** Routes , route , wrapRoute + , wrapRoutes , wrapAction + , wrapActions -- ** Middleware , middleware @@ -170,25 +172,42 @@ where in env { envRoutes = envRoutes env' <> envRoutes env } + -- | + -- Wrap all /following/ routes with a route transformer. + -- + wrapRoutes :: (Route r -> Route r) -> Dispatch r l () + wrapRoutes wrapper = Dispatch do + modify \env -> env { envRouteW = envRouteW env . wrapper } + + -- | -- Wrap all nested actions with an action transformer. -- + wrapAction :: (r -> r) -> Dispatch r Nested a -> Dispatch r l () + wrapAction wrapper disp = Dispatch do + modify \env -> + let env' = execState (unDispatch disp) + (env { envActionW = envActionW env . wrapper }) + in env { envRoutes = envRoutes env' <> envRoutes env } + + + -- | + -- Wrap all /following/ actions with an action transformer. + -- -- This can come in handy e.g. to tune cache control: -- -- @ -- app :: Application -- app = 'dispatch' runAction $ do - -- 'wrapAction' ('Hikaru.Action.defaultHeader' hCacheControl "no-cache" >>) $ do - -- 'route' $ getRootR \<$ 'get' - -- 'route' $ getHelloR \<$ 'get' <* 'seg' "hello" \<*\> 'arg' + -- 'wrapRoutes' ('Hikaru.Action.defaultHeader' hCacheControl "no-cache" >>) + -- + -- 'route' $ getRootR \<$ 'get' + -- 'route' $ getHelloR \<$ 'get' <* 'seg' "hello" \<*\> 'arg' -- @ -- - wrapAction :: (r -> r) -> Dispatch r Nested a -> Dispatch r l () - wrapAction wrapper disp = Dispatch do - modify \env -> - let env' = execState (unDispatch disp) - (env { envActionW = envActionW env . wrapper }) - in env { envRoutes = envRoutes env' <> envRoutes env } + wrapActions :: (r -> r) -> Dispatch r l () + wrapActions wrapper = Dispatch do + modify \env -> env { envActionW = envActionW env . wrapper } -- |