git.iko.soy/typeable/servant
on master help sign in
Fix servant-js based on changes to servant-foreign.
0b37222Maksymilian Owsianny
servant-js/src/Servant/JS.hs+4
@@ -147,6 +147,10 @@ writeJSForAPI :: (HasForeign api, GenerateList (Foreign api))
147 -> IO ()
147 -> IO ()
148writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen)
148writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen)
149
149
150+-- A catch all instance since JavaScript has no types.
151+instance HasForeignType a where
152+ typeFor _ = empty
153+
150-- | Utility class used by 'jsForAPI' which computes
154-- | Utility class used by 'jsForAPI' which computes
151-- the data needed to generate a function for each endpoint
155-- the data needed to generate a function for each endpoint
152-- and hands it all back in a list.
156-- and hands it all back in a list.
servant-js/src/Servant/JS/Angular.hs+7−6
@@ -2,6 +2,7 @@
2module Servant.JS.Angular where
2module Servant.JS.Angular where
3
3
4import Control.Lens
4import Control.Lens
5+import Data.Maybe (isJust)
5import Data.Monoid
6import Data.Monoid
6import qualified Data.Text as T
7import qualified Data.Text as T
7import Data.Text (Text)
8import Data.Text (Text)
@@ -74,9 +75,9 @@ generateAngularJSWith ngOptions opts req = "\n" <>
74 where argsStr = T.intercalate ", " args
75 where argsStr = T.intercalate ", " args
75 args = http
76 args = http
76 ++ captures
77 ++ captures
77− ++ map (view argName) queryparams
78+ ++ map (view $ argName._1) queryparams
78 ++ body
79 ++ body
79− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
80+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
80
81
81 -- If we want to generate Top Level Function, they must depend on
82 -- If we want to generate Top Level Function, they must depend on
82 -- the $http service, if we generate a service, the functions will
83 -- the $http service, if we generate a service, the functions will
@@ -85,7 +86,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
85 0 -> ["$http"]
86 0 -> ["$http"]
86 _ -> []
87 _ -> []
87
88
88− captures = map captureArg
89+ captures = map (fst . captureArg)
89 . filter isCapture
90 . filter isCapture
90 $ req ^. reqUrl.path
91 $ req ^. reqUrl.path
91
92
@@ -93,12 +94,12 @@ generateAngularJSWith ngOptions opts req = "\n" <>
93
94
94 queryparams = req ^.. reqUrl.queryStr.traverse
95 queryparams = req ^.. reqUrl.queryStr.traverse
95
96
96− body = if req ^. reqBody
97+ body = if isJust (req ^. reqBody)
97 then [requestBody opts]
98 then [requestBody opts]
98 else []
99 else []
99
100
100 dataBody =
101 dataBody =
101− if req ^. reqBody
102+ if isJust (req ^. reqBody)
102 then " , data: JSON.stringify(body)\n" <>
103 then " , data: JSON.stringify(body)\n" <>
103 " , contentType: 'application/json'\n"
104 " , contentType: 'application/json'\n"
104 else ""
105 else ""
@@ -110,7 +111,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
110
111
111 where headersStr = T.intercalate ", " $ map headerStr hs
112 where headersStr = T.intercalate ", " $ map headerStr hs
112 headerStr header = "\"" <>
113 headerStr header = "\"" <>
113− headerArgName header <>
114+ fst (headerArg header) <>
114 "\": " <> toJSHeader header
115 "\": " <> toJSHeader header
115
116
116 namespace =
117 namespace =
servant-js/src/Servant/JS/Axios.hs+7−6
@@ -2,6 +2,7 @@
2module Servant.JS.Axios where
2module Servant.JS.Axios where
3
3
4import Control.Lens
4import Control.Lens
5+import Data.Maybe (isJust)
5import Data.Monoid
6import Data.Monoid
6import Data.Text (Text)
7import Data.Text (Text)
7import qualified Data.Text as T
8import qualified Data.Text as T
@@ -60,11 +61,11 @@ generateAxiosJSWith aopts opts req = "\n" <>
60
61
61 where argsStr = T.intercalate ", " args
62 where argsStr = T.intercalate ", " args
62 args = captures
63 args = captures
63− ++ map (view argName) queryparams
64+ ++ map (view $ argName._1) queryparams
64 ++ body
65 ++ body
65− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
66+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
66
67
67− captures = map captureArg
68+ captures = map (fst . captureArg)
68 . filter isCapture
69 . filter isCapture
69 $ req ^. reqUrl.path
70 $ req ^. reqUrl.path
70
71
@@ -72,12 +73,12 @@ generateAxiosJSWith aopts opts req = "\n" <>
72
73
73 queryparams = req ^.. reqUrl.queryStr.traverse
74 queryparams = req ^.. reqUrl.queryStr.traverse
74
75
75− body = if req ^. reqBody
76+ body = if isJust (req ^. reqBody)
76 then [requestBody opts]
77 then [requestBody opts]
77 else []
78 else []
78
79
79 dataBody =
80 dataBody =
80− if req ^. reqBody
81+ if isJust (req ^. reqBody)
81 then " , data: body\n" <>
82 then " , data: body\n" <>
82 " , responseType: 'json'\n"
83 " , responseType: 'json'\n"
83 else ""
84 else ""
@@ -104,7 +105,7 @@ generateAxiosJSWith aopts opts req = "\n" <>
104
105
105 where headersStr = T.intercalate ", " $ map headerStr hs
106 where headersStr = T.intercalate ", " $ map headerStr hs
106 headerStr header = "\"" <>
107 headerStr header = "\"" <>
107− headerArgName header <>
108+ fst (headerArg header) <>
108 "\": " <> toJSHeader header
109 "\": " <> toJSHeader header
109
110
110 namespace =
111 namespace =
servant-js/src/Servant/JS/Internal.hs+7−6
@@ -18,6 +18,7 @@ module Servant.JS.Internal
18 , defReq
18 , defReq
19 , reqHeaders
19 , reqHeaders
20 , HasForeign(..)
20 , HasForeign(..)
21+ , HasForeignType(..)
21 , HeaderArg(..)
22 , HeaderArg(..)
22 , concatCase
23 , concatCase
23 , snakeCase
24 , snakeCase
@@ -31,7 +32,7 @@ module Servant.JS.Internal
31 , Header
32 , Header
32 ) where
33 ) where
33
34
34−import Control.Lens ((^.))
35+import Control.Lens ((^.), _1)
35import qualified Data.CharSet as Set
36import qualified Data.CharSet as Set
36import qualified Data.CharSet.Unicode.Category as Set
37import qualified Data.CharSet.Unicode.Category as Set
37import Data.Monoid
38import Data.Monoid
@@ -115,7 +116,7 @@ toValidFunctionName t =
115 ]
116 ]
116
117
117toJSHeader :: HeaderArg -> Text
118toJSHeader :: HeaderArg -> Text
118−toJSHeader (HeaderArg n) = toValidFunctionName ("header" <> n)
119+toJSHeader (HeaderArg n) = toValidFunctionName ("header" <> fst n)
119toJSHeader (ReplaceHeaderArg n p)
120toJSHeader (ReplaceHeaderArg n p)
120 | pn `T.isPrefixOf` p = pv <> " + \"" <> rp <> "\""
121 | pn `T.isPrefixOf` p = pv <> " + \"" <> rp <> "\""
121 | pn `T.isSuffixOf` p = "\"" <> rp <> "\" + " <> pv
122 | pn `T.isSuffixOf` p = "\"" <> rp <> "\" + " <> pv
@@ -123,8 +124,8 @@ toJSHeader (ReplaceHeaderArg n p)
123 <> "\""
124 <> "\""
124 | otherwise = p
125 | otherwise = p
125 where
126 where
126− pv = toValidFunctionName ("header" <> n)
127+ pv = toValidFunctionName ("header" <> fst n)
127− pn = "{" <> n <> "}"
128+ pn = "{" <> fst n <> "}"
128 rp = T.replace pn "" p
129 rp = T.replace pn "" p
129
130
130jsSegments :: [Segment] -> Text
131jsSegments :: [Segment] -> Text
@@ -138,7 +139,7 @@ segmentToStr (Segment st) notTheEnd =
138
139
139segmentTypeToStr :: SegmentType -> Text
140segmentTypeToStr :: SegmentType -> Text
140segmentTypeToStr (Static s) = s
141segmentTypeToStr (Static s) = s
141−segmentTypeToStr (Cap s) = "' + encodeURIComponent(" <> s <> ") + '"
142+segmentTypeToStr (Cap s) = "' + encodeURIComponent(" <> fst s <> ") + '"
142
143
143jsGParams :: Text -> [QueryArg] -> Text
144jsGParams :: Text -> [QueryArg] -> Text
144jsGParams _ [] = ""
145jsGParams _ [] = ""
@@ -160,4 +161,4 @@ paramToStr qarg notTheEnd =
160 <> "[]=' + encodeURIComponent("
161 <> "[]=' + encodeURIComponent("
161 <> name
162 <> name
162 <> if notTheEnd then ") + '" else ")"
163 <> 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 @@
2module Servant.JS.JQuery where
2module Servant.JS.JQuery where
3
3
4import Control.Lens
4import Control.Lens
5+import Data.Maybe (isJust)
5import Data.Monoid
6import Data.Monoid
6import qualified Data.Text as T
7import qualified Data.Text as T
7import Data.Text (Text)
8import Data.Text (Text)
@@ -40,12 +41,12 @@ generateJQueryJSWith opts req = "\n" <>
40
41
41 where argsStr = T.intercalate ", " args
42 where argsStr = T.intercalate ", " args
42 args = captures
43 args = captures
43− ++ map (view argName) queryparams
44+ ++ map (view $ argName._1) queryparams
44 ++ body
45 ++ body
45− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
46+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
46 ++ [onSuccess, onError]
47 ++ [onSuccess, onError]
47
48
48− captures = map captureArg
49+ captures = map (fst . captureArg)
49 . filter isCapture
50 . filter isCapture
50 $ req ^. reqUrl.path
51 $ req ^. reqUrl.path
51
52
@@ -53,7 +54,7 @@ generateJQueryJSWith opts req = "\n" <>
53
54
54 queryparams = req ^.. reqUrl.queryStr.traverse
55 queryparams = req ^.. reqUrl.queryStr.traverse
55
56
56− body = if req ^. reqBody
57+ body = if isJust (req ^. reqBody)
57 then [requestBody opts]
58 then [requestBody opts]
58 else []
59 else []
59
60
@@ -61,7 +62,7 @@ generateJQueryJSWith opts req = "\n" <>
61 onError = errorCallback opts
62 onError = errorCallback opts
62
63
63 dataBody =
64 dataBody =
64− if req ^. reqBody
65+ if isJust $ req ^. reqBody
65 then " , data: JSON.stringify(body)\n" <>
66 then " , data: JSON.stringify(body)\n" <>
66 " , contentType: 'application/json'\n"
67 " , contentType: 'application/json'\n"
67 else ""
68 else ""
@@ -73,7 +74,7 @@ generateJQueryJSWith opts req = "\n" <>
73
74
74 where headersStr = T.intercalate ", " $ map headerStr hs
75 where headersStr = T.intercalate ", " $ map headerStr hs
75 headerStr header = "\"" <>
76 headerStr header = "\"" <>
76− headerArgName header <>
77+ fst (headerArg header) <>
77 "\": " <> toJSHeader header
78 "\": " <> toJSHeader header
78
79
79 namespace = if (moduleName opts) == ""
80 namespace = if (moduleName opts) == ""
servant-js/src/Servant/JS/Vanilla.hs+7−6
@@ -2,6 +2,7 @@
2module Servant.JS.Vanilla where
2module Servant.JS.Vanilla where
3
3
4import Control.Lens
4import Control.Lens
5+import Data.Maybe (isJust)
5import Data.Text (Text)
6import Data.Text (Text)
6import qualified Data.Text as T
7import qualified Data.Text as T
7import Data.Monoid
8import Data.Monoid
@@ -47,12 +48,12 @@ generateVanillaJSWith opts req = "\n" <>
47
48
48 where argsStr = T.intercalate ", " args
49 where argsStr = T.intercalate ", " args
49 args = captures
50 args = captures
50− ++ map (view argName) queryparams
51+ ++ map (view $ argName._1) queryparams
51 ++ body
52 ++ body
52− ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
53+ ++ map (toValidFunctionName . (<>) "header" . fst . headerArg) hs
53 ++ [onSuccess, onError]
54 ++ [onSuccess, onError]
54
55
55− captures = map captureArg
56+ captures = map (fst . captureArg)
56 . filter isCapture
57 . filter isCapture
57 $ req ^. reqUrl.path
58 $ req ^. reqUrl.path
58
59
@@ -60,7 +61,7 @@ generateVanillaJSWith opts req = "\n" <>
60
61
61 queryparams = req ^.. reqUrl.queryStr.traverse
62 queryparams = req ^.. reqUrl.queryStr.traverse
62
63
63− body = if req ^. reqBody
64+ body = if isJust(req ^. reqBody)
64 then [requestBody opts]
65 then [requestBody opts]
65 else []
66 else []
66
67
@@ -68,7 +69,7 @@ generateVanillaJSWith opts req = "\n" <>
68 onError = errorCallback opts
69 onError = errorCallback opts
69
70
70 dataBody =
71 dataBody =
71− if req ^. reqBody
72+ if isJust (req ^. reqBody)
72 then "JSON.stringify(body)\n"
73 then "JSON.stringify(body)\n"
73 else "null"
74 else "null"
74
75
@@ -80,7 +81,7 @@ generateVanillaJSWith opts req = "\n" <>
80
81
81 where headersStr = T.intercalate "\n" $ map headerStr hs
82 where headersStr = T.intercalate "\n" $ map headerStr hs
82 headerStr header = " xhr.setRequestHeader(\"" <>
83 headerStr header = " xhr.setRequestHeader(\"" <>
83− headerArgName header <>
84+ fst (headerArg header) <>
84 "\", " <> toJSHeader header <> ");"
85 "\", " <> toJSHeader header <> ");"
85
86
86 namespace = if moduleName opts == ""
87 namespace = if moduleName opts == ""
servant-js/test/Servant/JSSpec/CustomHeaders.hs+3−3
@@ -26,7 +26,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
26 type Foreign (Authorization sym a :> sublayout) = Foreign sublayout
26 type Foreign (Authorization sym a :> sublayout) = Foreign sublayout
27
27
28 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
28 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
29− req & reqHeaders <>~ [ ReplaceHeaderArg "Authorization" $
29+ req & reqHeaders <>~ [ ReplaceHeaderArg ("Authorization", "") $
30 tokenType (pack . symbolVal $ (Proxy :: Proxy sym)) ]
30 tokenType (pack . symbolVal $ (Proxy :: Proxy sym)) ]
31 where
31 where
32 tokenType t = t <> " {Authorization}"
32 tokenType t = t <> " {Authorization}"
@@ -39,7 +39,7 @@ instance (HasForeign sublayout)
39 type Foreign (MyLovelyHorse a :> sublayout) = Foreign sublayout
39 type Foreign (MyLovelyHorse a :> sublayout) = Foreign sublayout
40
40
41 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
41 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
42− req & reqHeaders <>~ [ ReplaceHeaderArg "X-MyLovelyHorse" tpl ]
42+ req & reqHeaders <>~ [ ReplaceHeaderArg ("X-MyLovelyHorse", "") tpl ]
43 where
43 where
44 tpl = "I am good friends with {X-MyLovelyHorse}"
44 tpl = "I am good friends with {X-MyLovelyHorse}"
45
45
@@ -51,6 +51,6 @@ instance (HasForeign sublayout)
51 type Foreign (WhatsForDinner a :> sublayout) = Foreign sublayout
51 type Foreign (WhatsForDinner a :> sublayout) = Foreign sublayout
52
52
53 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
53 foreignFor Proxy req = foreignFor (Proxy :: Proxy sublayout) $
54− req & reqHeaders <>~ [ ReplaceHeaderArg "X-WhatsForDinner" tpl ]
54+ req & reqHeaders <>~ [ ReplaceHeaderArg ("X-WhatsForDinner", "") tpl ]
55 where
55 where
56 tpl = "I would like {X-WhatsForDinner} with a cherry on top."
56 tpl = "I would like {X-WhatsForDinner} with a cherry on top."