-
Notifications
You must be signed in to change notification settings - Fork 1
/
Http.purs
114 lines (89 loc) · 2.95 KB
/
Http.purs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module Bucketchain.Http
( Http
, toRequest
, httpStream
, httpVersion
, requestHeaders
, requestMethod
, requestOriginalURL
, requestURL
, requestBody
, toReadable
, responseHeaders
, statusCode
, setHeader
, setHeaders
, setRequestURL
, setStatusCode
, setStatusMessage
, toWritable
) where
import Prelude
import Bucketchain.Stream (convertToString)
import Effect (Effect)
import Effect.Aff (Aff)
import Foreign.Object (Object)
import Node.HTTP as HTTP
import Node.Stream (Readable, Writable)
-- | The type of a HTTP stream.
newtype Http = Http
{ req :: HTTP.Request
, res :: HTTP.Response
}
-- | Convert a HTTP stream to a Request stream.
toRequest :: Http -> HTTP.Request
toRequest (Http { req }) = req
toResponse :: Http -> HTTP.Response
toResponse (Http { res }) = res
-- | Create a HTTP stream.
httpStream :: HTTP.Request -> HTTP.Response -> Http
httpStream req res = Http { req, res }
-- | Get the request HTTP version.
httpVersion :: Http -> String
httpVersion = toRequest >>> HTTP.httpVersion
-- | Get the request headers.
requestHeaders :: Http -> Object String
requestHeaders = toRequest >>> HTTP.requestHeaders
-- | Get the request method (GET, POST, etc.).
requestMethod :: Http -> String
requestMethod = toRequest >>> HTTP.requestMethod
-- | Get the request original URL.
requestOriginalURL :: Http -> String
requestOriginalURL = toRequest >>> _requestOriginalURL
-- | Get the request URL.
requestURL :: Http -> String
requestURL = toRequest >>> HTTP.requestURL
-- | Get the request body.
requestBody :: Http -> Aff String
requestBody = toReadable >>> convertToString
-- | Convert a Http stream to a Readable stream.
toReadable :: Http -> Readable ()
toReadable = toRequest >>> HTTP.requestAsStream
-- | Get the response headers.
responseHeaders :: Http -> Object String
responseHeaders = toResponse >>> _responseHeaders
-- | Get the status code.
statusCode :: Http -> Int
statusCode = toResponse >>> _statusCode
-- | Set a header with a single value.
setHeader :: Http -> String -> String -> Effect Unit
setHeader = toResponse >>> HTTP.setHeader
-- | Set a header with multiple values.
setHeaders :: Http -> String -> Array String -> Effect Unit
setHeaders = toResponse >>> HTTP.setHeaders
-- | Set the request URL.
setRequestURL :: Http -> String -> Effect Unit
setRequestURL = toRequest >>> _setRequestURL
-- | Set the status code.
setStatusCode :: Http -> Int -> Effect Unit
setStatusCode = toResponse >>> HTTP.setStatusCode
-- | Set the status message.
setStatusMessage :: Http -> String -> Effect Unit
setStatusMessage = toResponse >>> HTTP.setStatusMessage
-- | This is for internal. Do not use it.
toWritable :: Http -> Writable ()
toWritable = toResponse >>> HTTP.responseAsStream
foreign import _setRequestURL :: HTTP.Request -> String -> Effect Unit
foreign import _requestOriginalURL :: HTTP.Request -> String
foreign import _responseHeaders :: HTTP.Response -> Object String
foreign import _statusCode :: HTTP.Response -> Int