@@ -0,0 +1,86 @@
1+module Fetch
2+ ( fetch
3+ , fetchBody
4+ , module Data.HTTP.Method
5+ , module Fetch.Core.RequestCache
6+ , module Fetch.Core.RequestCredentials
7+ , module Fetch.Core.RequestMode
8+ , module Fetch.Core.Referrer
9+ , module Fetch.Core.Integrity
10+ , module Fetch.Internal.Request
11+ , module Fetch.Internal.RequestBody
12+ , module Fetch.Internal.Response
13+ ) where
14+
15+import Prelude
16+
17+import Control.Promise as Promise
18+import Data.HTTP.Method (Method(..))
19+import Effect.Aff (Aff)
20+import Effect.Class (liftEffect)
21+import Fetch.Core as Core
22+import Fetch.Core.Integrity (Integrity(..))
23+import Fetch.Core.Referrer (Referrer(..))
24+import Fetch.Core.Request as CoreRequest
25+import Fetch.Core.RequestCache (RequestCache(..))
26+import Fetch.Core.RequestCredentials (RequestCredentials(Omit, Include))
27+import Fetch.Core.RequestMode (RequestMode(Cors, NoCors, Navigate))
28+import Fetch.Internal.Request (class ToCoreRequestOptions, class ToCoreRequestOptionsConverter, class ToCoreRequestOptionsHelper, HighlevelRequestOptions, convertHelper, convertImpl, new)
29+import Fetch.Internal.Request as Request
30+import Fetch.Internal.RequestBody (class ToRequestBody, toRequestBody)
31+import Fetch.Internal.Response (Response, ResponseR, arrayBuffer, blob, body, json, promiseToPromise, text)
32+import Fetch.Internal.Response as Response
33+import Prim.Row (class Union)
34+import Type.Row.Homogeneous (class Homogeneous)
35+
36+-- | Implementation of `fetch`, see https://developer.mozilla.org/en-US/docs/Web/API/fetch
37+-- | For usage with `String` bodies. For other body types, see `fetchBody`
38+-- |
39+-- | Usage:
40+-- | ```purescript
41+-- | do
42+-- | let requestUrl = "https://httpbin.org/get"
43+-- | { status, text } <- fetch requestUrl { headers: { "Accept": "application/json" }}
44+-- | responseBody <- text
45+-- | ```
46+-- | A more complex example doing a Post request:
47+-- | ```purescript
48+-- | do
49+-- | { status, json } <- fetch "https://httpbin.org/post"
50+-- | { method: POST
51+-- | , mode: RequestMode.Cors
52+-- | , body: """{"hello":"world"}"""
53+-- | , headers: { "Content-Type": "application/json" }
54+-- | , referrer: ReferrerUrl "https://httpbin.org"
55+-- | }
56+-- | foreignJsonValue <- json
57+-- | ```
58+fetch
59+ :: forall input output thruIn thruOut headers
60+ . Homogeneous headers String
61+ => Union input thruIn (HighlevelRequestOptions headers String)
62+ => Union output thruOut CoreRequest.UnsafeRequestOptions
63+ => ToCoreRequestOptions input output
64+ => String
65+ -> { | input }
66+ -> Aff Response
67+fetch url r = do
68+ request <- liftEffect $ new url $ Request.convert r
69+ cResponse <- Promise.toAffE $ Response.promiseToPromise <$> Core.fetch request
70+ pure $ Response.convert cResponse
71+
72+-- | Like `fetch`, but can accept arbitrary `RequestBody`s.
73+fetchBody
74+ :: forall input output thruIn thruOut headers body
75+ . Homogeneous headers String
76+ => ToRequestBody body
77+ => Union input thruIn (HighlevelRequestOptions headers body)
78+ => Union output thruOut CoreRequest.UnsafeRequestOptions
79+ => ToCoreRequestOptions input output
80+ => String
81+ -> { | input }
82+ -> Aff Response
83+fetchBody url r = do
84+ request <- liftEffect $ new url $ Request.convert r
85+ cResponse <- Promise.toAffE $ Response.promiseToPromise <$> Core.fetch request
86+ pure $ Response.convert cResponse