add a jsForAPI function that takes an API type proxy and generates js functions for each endpoint, collecting the result in a big String. fixes #12
This commit is contained in:
parent
8c6f327c0d
commit
21b6e96891
2 changed files with 26 additions and 0 deletions
|
@ -3,6 +3,7 @@
|
||||||
* Extend `HeaderArg` to support more advanced HTTP header handling (https://github.com/haskell-servant/servant-jquery/pull/6)
|
* 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)
|
* 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 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
|
0.2.2
|
||||||
-----
|
-----
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
{-# LANGUAGE DataKinds #-}
|
{-# LANGUAGE DataKinds #-}
|
||||||
{-# LANGUAGE TypeOperators #-}
|
{-# LANGUAGE TypeOperators #-}
|
||||||
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
{-# LANGUAGE FlexibleInstances #-}
|
{-# LANGUAGE FlexibleInstances #-}
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- |
|
-- |
|
||||||
|
@ -12,14 +13,17 @@
|
||||||
module Servant.JQuery
|
module Servant.JQuery
|
||||||
( jquery
|
( jquery
|
||||||
, generateJS
|
, generateJS
|
||||||
|
, jsForAPI
|
||||||
, printJS
|
, printJS
|
||||||
, module Servant.JQuery.Internal
|
, module Servant.JQuery.Internal
|
||||||
|
, GenerateCode(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Lens
|
import Control.Lens
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.Monoid
|
import Data.Monoid
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
|
import Servant.API
|
||||||
import Servant.JQuery.Internal
|
import Servant.JQuery.Internal
|
||||||
|
|
||||||
jquery :: HasJQ layout => Proxy layout -> JQ layout
|
jquery :: HasJQ layout => Proxy layout -> JQ layout
|
||||||
|
@ -90,3 +94,24 @@ generateJS req = "\n" <>
|
||||||
|
|
||||||
printJS :: AjaxReq -> IO ()
|
printJS :: AjaxReq -> IO ()
|
||||||
printJS = putStrLn . generateJS
|
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 api, GenerateCode (JQ api))
|
||||||
|
=> Proxy api -> String
|
||||||
|
jsForAPI p = jsFor (jquery p)
|
||||||
|
|
Loading…
Reference in a new issue