@@ -0,0 +1,173 @@
1+module Fetch
2+ ( URL(..)
3+ , Fetch
4+ , Response
5+ , Options
6+ , Method
7+ , Headers
8+ , Redirect
9+ , Credentials
10+ , defaultFetchOptions
11+ , getMethod
12+ , postMethod
13+ , putMethod
14+ , patchMethod
15+ , deleteMethod
16+ , headMethod
17+ , redirectError
18+ , redirectFollow
19+ , redirectManual
20+ , omitCredentials
21+ , sameOriginCredentials
22+ , includeCredentials
23+ , fetch
24+ , json
25+ , text
26+ , headers
27+ , arrayBuffer
28+ , makeHeaders
29+ , statusCode
30+ , url
31+ ) where
32+
33+import Type.Row.Homogeneous
34+
35+import Control.Promise (Promise, toAffE)
36+import Data.ArrayBuffer.Types (ArrayBuffer)
37+import Data.Newtype (class Newtype)
38+import Effect (Effect)
39+import Effect.Aff (Aff)
40+import Foreign (Foreign)
41+import Foreign.Object (Object)
42+import Foreign.Object as Object
43+import Fetch.Impl (FetchImpl)
44+import Prelude (class Eq, class Show, ($))
45+import Type.Row (class Union)
46+import Unsafe.Coerce (unsafeCoerce)
47+
48+-- | Create a map from a homogeneous record (all attributes have the same type).
49+fromRecord ∷ ∀ r t. Homogeneous r t ⇒ Record r → Object.Object t
50+fromRecord = unsafeCoerce
51+
52+newtype URL = URL String
53+
54+derive instance Newtype URL _
55+derive newtype instance Show URL
56+derive instance Eq URL
57+
58+type Fetch =
59+ ∀ options trash.
60+ Union options trash Options ⇒
61+ URL →
62+ Record (method ∷ Method | options) →
63+ Aff Response
64+
65+type Options =
66+ ( method ∷ Method
67+ , body ∷ String
68+ , headers ∷ Headers
69+ , credentials ∷ Credentials
70+ , follow ∷ Int
71+ , redirect ∷ Redirect
72+ )
73+
74+-- | See <https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials>.
75+foreign import data Credentials ∷ Type
76+
77+omitCredentials ∷ Credentials
78+omitCredentials = unsafeCoerce "omit"
79+
80+sameOriginCredentials ∷ Credentials
81+sameOriginCredentials = unsafeCoerce "same-origin"
82+
83+includeCredentials ∷ Credentials
84+includeCredentials = unsafeCoerce "include"
85+
86+foreign import data Method ∷ Type
87+
88+getMethod ∷ Method
89+getMethod = unsafeCoerce "GET"
90+
91+postMethod ∷ Method
92+postMethod = unsafeCoerce "POST"
93+
94+putMethod ∷ Method
95+putMethod = unsafeCoerce "PUT"
96+
97+patchMethod ∷ Method
98+patchMethod = unsafeCoerce "PATCH"
99+
100+deleteMethod ∷ Method
101+deleteMethod = unsafeCoerce "DELETE"
102+
103+headMethod ∷ Method
104+headMethod = unsafeCoerce "HEAD"
105+
106+foreign import data Redirect ∷ Type
107+
108+redirectError ∷ Redirect
109+redirectError = unsafeCoerce "error"
110+
111+redirectFollow ∷ Redirect
112+redirectFollow = unsafeCoerce "follow"
113+
114+redirectManual ∷ Redirect
115+redirectManual = unsafeCoerce "manual"
116+
117+type Headers = Object.Object String
118+
119+makeHeaders ∷
120+ ∀ r.
121+ Homogeneous r String ⇒
122+ Record r →
123+ Headers
124+makeHeaders = fromRecord
125+
126+defaultFetchOptions ∷ { method ∷ Method }
127+defaultFetchOptions =
128+ { method: getMethod
129+ }
130+
131+fetch ∷ FetchImpl → Fetch
132+fetch impl url' opts = toAffE $ _fetch impl url' opts
133+
134+json ∷ Response → Aff Foreign
135+json res = toAffE (jsonImpl res)
136+
137+text ∷ Response → Aff String
138+text res = toAffE (textImpl res)
139+
140+headers ∷ Response → Object String
141+headers = headersImpl
142+
143+arrayBuffer ∷ Response → Aff ArrayBuffer
144+arrayBuffer res = toAffE (arrayBufferImpl res)
145+
146+statusCode ∷ Response → Int
147+statusCode response = response'.status
148+ where
149+ response' ∷ { status ∷ Int }
150+ response' = unsafeCoerce response
151+
152+url ∷ Response → URL
153+url response = URL response'.url
154+ where
155+ response' ∷ { url ∷ String }
156+ response' = unsafeCoerce response
157+
158+foreign import data Response ∷ Type
159+
160+foreign import _fetch ∷
161+ ∀ options.
162+ FetchImpl →
163+ URL →
164+ Record options →
165+ Effect (Promise Response)
166+
167+foreign import jsonImpl ∷ Response → Effect (Promise Foreign)
168+
169+foreign import textImpl ∷ Response → Effect (Promise String)
170+
171+foreign import headersImpl ∷ Response → Object String
172+
173+foreign import arrayBufferImpl ∷ Response → Effect (Promise ArrayBuffer)