add jq support for Servant.API.Header

This commit is contained in:
Alp Mestanogullari 2014-12-08 13:32:47 +01:00
parent 0a2d3bc12c
commit 3d467cfab2
2 changed files with 30 additions and 5 deletions

View file

@ -34,6 +34,7 @@ generateJS req = "\n" <>
<> " { url: " <> url <> "\n" <> " { url: " <> url <> "\n"
<> " , success: onSuccess\n" <> " , success: onSuccess\n"
<> dataBody <> dataBody
<> reqheaders
<> " , error: onError\n" <> " , error: onError\n"
<> " , type: '" <> method <> "'\n" <> " , type: '" <> method <> "'\n"
<> " });\n" <> " });\n"
@ -43,12 +44,15 @@ generateJS req = "\n" <>
args = captures args = captures
++ map (view argName) queryparams ++ map (view argName) queryparams
++ body ++ body
++ map ("header"++) hs
++ ["onSuccess", "onError"] ++ ["onSuccess", "onError"]
captures = map captureArg captures = map captureArg
. filter isCapture . filter isCapture
$ req ^. reqUrl.path $ req ^. reqUrl.path
hs = req ^. reqHeaders
queryparams = req ^.. reqUrl.queryStr.traverse queryparams = req ^.. reqUrl.queryStr.traverse
body = if req ^. reqBody body = if req ^. reqBody
@ -60,6 +64,14 @@ generateJS req = "\n" <>
then "\n , data: JSON.stringify(body)\n" then "\n , data: JSON.stringify(body)\n"
else "" else ""
reqheaders =
if null hs
then ""
else "\n , headers: { " ++ headersStr hs ++ " } }\n"
where headersStr hs = intercalate ", " $ map headerStr hs
headerStr hname = hname ++ ": header" ++ hname
fname = req ^. funcName fname = req ^. funcName
method = req ^. reqMethod method = req ^. reqMethod
url = "'" url = "'"

View file

@ -51,6 +51,8 @@ data QueryArg = QueryArg
, _argType :: ArgType , _argType :: ArgType
} deriving (Eq, Show) } deriving (Eq, Show)
type HeaderArg = String
data Url = Url data Url = Url
{ _path :: Path { _path :: Path
, _queryStr :: [QueryArg] , _queryStr :: [QueryArg]
@ -63,10 +65,11 @@ type FunctionName = String
type Method = String type Method = String
data AjaxReq = AjaxReq data AjaxReq = AjaxReq
{ _reqUrl :: Url { _reqUrl :: Url
, _reqMethod :: Method , _reqMethod :: Method
, _reqBody :: Bool , _reqHeaders :: [HeaderArg]
, _funcName :: FunctionName , _reqBody :: Bool
, _funcName :: FunctionName
} deriving (Eq, Show) } deriving (Eq, Show)
makeLenses ''QueryArg makeLenses ''QueryArg
@ -96,7 +99,7 @@ paramToStr qarg notTheEnd =
where name = qarg ^. argName where name = qarg ^. argName
defReq :: AjaxReq defReq :: AjaxReq
defReq = AjaxReq defUrl "GET" False "" defReq = AjaxReq defUrl "GET" [] False ""
class HasJQ layout where class HasJQ layout where
type JQ layout :: * type JQ layout :: *
@ -134,6 +137,16 @@ instance HasJQ (Get a) where
req & funcName %~ ("get" <>) req & funcName %~ ("get" <>)
& reqMethod .~ "GET" & reqMethod .~ "GET"
instance (KnownSymbol sym, HasJQ sublayout)
=> HasJQ (Header sym a :> sublayout) where
type JQ (Header sym a :> sublayout) = JQ sublayout
jqueryFor Proxy req =
jqueryFor subP (req & reqHeaders <>~ [hname])
where hname = symbolVal (Proxy :: Proxy sym)
subP = Proxy :: Proxy sublayout
instance HasJQ (Post a) where instance HasJQ (Post a) where
type JQ (Post a) = AjaxReq type JQ (Post a) = AjaxReq