git.iko.soy/typeable/servant
on master help sign in
Fix servant-js based on changes to servant-foreign.
commit0b37222733eab874b98900af3e788716a3d9efec authorMaksymilian Owsianny <Maksymilian.Owsianny@gmail.com> dateSat 28 Nov 2015, 08:24 (11 years ago) parent8932cb2
7 files changed+42−33
servant-js/src/Servant/JS.hs+4 servant-js/src/Servant/JS/Angular.hs+7−6 servant-js/src/Servant/JS/Axios.hs+7−6 servant-js/src/Servant/JS/Internal.hs+7−6 servant-js/src/Servant/JS/JQuery.hs+7−6 servant-js/src/Servant/JS/Vanilla.hs+7−6 servant-js/test/Servant/JSSpec/CustomHeaders.hs+3−3
servant-js/src/Servant/JS.hs+4
@@ -147,6 +147,10 @@ writeJSForAPI :: (HasForeign api, GenerateList (Foreign api))
147147 -> IO ()
148148writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen)
149149
150+-- A catch all instance since JavaScript has no types.
151+instance HasForeignType a where
152+ typeFor _ = empty
153+
150154-- | Utility class used by 'jsForAPI' which computes
151155-- the data needed to generate a function for each endpoint
152156-- and hands it all back in a list.
servant-js/src/Servant/JS/Angular.hs+7−6
@@ -2,6 +2,7 @@
22module Servant.JS.Angular where
33
44import Control.Lens
5+import Data.Maybe (isJust)
56import Data.Monoid
67import qualified Data.Text as T
78import Data.Text (Text)
@@ -74,9 +75,9 @@ generateAngularJSWith ngOptions opts req = "\n" <>
7475 where argsStr = T.intercalate ", " args
7576 args = http
7677 ++ captures
77− ++ map (view argName) queryparams
78+ ++ map (view $ argName._1) queryparams
7879 ++ body
79− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
80+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
8081
8182 -- If we want to generate Top Level Function, they must depend on
8283 -- the $http service, if we generate a service, the functions will
@@ -85,7 +86,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
8586 0 -> ["$http"]
8687 _ -> []
8788
88− captures = map captureArg
89+ captures = map (fst . captureArg)
8990 . filter isCapture
9091 $ req ^. reqUrl.path
9192
@@ -93,12 +94,12 @@ generateAngularJSWith ngOptions opts req = "\n" <>
9394
9495 queryparams = req ^.. reqUrl.queryStr.traverse
9596
96− body = if req ^. reqBody
97+ body = if isJust (req ^. reqBody)
9798 then [requestBody opts]
9899 else []
99100
100101 dataBody =
101− if req ^. reqBody
102+ if isJust (req ^. reqBody)
102103 then " , data: JSON.stringify(body)\n" <>
103104 " , contentType: 'application/json'\n"
104105 else ""
@@ -110,7 +111,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
110111
111112 where headersStr = T.intercalate ", " $ map headerStr hs
112113 headerStr header = "\"" <>
113− headerArgName header <>
114+ fst (headerArg header) <>
114115 "\": " <> toJSHeader header
115116
116117 namespace =
servant-js/src/Servant/JS/Axios.hs+7−6
@@ -2,6 +2,7 @@
22module Servant.JS.Axios where
33
44import Control.Lens
5+import Data.Maybe (isJust)
56import Data.Monoid
67import Data.Text (Text)
78import qualified Data.Text as T
@@ -60,11 +61,11 @@ generateAxiosJSWith aopts opts req = "\n" <>
6061
6162 where argsStr = T.intercalate ", " args
6263 args = captures
63− ++ map (view argName) queryparams
64+ ++ map (view $ argName._1) queryparams
6465 ++ body
65− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
66+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
6667
67− captures = map captureArg
68+ captures = map (fst . captureArg)
6869 . filter isCapture
6970 $ req ^. reqUrl.path
7071
@@ -72,12 +73,12 @@ generateAxiosJSWith aopts opts req = "\n" <>
7273
7374 queryparams = req ^.. reqUrl.queryStr.traverse
7475
75− body = if req ^. reqBody
76+ body = if isJust (req ^. reqBody)
7677 then [requestBody opts]
7778 else []
7879
7980 dataBody =
80− if req ^. reqBody
81+ if isJust (req ^. reqBody)
8182 then " , data: body\n" <>
8283 " , responseType: 'json'\n"
8384 else ""
@@ -104,7 +105,7 @@ generateAxiosJSWith aopts opts req = "\n" <>
104105
105106 where headersStr = T.intercalate ", " $ map headerStr hs
106107 headerStr header = "\"" <>
107− headerArgName header <>
108+ fst (headerArg header) <>
108109 "\": " <> toJSHeader header
109110
110111 namespace =
servant-js/src/Servant/JS/Internal.hs+7−6
@@ -18,6 +18,7 @@ module Servant.JS.Internal
1818 , defReq
1919 , reqHeaders
2020 , HasForeign(..)
21+ , HasForeignType(..)
2122 , HeaderArg(..)
2223 , concatCase
2324 , snakeCase
@@ -31,7 +32,7 @@ module Servant.JS.Internal
3132 , Header
3233 ) where
3334
34−import Control.Lens ((^.))
35+import Control.Lens ((^.), _1)
3536import qualified Data.CharSet as Set
3637import qualified Data.CharSet.Unicode.Category as Set
3738import Data.Monoid
@@ -115,7 +116,7 @@ toValidFunctionName t =
115116 ]
116117
117118toJSHeader :: HeaderArg -> Text
118−toJSHeader (HeaderArg n) = toValidFunctionName ("header" <> n)
119+toJSHeader (HeaderArg n) = toValidFunctionName ("header" <> fst n)
119120toJSHeader (ReplaceHeaderArg n p)
120121 | pn `T.isPrefixOf` p = pv <> " + \"" <> rp <> "\""
121122 | pn `T.isSuffixOf` p = "\"" <> rp <> "\" + " <> pv
@@ -123,8 +124,8 @@ toJSHeader (ReplaceHeaderArg n p)
123124 <> "\""
124125 | otherwise = p
125126 where
126− pv = toValidFunctionName ("header" <> n)
127− pn = "{" <> n <> "}"
127+ pv = toValidFunctionName ("header" <> fst n)
128+ pn = "{" <> fst n <> "}"
128129 rp = T.replace pn "" p
129130
130131jsSegments :: [Segment] -> Text
@@ -138,7 +139,7 @@ segmentToStr (Segment st) notTheEnd =
138139
139140segmentTypeToStr :: SegmentType -> Text
140141segmentTypeToStr (Static s) = s
141−segmentTypeToStr (Cap s) = "' + encodeURIComponent(" <> s <> ") + '"
142+segmentTypeToStr (Cap s) = "' + encodeURIComponent(" <> fst s <> ") + '"
142143
143144jsGParams :: Text -> [QueryArg] -> Text
144145jsGParams _ [] = ""
@@ -160,4 +161,4 @@ paramToStr qarg notTheEnd =
160161 <> "[]=' + encodeURIComponent("
161162 <> name
162163 <> if notTheEnd then ") + '" else ")"
163− where name = qarg ^. argName
164+ where name = qarg ^. argName . _1
servant-js/src/Servant/JS/JQuery.hs+7−6
@@ -2,6 +2,7 @@
22module Servant.JS.JQuery where
33
44import Control.Lens
5+import Data.Maybe (isJust)
56import Data.Monoid
67import qualified Data.Text as T
78import Data.Text (Text)
@@ -40,12 +41,12 @@ generateJQueryJSWith opts req = "\n" <>
4041
4142 where argsStr = T.intercalate ", " args
4243 args = captures
43− ++ map (view argName) queryparams
44+ ++ map (view $ argName._1) queryparams
4445 ++ body
45− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
46+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
4647 ++ [onSuccess, onError]
4748
48− captures = map captureArg
49+ captures = map (fst . captureArg)
4950 . filter isCapture
5051 $ req ^. reqUrl.path
5152
@@ -53,7 +54,7 @@ generateJQueryJSWith opts req = "\n" <>
5354
5455 queryparams = req ^.. reqUrl.queryStr.traverse
5556
56− body = if req ^. reqBody
57+ body = if isJust (req ^. reqBody)
5758 then [requestBody opts]
5859 else []
5960
@@ -61,7 +62,7 @@ generateJQueryJSWith opts req = "\n" <>
6162 onError = errorCallback opts
6263
6364 dataBody =
64− if req ^. reqBody
65+ if isJust $ req ^. reqBody
6566 then " , data: JSON.stringify(body)\n" <>
6667 " , contentType: 'application/json'\n"
6768 else ""
@@ -73,7 +74,7 @@ generateJQueryJSWith opts req = "\n" <>
7374
7475 where headersStr = T.intercalate ", " $ map headerStr hs
7576 headerStr header = "\"" <>
76− headerArgName header <>
77+ fst (headerArg header) <>
7778 "\": " <> toJSHeader header
7879
7980 namespace = if (moduleName opts) == ""
servant-js/src/Servant/JS/Vanilla.hs+7−6
@@ -2,6 +2,7 @@
22module Servant.JS.Vanilla where
33
44import Control.Lens
5+import Data.Maybe (isJust)
56import Data.Text (Text)
67import qualified Data.Text as T
78import Data.Monoid
@@ -47,12 +48,12 @@ generateVanillaJSWith opts req = "\n" <>
4748
4849 where argsStr = T.intercalate ", " args
4950 args = captures
50− ++ map (view argName) queryparams
51+ ++ map (view $ argName._1) queryparams
5152 ++ body
52− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
53+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
5354 ++ [onSuccess, onError]
5455
55− captures = map captureArg
56+ captures = map (fst . captureArg)
5657 . filter isCapture
5758 $ req ^. reqUrl.path
5859
@@ -60,7 +61,7 @@ generateVanillaJSWith opts req = "\n" <>
6061
6162 queryparams = req ^.. reqUrl.queryStr.traverse
6263
63− body = if req ^. reqBody
64+ body = if isJust(req ^. reqBody)
6465 then [requestBody opts]
6566 else []
6667
@@ -68,7 +69,7 @@ generateVanillaJSWith opts req = "\n" <>
6869 onError = errorCallback opts
6970
7071 dataBody =
71− if req ^. reqBody
72+ if isJust (req ^. reqBody)
7273 then "JSON.stringify(body)\n"
7374 else "null"
7475
@@ -80,7 +81,7 @@ generateVanillaJSWith opts req = "\n" <>
8081
8182 where headersStr = T.intercalate "\n" $ map headerStr hs
8283 headerStr header = " xhr.setRequestHeader(\"" <>
83− headerArgName header <>
84+ fst (headerArg header) <>
8485 "\", " <> toJSHeader header <> ");"
8586
8687 namespace = if moduleName opts == ""
servant-js/test/Servant/JSSpec/CustomHeaders.hs+3−3
@@ -26,7 +26,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
2626 type Foreign (Authorization sym a :> sublayout) = Foreign sublayout
2727
2828 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
29− req & reqHeaders <>~ [ ReplaceHeaderArg "Authorization" $
29+ req & reqHeaders <>~ [ ReplaceHeaderArg ("Authorization", "") $
3030 tokenType (pack . symbolVal $ (Proxy :: Proxy sym)) ]
3131 where
3232 tokenType t = t <> " {Authorization}"
@@ -39,7 +39,7 @@ instance (HasForeign sublayout)
3939 type Foreign (MyLovelyHorse a :> sublayout) = Foreign sublayout
4040
4141 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
42− req & reqHeaders <>~ [ ReplaceHeaderArg "X-MyLovelyHorse" tpl ]
42+ req & reqHeaders <>~ [ ReplaceHeaderArg ("X-MyLovelyHorse", "") tpl ]
4343 where
4444 tpl = "I am good friends with {X-MyLovelyHorse}"
4545
@@ -51,6 +51,6 @@ instance (HasForeign sublayout)
5151 type Foreign (WhatsForDinner a :> sublayout) = Foreign sublayout
5252
5353 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
54− req & reqHeaders <>~ [ ReplaceHeaderArg "X-WhatsForDinner" tpl ]
54+ req & reqHeaders <>~ [ ReplaceHeaderArg ("X-WhatsForDinner", "") tpl ]
5555 where
5656 tpl = "I would like {X-WhatsForDinner} with a cherry on top."