diff --git a/CHANGELOG.md b/CHANGELOG.md index ceac30d4..99eeaf5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Extend `HeaderArg` to support more advanced HTTP header handling (https://github.com/haskell-servant/servant-jquery/pull/6) * Support content-type aware combinators (but require that endpoints support JSON) * Add support for Matrix params (https://github.com/haskell-servant/servant-jquery/pull/11) +* Add functions that directly generate the Javascript code from the API type without having to manually pattern match on the result. 0.2.2 ----- diff --git a/servant-jquery.cabal b/servant-jquery.cabal index d21d4862..e6acc9ff 100644 --- a/servant-jquery.cabal +++ b/servant-jquery.cabal @@ -74,5 +74,5 @@ test-suite spec , servant , hspec >= 2.0 , hspec-expectations - , language-ecmascript == 0.16.* + , language-ecmascript >= 0.16 default-language: Haskell2010 diff --git a/src/Servant/JQuery.hs b/src/Servant/JQuery.hs index b1c54f4b..4460e0e9 100644 --- a/src/Servant/JQuery.hs +++ b/src/Servant/JQuery.hs @@ -13,8 +13,10 @@ module Servant.JQuery ( jquery , generateJS + , jsForAPI , printJS , module Servant.JQuery.Internal + , GenerateCode(..) ) where import Control.Lens @@ -92,3 +94,24 @@ generateJS req = "\n" <> printJS :: AjaxReq -> IO () printJS = putStrLn . generateJS + +-- | Utility class used by 'jsForAPI' which will +-- directly hand you all the Javascript code +-- instead of handing you a ':<|>'-separated list +-- of 'AjaxReq' like 'jquery' and then having to +-- use 'generateJS' on each 'AjaxReq'. +class GenerateCode reqs where + jsFor :: reqs -> String + +instance GenerateCode AjaxReq where + jsFor = generateJS + +instance GenerateCode rest => GenerateCode (AjaxReq :<|> rest) where + jsFor (req :<|> rest) = jsFor req ++ jsFor rest + +-- | Directly generate all the javascript functions for your API +-- from a 'Proxy' for your API type. You can then write it to +-- a file or integrate it in a page, for example. +jsForAPI :: (HasJQ (Canonicalize api), GenerateCode (JQ api)) + => Proxy api -> String +jsForAPI p = jsFor (jquery p) diff --git a/test/Servant/JQuerySpec.hs b/test/Servant/JQuerySpec.hs index 077b9b87..b8bc5152 100644 --- a/test/Servant/JQuerySpec.hs +++ b/test/Servant/JQuerySpec.hs @@ -90,3 +90,7 @@ generateJSSpec = describe "generateJS" $ do parseFromString jsText `shouldSatisfy` isRight jsText `shouldContain` "headerXWhatsForDinner" jsText `shouldContain` "headers: { \"X-WhatsForDinner\": \"I would like \" + headerXWhatsForDinner + \" with a cherry on top.\" }\n" + + it "can generate the whole javascript code string at once with jsForAPI" $ do + let jsStr = jsForAPI (Proxy :: Proxy TestAPI) + parseFromString jsStr `shouldSatisfy` isRight