Complete usage example (empty HasServer instance)

This commit is contained in:
Gaël Deest 2022-04-13 11:28:11 +02:00
parent 6d43580208
commit a3efe4163d
2 changed files with 30 additions and 10 deletions

View file

@ -1,8 +1,11 @@
{-# LANGUAGE DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeOperators #-}
@ -29,7 +32,7 @@ instance FromJSON Greet
instance ToJSON Greet instance ToJSON Greet
-- API specification -- API specification
type TestApi = type TestApi' =
-- GET /hello/:name?capital={true, false} returns a Greet as JSON -- GET /hello/:name?capital={true, false} returns a Greet as JSON
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet
@ -42,6 +45,11 @@ type TestApi =
:<|> NamedRoutes OtherRoutes :<|> NamedRoutes OtherRoutes
type TestApi =
TestApi'
:<|> "redirect" :> Capture "redirectValue" Int :> RedirectOf TestApi'
data OtherRoutes mode = OtherRoutes data OtherRoutes mode = OtherRoutes
{ version :: mode :- Get '[JSON] Int { version :: mode :- Get '[JSON] Int
, bye :: mode :- "bye" :> Capture "name" Text :> Get '[JSON] Text , bye :: mode :- "bye" :> Capture "name" Text :> Get '[JSON] Text
@ -58,7 +66,7 @@ testApi = Proxy
-- --
-- Each handler runs in the 'Handler' monad. -- Each handler runs in the 'Handler' monad.
server :: Server TestApi server :: Server TestApi
server = helloH :<|> postGreetH :<|> deleteGreetH :<|> otherRoutes server = (helloH :<|> postGreetH :<|> deleteGreetH :<|> otherRoutes) :<|> redirect
where otherRoutes = OtherRoutes {..} where otherRoutes = OtherRoutes {..}
bye name = pure $ "Bye, " <> name <> " !" bye name = pure $ "Bye, " <> name <> " !"
@ -72,6 +80,13 @@ server = helloH :<|> postGreetH :<|> deleteGreetH :<|> otherRoutes
deleteGreetH _ = return NoContent deleteGreetH _ = return NoContent
redirect 42 = pure $
RedirectOf (Proxy @("hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet))
(\buildPath -> buildPath "Nicolas" (Just True))
-- Fail in any other case
redirect _ = throwError err500
-- Turn the server into a WAI app. 'serve' is provided by servant, -- Turn the server into a WAI app. 'serve' is provided by servant,
-- more precisely by the Servant.Server module. -- more precisely by the Servant.Server module.
test :: Application test :: Application

View file

@ -76,7 +76,7 @@ import Servant.API
QueryParam', QueryParams, Raw, ReflectMethod (reflectMethod), QueryParam', QueryParams, Raw, ReflectMethod (reflectMethod),
RemoteHost, ReqBody', SBool (..), SBoolI (..), SourceIO, RemoteHost, ReqBody', SBool (..), SBoolI (..), SourceIO,
Stream, StreamBody', Summary, ToSourceIO (..), Vault, Verb, Stream, StreamBody', Summary, ToSourceIO (..), Vault, Verb,
WithNamedContext, NamedRoutes) WithNamedContext, NamedRoutes, RedirectOf(..))
import Servant.API.Generic (GenericMode(..), ToServant, ToServantApi, GServantProduct, toServant, fromServant) import Servant.API.Generic (GenericMode(..), ToServant, ToServantApi, GServantProduct, toServant, fromServant)
import Servant.API.ContentTypes import Servant.API.ContentTypes
(AcceptHeader (..), AllCTRender (..), AllCTUnrender (..), (AcceptHeader (..), AllCTRender (..), AllCTUnrender (..),
@ -888,6 +888,11 @@ instance (AtLeastOneFragment api, FragmentUnique (Fragment a1 :> api), HasServer
hoistServerWithContext _ = hoistServerWithContext (Proxy :: Proxy api) hoistServerWithContext _ = hoistServerWithContext (Proxy :: Proxy api)
instance HasServer (RedirectOf api) context where
type ServerT (RedirectOf api) m = m (RedirectOf api)
route _ = undefined
hoistServerWithContext _ _ _ = undefined
-- $setup -- $setup
-- >>> import Servant -- >>> import Servant