forked from scarf-sh/tie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.template.hs
More file actions
31 lines (26 loc) · 905 Bytes
/
Response.template.hs
File metadata and controls
31 lines (26 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Tie.Template.Response_
( ToResponse (..),
-- * NDJSON support
NDJSON,
responseNDJSON,
)
where
import qualified Data.Aeson
import qualified Data.Aeson.Encoding
import qualified Data.ByteString.Builder
import qualified Network.HTTP.Types
import qualified Network.Wai
type NDJSON element = ((element -> IO ()) -> IO () -> IO ())
responseNDJSON :: (Data.Aeson.ToJSON element) => Network.HTTP.Types.Status -> Network.HTTP.Types.ResponseHeaders -> NDJSON element -> Network.Wai.Response
responseNDJSON status responseHeaders stream =
Network.Wai.responseStream status responseHeaders $ \emit flush ->
stream
( \element ->
emit
( Data.Aeson.Encoding.fromEncoding (Data.Aeson.toEncoding element)
<> Data.ByteString.Builder.char7 '\n'
)
)
flush
class ToResponse a where
toResponse :: a -> Network.Wai.Response