Skip to content

Commit 2a0bae9

Browse files
committed
Added OperationId combinator
1 parent a8f584f commit 2a0bae9

File tree

8 files changed

+58
-13
lines changed

8 files changed

+58
-13
lines changed

servant-client-core/src/Servant/Client/Core/HasClient.hs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ import Servant.API
7272
FromSourceIO (..), Header', Headers (..), HttpVersion,
7373
IsSecure, MimeRender (mimeRender),
7474
MimeUnrender (mimeUnrender), NoContent (NoContent),
75-
NoContentVerb, QueryFlag, QueryParam', QueryParams, Raw,
76-
ReflectMethod (..), RemoteHost, ReqBody', SBoolI, Stream,
77-
StreamBody', Summary, ToHttpApiData, ToSourceIO (..), Vault,
78-
Verb, WithNamedContext, contentType, getHeadersHList,
79-
getResponse, toQueryParam, toUrlPiece)
75+
NoContentVerb, OperationId, QueryFlag, QueryParam',
76+
QueryParams, Raw, ReflectMethod (..), RemoteHost, ReqBody',
77+
SBoolI, Stream, StreamBody', Summary, ToHttpApiData,
78+
ToSourceIO (..), Vault, Verb, WithNamedContext, contentType,
79+
getHeadersHList, getResponse, toQueryParam, toUrlPiece)
8080
import Servant.API.ContentTypes
8181
(contentTypes, AllMime (allMime), AllMimeUnrender (allMimeUnrender))
8282
import Servant.API.TypeLevel (FragmentUnique, AtLeastOneFragment)
@@ -496,6 +496,14 @@ instance HasClient m api => HasClient m (Description desc :> api) where
496496

497497
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
498498

499+
-- | Ignore @'OperationId'@ in client functions.
500+
instance HasClient m api => HasClient m (OperationId opid :> api) where
501+
type Client m (OperationId opid :> api) = Client m api
502+
503+
clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
504+
505+
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
506+
499507
-- | If you use a 'QueryParam' in one of your endpoints in your API,
500508
-- the corresponding querying function will automatically take
501509
-- an additional argument of the type specified by your 'QueryParam',
@@ -753,7 +761,7 @@ instance ( HasClient m api
753761

754762
-- | Ignore @'Fragment'@ in client functions.
755763
-- See <https://ietf.org/rfc/rfc2616.html#section-15.1.3> for more details.
756-
--
764+
--
757765
-- Example:
758766
--
759767
-- > type MyApi = "books" :> Fragment Text :> Get '[JSON] [Book]
@@ -774,7 +782,7 @@ instance ( HasClient m api
774782

775783
type Client m (Fragment a :> api) = Client m api
776784

777-
clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
785+
clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
778786

779787
hoistClientMonad pm _ = hoistClientMonad pm (Proxy :: Proxy api)
780788

servant-docs/src/Servant/Docs/Internal.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,16 @@ instance (KnownSymbol desc, HasDocs api)
10201020
action' = over notes (|> note) action
10211021
note = DocNote (symbolVal (Proxy :: Proxy desc)) []
10221022

1023+
instance (KnownSymbol opid, HasDocs api)
1024+
=> HasDocs (OperationId opid :> api) where
1025+
1026+
docsFor Proxy (endpoint, action) =
1027+
docsFor subApiP (endpoint, action')
1028+
1029+
where subApiP = Proxy :: Proxy api
1030+
action' = over notes (|> note) action
1031+
note = DocNote (symbolVal (Proxy :: Proxy opid)) []
1032+
10231033
instance (KnownSymbol desc, HasDocs api)
10241034
=> HasDocs (Summary desc :> api) where
10251035

servant-foreign/src/Servant/Foreign/Internal.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ instance HasForeign lang ftype api
422422
foreignFor lang ftype Proxy req =
423423
foreignFor lang ftype (Proxy :: Proxy api) req
424424

425+
instance HasForeign lang ftype api
426+
=> HasForeign lang ftype (OperationId opid :> api) where
427+
type Foreign ftype (OperationId opid :> api) = Foreign ftype api
428+
429+
foreignFor lang ftype Proxy req =
430+
foreignFor lang ftype (Proxy :: Proxy api) req
431+
425432
-- | Utility class used by 'listFromAPI' which computes
426433
-- the data needed to generate a function for each endpoint
427434
-- and hands it all back in a list.

servant-server/src/Servant/Server/Internal.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ import Servant.API
7474
CaptureAll, Description, EmptyAPI, Fragment,
7575
FramingRender (..), FramingUnrender (..), FromSourceIO (..),
7676
Header', If, IsSecure (..), NoContentVerb, QueryFlag,
77-
QueryParam', QueryParams, Raw, ReflectMethod (reflectMethod),
78-
RemoteHost, ReqBody', SBool (..), SBoolI (..), SourceIO,
79-
Stream, StreamBody', Summary, ToSourceIO (..), Vault, Verb,
80-
WithNamedContext)
77+
OperationId, QueryParam', QueryParams, Raw,
78+
ReflectMethod (reflectMethod), RemoteHost, ReqBody',
79+
SBool (..), SBoolI (..), SourceIO, Stream, StreamBody',
80+
Summary, ToSourceIO (..), Vault, Verb, WithNamedContext)
8181
import Servant.API.ContentTypes
8282
(AcceptHeader (..), AllCTRender (..), AllCTUnrender (..),
8383
AllMime, MimeRender (..), MimeUnrender (..), NoContent,
@@ -745,6 +745,13 @@ instance HasServer api ctx => HasServer (Description desc :> api) ctx where
745745
route _ = route (Proxy :: Proxy api)
746746
hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt s
747747

748+
-- Ignore @'OperationId'@ in server handlers
749+
instance HasServer api ctx => HasServer (OperationId opid :> api) ctx where
750+
type ServerT (OperationId opid :> api) m = ServerT api m
751+
752+
route _ = route (Proxy :: Proxy api)
753+
hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt s
754+
748755
-- | Singleton type representing a server that serves an empty API.
749756
data EmptyServer = EmptyServer deriving (Typeable, Eq, Show, Bounded, Enum)
750757

servant/src/Servant/API.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import Servant.API.ContentTypes
9090
MimeUnrender (..), NoContent (NoContent), OctetStream,
9191
PlainText)
9292
import Servant.API.Description
93-
(Description, Summary)
93+
(Description, OperationId, Summary)
9494
import Servant.API.Empty
9595
(EmptyAPI (..))
9696
import Servant.API.Experimental.Auth

servant/src/Servant/API/Description.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
module Servant.API.Description (
1010
-- * Combinators
1111
Description,
12+
OperationId,
1213
Summary,
1314
-- * Used as modifiers
1415
FoldDescription,
@@ -46,6 +47,13 @@ data Summary (sym :: Symbol)
4647
data Description (sym :: Symbol)
4748
deriving (Typeable)
4849

50+
-- | Add a unique identifier for an endpoint
51+
--
52+
-- Example:
53+
--
54+
-- >>> type MyApi = OperatorId "getBooksByISBN" :> "books" :> Capture "isbn" Text :> Get '[JSON] Book
55+
data OperationId (sym :: Symbol)
56+
4957
-- | Fold list of modifiers to extract description as a type-level String.
5058
--
5159
-- >>> :kind! FoldDescription '[]

servant/src/Servant/Links.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ import Servant.API.BasicAuth
149149
import Servant.API.Capture
150150
(Capture', CaptureAll)
151151
import Servant.API.Description
152-
(Description, Summary)
152+
(Description, OperationId, Summary)
153153
import Servant.API.Empty
154154
(EmptyAPI (..))
155155
import Servant.API.Experimental.Auth
@@ -532,6 +532,10 @@ instance HasLink sub => HasLink (Description s :> sub) where
532532
type MkLink (Description s :> sub) a = MkLink sub a
533533
toLink = simpleToLink (Proxy :: Proxy sub)
534534

535+
instance HasLink sub => HasLink (OperationId opid :> sub) where
536+
type MkLink (OperationId opid :> sub) a = MkLink sub a
537+
toLink = simpleToLink (Proxy :: Proxy sub)
538+
535539
instance HasLink sub => HasLink (Summary s :> sub) where
536540
type MkLink (Summary s :> sub) a = MkLink sub a
537541
toLink = simpleToLink (Proxy :: Proxy sub)

servant/src/Servant/Test/ComprehensiveAPI.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type ComprehensiveAPIWithoutStreamingOrRaw' endpoint =
7070
:<|> "capture-all" :> CaptureAll "foo" Int :> GET
7171
:<|> "summary" :> Summary "foo" :> GET
7272
:<|> "description" :> Description "foo" :> GET
73+
:<|> "operation-id" :> OperationId "getFoo" :> GET
7374
:<|> "alternative" :> ("left" :> GET :<|> "right" :> GET)
7475
:<|> "fragment" :> Fragment Int :> GET
7576
:<|> endpoint

0 commit comments

Comments
 (0)