Skip to content

Commit

Permalink
Consolidate fetch and fetchBody
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-murphy authored and sigma-andex committed Sep 16, 2023
1 parent 71bf92d commit 8e126bd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
21 changes: 2 additions & 19 deletions src/Fetch.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Fetch
( fetch
, fetchBody
, module Data.HTTP.Method
, module Fetch.Core.RequestCache
, module Fetch.Core.RequestCredentials
Expand Down Expand Up @@ -35,7 +34,6 @@ import Prim.Row (class Union)
import Promise.Aff as Promise.Aff

-- | Implementation of `fetch`, see https://developer.mozilla.org/en-US/docs/Web/API/fetch
-- | For usage with `String` bodies. For other body types, see `fetchBody`
-- |
-- | Usage:
-- | ```purescript
Expand All @@ -57,29 +55,14 @@ import Promise.Aff as Promise.Aff
-- | foreignJsonValue <- json
-- | ```
fetch
:: forall input output @thruIn thruOut headers
. Union input thruIn (HighlevelRequestOptions headers String)
=> Union output thruOut CoreRequest.UnsafeRequestOptions
=> ToCoreRequestOptions input output
=> String
-> { | input }
-> Aff Response
fetch url r = do
request <- liftEffect $ new url $ Request.convert r
cResponse <- Promise.Aff.toAffE $ Core.fetch request
pure $ Response.convert cResponse

-- | Like `fetch`, but can accept arbitrary `RequestBody`s.
fetchBody
:: forall input output @thruIn thruOut headers body
. ToRequestBody body
=> Union input thruIn (HighlevelRequestOptions headers body)
. Union input thruIn (HighlevelRequestOptions headers body)
=> Union output thruOut CoreRequest.UnsafeRequestOptions
=> ToCoreRequestOptions input output
=> String
-> { | input }
-> Aff Response
fetchBody url r = do
fetch url r = do
request <- liftEffect $ new url $ Request.convert r
cResponse <- Promise.Aff.toAffE $ Core.fetch request
pure $ Response.convert cResponse
4 changes: 2 additions & 2 deletions test/Test/FetchSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Prelude

import Data.ArrayBuffer.Types (Uint8Array)
import Data.Maybe (Maybe(..))
import Fetch (Method(..), Referrer(..), RequestMode(..), fetch, fetchBody)
import Fetch (Method(..), Referrer(..), RequestMode(..), fetch)
import Fetch as Fetch
import Fetch.Core.Duplex (Duplex(..))
import Foreign (unsafeFromForeign)
Expand Down Expand Up @@ -42,7 +42,7 @@ spec =

it "should send a Post request with stream body" do

{ status, json } <- fetchBody "https://httpbin.org/post"
{ status, json } <- fetch "https://httpbin.org/post"
{ method: POST
, mode: Cors
, body: helloWorldStream
Expand Down

0 comments on commit 8e126bd

Please sign in to comment.