diff --git a/servant-foreign/src/Servant/Foreign.hs b/servant-foreign/src/Servant/Foreign.hs index e2d212b61..8cd925345 100644 --- a/servant-foreign/src/Servant/Foreign.hs +++ b/servant-foreign/src/Servant/Foreign.hs @@ -23,6 +23,7 @@ module Servant.Foreign , reqBody , reqReturnType , reqFuncName + , reqApiType , path , queryStr , queryArgName diff --git a/servant-foreign/src/Servant/Foreign/Internal.hs b/servant-foreign/src/Servant/Foreign/Internal.hs index 4e4578973..b9d33ee58 100644 --- a/servant-foreign/src/Servant/Foreign/Internal.hs +++ b/servant-foreign/src/Servant/Foreign/Internal.hs @@ -13,6 +13,7 @@ import Control.Lens (makePrisms, makeLenses, Getter, (&), (<>~), (%~), import Data.Monoid #endif import Data.Proxy +import Data.Typeable (Typeable, TypeRep, typeOf) import Data.String import Data.Text import Data.Text.Encoding (decodeUtf8) @@ -125,6 +126,7 @@ data Req f = Req , _reqBody :: Maybe f , _reqReturnType :: Maybe f , _reqFuncName :: FunctionName + , _reqApiType :: TypeRep } deriving instance Eq f => Eq (Req f) @@ -133,7 +135,7 @@ deriving instance Show f => Show (Req f) makeLenses ''Req defReq :: Req ftype -defReq = Req defUrl "GET" [] Nothing Nothing (FunctionName []) +defReq = Req defUrl "GET" [] Nothing Nothing (FunctionName []) (typeOf ()) -- | To be used exclusively as a "negative" return type/constraint -- by @'Elem`@ type family. @@ -188,13 +190,13 @@ class HasForeign lang ftype (layout :: *) where type Foreign ftype layout :: * foreignFor :: Proxy lang -> Proxy ftype -> Proxy layout -> Req ftype -> Foreign ftype layout -instance (HasForeign lang ftype a, HasForeign lang ftype b) +instance (HasForeign lang ftype a, HasForeign lang ftype b, Typeable a, Typeable b) => HasForeign lang ftype (a :<|> b) where type Foreign ftype (a :<|> b) = Foreign ftype a :<|> Foreign ftype b foreignFor lang ftype Proxy req = - foreignFor lang ftype (Proxy :: Proxy a) req - :<|> foreignFor lang ftype (Proxy :: Proxy b) req + foreignFor lang ftype (Proxy :: Proxy a) (req & reqApiType .~ typeOf (undefined :: a)) + :<|> foreignFor lang ftype (Proxy :: Proxy b) (req & reqApiType .~ typeOf (undefined :: b)) instance (KnownSymbol sym, HasForeignType lang ftype t, HasForeign lang ftype sublayout) => HasForeign lang ftype (Capture sym t :> sublayout) where diff --git a/servant-js/src/Servant/JS/JQuery.hs b/servant-js/src/Servant/JS/JQuery.hs index 98038f0c6..b02996158 100644 --- a/servant-js/src/Servant/JS/JQuery.hs +++ b/servant-js/src/Servant/JS/JQuery.hs @@ -29,7 +29,8 @@ generateJQueryJS = generateJQueryJSWith defCommonGeneratorOptions -- | js codegen using JQuery generateJQueryJSWith :: CommonGeneratorOptions -> AjaxReq -> Text generateJQueryJSWith opts req = "\n" <> - fname <> " = function(" <> argsStr <> ")\n" + jsdocs + <> fname <> " = function(" <> argsStr <> ")\n" <> "{\n" <> " $.ajax(\n" <> " { url: " <> url <> "\n" @@ -101,3 +102,6 @@ generateJQueryJSWith opts req = "\n" <> queryArgs = if null queryparams then "" else " + '?" <> jsParams queryparams + + jsdocs :: Text + jsdocs = "// " <> (T.pack . show $ req ^. reqApiType) <> "\n"