@@ -13,8 +13,10 @@
1313module Servant.JQuery
1414 ( jquery
1515 , generateJS
16+ , jsForAPI
1617 , printJS
1718 , module Servant.JQuery.Internal
19+ , GenerateCode(..)
1820 ) where
1921
2022import Control.Lens
@@ -92,3 +94,24 @@ generateJS req = "\n" <>
9294
9395printJS :: AjaxReq -> IO ()
9496printJS = putStrLn . generateJS
97+
98+-- | Utility class used by 'jsForAPI' which will
99+-- directly hand you all the Javascript code
100+-- instead of handing you a ':<|>'-separated list
101+-- of 'AjaxReq' like 'jquery' and then having to
102+-- use 'generateJS' on each 'AjaxReq'.
103+class GenerateCode reqs where
104+ jsFor :: reqs -> String
105+
106+instance GenerateCode AjaxReq where
107+ jsFor = generateJS
108+
109+instance GenerateCode rest => GenerateCode (AjaxReq :<|> rest) where
110+ jsFor (req :<|> rest) = jsFor req ++ jsFor rest
111+
112+-- | Directly generate all the javascript functions for your API
113+-- from a 'Proxy' for your API type. You can then write it to
114+-- a file or integrate it in a page, for example.
115+jsForAPI :: (HasJQ (Canonicalize api), GenerateCode (JQ api))
116+ => Proxy api -> String
117+jsForAPI p = jsFor (jquery p)