Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add duplex option #3

Merged
merged 6 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

- uses: actions/setup-node@v2
with:
node-version: "14"
node-version: "20"

- name: Install dependencies
run: |
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict"
"build": "eslint src && pulp build -- --censor-lib --strict",
"test": "spago -x test.dhall test"
},
"devDependencies": {
"eslint": "^7.15.0",
Expand Down
21 changes: 21 additions & 0 deletions src/Fetch/Core/Duplex.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Fetch.Core.Duplex where

import Prelude

import Data.Maybe (Maybe(..))

data Duplex = Half | Full

derive instance Eq Duplex
derive instance Ord Duplex

toString :: Duplex -> String
toString = case _ of
Half -> "half"
Full -> "full"

fromString :: String -> Maybe Duplex
fromString = case _ of
"full" -> Just Full
"half" -> Just Half
_ -> Nothing
6 changes: 6 additions & 0 deletions src/Fetch/Core/Request.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Data.Newtype (un)
import Data.Symbol (class IsSymbol)
import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Fetch.Core.Duplex (Duplex)
import Fetch.Core.Duplex as Duplex
import Fetch.Core.Headers (Headers)
import Fetch.Core.Integrity (Integrity(..))
import Fetch.Core.Referrer (Referrer)
Expand Down Expand Up @@ -53,6 +55,7 @@ type UnsafeRequestOptions =
, referrer :: String
, referrerPolicy :: String
, integrity :: String
, duplex :: String
)

type RequestOptions =
Expand All @@ -65,6 +68,7 @@ type RequestOptions =
, referrer :: Referrer
, referrerPolicy :: ReferrerPolicy
, integrity :: Integrity
, duplex :: Duplex
)

toUnsafeOptions
Expand Down Expand Up @@ -150,3 +154,5 @@ instance ToInternalConverter ReferrerPolicy String where
instance ToInternalConverter Integrity String where
convertImpl = un Integrity

instance ToInternalConverter Duplex String where
convertImpl = Duplex.toString
7 changes: 3 additions & 4 deletions test.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ in conf
conf.dependencies
# [ "aff"
, "aff-promise"
, "effect"
, "spec"
, "spec-discovery"
, "strings"
, "console"
, "debug"
, "effect"
, "unsafe-coerce"
]
}
15 changes: 8 additions & 7 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import Control.Promise as Promise
import Data.HTTP.Method (Method(..))
import Debug (spy)
import Effect (Effect)
import Effect.Aff (launchAff, launchAff_)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Effect.Class.Console (log)
import Fetch.Core as Fetch
import Fetch.Core.Duplex (Duplex(..))
import Fetch.Core.Headers as Headers
import Fetch.Core.Request as Request
import Fetch.Core.RequestBody as RequestBody
Expand All @@ -20,12 +21,12 @@ main :: Effect Unit
main = launchAff_ do
let requestBody = """{"hello":"world"}"""
request <- liftEffect $ Request.new "http://httpbin.org/post"
{ method: POST
, body: RequestBody.fromString requestBody
, headers: Headers.fromRecord { "Content-Type": "application/json" }
}
let
_ = spy "request" request
{ method: POST
, body: RequestBody.fromString requestBody
, headers: Headers.fromRecord { "Content-Type": "application/json" }
, duplex: Half
}
let _ = spy "request" request
response <- Promise.toAffE $ unsafeCoerce $ Fetch.fetch request
responseBody <- Promise.toAffE $ unsafeCoerce $ Response.text response
let _ = spy "response" response
Expand Down
Loading