Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Rename package and improve request creation
- Rename packages to Fetch.Core
- Improve request creation and only generate minimal request options
- Add basic spago set up
  • Loading branch information
sigma-andex committed Aug 18, 2022
commit b1e53dc7b548a91910591cda5d2ee1aaaa20f573
6 changes: 6 additions & 0 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20220816/packages.dhall
sha256:8b4467b4b5041914f9b765779c8936d6d4c230b1f60eb64f6269c71812fd7e98

in upstream
27 changes: 27 additions & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{ name = "fetch-core"
, dependencies =
[ "arraybuffer-types"
, "arrays"
, "console"
, "effect"
, "foldable-traversable"
, "foreign"
, "foreign-object"
, "functions"
, "http-methods"
, "maybe"
, "newtype"
, "nullable"
, "prelude"
, "record"
, "tuples"
, "typelevel-prelude"
, "unfoldable"
, "unsafe-coerce"
, "web-file"
, "web-promise"
, "web-streams"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs" ]
}
File renamed without changes.
10 changes: 5 additions & 5 deletions src/Web/Fetch.purs → src/Fetch/Core/Fetch.purs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch
module Fetch.Core
( FetchOptions
, fetch
, fetchWithOptions
Expand All @@ -7,9 +7,9 @@ module Web.Fetch
import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Prim.Row as Row
import Web.Fetch.AbortController (AbortSignal)
import Web.Fetch.Request (Request)
import Web.Fetch.Response (Response)
import Fetch.Core.AbortController (AbortSignal)
import Fetch.Core.Request (Request)
import Fetch.Core.Response (Response)
import Web.Promise (Promise)

type FetchOptions =
Expand All @@ -23,4 +23,4 @@ fetch :: Request -> Effect (Promise Response)
fetch req = runEffectFn2 _fetch req {}

fetchWithOptions :: forall r rx. Row.Union r rx FetchOptions => Request -> { | r } -> Effect (Promise Response)
fetchWithOptions = runEffectFn2 _fetch
fetchWithOptions = runEffectFn2 _fetch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.AbortController where
module Fetch.Core.AbortController where

import Effect (Effect)
import Prelude (Unit)
Expand All @@ -11,4 +11,4 @@ foreign import new :: Effect AbortController

foreign import abort :: AbortController -> Effect Unit

foreign import signal :: AbortController -> AbortSignal
foreign import signal :: AbortController -> AbortSignal
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.Headers
module Fetch.Core.Headers
( Headers
, fromFoldable
, fromRecord
Expand Down Expand Up @@ -50,4 +50,4 @@ toUnfoldable :: forall f. Unfoldable f => Headers -> f (Tuple String String)
toUnfoldable = Array.toUnfoldable <<< toArray

empty :: Headers
empty = fromFoldable []
empty = fromFoldable []
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.Integrity where
module Fetch.Core.Integrity where

import Data.Newtype (class Newtype)
import Prelude (class Eq, class Ord)
Expand All @@ -7,4 +7,4 @@ newtype Integrity = Integrity String

derive instance newtypeIntegrity :: Newtype Integrity _
derive newtype instance eqIntegrity :: Eq Integrity
derive newtype instance ordIntegrity :: Ord Integrity
derive newtype instance ordIntegrity :: Ord Integrity
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.Referrer where
module Fetch.Core.Referrer where

data Referrer
= ReferrerNone
Expand All @@ -15,4 +15,4 @@ fromString :: String -> Referrer
fromString = case _ of
"none" -> ReferrerNone
"client" -> ReferrerClient
url -> ReferrerUrl url
url -> ReferrerUrl url
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.ReferrerPolicy where
module Fetch.Core.ReferrerPolicy where

import Data.Maybe (Maybe(..))

Expand Down
File renamed without changes.
153 changes: 153 additions & 0 deletions src/Fetch/Core/Fetch/Request.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
module Fetch.Core.Request
( Request
, RequestOptions
, UnsafeRequestOptions
, class ToInternal
, class ToInternalConverter
, class ToInternalHelper
, convert
, convertHelper
, convertImpl
, new
, unsafeNew
) where

import Prelude

import Data.HTTP.Method (Method)
import Data.Maybe (Maybe)
import Data.Newtype (un)
import Data.Nullable (Nullable)
import Data.Symbol (class IsSymbol)
import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Fetch.Core.Headers (Headers)
import Fetch.Core.Integrity (Integrity(..))
import Fetch.Core.Referrer (Referrer)
import Fetch.Core.Referrer as Referrer
import Fetch.Core.ReferrerPolicy (ReferrerPolicy)
import Fetch.Core.ReferrerPolicy as ReferrerPolicy
import Fetch.Core.RequestBody (RequestBody)
import Fetch.Core.RequestCache (RequestCache)
import Fetch.Core.RequestCache as RequestCache
import Fetch.Core.RequestCredentials (RequestCredentials)
import Fetch.Core.RequestCredentials as RequestCredentials
import Fetch.Core.RequestMode (RequestMode)
import Fetch.Core.RequestMode as RequestMode
import Prim.Row (class Lacks, class Union)
import Prim.Row as R
import Prim.RowList as RL
import Record (delete, get, insert)
import Type.Proxy (Proxy(..))

foreign import data Request :: Type

foreign import _unsafeNew :: forall r. EffectFn2 String { | r } Request

type UnsafeRequestOptions =
( method :: String
, headers :: Headers
, body :: RequestBody
, credentials :: String
, cache :: String
, mode :: String
, referrer :: Nullable String
, referrerPolicy :: String
, integrity :: String
)

type RequestOptions =
( method :: Method
, headers :: Headers
, body :: RequestBody
, credentials :: RequestCredentials
, cache :: RequestCache
, mode :: RequestMode
, referrer :: Maybe Referrer
, referrerPolicy :: ReferrerPolicy
, integrity :: Integrity
)

toUnsafeOptions
:: forall input output thruIn thruOut
. Union input thruIn RequestOptions
=> Union output thruOut UnsafeRequestOptions
=> ToInternal input output
=> { | input }
-> { | output }
toUnsafeOptions = convert

unsafeNew :: String -> { | UnsafeRequestOptions } -> Effect Request
unsafeNew = runEffectFn2 _unsafeNew

new
:: forall input output thruIn thruOut
. Union input thruIn RequestOptions
=> Union output thruOut UnsafeRequestOptions
=> ToInternal input output
=> String
-> { | input }
-> Effect Request
new url options = runEffectFn2 _unsafeNew url (toUnsafeOptions options)

class ToInternal input output | input -> output where
convert :: Record input -> Record output

instance (Union rIn thru RequestOptions, RL.RowToList rIn rInRL, ToInternalHelper rIn rInRL rOut) => ToInternal (| rIn) (| rOut) where
convert = convertHelper (Proxy :: Proxy rInRL)

class ToInternalHelper :: forall k. Row Type -> k -> Row Type -> Constraint
class ToInternalHelper input inputRL output | inputRL -> output where
convertHelper :: Proxy inputRL -> Record input -> Record output

instance ToInternalHelper r RL.Nil () where
convertHelper _ _ = {}
else instance
( ToInternalConverter tpeIn tpeOut
, R.Cons sym tpeIn tailIn r
, RL.RowToList tailIn tailInRL
, Lacks sym tailIn
, IsSymbol sym
, ToInternalHelper tailIn tailInRL tailOutput
, R.Cons sym tpeOut tailOutput output
, Lacks sym tailOutput
) =>
ToInternalHelper r (RL.Cons sym tpeIn tailInRL) output where
convertHelper _ r = insert (Proxy :: Proxy sym) head tail
where
tail :: Record tailOutput
tail = delete (Proxy :: Proxy sym) r # convertHelper (Proxy :: Proxy tailInRL)

head :: tpeOut
head = get (Proxy :: Proxy sym) r # convertImpl

class ToInternalConverter input output | input -> output where
convertImpl :: input -> output

instance ToInternalConverter Method String where
convertImpl = show

instance ToInternalConverter Headers Headers where
convertImpl = identity

instance ToInternalConverter RequestBody RequestBody where
convertImpl = identity

instance ToInternalConverter RequestCredentials String where
convertImpl = RequestCredentials.toString

instance ToInternalConverter RequestCache String where
convertImpl = RequestCache.toString

instance ToInternalConverter RequestMode String where
convertImpl = RequestMode.toString

instance ToInternalConverter Referrer String where
convertImpl = Referrer.toString

instance ToInternalConverter ReferrerPolicy String where
convertImpl = ReferrerPolicy.toString

instance ToInternalConverter Integrity String where
convertImpl = un Integrity

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.RequestBody where
module Fetch.Core.RequestBody where

import Data.ArrayBuffer.Types (ArrayBuffer, ArrayView, Uint8Array)
import Web.Streams.ReadableStream (ReadableStream)
Expand All @@ -13,4 +13,4 @@ foreign import fromString :: String -> RequestBody

foreign import fromReadableStream :: ReadableStream Uint8Array -> RequestBody

foreign import empty :: RequestBody
foreign import empty :: RequestBody
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.RequestCache where
module Fetch.Core.RequestCache where

import Data.Maybe (Maybe(..))

Expand Down Expand Up @@ -27,4 +27,4 @@ fromString = case _ of
"no-cache" -> Just NoCache
"force-cache" -> Just ForceCache
"only-if-cached" -> Just OnlyIfCached
_ -> Nothing
_ -> Nothing
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.RequestCredentials where
module Fetch.Core.RequestCredentials where

import Data.Maybe (Maybe(..))

Expand All @@ -18,4 +18,4 @@ fromString = case _ of
"omit" -> Just Omit
"same-origin" -> Just SameOrigin
"include" -> Just Include
_ -> Nothing
_ -> Nothing
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.RequestMode where
module Fetch.Core.RequestMode where

import Data.Maybe (Maybe(..))

Expand All @@ -21,4 +21,4 @@ fromString = case _ of
"no-cors" -> Just NoCors
"same-origin" -> Just SameOrigin
"navigate" -> Just Navigate
_ -> Nothing
_ -> Nothing
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Web.Fetch.RequestRedirect where
module Fetch.Core.RequestRedirect where

import Data.Maybe (Maybe(..))

Expand All @@ -18,4 +18,4 @@ fromString = case _ of
"follow" -> Just Follow
"error" -> Just Error
"manual" -> Just Manual
_ -> Nothing
_ -> Nothing
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ export function text(resp) {
return resp.text();
};
}

export function json(resp) {
return function() {
return resp.json();
};
}
23 changes: 20 additions & 3 deletions src/Web/Fetch/Response.purs → src/Fetch/Core/Fetch/Response.purs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
module Web.Fetch.Response where
module Fetch.Core.Response
( Response
, arrayBuffer
, blob
, body
, headers
, json
, ok
, redirected
, status
, statusText
, text
, url
)
where

import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
import Effect (Effect)
import Web.Fetch.Headers (Headers)
import Fetch.Core.Headers (Headers)
import Foreign (Foreign)
import Web.File.Blob (Blob)
import Web.Promise (Promise)
import Web.Streams.ReadableStream (ReadableStream)
Expand All @@ -27,4 +42,6 @@ foreign import arrayBuffer :: Response -> Effect (Promise ArrayBuffer)

foreign import blob :: Response -> Effect (Promise Blob)

foreign import text :: Response -> Effect (Promise String)
foreign import text :: Response -> Effect (Promise String)

foreign import json :: Response -> Effect (Promise Foreign)
Loading