git.iko.soy/typeable/servant
on master help sign in
Revert "Move WithStatus to cookbook [WIP]"

This reverts commit 5e43135e37e0b033ce5654cdfdf9daadcc7728e3.

commit49801db15149cab5e1b211d7d06ae65d4fa0d397 authorMatthias Fischmann <mf@zerobuzz.net> dateSun 6 Dec 2020, 14:56 (6 years ago) parent5e43135
2 files changed+33−2
servant/src/Servant/API.hs+1−1 servant/src/Servant/API/UVerb.hs+32−1
servant/src/Servant/API.hs+1−1
@@ -127,7 +127,7 @@ import Servant.API.Sub
127127 ((:>))
128128import Servant.API.UVerb
129129 (HasStatus, IsMember, StatusOf, Statuses, UVerb, Union,
130− Unique, inject, statusOf)
130+ Unique, WithStatus (..), inject, statusOf)
131131import Servant.API.Vault
132132 (Vault)
133133import Servant.API.Verbs
servant/src/Servant/API/UVerb.hs+32−1
@@ -28,6 +28,7 @@ module Servant.API.UVerb
2828 HasStatus (StatusOf),
2929 statusOf,
3030 HasStatuses (Statuses, statuses),
31+ WithStatus (..),
3132 module Servant.API.UVerb.Union,
3233 )
3334where
@@ -35,7 +36,7 @@ where
3536import Data.Proxy (Proxy (Proxy))
3637import GHC.TypeLits (Nat)
3738import Network.HTTP.Types (Status, StdMethod)
38−import Servant.API.ContentTypes (MimeRender (mimeRender), MimeUnrender (mimeUnrender), NoContent)
39+import Servant.API.ContentTypes (NoContent, MimeRender(mimeRender), MimeUnrender(mimeUnrender))
3940import Servant.API.Status (KnownStatus, statusVal)
4041import Servant.API.UVerb.Union
4142
@@ -63,6 +64,36 @@ instance (HasStatus a, HasStatuses as) => HasStatuses (a ': as) where
6364 type Statuses (a ': as) = StatusOf a ': Statuses as
6465 statuses _ = statusOf (Proxy :: Proxy a) : statuses (Proxy :: Proxy as)
6566
67+-- | A simple newtype wrapper that pairs a type with its status code. It
68+-- implements all the content types that Servant ships with by default.
69+newtype WithStatus (k :: Nat) a = WithStatus a
70+ deriving (Eq, Show)
71+
72+instance MimeRender ctype a => MimeRender ctype (WithStatus _status a) where
73+ mimeRender contentTypeProxy (WithStatus a) = mimeRender contentTypeProxy a
74+
75+instance MimeUnrender ctype a => MimeUnrender ctype (WithStatus _status a) where
76+ mimeUnrender contentTypeProxy input =
77+ WithStatus <$> mimeUnrender contentTypeProxy input
78+
79+-- | an instance of this typeclass assigns a HTTP status code to a return type
80+--
81+-- Example:
82+--
83+-- @
84+-- data NotFoundError = NotFoundError String
85+--
86+-- instance HasStatus NotFoundError where
87+-- type StatusOf NotFoundError = 404
88+-- @
89+--
90+-- You can also use the convience newtype wrapper 'WithStatus' if you want to
91+-- avoid writing a 'HasStatus' instance manually. It also has the benefit of
92+-- showing the status code in the type; which might aid in readability.
93+instance KnownStatus n => HasStatus (WithStatus n a) where
94+ type StatusOf (WithStatus n a) = n
95+
96+
6697-- | A variant of 'Verb' that can have any of a number of response values and status codes.
6798--
6899-- FUTUREWORK: it would be nice to make 'Verb' a special case of 'UVerb', and only write