git.iko.soy/typeable/servant
on master help sign in
add jq support for Servant.API.Header
commit3d467cfab258ae57f4d5593a6411620297c5fa09 authorAlp Mestanogullari <alpmestan@gmail.com> dateMon 8 Dec 2014, 13:32 (12 years ago) parent0a2d3bc
2 files changed+30−5
src/Servant/JQuery.hs+12 src/Servant/JQuery/Internal.hs+18−5
src/Servant/JQuery.hs+12
@@ -34,6 +34,7 @@ generateJS req = "\n" <>
3434 <> " { url: " <> url <> "\n"
3535 <> " , success: onSuccess\n"
3636 <> dataBody
37+ <> reqheaders
3738 <> " , error: onError\n"
3839 <> " , type: '" <> method <> "'\n"
3940 <> " });\n"
@@ -43,12 +44,15 @@ generateJS req = "\n" <>
4344 args = captures
4445 ++ map (view argName) queryparams
4546 ++ body
47+ ++ map ("header"++) hs
4648 ++ ["onSuccess", "onError"]
4749
4850 captures = map captureArg
4951 . filter isCapture
5052 $ req ^. reqUrl.path
5153
54+ hs = req ^. reqHeaders
55+
5256 queryparams = req ^.. reqUrl.queryStr.traverse
5357
5458 body = if req ^. reqBody
@@ -60,6 +64,14 @@ generateJS req = "\n" <>
6064 then "\n , data: JSON.stringify(body)\n"
6165 else ""
6266
67+ reqheaders =
68+ if null hs
69+ then ""
70+ else "\n , headers: { " ++ headersStr hs ++ " } }\n"
71+
72+ where headersStr hs = intercalate ", " $ map headerStr hs
73+ headerStr hname = hname ++ ": header" ++ hname
74+
6375 fname = req ^. funcName
6476 method = req ^. reqMethod
6577 url = "'"
src/Servant/JQuery/Internal.hs+18−5
@@ -51,6 +51,8 @@ data QueryArg = QueryArg
5151 , _argType :: ArgType
5252 } deriving (Eq, Show)
5353
54+type HeaderArg = String
55+
5456data Url = Url
5557 { _path :: Path
5658 , _queryStr :: [QueryArg]
@@ -63,10 +65,11 @@ type FunctionName = String
6365type Method = String
6466
6567data AjaxReq = AjaxReq
66− { _reqUrl :: Url
67− , _reqMethod :: Method
68− , _reqBody :: Bool
69− , _funcName :: FunctionName
68+ { _reqUrl :: Url
69+ , _reqMethod :: Method
70+ , _reqHeaders :: [HeaderArg]
71+ , _reqBody :: Bool
72+ , _funcName :: FunctionName
7073 } deriving (Eq, Show)
7174
7275makeLenses ''QueryArg
@@ -96,7 +99,7 @@ paramToStr qarg notTheEnd =
9699 where name = qarg ^. argName
97100
98101defReq :: AjaxReq
99−defReq = AjaxReq defUrl "GET" False ""
102+defReq = AjaxReq defUrl "GET" [] False ""
100103
101104class HasJQ layout where
102105 type JQ layout :: *
@@ -134,6 +137,16 @@ instance HasJQ (Get a) where
134137 req & funcName %~ ("get" <>)
135138 & reqMethod .~ "GET"
136139
140+instance (KnownSymbol sym, HasJQ sublayout)
141+ => HasJQ (Header sym a :> sublayout) where
142+ type JQ (Header sym a :> sublayout) = JQ sublayout
143+
144+ jqueryFor Proxy req =
145+ jqueryFor subP (req & reqHeaders <>~ [hname])
146+
147+ where hname = symbolVal (Proxy :: Proxy sym)
148+ subP = Proxy :: Proxy sublayout
149+
137150instance HasJQ (Post a) where
138151 type JQ (Post a) = AjaxReq
139152