git.iko.soy/typeable/servant
on master help sign in
Added default NoTypes parameter for dynamic languages.
721151aMaksymilian Owsianny
servant-foreign/src/Servant/Foreign.hs+1
@@ -30,6 +30,7 @@ module Servant.Foreign
30 , queryStr
30 , queryStr
31 , listFromAPI
31 , listFromAPI
32 , GenerateList(..)
32 , GenerateList(..)
33+ , NoTypes
33 -- re-exports
34 -- re-exports
34 , module Servant.API
35 , module Servant.API
35 ) where
36 ) where
servant-foreign/src/Servant/Foreign/Internal.hs+12−5
@@ -130,11 +130,6 @@ type family Elem (a :: *) (ls::[*]) :: Constraint where
130-- > -- instances.
130-- > -- instances.
131-- > data LangX
131-- > data LangX
132-- >
132-- >
133−-- > -- If the language __X__ is dynamically typed then you only need
134−-- > -- a catch all instance of a form
135−-- > instance HasForeignType LangX a where
136−-- > typeFor _ _ = empty
137−-- >
138-- > -- Otherwise you define instances for the types you need
133-- > -- Otherwise you define instances for the types you need
139-- > instance HasForeignType LangX Int where
134-- > instance HasForeignType LangX Int where
140-- > typeFor _ _ = "intX"
135-- > typeFor _ _ = "intX"
@@ -150,9 +145,21 @@ type family Elem (a :: *) (ls::[*]) :: Constraint where
150-- > => Proxy api -> [Req]
145-- > => Proxy api -> [Req]
151-- > getEndpoints api = listFromAPI (Proxy :: Proxy LangX) api
146-- > getEndpoints api = listFromAPI (Proxy :: Proxy LangX) api
152--
147--
148+-- > -- If language __X__ is dynamically typed then you can use
149+-- > -- a predefined NoTypes parameter
150+-- > getEndpoints :: (HasForeign NoTypes api, GenerateList (Foreign api))
151+-- > => Proxy api -> [Req]
152+-- > getEndpoints api = listFromAPI (Proxy :: Proxy NoTypes) api
153+-- >
154+--
153class HasForeignType lang a where
155class HasForeignType lang a where
154 typeFor :: Proxy lang -> Proxy a -> ForeignType
156 typeFor :: Proxy lang -> Proxy a -> ForeignType
155
157
158+data NoTypes
159+
160+instance HasForeignType NoTypes a where
161+ typeFor _ _ = empty
162+
156class HasForeign lang (layout :: *) where
163class HasForeign lang (layout :: *) where
157 type Foreign layout :: *
164 type Foreign layout :: *
158 foreignFor :: Proxy lang -> Proxy layout -> Req -> Foreign layout
165 foreignFor :: Proxy lang -> Proxy layout -> Req -> Foreign layout
servant-js/src/Servant/JS.hs+7−14
@@ -110,7 +110,7 @@ module Servant.JS
110 , -- * Misc.
110 , -- * Misc.
111 listFromAPI
111 listFromAPI
112 , javascript
112 , javascript
113− , LangJS
113+ , NoTypes
114 , GenerateList(..)
114 , GenerateList(..)
115 ) where
115 ) where
116
116
@@ -123,37 +123,30 @@ import Servant.JS.Axios
123import Servant.JS.Internal
123import Servant.JS.Internal
124import Servant.JS.JQuery
124import Servant.JS.JQuery
125import Servant.JS.Vanilla
125import Servant.JS.Vanilla
126−import Servant.Foreign (GenerateList(..), listFromAPI)
126+import Servant.Foreign (GenerateList(..), listFromAPI, NoTypes)
127−
128−-- Dummy type specifying target language
129−data LangJS
130
127
131-- | Generate the data necessary to generate javascript code
128-- | Generate the data necessary to generate javascript code
132-- for all the endpoints of an API, as ':<|>'-separated values
129-- for all the endpoints of an API, as ':<|>'-separated values
133-- of type 'AjaxReq'.
130-- of type 'AjaxReq'.
134−javascript :: HasForeign LangJS layout => Proxy layout -> Foreign layout
131+javascript :: HasForeign NoTypes layout => Proxy layout -> Foreign layout
135−javascript p = foreignFor (Proxy :: Proxy LangJS) p defReq
132+javascript p = foreignFor (Proxy :: Proxy NoTypes) p defReq
136
133
137-- | Directly generate all the javascript functions for your API
134-- | Directly generate all the javascript functions for your API
138-- from a 'Proxy' for your API type. You can then write it to
135-- from a 'Proxy' for your API type. You can then write it to
139-- a file or integrate it in a page, for example.
136-- a file or integrate it in a page, for example.
140−jsForAPI :: (HasForeign LangJS api, GenerateList (Foreign api))
137+jsForAPI :: (HasForeign NoTypes api, GenerateList (Foreign api))
141 => Proxy api -- ^ proxy for your API type
138 => Proxy api -- ^ proxy for your API type
142 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
139 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
143 -> Text -- ^ a text that you can embed in your pages or write to a file
140 -> Text -- ^ a text that you can embed in your pages or write to a file
144−jsForAPI p gen = gen (listFromAPI (Proxy :: Proxy LangJS) p)
141+jsForAPI p gen = gen (listFromAPI (Proxy :: Proxy NoTypes) p)
145
142
146-- | Directly generate all the javascript functions for your API
143-- | Directly generate all the javascript functions for your API
147-- from a 'Proxy' for your API type using the given generator
144-- from a 'Proxy' for your API type using the given generator
148-- and write the resulting code to a file at the given path.
145-- and write the resulting code to a file at the given path.
149−writeJSForAPI :: (HasForeign LangJS api, GenerateList (Foreign api))
146+writeJSForAPI :: (HasForeign NoTypes api, GenerateList (Foreign api))
150 => Proxy api -- ^ proxy for your API type
147 => Proxy api -- ^ proxy for your API type
151 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
148 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
152 -> FilePath -- ^ path to the file you want to write the resulting javascript code into
149 -> FilePath -- ^ path to the file you want to write the resulting javascript code into
153 -> IO ()
150 -> IO ()
154writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen)
151writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen)
155
152
156−-- A catch all instance since JavaScript has no types.
157−instance HasForeignType LangJS a where
158− typeFor _ _ = empty
159−
servant-js/test/Servant/JSSpec.hs+2−2
@@ -98,7 +98,7 @@ a `shouldNotContain` b = shouldNotSatisfy a (T.isInfixOf b)
98
98
99axiosSpec :: Spec
99axiosSpec :: Spec
100axiosSpec = describe specLabel $ do
100axiosSpec = describe specLabel $ do
101− let reqList = listFromAPI (Proxy :: Proxy LangJS) (Proxy :: Proxy TestAPI)
101+ let reqList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy TestAPI)
102 it "should add withCredentials when needed" $ do
102 it "should add withCredentials when needed" $ do
103 let jsText = genJS withCredOpts $ reqList
103 let jsText = genJS withCredOpts $ reqList
104 output jsText
104 output jsText
@@ -122,7 +122,7 @@ axiosSpec = describe specLabel $ do
122
122
123angularSpec :: TestNames -> Spec
123angularSpec :: TestNames -> Spec
124angularSpec test = describe specLabel $ do
124angularSpec test = describe specLabel $ do
125− let reqList = listFromAPI (Proxy :: Proxy LangJS) (Proxy :: Proxy TestAPI)
125+ let reqList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy TestAPI)
126 it "should implement a service globally" $ do
126 it "should implement a service globally" $ do
127 let jsText = genJS reqList
127 let jsText = genJS reqList
128 output jsText
128 output jsText