git.iko.soy/typeable/servant
on master help sign in
Added default NoTypes parameter for dynamic languages.
commit721151a32d3f2154ee26eb33e16fc1269789d00a authorMaksymilian Owsianny <Maksymilian.Owsianny@gmail.com> dateWed 2 Dec 2015, 15:56 (11 years ago) parent83600d5
4 files changed+22−21
servant-foreign/src/Servant/Foreign.hs+1 servant-foreign/src/Servant/Foreign/Internal.hs+12−5 servant-js/src/Servant/JS.hs+7−14 servant-js/test/Servant/JSSpec.hs+2−2
servant-foreign/src/Servant/Foreign.hs+1
@@ -30,6 +30,7 @@ module Servant.Foreign
3030 , queryStr
3131 , listFromAPI
3232 , GenerateList(..)
33+ , NoTypes
3334 -- re-exports
3435 , module Servant.API
3536 ) where
servant-foreign/src/Servant/Foreign/Internal.hs+12−5
@@ -130,11 +130,6 @@ type family Elem (a :: *) (ls::[*]) :: Constraint where
130130-- > -- instances.
131131-- > data LangX
132132-- >
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−-- >
138133-- > -- Otherwise you define instances for the types you need
139134-- > instance HasForeignType LangX Int where
140135-- > typeFor _ _ = "intX"
@@ -150,9 +145,21 @@ type family Elem (a :: *) (ls::[*]) :: Constraint where
150145-- > => Proxy api -> [Req]
151146-- > getEndpoints api = listFromAPI (Proxy :: Proxy LangX) api
152147--
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+--
153155class HasForeignType lang a where
154156 typeFor :: Proxy lang -> Proxy a -> ForeignType
155157
158+data NoTypes
159+
160+instance HasForeignType NoTypes a where
161+ typeFor _ _ = empty
162+
156163class HasForeign lang (layout :: *) where
157164 type Foreign layout :: *
158165 foreignFor :: Proxy lang -> Proxy layout -> Req -> Foreign layout
servant-js/src/Servant/JS.hs+7−14
@@ -110,7 +110,7 @@ module Servant.JS
110110 , -- * Misc.
111111 listFromAPI
112112 , javascript
113− , LangJS
113+ , NoTypes
114114 , GenerateList(..)
115115 ) where
116116
@@ -123,37 +123,30 @@ import Servant.JS.Axios
123123import Servant.JS.Internal
124124import Servant.JS.JQuery
125125import Servant.JS.Vanilla
126−import Servant.Foreign (GenerateList(..), listFromAPI)
127−
128−-- Dummy type specifying target language
129−data LangJS
126+import Servant.Foreign (GenerateList(..), listFromAPI, NoTypes)
130127
131128-- | Generate the data necessary to generate javascript code
132129-- for all the endpoints of an API, as ':<|>'-separated values
133130-- of type 'AjaxReq'.
134−javascript :: HasForeign LangJS layout => Proxy layout -> Foreign layout
135−javascript p = foreignFor (Proxy :: Proxy LangJS) p defReq
131+javascript :: HasForeign NoTypes layout => Proxy layout -> Foreign layout
132+javascript p = foreignFor (Proxy :: Proxy NoTypes) p defReq
136133
137134-- | Directly generate all the javascript functions for your API
138135-- from a 'Proxy' for your API type. You can then write it to
139136-- 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))
141138 => Proxy api -- ^ proxy for your API type
142139 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
143140 -> 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)
145142
146143-- | Directly generate all the javascript functions for your API
147144-- from a 'Proxy' for your API type using the given generator
148145-- 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))
150147 => Proxy api -- ^ proxy for your API type
151148 -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others)
152149 -> FilePath -- ^ path to the file you want to write the resulting javascript code into
153150 -> IO ()
154151writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen)
155152
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)
9898
9999axiosSpec :: Spec
100100axiosSpec = describe specLabel $ do
101− let reqList = listFromAPI (Proxy :: Proxy LangJS) (Proxy :: Proxy TestAPI)
101+ let reqList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy TestAPI)
102102 it "should add withCredentials when needed" $ do
103103 let jsText = genJS withCredOpts $ reqList
104104 output jsText
@@ -122,7 +122,7 @@ axiosSpec = describe specLabel $ do
122122
123123angularSpec :: TestNames -> Spec
124124angularSpec test = describe specLabel $ do
125− let reqList = listFromAPI (Proxy :: Proxy LangJS) (Proxy :: Proxy TestAPI)
125+ let reqList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy TestAPI)
126126 it "should implement a service globally" $ do
127127 let jsText = genJS reqList
128128 output jsText