@@ -132,9 +132,12 @@ data WithHeaders (headers :: [Type]) (returnType :: Type) (response :: Type)
132
132
133
133
-- | This is used to convert a response containing headers to a custom type
134
134
-- 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 )
138
141
139
142
-- | Single-header empty response
140
143
instance AsHeaders '[a ] () a where
@@ -146,6 +149,11 @@ instance AsHeaders '[h] a (a, h) where
146
149
toHeaders (t, cc) = (I cc :* Nil , t)
147
150
fromHeaders (I cc :* Nil , t) = (t, cc)
148
151
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
+
149
157
data DescHeader (name :: Symbol ) (description :: Symbol ) (a :: Type )
150
158
151
159
-- | A wrapper to turn a response header into an optional one.
@@ -420,6 +428,10 @@ instance AsConstructor '[] (RespondEmpty code description) where
420
428
toConstructor _ = Nil
421
429
fromConstructor _ = ()
422
430
431
+ instance AsConstructor '[a ] (WithHeaders headers a response ) where
432
+ toConstructor a = I a :* Nil
433
+ fromConstructor (I a :* Nil ) = a
434
+
423
435
newtype GenericAsConstructor r = GenericAsConstructor r
424
436
425
437
type instance ResponseType (GenericAsConstructor r ) = ResponseType r
0 commit comments