{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Miso.Fetch
(
getJSON
, postJSON
, postJSON'
, putJSON
, getText
, postText
, putText
, getBlob
, postBlob
, putBlob
, getFormData
, postFormData
, putFormData
, getUint8Array
, postUint8Array
, putUint8Array
, postImage
, putImage
, getArrayBuffer
, postArrayBuffer
, putArrayBuffer
, accept
, contentType
, applicationJSON
, textPlain
, formData
, Body
, Response (..)
, CONTENT_TYPE (..)
, getJSON_
, postJSON_
, postJSON'_
, putJSON_
, getText_
, postText_
, putText_
, getBlob_
, postBlob_
, putBlob_
, getFormData_
, postFormData_
, putFormData_
, getUint8Array_
, postUint8Array_
, putUint8Array_
, postImage_
, putImage_
, getArrayBuffer_
, postArrayBuffer_
, putArrayBuffer_
, fetch
) where
import Control.Concurrent (MVar, newEmptyMVar, putMVar, takeMVar)
import Miso.JSON
import qualified Data.Map.Strict as M
import Miso.DSL (toJSVal, FromJSVal(..), JSVal)
import qualified Miso.FFI.Internal as FFI
import Miso.Effect (Effect, withSink)
import Miso.String (MisoString, ms)
import Miso.Util ((=:))
import Miso.FFI.Internal (Response(..), Blob, FormData, ArrayBuffer, Uint8Array, Image, fetch, CONTENT_TYPE(..))
getJSON
:: (FromJSON body, FromJSVal error)
=> MisoString
-> [(MisoString, MisoString)]
-> (Response body -> action)
-> (Response error -> action)
-> Effect context props model action
getJSON :: forall body error action context props model.
(FromJSON body, FromJSVal error) =>
Text
-> [(Text, Text)]
-> (Response body -> action)
-> (Response error -> action)
-> Effect context props model action
getJSON Text
url [(Text, Text)]
headers_ Response body -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink ->
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response JSVal -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
forall success error.
(FromJSVal success, FromJSVal error) =>
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response success -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
FFI.fetch Text
url Text
"GET" Maybe JSVal
forall a. Maybe a
Nothing [(Text, Text)]
jsonHeaders
(Sink action -> Response JSVal -> IO ()
forall {b}. (action -> IO b) -> Response JSVal -> IO b
handleJSON Sink action
sink)
(Sink action
sink Sink action
-> (Response error -> action) -> Response error -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response error -> action
errorful)
CONTENT_TYPE
JSON
where
jsonHeaders :: [(Text, Text)]
jsonHeaders = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
handleJSON :: (action -> IO b) -> Response JSVal -> IO b
handleJSON action -> IO b
sink resp :: Response JSVal
resp@Response {Maybe Int
Maybe Text
Map Text Text
JSVal
status :: Maybe Int
headers :: Map Text Text
errorMessage :: Maybe Text
body :: JSVal
body :: forall body. Response body -> body
errorMessage :: forall body. Response body -> Maybe Text
headers :: forall body. Response body -> Map Text Text
status :: forall body. Response body -> Maybe Int
..} =
(Value -> Result body) -> Maybe Value -> Maybe (Result body)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> Result body
forall a. FromJSON a => Value -> Result a
fromJSON (Maybe Value -> Maybe (Result body))
-> IO (Maybe Value) -> IO (Maybe (Result body))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> IO (Maybe Value)
forall a. FromJSVal a => JSVal -> IO (Maybe a)
fromJSVal JSVal
body IO (Maybe (Result body)) -> (Maybe (Result body) -> IO b) -> IO b
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe (Result body)
Nothing -> do
err <- JSVal -> IO error
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
sink $ errorful $ Response
{ body = err
, errorMessage = Just "Not a valid JSON object"
, ..
}
Just (Success body
result) ->
action -> IO b
sink (action -> IO b) -> action -> IO b
forall a b. (a -> b) -> a -> b
$ Response body -> action
successful Response JSVal
resp { body = result }
Just (Error Text
msg) -> do
err <- JSVal -> IO error
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
sink $ errorful $ Response
{ body = err
, errorMessage = Just (ms msg)
, ..
}
postJSON
:: (FromJSVal error, ToJSON body)
=> MisoString
-> body
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postJSON :: forall error body action context props model.
(FromJSVal error, ToJSON body) =>
Text
-> body
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postJSON Text
url body
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- Text -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (body -> Text
forall a. ToJSON a => a -> Text
encode body
body_)
FFI.fetch url "POST" (Just bodyVal) jsonHeaders_
(sink . successful)
(sink . errorful)
NONE
where
jsonHeaders_ :: [(Text, Text)]
jsonHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
postJSON'
:: (FromJSVal error, ToJSON body, FromJSON return)
=> MisoString
-> body
-> [(MisoString, MisoString)]
-> (Response return -> action)
-> (Response error -> action)
-> Effect context props model action
postJSON' :: forall error body return action context props model.
(FromJSVal error, ToJSON body, FromJSON return) =>
Text
-> body
-> [(Text, Text)]
-> (Response return -> action)
-> (Response error -> action)
-> Effect context props model action
postJSON' Text
url body
body_ [(Text, Text)]
headers_ Response return -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- Text -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (body -> Text
forall a. ToJSON a => a -> Text
encode body
body_)
FFI.fetch url "POST" (Just bodyVal) jsonHeaders_
(handleJSON sink)
(sink . errorful)
JSON
where
jsonHeaders_ :: [(Text, Text)]
jsonHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON, Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
handleJSON :: (action -> IO b) -> Response JSVal -> IO b
handleJSON action -> IO b
sink resp :: Response JSVal
resp@Response {Maybe Int
Maybe Text
Map Text Text
JSVal
body :: forall body. Response body -> body
errorMessage :: forall body. Response body -> Maybe Text
headers :: forall body. Response body -> Map Text Text
status :: forall body. Response body -> Maybe Int
status :: Maybe Int
headers :: Map Text Text
errorMessage :: Maybe Text
body :: JSVal
..} =
(Value -> Result return) -> Maybe Value -> Maybe (Result return)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> Result return
forall a. FromJSON a => Value -> Result a
fromJSON (Maybe Value -> Maybe (Result return))
-> IO (Maybe Value) -> IO (Maybe (Result return))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> IO (Maybe Value)
forall a. FromJSVal a => JSVal -> IO (Maybe a)
fromJSVal JSVal
body IO (Maybe (Result return))
-> (Maybe (Result return) -> IO b) -> IO b
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe (Result return)
Nothing -> do
err <- JSVal -> IO error
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
sink $ errorful $ Response
{ body = err
, errorMessage = Just "Not a valid JSON object"
, ..
}
Just (Success return
result) ->
action -> IO b
sink (action -> IO b) -> action -> IO b
forall a b. (a -> b) -> a -> b
$ Response return -> action
successful Response JSVal
resp { body = result }
Just (Error Text
msg) -> do
err <- JSVal -> IO error
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
sink $ errorful $ Response
{ body = err
, errorMessage = Just (ms msg)
, ..
}
putJSON
:: (FromJSVal error, ToJSON body)
=> MisoString
-> body
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putJSON :: forall error body action context props model.
(FromJSVal error, ToJSON body) =>
Text
-> body
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putJSON Text
url body
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- Text -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (body -> Text
forall a. ToJSON a => a -> Text
encode body
body_)
FFI.fetch url "PUT" (Just bodyVal) jsonHeaders_
(sink . successful)
(sink . errorful)
NONE
where
jsonHeaders_ :: [(Text, Text)]
jsonHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
getText
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> (Response MisoString -> action)
-> (Response error -> action)
-> Effect context props model action
getText :: forall error action context props model.
FromJSVal error =>
Text
-> [(Text, Text)]
-> (Response Text -> action)
-> (Response error -> action)
-> Effect context props model action
getText Text
url [(Text, Text)]
headers_ Response Text -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink ->
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response Text -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
forall success error.
(FromJSVal success, FromJSVal error) =>
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response success -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
FFI.fetch Text
url Text
"GET" Maybe JSVal
forall a. Maybe a
Nothing [(Text, Text)]
textHeaders_
(Sink action
sink Sink action -> (Response Text -> action) -> Response Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response Text -> action
successful)
(Sink action
sink Sink action
-> (Response error -> action) -> Response error -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response error -> action
errorful)
CONTENT_TYPE
TEXT
where
textHeaders_ :: [(Text, Text)]
textHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
textPlain]
postText
:: FromJSVal error
=> MisoString
-> MisoString
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postText :: forall error action context props model.
FromJSVal error =>
Text
-> Text
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postText Text
url Text
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- Text -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (Text -> Text
forall a. ToJSON a => a -> Text
encode Text
body_)
FFI.fetch url "POST" (Just bodyVal) textHeaders_
(sink . successful)
(sink . errorful)
NONE
where
textHeaders_ :: [(Text, Text)]
textHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
textPlain]
putText
:: FromJSVal error
=> MisoString
-> MisoString
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putText :: forall error action context props model.
FromJSVal error =>
Text
-> Text
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putText Text
url Text
imageBody [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
body_ <- Text -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal Text
imageBody
FFI.fetch url "PUT" (Just body_) textHeaders_
(sink . successful)
(sink . errorful)
NONE
where
textHeaders_ :: [(Text, Text)]
textHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
textPlain]
getBlob
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> (Response Blob -> action)
-> (Response error -> action)
-> Effect context props model action
getBlob :: forall error action context props model.
FromJSVal error =>
Text
-> [(Text, Text)]
-> (Response Blob -> action)
-> (Response error -> action)
-> Effect context props model action
getBlob Text
url [(Text, Text)]
headers_ Response Blob -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink ->
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response Blob -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
forall success error.
(FromJSVal success, FromJSVal error) =>
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response success -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
FFI.fetch Text
url Text
"GET" Maybe JSVal
forall a. Maybe a
Nothing [(Text, Text)]
blobHeaders_
(Sink action
sink Sink action -> (Response Blob -> action) -> Response Blob -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response Blob -> action
successful)
(Sink action
sink Sink action
-> (Response error -> action) -> Response error -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response error -> action
errorful)
CONTENT_TYPE
BLOB
where
blobHeaders_ :: [(Text, Text)]
blobHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
postBlob
:: FromJSVal error
=> MisoString
-> Blob
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postBlob :: forall error action context props model.
FromJSVal error =>
Text
-> Blob
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postBlob Text
url Blob
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- Blob -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal Blob
body_
FFI.fetch url "POST" (Just bodyVal) blobHeaders_
(sink . successful)
(sink . errorful)
NONE
where
blobHeaders_ :: [(Text, Text)]
blobHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
putBlob
:: FromJSVal error
=> MisoString
-> Blob
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putBlob :: forall error action context props model.
FromJSVal error =>
Text
-> Blob
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putBlob Text
url Blob
imageBody [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
body_ <- Blob -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal Blob
imageBody
FFI.fetch url "PUT" (Just body_) blobHeaders_
(sink . successful)
(sink . errorful)
NONE
where
blobHeaders_ :: [(Text, Text)]
blobHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
getFormData
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> (Response FormData -> action)
-> (Response error -> action)
-> Effect context props model action
getFormData :: forall error action context props model.
FromJSVal error =>
Text
-> [(Text, Text)]
-> (Response FormData -> action)
-> (Response error -> action)
-> Effect context props model action
getFormData Text
url [(Text, Text)]
headers_ Response FormData -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink ->
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response FormData -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
forall success error.
(FromJSVal success, FromJSVal error) =>
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response success -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
FFI.fetch Text
url Text
"GET" Maybe JSVal
forall a. Maybe a
Nothing [(Text, Text)]
formDataHeaders_
(Sink action
sink Sink action
-> (Response FormData -> action) -> Response FormData -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response FormData -> action
successful)
(Sink action
sink Sink action
-> (Response error -> action) -> Response error -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response error -> action
errorful)
CONTENT_TYPE
FORM_DATA
where
formDataHeaders_ :: [(Text, Text)]
formDataHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
formData]
postFormData
:: FromJSVal error
=> MisoString
-> FormData
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postFormData :: forall error action context props model.
FromJSVal error =>
Text
-> FormData
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postFormData Text
url FormData
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- FormData -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal FormData
body_
FFI.fetch url "POST" (Just bodyVal) formDataHeaders_
(sink . successful)
(sink . errorful)
NONE
where
formDataHeaders_ :: [(Text, Text)]
formDataHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
formData]
putFormData
:: FromJSVal error
=> MisoString
-> FormData
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putFormData :: forall error action context props model.
FromJSVal error =>
Text
-> FormData
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putFormData Text
url FormData
imageBody [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
body_ <- FormData -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal FormData
imageBody
FFI.fetch url "PUT" (Just body_) formDataHeaders_
(sink . successful)
(sink . errorful)
NONE
where
formDataHeaders_ :: [(Text, Text)]
formDataHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
formData]
getArrayBuffer
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> (Response ArrayBuffer -> action)
-> (Response error -> action)
-> Effect context props model action
getArrayBuffer :: forall error action context props model.
FromJSVal error =>
Text
-> [(Text, Text)]
-> (Response ArrayBuffer -> action)
-> (Response error -> action)
-> Effect context props model action
getArrayBuffer Text
url [(Text, Text)]
headers_ Response ArrayBuffer -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink ->
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response ArrayBuffer -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
forall success error.
(FromJSVal success, FromJSVal error) =>
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response success -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
FFI.fetch Text
url Text
"GET" Maybe JSVal
forall a. Maybe a
Nothing [(Text, Text)]
arrayBufferHeaders_
(Sink action
sink Sink action
-> (Response ArrayBuffer -> action)
-> Response ArrayBuffer
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response ArrayBuffer -> action
successful)
(Sink action
sink Sink action
-> (Response error -> action) -> Response error -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response error -> action
errorful)
CONTENT_TYPE
ARRAY_BUFFER
where
arrayBufferHeaders_ :: [(Text, Text)]
arrayBufferHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
postArrayBuffer
:: FromJSVal error
=> MisoString
-> ArrayBuffer
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postArrayBuffer :: forall error action context props model.
FromJSVal error =>
Text
-> ArrayBuffer
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postArrayBuffer Text
url ArrayBuffer
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- ArrayBuffer -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal ArrayBuffer
body_
FFI.fetch url "POST" (Just bodyVal) arrayBufferHeaders_
(sink . successful)
(sink . errorful)
NONE
where
arrayBufferHeaders_ :: [(Text, Text)]
arrayBufferHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
putArrayBuffer
:: FromJSVal error
=> MisoString
-> ArrayBuffer
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putArrayBuffer :: forall error action context props model.
FromJSVal error =>
Text
-> ArrayBuffer
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putArrayBuffer Text
url ArrayBuffer
arrayBuffer_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
body_ <- ArrayBuffer -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal ArrayBuffer
arrayBuffer_
FFI.fetch url "PUT" (Just body_) arrayBufferHeaders_
(sink . successful)
(sink . errorful)
NONE
where
arrayBufferHeaders_ :: [(Text, Text)]
arrayBufferHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
getUint8Array
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> (Response Uint8Array -> action)
-> (Response error -> action)
-> Effect context props model action
getUint8Array :: forall error action context props model.
FromJSVal error =>
Text
-> [(Text, Text)]
-> (Response Uint8Array -> action)
-> (Response error -> action)
-> Effect context props model action
getUint8Array Text
url [(Text, Text)]
headers_ Response Uint8Array -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink ->
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response Uint8Array -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
forall success error.
(FromJSVal success, FromJSVal error) =>
Text
-> Text
-> Maybe JSVal
-> [(Text, Text)]
-> (Response success -> IO ())
-> (Response error -> IO ())
-> CONTENT_TYPE
-> IO ()
FFI.fetch Text
url Text
"GET" Maybe JSVal
forall a. Maybe a
Nothing [(Text, Text)]
uint8ArrayHeaders_
(Sink action
sink Sink action
-> (Response Uint8Array -> action) -> Response Uint8Array -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response Uint8Array -> action
successful)
(Sink action
sink Sink action
-> (Response error -> action) -> Response error -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response error -> action
errorful)
CONTENT_TYPE
BYTES
where
uint8ArrayHeaders_ :: [(Text, Text)]
uint8ArrayHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
postUint8Array
:: FromJSVal error
=> MisoString
-> Uint8Array
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postUint8Array :: forall error action context props model.
FromJSVal error =>
Text
-> Uint8Array
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postUint8Array Text
url Uint8Array
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- Uint8Array -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal Uint8Array
body_
FFI.fetch url "POST" (Just bodyVal) uint8ArrayHeaders_
(sink . successful)
(sink . errorful)
NONE
where
uint8ArrayHeaders_ :: [(Text, Text)]
uint8ArrayHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
putUint8Array
:: FromJSVal error
=> MisoString
-> Uint8Array
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putUint8Array :: forall error action context props model.
FromJSVal error =>
Text
-> Uint8Array
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putUint8Array Text
url Uint8Array
uint8Array_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
body_ <- Uint8Array -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal Uint8Array
uint8Array_
FFI.fetch url "PUT" (Just body_) uint8ArrayHeaders_
(sink . successful)
(sink . errorful)
NONE
where
uint8ArrayHeaders_ :: [(Text, Text)]
uint8ArrayHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
postImage
:: FromJSVal error
=> MisoString
-> Image
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postImage :: forall error action context props model.
FromJSVal error =>
Text
-> Image
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
postImage Text
url Image
body_ [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
bodyVal <- Image -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal Image
body_
FFI.fetch url "POST" (Just bodyVal) headers_
(sink . successful)
(sink . errorful)
NONE
putImage
:: FromJSVal error
=> MisoString
-> Image
-> [(MisoString, MisoString)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putImage :: forall error action context props model.
FromJSVal error =>
Text
-> Image
-> [(Text, Text)]
-> (Response () -> action)
-> (Response error -> action)
-> Effect context props model action
putImage Text
url Image
imageBody [(Text, Text)]
headers_ Response () -> action
successful Response error -> action
errorful =
(Sink action -> IO ()) -> Effect context props model action
forall action context props model.
(Sink action -> IO ()) -> Effect context props model action
withSink ((Sink action -> IO ()) -> Effect context props model action)
-> (Sink action -> IO ()) -> Effect context props model action
forall a b. (a -> b) -> a -> b
$ \Sink action
sink -> do
body_ <- Image -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal Image
imageBody
FFI.fetch url "PUT" (Just body_) headers_
(sink . successful)
(sink . errorful)
NONE
type Body = JSVal
accept :: MisoString
accept :: Text
accept = Text
"Accept"
contentType :: MisoString
contentType :: Text
contentType = Text
"Content-Type"
applicationJSON :: MisoString
applicationJSON :: Text
applicationJSON = Text
"application/json"
textPlain :: MisoString
textPlain :: Text
textPlain = Text
"text/plain"
octetStream :: MisoString
octetStream :: Text
octetStream = Text
"application/octet-stream"
formData :: MisoString
formData :: Text
formData = Text
"multipart/form-data"
biasHeaders :: Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
[(k, a)]
userDefined [(k, a)]
contentSpecific
= Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
M.toList
(Map k a -> [(k, a)]) -> Map k a -> [(k, a)]
forall a b. (a -> b) -> a -> b
$ [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(k, a)]
userDefined Map k a -> Map k a -> Map k a
forall a. Semigroup a => a -> a -> a
<> [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(k, a)]
contentSpecific
getJSON_
:: (FromJSON body, FromJSVal error)
=> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response body))
getJSON_ :: forall body error.
(FromJSON body, FromJSVal error) =>
Text
-> [(Text, Text)] -> IO (Either (Response error) (Response body))
getJSON_ Text
url [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response body)))
forall a. IO (MVar a)
forall {error} {body}.
IO (MVar (Either (Response error) (Response body)))
newEmptyMVar :: IO (MVar (Either (Response error) (Response body)))
FFI.fetch url "GET" Nothing jsonHeaders
(handleJSON mvar)
(putMVar mvar . Left)
JSON
takeMVar mvar
where
jsonHeaders :: [(Text, Text)]
jsonHeaders = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
handleJSON :: MVar (Either (Response body) (Response body))
-> Response JSVal -> IO ()
handleJSON MVar (Either (Response body) (Response body))
mvar resp :: Response JSVal
resp@Response {Maybe Int
Maybe Text
Map Text Text
JSVal
body :: forall body. Response body -> body
errorMessage :: forall body. Response body -> Maybe Text
headers :: forall body. Response body -> Map Text Text
status :: forall body. Response body -> Maybe Int
status :: Maybe Int
headers :: Map Text Text
errorMessage :: Maybe Text
body :: JSVal
..} =
(Value -> Result body) -> Maybe Value -> Maybe (Result body)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> Result body
forall a. FromJSON a => Value -> Result a
fromJSON (Maybe Value -> Maybe (Result body))
-> IO (Maybe Value) -> IO (Maybe (Result body))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> IO (Maybe Value)
forall a. FromJSVal a => JSVal -> IO (Maybe a)
fromJSVal JSVal
body IO (Maybe (Result body)) -> (Maybe (Result body) -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe (Result body)
Nothing -> do
err <- JSVal -> IO body
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
putMVar mvar $ Left $ Response
{ body = err
, errorMessage = Just "Not a valid JSON object"
, ..
}
Just (Success body
result) ->
MVar (Either (Response body) (Response body))
-> Either (Response body) (Response body) -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (Either (Response body) (Response body))
mvar (Either (Response body) (Response body) -> IO ())
-> Either (Response body) (Response body) -> IO ()
forall a b. (a -> b) -> a -> b
$ Response body -> Either (Response body) (Response body)
forall a b. b -> Either a b
Right Response JSVal
resp { body = result }
Just (Error Text
msg) -> do
err <- JSVal -> IO body
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
putMVar mvar $ Left $ Response
{ body = err
, errorMessage = Just (ms msg)
, ..
}
{-# INLINE getJSON_ #-}
postJSON_
:: (FromJSVal error, ToJSON body)
=> MisoString
-> body
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
postJSON_ :: forall error body.
(FromJSVal error, ToJSON body) =>
Text
-> body
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
postJSON_ Text
url body
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal (encode body_)
FFI.fetch url "POST" (Just bodyVal) jsonHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
jsonHeaders_ :: [(Text, Text)]
jsonHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
{-# INLINE postJSON_ #-}
postJSON'_
:: (FromJSVal error, ToJSON body, FromJSON return)
=> MisoString
-> body
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response return))
postJSON'_ :: forall error body return.
(FromJSVal error, ToJSON body, FromJSON return) =>
Text
-> body
-> [(Text, Text)]
-> IO (Either (Response error) (Response return))
postJSON'_ Text
url body
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response return)))
forall a. IO (MVar a)
forall {error} {body}.
IO (MVar (Either (Response error) (Response body)))
newEmptyMVar :: IO (MVar (Either (Response error) (Response return)))
bodyVal <- toJSVal (encode body_)
FFI.fetch url "POST" (Just bodyVal) jsonHeaders_
(handleJSON mvar)
(putMVar mvar . Left)
JSON
takeMVar mvar
where
jsonHeaders_ :: [(Text, Text)]
jsonHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON, Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
handleJSON :: MVar (Either (Response body) (Response body))
-> Response JSVal -> IO ()
handleJSON MVar (Either (Response body) (Response body))
mvar resp :: Response JSVal
resp@Response {Maybe Int
Maybe Text
Map Text Text
JSVal
body :: forall body. Response body -> body
errorMessage :: forall body. Response body -> Maybe Text
headers :: forall body. Response body -> Map Text Text
status :: forall body. Response body -> Maybe Int
status :: Maybe Int
headers :: Map Text Text
errorMessage :: Maybe Text
body :: JSVal
..} =
(Value -> Result body) -> Maybe Value -> Maybe (Result body)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> Result body
forall a. FromJSON a => Value -> Result a
fromJSON (Maybe Value -> Maybe (Result body))
-> IO (Maybe Value) -> IO (Maybe (Result body))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSVal -> IO (Maybe Value)
forall a. FromJSVal a => JSVal -> IO (Maybe a)
fromJSVal JSVal
body IO (Maybe (Result body)) -> (Maybe (Result body) -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe (Result body)
Nothing -> do
err <- JSVal -> IO body
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
putMVar mvar $ Left $ Response
{ body = err
, errorMessage = Just "Not a valid JSON object"
, ..
}
Just (Success body
result) ->
MVar (Either (Response body) (Response body))
-> Either (Response body) (Response body) -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (Either (Response body) (Response body))
mvar (Either (Response body) (Response body) -> IO ())
-> Either (Response body) (Response body) -> IO ()
forall a b. (a -> b) -> a -> b
$ Response body -> Either (Response body) (Response body)
forall a b. b -> Either a b
Right Response JSVal
resp { body = result }
Just (Error Text
msg) -> do
err <- JSVal -> IO body
forall a. FromJSVal a => JSVal -> IO a
fromJSValUnchecked JSVal
body
putMVar mvar $ Left $ Response
{ body = err
, errorMessage = Just (ms msg)
, ..
}
{-# INLINE postJSON'_ #-}
putJSON_
:: (FromJSVal error, ToJSON body)
=> MisoString
-> body
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
putJSON_ :: forall error body.
(FromJSVal error, ToJSON body) =>
Text
-> body
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
putJSON_ Text
url body
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal (encode body_)
FFI.fetch url "PUT" (Just bodyVal) jsonHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
jsonHeaders_ :: [(Text, Text)]
jsonHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
applicationJSON]
{-# INLINE putJSON_ #-}
getText_
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response MisoString))
getText_ :: forall error.
FromJSVal error =>
Text
-> [(Text, Text)] -> IO (Either (Response error) (Response Text))
getText_ Text
url [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response Text)))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response Text)))
newEmptyMVar :: IO (MVar (Either (Response error) (Response MisoString)))
FFI.fetch url "GET" Nothing textHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
TEXT
takeMVar mvar
where
textHeaders_ :: [(Text, Text)]
textHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
textPlain]
{-# INLINE getText_ #-}
postText_
:: FromJSVal error
=> MisoString
-> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
postText_ :: forall error.
FromJSVal error =>
Text
-> Text
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
postText_ Text
url Text
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal (encode body_)
FFI.fetch url "POST" (Just bodyVal) textHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
textHeaders_ :: [(Text, Text)]
textHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
textPlain]
{-# INLINE postText_ #-}
putText_
:: FromJSVal error
=> MisoString
-> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
putText_ :: forall error.
FromJSVal error =>
Text
-> Text
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
putText_ Text
url Text
imageBody [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
body_ <- toJSVal imageBody
FFI.fetch url "PUT" (Just body_) textHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
textHeaders_ :: [(Text, Text)]
textHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
textPlain]
{-# INLINE putText_ #-}
getBlob_
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response Blob))
getBlob_ :: forall error.
FromJSVal error =>
Text
-> [(Text, Text)] -> IO (Either (Response error) (Response Blob))
getBlob_ Text
url [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response Blob)))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response Blob)))
newEmptyMVar :: IO (MVar (Either (Response error) (Response Blob)))
FFI.fetch url "GET" Nothing blobHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
BLOB
takeMVar mvar
where
blobHeaders_ :: [(Text, Text)]
blobHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE getBlob_ #-}
postBlob_
:: FromJSVal error
=> MisoString
-> Blob
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
postBlob_ :: forall error.
FromJSVal error =>
Text
-> Blob
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
postBlob_ Text
url Blob
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal body_
FFI.fetch url "POST" (Just bodyVal) blobHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
blobHeaders_ :: [(Text, Text)]
blobHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE postBlob_ #-}
putBlob_
:: FromJSVal error
=> MisoString
-> Blob
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
putBlob_ :: forall error.
FromJSVal error =>
Text
-> Blob
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
putBlob_ Text
url Blob
imageBody [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
body_ <- toJSVal imageBody
FFI.fetch url "PUT" (Just body_) blobHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
blobHeaders_ :: [(Text, Text)]
blobHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE putBlob_ #-}
getFormData_
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response FormData))
getFormData_ :: forall error.
FromJSVal error =>
Text
-> [(Text, Text)]
-> IO (Either (Response error) (Response FormData))
getFormData_ Text
url [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response FormData)))
forall a. IO (MVar a)
forall {error}.
IO (MVar (Either (Response error) (Response FormData)))
newEmptyMVar :: IO (MVar (Either (Response error) (Response FormData)))
FFI.fetch url "GET" Nothing formDataHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
FORM_DATA
takeMVar mvar
where
formDataHeaders_ :: [(Text, Text)]
formDataHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
formData]
{-# INLINE getFormData_ #-}
postFormData_
:: FromJSVal error
=> MisoString
-> FormData
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
postFormData_ :: forall error.
FromJSVal error =>
Text
-> FormData
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
postFormData_ Text
url FormData
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal body_
FFI.fetch url "POST" (Just bodyVal) formDataHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
formDataHeaders_ :: [(Text, Text)]
formDataHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
formData]
{-# INLINE postFormData_ #-}
putFormData_
:: FromJSVal error
=> MisoString
-> FormData
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
putFormData_ :: forall error.
FromJSVal error =>
Text
-> FormData
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
putFormData_ Text
url FormData
imageBody [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
body_ <- toJSVal imageBody
FFI.fetch url "PUT" (Just body_) formDataHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
formDataHeaders_ :: [(Text, Text)]
formDataHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
formData]
{-# INLINE putFormData_ #-}
getArrayBuffer_
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ArrayBuffer))
getArrayBuffer_ :: forall error.
FromJSVal error =>
Text
-> [(Text, Text)]
-> IO (Either (Response error) (Response ArrayBuffer))
getArrayBuffer_ Text
url [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ArrayBuffer)))
forall a. IO (MVar a)
forall {error}.
IO (MVar (Either (Response error) (Response ArrayBuffer)))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ArrayBuffer)))
FFI.fetch url "GET" Nothing arrayBufferHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
ARRAY_BUFFER
takeMVar mvar
where
arrayBufferHeaders_ :: [(Text, Text)]
arrayBufferHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE getArrayBuffer_ #-}
postArrayBuffer_
:: FromJSVal error
=> MisoString
-> ArrayBuffer
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
postArrayBuffer_ :: forall error.
FromJSVal error =>
Text
-> ArrayBuffer
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
postArrayBuffer_ Text
url ArrayBuffer
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal body_
FFI.fetch url "POST" (Just bodyVal) arrayBufferHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
arrayBufferHeaders_ :: [(Text, Text)]
arrayBufferHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE postArrayBuffer_ #-}
putArrayBuffer_
:: FromJSVal error
=> MisoString
-> ArrayBuffer
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
putArrayBuffer_ :: forall error.
FromJSVal error =>
Text
-> ArrayBuffer
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
putArrayBuffer_ Text
url ArrayBuffer
arrayBuffer_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
body_ <- toJSVal arrayBuffer_
FFI.fetch url "PUT" (Just body_) arrayBufferHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
arrayBufferHeaders_ :: [(Text, Text)]
arrayBufferHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE putArrayBuffer_ #-}
getUint8Array_
:: FromJSVal error
=> MisoString
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response Uint8Array))
getUint8Array_ :: forall error.
FromJSVal error =>
Text
-> [(Text, Text)]
-> IO (Either (Response error) (Response Uint8Array))
getUint8Array_ Text
url [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response Uint8Array)))
forall a. IO (MVar a)
forall {error}.
IO (MVar (Either (Response error) (Response Uint8Array)))
newEmptyMVar :: IO (MVar (Either (Response error) (Response Uint8Array)))
FFI.fetch url "GET" Nothing uint8ArrayHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
BYTES
takeMVar mvar
where
uint8ArrayHeaders_ :: [(Text, Text)]
uint8ArrayHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
accept Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE getUint8Array_ #-}
postUint8Array_
:: FromJSVal error
=> MisoString
-> Uint8Array
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
postUint8Array_ :: forall error.
FromJSVal error =>
Text
-> Uint8Array
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
postUint8Array_ Text
url Uint8Array
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal body_
FFI.fetch url "POST" (Just bodyVal) uint8ArrayHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
uint8ArrayHeaders_ :: [(Text, Text)]
uint8ArrayHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE postUint8Array_ #-}
putUint8Array_
:: FromJSVal error
=> MisoString
-> Uint8Array
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
putUint8Array_ :: forall error.
FromJSVal error =>
Text
-> Uint8Array
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
putUint8Array_ Text
url Uint8Array
uint8Array_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
body_ <- toJSVal uint8Array_
FFI.fetch url "PUT" (Just body_) uint8ArrayHeaders_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
where
uint8ArrayHeaders_ :: [(Text, Text)]
uint8ArrayHeaders_ = [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall k a. Ord k => [(k, a)] -> [(k, a)] -> [(k, a)]
biasHeaders [(Text, Text)]
headers_ [Text
contentType Text -> Text -> (Text, Text)
forall k v. k -> v -> (k, v)
=: Text
octetStream]
{-# INLINE putUint8Array_ #-}
postImage_
:: FromJSVal error
=> MisoString
-> Image
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
postImage_ :: forall error.
FromJSVal error =>
Text
-> Image
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
postImage_ Text
url Image
body_ [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
bodyVal <- toJSVal body_
FFI.fetch url "POST" (Just bodyVal) headers_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
{-# INLINE postImage_ #-}
putImage_
:: FromJSVal error
=> MisoString
-> Image
-> [(MisoString, MisoString)]
-> IO (Either (Response error) (Response ()))
putImage_ :: forall error.
FromJSVal error =>
Text
-> Image
-> [(Text, Text)]
-> IO (Either (Response error) (Response ()))
putImage_ Text
url Image
imageBody [(Text, Text)]
headers_ = do
mvar <- IO (MVar (Either (Response error) (Response ())))
forall a. IO (MVar a)
forall {error}. IO (MVar (Either (Response error) (Response ())))
newEmptyMVar :: IO (MVar (Either (Response error) (Response ())))
body_ <- toJSVal imageBody
FFI.fetch url "PUT" (Just body_) headers_
(putMVar mvar . Right)
(putMVar mvar . Left)
NONE
takeMVar mvar
{-# INLINE putImage_ #-}