Skip to content

Commit 527e99e

Browse files
authored
Add more multiverb (#1804)
1 parent 67cb6c6 commit 527e99e

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

servant/src/Servant/API/MultiVerb.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,12 @@ data WithHeaders (headers :: [Type]) (returnType :: Type) (response :: Type)
132132

133133
-- | This is used to convert a response containing headers to a custom type
134134
-- including the information in the headers.
135-
class AsHeaders xs a b where
136-
fromHeaders :: (NP I xs, a) -> b
137-
toHeaders :: b -> (NP I xs, a)
135+
--
136+
-- If you need to send a combination of headers and response that is not provided by Servant,
137+
-- you can cwrite your own instance. Take example on the ones provided.
138+
class AsHeaders headers response returnType where
139+
fromHeaders :: (NP I headers, response) -> returnType
140+
toHeaders :: returnType -> (NP I headers, response)
138141

139142
-- | Single-header empty response
140143
instance AsHeaders '[a] () a where
@@ -146,6 +149,11 @@ instance AsHeaders '[h] a (a, h) where
146149
toHeaders (t, cc) = (I cc :* Nil, t)
147150
fromHeaders (I cc :* Nil, t) = (t, cc)
148151

152+
-- | Two headers and an empty response, return value is a tuple of the response and the header
153+
instance AsHeaders '[a, b] () (a, b) where
154+
toHeaders (h1, h2) = (I h1 :* I h2 :* Nil, ())
155+
fromHeaders (I h1 :* I h2 :* Nil, ()) = (h1, h2)
156+
149157
data DescHeader (name :: Symbol) (description :: Symbol) (a :: Type)
150158

151159
-- | A wrapper to turn a response header into an optional one.
@@ -420,6 +428,10 @@ instance AsConstructor '[] (RespondEmpty code description) where
420428
toConstructor _ = Nil
421429
fromConstructor _ = ()
422430

431+
instance AsConstructor '[a] (WithHeaders headers a response) where
432+
toConstructor a = I a :* Nil
433+
fromConstructor (I a :* Nil) = a
434+
423435
newtype GenericAsConstructor r = GenericAsConstructor r
424436

425437
type instance ResponseType (GenericAsConstructor r) = ResponseType r

0 commit comments

Comments
 (0)