2014-11-25 01:36:34 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-04-19 10:07:58 +02:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2014-11-25 01:36:34 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- |
|
|
|
|
-- Module : Servant.JQuery
|
|
|
|
-- License : BSD3
|
|
|
|
-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : non-portable
|
2015-07-22 12:55:44 +02:00
|
|
|
module Servant.JS
|
2015-07-17 23:36:38 +02:00
|
|
|
( javascript
|
2014-11-25 19:42:52 +01:00
|
|
|
, generateJS
|
2015-04-19 10:07:58 +02:00
|
|
|
, jsForAPI
|
2015-07-17 23:36:38 +02:00
|
|
|
, listFromAPI
|
2014-11-25 19:42:52 +01:00
|
|
|
, printJS
|
2015-07-22 12:55:44 +02:00
|
|
|
, module Servant.JS.Internal
|
2015-04-19 10:07:58 +02:00
|
|
|
, GenerateCode(..)
|
2015-07-17 23:36:38 +02:00
|
|
|
, CommonGeneratorOptions(..)
|
2014-11-25 19:42:52 +01:00
|
|
|
) where
|
2014-11-25 01:36:34 +01:00
|
|
|
|
|
|
|
import Data.Proxy
|
2015-04-19 10:07:58 +02:00
|
|
|
import Servant.API
|
2015-07-22 12:55:44 +02:00
|
|
|
import Servant.JS.Internal
|
2014-11-25 01:36:34 +01:00
|
|
|
|
2015-07-17 23:36:38 +02:00
|
|
|
javascript :: HasJS layout => Proxy layout -> JS layout
|
|
|
|
javascript p = javascriptFor p defReq
|
2014-11-25 01:36:34 +01:00
|
|
|
|
2015-07-17 23:36:38 +02:00
|
|
|
printJS :: AjaxReq -> JavaScriptGenerator -> IO ()
|
|
|
|
printJS req gen = putStrLn (generateJS req gen)
|
2014-11-25 01:36:34 +01:00
|
|
|
|
2015-07-17 23:36:38 +02:00
|
|
|
generateJS :: AjaxReq -> JavaScriptGenerator -> String
|
|
|
|
generateJS req gen = gen $ req
|
2015-04-19 10:07:58 +02:00
|
|
|
|
|
|
|
-- | Utility class used by 'jsForAPI' which will
|
|
|
|
-- directly hand you all the Javascript code
|
|
|
|
-- instead of handing you a ':<|>'-separated list
|
2015-07-17 23:36:38 +02:00
|
|
|
-- of 'AjaxReq' like 'javascript' and then having to
|
2015-04-19 10:07:58 +02:00
|
|
|
-- use 'generateJS' on each 'AjaxReq'.
|
|
|
|
class GenerateCode reqs where
|
2015-07-17 23:36:38 +02:00
|
|
|
jsFor :: reqs -> JavaScriptGenerator -> String
|
2015-04-19 10:07:58 +02:00
|
|
|
|
|
|
|
instance GenerateCode AjaxReq where
|
|
|
|
jsFor = generateJS
|
|
|
|
|
|
|
|
instance GenerateCode rest => GenerateCode (AjaxReq :<|> rest) where
|
2015-07-17 23:36:38 +02:00
|
|
|
jsFor (req :<|> rest) gen = jsFor req gen ++ jsFor rest gen
|
2015-04-19 10:07:58 +02:00
|
|
|
|
|
|
|
-- | 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.
|
2015-07-17 23:36:38 +02:00
|
|
|
jsForAPI :: (HasJS api, GenerateCode (JS api)) => Proxy api
|
|
|
|
-> JavaScriptGenerator -> String
|
|
|
|
jsForAPI p = jsFor (javascript p)
|
|
|
|
|
|
|
|
class GenerateList reqs where
|
|
|
|
generateList :: reqs -> [AjaxReq]
|
|
|
|
|
|
|
|
instance GenerateList AjaxReq where
|
|
|
|
generateList r = [r]
|
|
|
|
|
|
|
|
instance GenerateList rest => GenerateList (AjaxReq :<|> rest) where
|
|
|
|
generateList (r :<|> rest) = r : generateList rest
|
|
|
|
|
|
|
|
listFromAPI :: (HasJS api, GenerateList (JS api)) => Proxy api -> [AjaxReq]
|
|
|
|
listFromAPI p = generateList (javascript p)
|