Skip to content

[RFC] Add TypeRep of api end-point to Foreign.Req. #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions servant-foreign/src/Servant/Foreign.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Servant.Foreign
, reqBody
, reqReturnType
, reqFuncName
, reqApiType
, path
, queryStr
, queryArgName
Expand Down
10 changes: 6 additions & 4 deletions servant-foreign/src/Servant/Foreign/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion servant-js/src/Servant/JS/JQuery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"