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
|
|
|
|
-- Copyright : (C) 2014 Alp Mestanogullari
|
|
|
|
-- License : BSD3
|
|
|
|
-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : non-portable
|
2014-11-25 19:42:52 +01:00
|
|
|
module Servant.JQuery
|
|
|
|
( jquery
|
|
|
|
, generateJS
|
2015-04-19 10:07:58 +02:00
|
|
|
, jsForAPI
|
2014-11-25 19:42:52 +01:00
|
|
|
, printJS
|
|
|
|
, module Servant.JQuery.Internal
|
2015-04-19 10:07:58 +02:00
|
|
|
, GenerateCode(..)
|
2014-11-25 19:42:52 +01:00
|
|
|
) where
|
2014-11-25 01:36:34 +01:00
|
|
|
|
|
|
|
import Control.Lens
|
|
|
|
import Data.List
|
2014-12-01 17:23:25 +01:00
|
|
|
import Data.Monoid
|
2014-11-25 01:36:34 +01:00
|
|
|
import Data.Proxy
|
2015-04-19 10:07:58 +02:00
|
|
|
import Servant.API
|
2014-11-25 01:36:34 +01:00
|
|
|
import Servant.JQuery.Internal
|
|
|
|
|
2015-05-03 01:45:17 +02:00
|
|
|
jquery :: HasJQ layout => Proxy layout -> JQ layout
|
|
|
|
jquery p = jqueryFor p defReq
|
2014-11-25 01:36:34 +01:00
|
|
|
|
|
|
|
-- js codegen
|
|
|
|
generateJS :: AjaxReq -> String
|
2014-12-01 17:23:25 +01:00
|
|
|
generateJS req = "\n" <>
|
|
|
|
"function " <> fname <> "(" <> argsStr <> ")\n"
|
|
|
|
<> "{\n"
|
|
|
|
<> " $.ajax(\n"
|
|
|
|
<> " { url: " <> url <> "\n"
|
|
|
|
<> " , success: onSuccess\n"
|
|
|
|
<> dataBody
|
2014-12-08 13:32:47 +01:00
|
|
|
<> reqheaders
|
2014-12-01 17:23:25 +01:00
|
|
|
<> " , error: onError\n"
|
|
|
|
<> " , type: '" <> method <> "'\n"
|
|
|
|
<> " });\n"
|
|
|
|
<> "}\n"
|
2014-11-25 01:36:34 +01:00
|
|
|
|
|
|
|
where argsStr = intercalate ", " args
|
|
|
|
args = captures
|
|
|
|
++ map (view argName) queryparams
|
|
|
|
++ body
|
2015-01-21 09:32:06 +01:00
|
|
|
++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
|
2014-11-25 01:36:34 +01:00
|
|
|
++ ["onSuccess", "onError"]
|
2015-03-27 10:27:27 +01:00
|
|
|
|
2014-11-25 01:36:34 +01:00
|
|
|
captures = map captureArg
|
|
|
|
. filter isCapture
|
|
|
|
$ req ^. reqUrl.path
|
|
|
|
|
2014-12-08 13:32:47 +01:00
|
|
|
hs = req ^. reqHeaders
|
|
|
|
|
2014-11-25 01:36:34 +01:00
|
|
|
queryparams = req ^.. reqUrl.queryStr.traverse
|
|
|
|
|
|
|
|
body = if req ^. reqBody
|
|
|
|
then ["body"]
|
|
|
|
else []
|
|
|
|
|
|
|
|
dataBody =
|
|
|
|
if req ^. reqBody
|
2015-07-08 06:25:32 +02:00
|
|
|
then " , data: JSON.stringify(body)\n" <>
|
|
|
|
" , contentType: 'application/json'\n"
|
2014-11-25 01:36:34 +01:00
|
|
|
else ""
|
|
|
|
|
2014-12-08 13:32:47 +01:00
|
|
|
reqheaders =
|
|
|
|
if null hs
|
|
|
|
then ""
|
2015-07-08 06:25:32 +02:00
|
|
|
else " , headers: { " ++ headersStr ++ " }\n"
|
2014-12-08 13:32:47 +01:00
|
|
|
|
2014-12-09 15:39:41 +01:00
|
|
|
where headersStr = intercalate ", " $ map headerStr hs
|
2015-01-21 09:32:06 +01:00
|
|
|
headerStr header = "\"" ++
|
|
|
|
headerArgName header ++
|
|
|
|
"\": " ++ show header
|
2014-12-08 13:32:47 +01:00
|
|
|
|
2014-11-25 01:36:34 +01:00
|
|
|
fname = req ^. funcName
|
|
|
|
method = req ^. reqMethod
|
2015-03-27 10:27:27 +01:00
|
|
|
url = if url' == "'" then "'/'" else url'
|
|
|
|
url' = "'"
|
2014-11-25 01:36:34 +01:00
|
|
|
++ urlArgs
|
|
|
|
++ queryArgs
|
|
|
|
|
|
|
|
urlArgs = jsSegments
|
|
|
|
$ req ^.. reqUrl.path.traverse
|
|
|
|
|
|
|
|
queryArgs = if null queryparams
|
|
|
|
then ""
|
|
|
|
else " + '?" ++ jsParams queryparams
|
|
|
|
|
|
|
|
printJS :: AjaxReq -> IO ()
|
|
|
|
printJS = putStrLn . generateJS
|
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
|
|
|
|
-- 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.
|
2015-05-03 01:45:17 +02:00
|
|
|
jsForAPI :: (HasJQ api, GenerateCode (JQ api)) => Proxy api -> String
|
2015-04-19 10:07:58 +02:00
|
|
|
jsForAPI p = jsFor (jquery p)
|