/
Response.purs
77 lines (66 loc) · 2.05 KB
/
Response.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
module Fetch.Internal.Response
( Response
, ResponseR
, arrayBuffer
, blob
, body
, convert
, json
, promiseToPromise
, text
) where
import Prelude
import Control.Promise as Control
import Control.Promise as Promise
import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
import Effect (Effect)
import Effect.Aff (Aff)
import Fetch.Core.Headers (Headers)
import Fetch.Core.Response as CoreResponse
import Foreign (Foreign)
import Unsafe.Coerce (unsafeCoerce)
import Web.File.Blob (Blob)
import Web.Promise as Web
import Web.Streams.ReadableStream (ReadableStream)
type ResponseR =
( headers :: Headers
, ok :: Boolean
, redirected :: Boolean
, status :: Int
, statusText :: String
, url :: String
, text :: Aff String
, json :: Aff Foreign
, body :: Effect (ReadableStream Uint8Array)
, arrayBuffer :: Aff ArrayBuffer
, blob :: Aff Blob
)
type Response =
{ | ResponseR
}
text :: CoreResponse.Response -> Aff String
text response = CoreResponse.text response <#> promiseToPromise # Promise.toAffE
body :: CoreResponse.Response -> Effect (ReadableStream Uint8Array)
body = CoreResponse.body
json :: CoreResponse.Response -> Aff Foreign
json response = CoreResponse.json response <#> promiseToPromise # Promise.toAffE
arrayBuffer :: CoreResponse.Response -> Aff ArrayBuffer
arrayBuffer response = CoreResponse.arrayBuffer response <#> promiseToPromise # Promise.toAffE
blob :: CoreResponse.Response -> Aff Blob
blob response = CoreResponse.blob response <#> promiseToPromise # Promise.toAffE
promiseToPromise :: Web.Promise ~> Control.Promise
promiseToPromise = unsafeCoerce
convert :: CoreResponse.Response -> Response
convert response =
{ headers: CoreResponse.headers response
, ok: CoreResponse.ok response
, redirected: CoreResponse.redirected response
, status: CoreResponse.status response
, statusText: CoreResponse.statusText response
, url: CoreResponse.url response
, text: text response
, json: json response
, body: CoreResponse.body response
, arrayBuffer: arrayBuffer response
, blob: blob response
}