@@ -19,6 +19,7 @@
1919module Obelisk.Route
2020 ( R
2121 , pattern (:/)
22+ , hoistR
2223 , PageName
2324 , PathQuery
2425 , Encoder
@@ -78,6 +79,9 @@ module Obelisk.Route
7879 , void1Encoder
7980 , pathSegmentsTextEncoder
8081 , queryParametersTextEncoder
82+ , renderObeliskRoute
83+ , renderBackendRoute
84+ , renderFrontendRoute
8185 ) where
8286
8387import Prelude hiding ((.), id)
@@ -149,6 +153,9 @@ pattern a :/ b = a :=> Identity b
149153mapSome :: (forall a. f a -> g a) -> Some f -> Some g
150154mapSome f (Some.This a) = Some.This $ f a
151155
156+hoistR :: (forall x. f x -> g x) -> R f -> R g
157+hoistR f (x :=> Identity y) = f x :/ y
158+
152159--------------------------------------------------------------------------------
153160-- Encoder fundamentals
154161--------------------------------------------------------------------------------
@@ -824,4 +831,32 @@ makePrisms ''ObeliskRoute
824831deriveGEq ''Void1
825832deriveGCompare ''Void1
826833
834+-- | Given a backend route and a checked route encoder, render the route (path
835+-- and query string). See 'checkEncoder' for how to produce a checked encoder.
836+renderBackendRoute
837+ :: forall br a.
838+ Encoder Identity Identity (R (Sum br a)) PageName
839+ -> R br
840+ -> Text
841+renderBackendRoute enc = renderObeliskRoute enc . hoistR InL
842+
843+-- | Renders a frontend route with the supplied checked encoder
844+renderFrontendRoute
845+ :: forall a fr.
846+ Encoder Identity Identity (R (Sum a (ObeliskRoute fr))) PageName
847+ -> R fr
848+ -> Text
849+renderFrontendRoute enc = renderObeliskRoute enc . hoistR (InR . ObeliskRoute_App)
850+
851+-- | Renders a route of the form typically found in an Obelisk project
852+renderObeliskRoute
853+ :: forall a b.
854+ Encoder Identity Identity (R (Sum a b)) PageName
855+ -> R (Sum a b)
856+ -> Text
857+renderObeliskRoute e r =
858+ let enc :: Encoder Identity (Either Text) (R (Sum a b)) PathQuery
859+ enc = (pageNameEncoder . hoistParse (pure . runIdentity) e)
860+ in (T.pack . uncurry (<>)) $ encode enc r
861+
827862--TODO: decodeURIComponent as appropriate