From 2d8db459077df2915e19de41690561bcf51f9b74 Mon Sep 17 00:00:00 2001 From: Maksymilian Owsianny Date: Wed, 2 Dec 2015 11:21:37 +0000 Subject: [PATCH] Moved GenerateList to servant-foreign. --- servant-foreign/src/Servant/Foreign.hs | 2 ++ .../src/Servant/Foreign/Internal.hs | 18 +++++++++++++++++ servant-js/src/Servant/JS.hs | 20 ++----------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/servant-foreign/src/Servant/Foreign.hs b/servant-foreign/src/Servant/Foreign.hs index 087284ce..cfa1acba 100644 --- a/servant-foreign/src/Servant/Foreign.hs +++ b/servant-foreign/src/Servant/Foreign.hs @@ -28,6 +28,8 @@ module Servant.Foreign , reqReturnType , segment , queryStr + , listFromAPI + , GenerateList(..) -- re-exports , module Servant.API ) where diff --git a/servant-foreign/src/Servant/Foreign/Internal.hs b/servant-foreign/src/Servant/Foreign/Internal.hs index 25f6c35f..349b0c71 100644 --- a/servant-foreign/src/Servant/Foreign/Internal.hs +++ b/servant-foreign/src/Servant/Foreign/Internal.hs @@ -294,3 +294,21 @@ instance HasForeign lang sublayout => HasForeign lang (HttpVersion :> sublayout) foreignFor lang Proxy req = foreignFor lang (Proxy :: Proxy sublayout) req + +-- | Utility class used by 'listFromAPI' which computes +-- the data needed to generate a function for each endpoint +-- and hands it all back in a list. +class GenerateList reqs where + generateList :: reqs -> [Req] + +instance GenerateList Req where + generateList r = [r] + +instance (GenerateList start, GenerateList rest) => GenerateList (start :<|> rest) where + generateList (start :<|> rest) = (generateList start) ++ (generateList rest) + +-- | Generate the necessary data for codegen as a list, each 'Req' +-- describing one endpoint from your API type. +listFromAPI :: (HasForeign lang api, GenerateList (Foreign api)) => Proxy lang -> Proxy api -> [Req] +listFromAPI lang p = generateList (foreignFor lang p defReq) + diff --git a/servant-js/src/Servant/JS.hs b/servant-js/src/Servant/JS.hs index d25706ec..b2e63ca3 100644 --- a/servant-js/src/Servant/JS.hs +++ b/servant-js/src/Servant/JS.hs @@ -122,6 +122,7 @@ import Servant.JS.Axios import Servant.JS.Internal import Servant.JS.JQuery import Servant.JS.Vanilla +import Servant.Foreign (GenerateList(..), listFromAPI) -- Dummy type specifying target language data LangJS @@ -139,7 +140,7 @@ jsForAPI :: (HasForeign LangJS api, GenerateList (Foreign api)) => Proxy api -- ^ proxy for your API type -> JavaScriptGenerator -- ^ js code generator to use (angular, vanilla js, jquery, others) -> Text -- ^ a text that you can embed in your pages or write to a file -jsForAPI p gen = gen (listFromAPI p) +jsForAPI p gen = gen (listFromAPI (Proxy :: Proxy LangJS) p) -- | Directly generate all the javascript functions for your API -- from a 'Proxy' for your API type using the given generator @@ -155,20 +156,3 @@ writeJSForAPI p gen fp = writeFile fp (jsForAPI p gen) instance HasForeignType LangJS a where typeFor _ _ = empty --- | Utility class used by 'jsForAPI' which computes --- the data needed to generate a function for each endpoint --- and hands it all back in a list. -class GenerateList reqs where - generateList :: reqs -> [AjaxReq] - -instance GenerateList AjaxReq where - generateList r = [r] - -instance (GenerateList start, GenerateList rest) => GenerateList (start :<|> rest) where - generateList (start :<|> rest) = (generateList start) ++ (generateList rest) - --- | Generate the necessary data for JS codegen as a list, each 'AjaxReq' --- describing one endpoint from your API type. -listFromAPI :: (HasForeign LangJS api, GenerateList (Foreign api)) => Proxy api -> [AjaxReq] -listFromAPI p = generateList (javascript p) -