@@ -3,7 +3,6 @@ module HTTPure.Response
3
3
, ResponseM
4
4
, send
5
5
, response , response'
6
- , binaryResponse , binaryResponse'
7
6
, emptyResponse , emptyResponse'
8
7
9
8
-- 1xx
@@ -79,14 +78,17 @@ module HTTPure.Response
79
78
80
79
import Prelude
81
80
81
+ import Data.Maybe as Maybe
82
82
import Effect as Effect
83
83
import Effect.Aff as Aff
84
- import Node.Buffer as Buffer
84
+ import Effect.Class as EffectClass
85
85
import Node.HTTP as HTTP
86
+ import Node.Stream as Stream
86
87
87
88
import HTTPure.Body as Body
88
89
import HTTPure.Headers as Headers
89
90
import HTTPure.Status as Status
91
+ import HTTPure.Streamable as Streamable
90
92
91
93
-- | The `ResponseM` type simply conveniently wraps up an HTTPure monad that
92
94
-- | returns a response. This type is the return type of all router/route
@@ -97,53 +99,44 @@ type ResponseM = Aff.Aff Response
97
99
type Response =
98
100
{ status :: Status.Status
99
101
, headers :: Headers.Headers
100
- , body :: Body.Body
102
+ , body :: Stream.Readable ()
103
+ , size :: Maybe.Maybe Int
101
104
}
102
105
103
106
-- | Given an HTTP `Response` and a HTTPure `Response`, this method will return
104
107
-- | a monad encapsulating writing the HTTPure `Response` to the HTTP `Response`
105
108
-- | and closing the HTTP `Response`.
106
109
send :: HTTP.Response -> Response -> Effect.Effect Unit
107
- send httpresponse { status, headers, body } = do
110
+ send httpresponse { status, headers, body, size } = do
108
111
Status .write httpresponse $ status
109
- size <- Body .size body
110
112
Headers .write httpresponse $ headers <> contentLength size
111
113
Body .write httpresponse $ body
112
114
where
113
- contentLength size = Headers .header " Content-Length" $ show size
115
+ contentLength (Maybe.Just s) = Headers .header " Content-Length" $ show s
116
+ contentLength Maybe.Nothing = Headers .empty
114
117
115
118
-- | For custom response statuses or providing a body for response codes that
116
119
-- | don't typically send one.
117
- response :: Status.Status -> String -> ResponseM
120
+ response :: forall b . Body.Body b => Status.Status -> b -> ResponseM
118
121
response status = response' status Headers .empty
119
122
120
123
-- | The same as `response` but with headers.
121
- response' :: Status.Status ->
124
+ response' :: forall b . Body.Body b =>
125
+ Status.Status ->
122
126
Headers.Headers ->
123
- String ->
127
+ b ->
124
128
ResponseM
125
- response' status headers body =
126
- pure $ { status, headers, body: Body.StringBody body }
127
-
128
- -- | Like `response`, but the response body is binary data.
129
- binaryResponse :: Status.Status -> Buffer.Buffer -> Aff.Aff Response
130
- binaryResponse status = binaryResponse' status Headers .empty
131
-
132
- -- | The same as `binaryResponse` but with headers.
133
- binaryResponse' :: Status.Status ->
134
- Headers.Headers ->
135
- Buffer.Buffer ->
136
- Aff.Aff Response
137
- binaryResponse' status headers body
138
- = pure $ { status, headers, body: Body.BinaryBody body }
129
+ response' status headers body = do
130
+ size <- EffectClass .liftEffect $ Body .size body
131
+ pure $ { status , headers , size , body: Streamable .toStream body }
139
132
140
133
-- | The same as `response` but without a body.
141
134
emptyResponse :: Status.Status -> ResponseM
142
135
emptyResponse status = emptyResponse' status Headers .empty
143
136
144
137
-- | The same as `emptyResponse` but with headers.
145
138
emptyResponse' :: Status.Status -> Headers.Headers -> ResponseM
146
- emptyResponse' status headers = response' status headers " "
139
+ emptyResponse' status headers = response' status headers $ " "
147
140
148
141
-- -------
149
142
-- 1xx --
@@ -178,11 +171,11 @@ processing' = emptyResponse' Status.processing
178
171
-- -------
179
172
180
173
-- | 200
181
- ok :: String -> ResponseM
174
+ ok :: forall b . Body.Body b => b -> ResponseM
182
175
ok = ok' Headers .empty
183
176
184
177
-- | 200 with headers
185
- ok' :: Headers.Headers -> String -> ResponseM
178
+ ok' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
186
179
ok' = response' Status .ok
187
180
188
181
-- | 201
@@ -202,12 +195,13 @@ accepted' :: Headers.Headers -> ResponseM
202
195
accepted' = emptyResponse' Status .accepted
203
196
204
197
-- | 203
205
- nonAuthoritativeInformation :: String -> ResponseM
198
+ nonAuthoritativeInformation :: forall b . Body.Body b => b -> ResponseM
206
199
nonAuthoritativeInformation = nonAuthoritativeInformation' Headers .empty
207
200
208
201
-- | 203 with headers
209
- nonAuthoritativeInformation' :: Headers.Headers ->
210
- String ->
202
+ nonAuthoritativeInformation' :: forall b . Body.Body b =>
203
+ Headers.Headers ->
204
+ b ->
211
205
ResponseM
212
206
nonAuthoritativeInformation' = response' Status .nonAuthoritativeInformation
213
207
@@ -228,19 +222,19 @@ resetContent' :: Headers.Headers -> ResponseM
228
222
resetContent' = emptyResponse' Status .resetContent
229
223
230
224
-- | 206
231
- partialContent :: String -> ResponseM
225
+ partialContent :: forall b . Body.Body b => b -> ResponseM
232
226
partialContent = partialContent' Headers .empty
233
227
234
228
-- | 206 with headers
235
- partialContent' :: Headers.Headers -> String -> ResponseM
229
+ partialContent' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
236
230
partialContent' = response' Status .partialContent
237
231
238
232
-- | 207
239
- multiStatus :: String -> ResponseM
233
+ multiStatus :: forall b . Body.Body b => b -> ResponseM
240
234
multiStatus = multiStatus' Headers .empty
241
235
242
236
-- | 207 with headers
243
- multiStatus' :: Headers.Headers -> String -> ResponseM
237
+ multiStatus' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
244
238
multiStatus' = response' Status .multiStatus
245
239
246
240
-- | 208
@@ -252,47 +246,47 @@ alreadyReported' :: Headers.Headers -> ResponseM
252
246
alreadyReported' = emptyResponse' Status .alreadyReported
253
247
254
248
-- | 226
255
- iMUsed :: String -> ResponseM
249
+ iMUsed :: forall b . Body.Body b => b -> ResponseM
256
250
iMUsed = iMUsed' Headers .empty
257
251
258
252
-- | 226 with headers
259
- iMUsed' :: Headers.Headers -> String -> ResponseM
253
+ iMUsed' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
260
254
iMUsed' = response' Status .iMUsed
261
255
262
256
-- -------
263
257
-- 3xx --
264
258
-- -------
265
259
266
260
-- | 300
267
- multipleChoices :: String -> ResponseM
261
+ multipleChoices :: forall b . Body.Body b => b -> ResponseM
268
262
multipleChoices = multipleChoices' Headers .empty
269
263
270
264
-- | 300 with headers
271
- multipleChoices' :: Headers.Headers -> String -> ResponseM
265
+ multipleChoices' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
272
266
multipleChoices' = response' Status .multipleChoices
273
267
274
268
-- | 301
275
- movedPermanently :: String -> ResponseM
269
+ movedPermanently :: forall b . Body.Body b => b -> ResponseM
276
270
movedPermanently = movedPermanently' Headers .empty
277
271
278
272
-- | 301 with headers
279
- movedPermanently' :: Headers.Headers -> String -> ResponseM
273
+ movedPermanently' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
280
274
movedPermanently' = response' Status .movedPermanently
281
275
282
276
-- | 302
283
- found :: String -> ResponseM
277
+ found :: forall b . Body.Body b => b -> ResponseM
284
278
found = found' Headers .empty
285
279
286
280
-- | 302 with headers
287
- found' :: Headers.Headers -> String -> ResponseM
281
+ found' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
288
282
found' = response' Status .found
289
283
290
284
-- | 303
291
- seeOther :: String -> ResponseM
285
+ seeOther :: forall b . Body.Body b => b -> ResponseM
292
286
seeOther = seeOther' Headers .empty
293
287
294
288
-- | 303 with headers
295
- seeOther' :: Headers.Headers -> String -> ResponseM
289
+ seeOther' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
296
290
seeOther' = response' Status .seeOther
297
291
298
292
-- | 304
@@ -304,27 +298,27 @@ notModified' :: Headers.Headers -> ResponseM
304
298
notModified' = emptyResponse' Status .notModified
305
299
306
300
-- | 305
307
- useProxy :: String -> ResponseM
301
+ useProxy :: forall b . Body.Body b => b -> ResponseM
308
302
useProxy = useProxy' Headers .empty
309
303
310
304
-- | 305 with headers
311
- useProxy' :: Headers.Headers -> String -> ResponseM
305
+ useProxy' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
312
306
useProxy' = response' Status .useProxy
313
307
314
308
-- | 307
315
- temporaryRedirect :: String -> ResponseM
309
+ temporaryRedirect :: forall b . Body.Body b => b -> ResponseM
316
310
temporaryRedirect = temporaryRedirect' Headers .empty
317
311
318
312
-- | 307 with headers
319
- temporaryRedirect' :: Headers.Headers -> String -> ResponseM
313
+ temporaryRedirect' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
320
314
temporaryRedirect' = response' Status .temporaryRedirect
321
315
322
316
-- | 308
323
- permanentRedirect :: String -> ResponseM
317
+ permanentRedirect :: forall b . Body.Body b => b -> ResponseM
324
318
permanentRedirect = permanentRedirect' Headers .empty
325
319
326
320
-- | 308 with headers
327
- permanentRedirect' :: Headers.Headers -> String -> ResponseM
321
+ permanentRedirect' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
328
322
permanentRedirect' = response' Status .permanentRedirect
329
323
330
324
@@ -333,11 +327,11 @@ permanentRedirect' = response' Status.permanentRedirect
333
327
-- -------
334
328
335
329
-- | 400
336
- badRequest :: String -> ResponseM
330
+ badRequest :: forall b . Body.Body b => b -> ResponseM
337
331
badRequest = badRequest' Headers .empty
338
332
339
333
-- | 400 with headers
340
- badRequest' :: Headers.Headers -> String -> ResponseM
334
+ badRequest' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
341
335
badRequest' = response' Status .badRequest
342
336
343
337
-- | 401
@@ -405,11 +399,11 @@ requestTimeout' :: Headers.Headers -> ResponseM
405
399
requestTimeout' = emptyResponse' Status .requestTimeout
406
400
407
401
-- | 409
408
- conflict :: String -> ResponseM
402
+ conflict :: forall b . Body.Body b => b -> ResponseM
409
403
conflict = conflict' Headers .empty
410
404
411
405
-- | 409 with headers
412
- conflict' :: Headers.Headers -> String -> ResponseM
406
+ conflict' :: forall b . Body.Body b => Headers.Headers -> b -> ResponseM
413
407
conflict' = response' Status .conflict
414
408
415
409
-- | 410
@@ -561,11 +555,14 @@ unavailableForLegalReasons' = emptyResponse' Status.unavailableForLegalReasons
561
555
-- -------
562
556
563
557
-- | 500
564
- internalServerError :: String -> ResponseM
558
+ internalServerError :: forall b . Body.Body b => b -> ResponseM
565
559
internalServerError = internalServerError' Headers .empty
566
560
567
561
-- | 500 with headers
568
- internalServerError' :: Headers.Headers -> String -> ResponseM
562
+ internalServerError' :: forall b . Body.Body b =>
563
+ Headers.Headers ->
564
+ b ->
565
+ ResponseM
569
566
internalServerError' = response' Status .internalServerError
570
567
571
568
-- | 501
0 commit comments