Merge pull request #14 from haskell-servant/api-to-js-code-string
add a jsForAPI function that takes an API type proxy and generates js fu...
This commit is contained in:
commit
6a11656aa7
4 changed files with 29 additions and 1 deletions
|
@ -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
|
||||
-----
|
||||
|
|
|
@ -74,5 +74,5 @@ test-suite spec
|
|||
, servant
|
||||
, hspec >= 2.0
|
||||
, hspec-expectations
|
||||
, language-ecmascript == 0.16.*
|
||||
, language-ecmascript >= 0.16
|
||||
default-language: Haskell2010
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue