mirror of
https://github.com/rowtype-yoga/purescript-fetch.git
synced 2024-11-26 07:41:31 +03:00
Make response headers a map
This commit is contained in:
parent
c3a2fb8b94
commit
bd6537df1c
@ -5,6 +5,7 @@ Notable changes to this project are documented in this file. The format is based
|
||||
## [Unreleased]
|
||||
|
||||
Breaking changes:
|
||||
- Switch response headers to `Map CaseInsensitiveString String`
|
||||
|
||||
New features:
|
||||
Bugfixes:
|
||||
|
@ -3,11 +3,14 @@
|
||||
[ "aff"
|
||||
, "aff-promise"
|
||||
, "arraybuffer-types"
|
||||
, "bifunctors"
|
||||
, "effect"
|
||||
, "fetch-core"
|
||||
, "foreign"
|
||||
, "http-methods"
|
||||
, "maybe"
|
||||
, "newtype"
|
||||
, "ordered-collections"
|
||||
, "prelude"
|
||||
, "record"
|
||||
, "typelevel-prelude"
|
||||
|
@ -10,6 +10,7 @@ module Fetch
|
||||
, module Fetch.Internal.Request
|
||||
, module Fetch.Internal.RequestBody
|
||||
, module Fetch.Internal.Response
|
||||
, module Fetch.Internal.Headers
|
||||
) where
|
||||
|
||||
import Prelude
|
||||
@ -27,6 +28,7 @@ import Fetch.Core.RequestCredentials (RequestCredentials(Omit, Include))
|
||||
import Fetch.Core.RequestMode (RequestMode(Cors, NoCors, Navigate))
|
||||
import Fetch.Internal.Request (class ToCoreRequestOptions, class ToCoreRequestOptionsConverter, class ToCoreRequestOptionsHelper, HighlevelRequestOptions, convertHelper, convertImpl, new)
|
||||
import Fetch.Internal.Request as Request
|
||||
import Fetch.Internal.Headers (lookup, toHeaders, contains)
|
||||
import Fetch.Internal.RequestBody (class ToRequestBody, toRequestBody)
|
||||
import Fetch.Internal.Response (Response, ResponseR, arrayBuffer, blob, body, json, promiseToPromise, text)
|
||||
import Fetch.Internal.Response as Response
|
||||
|
22
src/Fetch/Internal/Headers.purs
Normal file
22
src/Fetch/Internal/Headers.purs
Normal file
@ -0,0 +1,22 @@
|
||||
module Fetch.Internal.Headers
|
||||
( contains
|
||||
, lookup
|
||||
, toHeaders
|
||||
) where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Data.Bifunctor (lmap)
|
||||
import Data.Map as Map
|
||||
import Data.Maybe (Maybe)
|
||||
import Data.String.CaseInsensitive (CaseInsensitiveString(..))
|
||||
import Fetch.Core.Headers as CoreHeaders
|
||||
|
||||
toHeaders :: CoreHeaders.Headers -> Map.Map CaseInsensitiveString String
|
||||
toHeaders = CoreHeaders.toArray >>> map (lmap CaseInsensitiveString) >>> Map.fromFoldable
|
||||
|
||||
lookup :: String -> Map.Map CaseInsensitiveString String -> Maybe String
|
||||
lookup key headers = Map.lookup (CaseInsensitiveString key) headers
|
||||
|
||||
contains ∷ String → Map.Map CaseInsensitiveString String → Boolean
|
||||
contains key headers = Map.member (CaseInsensitiveString key) headers
|
@ -15,10 +15,14 @@ import Prelude
|
||||
import Control.Promise as Control
|
||||
import Control.Promise as Promise
|
||||
import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
|
||||
import Data.Bifunctor (lmap)
|
||||
import Data.Map as Map
|
||||
import Data.String.CaseInsensitive (CaseInsensitiveString(..))
|
||||
import Effect (Effect)
|
||||
import Effect.Aff (Aff)
|
||||
import Fetch.Core.Headers (Headers)
|
||||
import Fetch.Core.Headers as CoreHeaders
|
||||
import Fetch.Core.Response as CoreResponse
|
||||
import Fetch.Internal.Headers (toHeaders)
|
||||
import Foreign (Foreign)
|
||||
import Unsafe.Coerce (unsafeCoerce)
|
||||
import Web.File.Blob (Blob)
|
||||
@ -26,7 +30,7 @@ import Web.Promise as Web
|
||||
import Web.Streams.ReadableStream (ReadableStream)
|
||||
|
||||
type ResponseR =
|
||||
( headers :: Headers
|
||||
( headers :: Map.Map CaseInsensitiveString String
|
||||
, ok :: Boolean
|
||||
, redirected :: Boolean
|
||||
, status :: Int
|
||||
@ -63,7 +67,7 @@ promiseToPromise = unsafeCoerce
|
||||
|
||||
convert :: CoreResponse.Response -> Response
|
||||
convert response =
|
||||
{ headers: CoreResponse.headers response
|
||||
{ headers: CoreResponse.headers response # toHeaders
|
||||
, ok: CoreResponse.ok response
|
||||
, redirected: CoreResponse.redirected response
|
||||
, status: CoreResponse.status response
|
||||
|
@ -3,7 +3,9 @@ module Test.FetchSpec where
|
||||
import Prelude
|
||||
|
||||
import Data.ArrayBuffer.Types (Uint8Array)
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Fetch (Method(..), Referrer(..), RequestMode(..), fetch, fetchBody)
|
||||
import Fetch as Fetch
|
||||
import Foreign (unsafeFromForeign)
|
||||
import Test.Spec (Spec, describe, it)
|
||||
import Test.Spec.Assertions (shouldEqual)
|
||||
@ -49,3 +51,15 @@ spec =
|
||||
{ json: j } <- json <#> unsafeFromForeign
|
||||
j `shouldEqual` { hello: "world" }
|
||||
status `shouldEqual` 200
|
||||
|
||||
it "should retrieve correct headers" do
|
||||
|
||||
{ status, headers } <- fetch "https://httpbin.org/post"
|
||||
{ method: POST
|
||||
, body: """{"hello":"world"}"""
|
||||
, headers: { "Content-Type": "application/json" }
|
||||
}
|
||||
Fetch.lookup "Access-Control-Allow-Origin" headers `shouldEqual` Just "*"
|
||||
Fetch.lookup "Content-Type" headers `shouldEqual` Just "application/json"
|
||||
Fetch.contains "Content-Length" headers `shouldEqual` true
|
||||
status `shouldEqual` 200
|
||||
|
Loading…
Reference in New Issue
Block a user