miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Fetch

Description

Interface to the browser's Fetch API for making HTTP requests inside Miso's Effect monad.

Each function accepts a URL, optional request headers, a success callback, and an error callback of the form Response x -> action. The resulting Effect dispatches the appropriate action into the MVU loop when the response arrives.

Functions are grouped by HTTP method and body/response type:

Use getJSON or postJSON for typical REST calls; use postJSON' when the server also returns a JSON response body.

Every function above has a synchronous counterpart suffixed with _ (e.g. getJSON_, postJSON_) that blocks the calling thread until the browser resolves the underlying promise, returning Right response on success or Left response on failure instead of dispatching an action. These are best used with io or io_, to avoid blocking the scheduler thread.

For Servant-style typed client generation, see the miso README.

Synopsis

JSON

getJSON Source #

Arguments

:: (FromJSON body, FromJSVal error) 
=> MisoString

url

-> [(MisoString, MisoString)]

headers

-> (Response body -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Retrieve a JSON resource via GET.

data Action
 = FetchGitHub
 | SetGitHub GitHub
 | ErrorHandler MisoString
 deriving (Show, Eq)

updateModel :: Action -> Effect Model Action
updateModel = case
  FetchGitHub -> getJSON "https://api.github.com" [] SetGitHub ErrorHandler
  SetGitHub apiInfo -> info ?= apiInfo
  ErrorHandler msg -> io_ (consoleError msg)

postJSON Source #

Arguments

:: (FromJSVal error, ToJSON body) 
=> MisoString

url

-> body

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with a JSON-encoded body; ignores the response body.

Sets Content-Type: application/json automatically. Use postJSON' when you also need to parse a JSON response body.

postJSON' Source #

Arguments

:: (FromJSVal error, ToJSON body, FromJSON return) 
=> MisoString

url

-> body

Body

-> [(MisoString, MisoString)]

headers_

-> (Response return -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with a JSON-encoded body and parse a JSON response.

Sets both Content-Type: application/json and Accept: application/json automatically. Use postJSON when the response body is not needed.

putJSON Source #

Arguments

:: (FromJSVal error, ToJSON body) 
=> MisoString

url

-> body

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a PUT request with a JSON-encoded body; ignores the response body.

Sets Content-Type: application/json automatically.

Text

getText Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response MisoString -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Retrieve a plain-text resource via GET.

Sets Accept: text/plain automatically. The response body is delivered as a MisoString.

postText Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> MisoString

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with a plain-text body; ignores the response body.

Sets Content-Type: text/plain automatically.

putText Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> MisoString

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a PUT request with a plain-text body; ignores the response body.

Sets Content-Type: text/plain automatically.

Blob

getBlob Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response Blob -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Retrieve a binary resource as a Blob via GET.

Sets Accept: application/octet-stream automatically.

postBlob Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Blob

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with a Blob body; ignores the response body.

Sets Content-Type: application/octet-stream automatically.

putBlob Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Blob

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a PUT request with a Blob body; ignores the response body.

Sets Content-Type: application/octet-stream automatically.

FormData

getFormData Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response FormData -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Retrieve a multipart resource as FormData via GET.

Sets Accept: multipart/form-data automatically.

postFormData Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> FormData

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with a FormData body; ignores the response body.

Sets Content-Type: multipart/form-data automatically.

putFormData Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> FormData

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a PUT request with a FormData body; ignores the response body.

Sets Content-Type: multipart/form-data automatically.

Uint8Array

getUint8Array Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response Uint8Array -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Retrieve a binary resource as a Uint8Array via GET.

Sets Accept: application/octet-stream automatically.

postUint8Array Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Uint8Array

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with a Uint8Array body; ignores the response body.

Sets Content-Type: application/octet-stream automatically.

putUint8Array Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Uint8Array

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a PUT request with a Uint8Array body; ignores the response body.

Sets Content-Type: application/octet-stream automatically.

Image

postImage Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Image

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with an Image body; ignores the response body.

putImage Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Image

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a PUT request with an Image body; ignores the response body.

ArrayBuffer

getArrayBuffer Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response ArrayBuffer -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Retrieve a binary resource as an ArrayBuffer via GET.

Sets Accept: application/octet-stream automatically.

postArrayBuffer Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> ArrayBuffer

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a POST request with an ArrayBuffer body; ignores the response body.

Sets Content-Type: application/octet-stream automatically.

putArrayBuffer Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> ArrayBuffer

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent props model action 

Send a PUT request with an ArrayBuffer body; ignores the response body.

Sets Content-Type: application/octet-stream automatically.

Header helpers

accept :: MisoString Source #

HTTP header name "Accept". Use with =: to build request headers, e.g. accept =: applicationJSON.

contentType :: MisoString Source #

HTTP header name "Content-Type". Use with =: to build request headers, e.g. contentType =: textPlain.

applicationJSON :: MisoString Source #

MIME type "application/json".

textPlain :: MisoString Source #

MIME type "text/plain".

formData :: MisoString Source #

MIME type "multipart/form-data".

Types

type Body = JSVal Source #

Type synonym for a raw JavaScript request body.

data Response body Source #

Type returned from a fetch request

Constructors

Response 

Fields

Instances

Instances details
Functor Response Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

fmap :: (a -> b) -> Response a -> Response b #

(<$) :: a -> Response b -> Response a #

FromJSVal body => FromJSVal (Response body) Source # 
Instance details

Defined in Miso.FFI.Internal

data CONTENT_TYPE Source #

List of possible content types that are available for use with the fetch API

Instances

Instances details
Show CONTENT_TYPE Source # 
Instance details

Defined in Miso.FFI.Internal

Eq CONTENT_TYPE Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal CONTENT_TYPE Source # 
Instance details

Defined in Miso.FFI.Internal

Synchronous API variants

getJSON_ Source #

Arguments

:: (FromJSON body, FromJSVal error) 
=> MisoString

url

-> [(MisoString, MisoString)]

headers

-> IO (Either (Response error) (Response body)) 

Synchronous API variant of getJSON.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postJSON_ Source #

Arguments

:: (FromJSVal error, ToJSON body) 
=> MisoString

url

-> body

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of postJSON.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postJSON'_ Source #

Arguments

:: (FromJSVal error, ToJSON body, FromJSON return) 
=> MisoString

url

-> body

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response return)) 

Synchronous API variant of postJSON'.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

putJSON_ Source #

Arguments

:: (FromJSVal error, ToJSON body) 
=> MisoString

url

-> body

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of putJSON.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

getText_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response MisoString)) 

Synchronous API variant of getText.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postText_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> MisoString

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of postText.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

putText_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> MisoString

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of putText.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

getBlob_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response Blob)) 

Synchronous API variant of getBlob.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postBlob_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Blob

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of postBlob.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

putBlob_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Blob

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of putBlob.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

getFormData_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response FormData)) 

Synchronous API variant of getFormData.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postFormData_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> FormData

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of postFormData.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

putFormData_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> FormData

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of putFormData.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

getUint8Array_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response Uint8Array)) 

Synchronous API variant of getUint8Array.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postUint8Array_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Uint8Array

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of postUint8Array.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

putUint8Array_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Uint8Array

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of putUint8Array.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postImage_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Image

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of postImage.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

putImage_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Image

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of putImage.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

getArrayBuffer_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ArrayBuffer)) 

Synchronous API variant of getArrayBuffer.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

postArrayBuffer_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> ArrayBuffer

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of postArrayBuffer.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

putArrayBuffer_ Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> ArrayBuffer

Body

-> [(MisoString, MisoString)]

headers_

-> IO (Either (Response error) (Response ())) 

Synchronous API variant of putArrayBuffer.

Blocks the calling thread until the browser resolves the promise, returning Right response on success or Left response on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

Internal

fetch Source #

Arguments

:: (FromJSVal success, FromJSVal error) 
=> MisoString

url

-> MisoString

method

-> Maybe JSVal

body

-> [(MisoString, MisoString)]

headers

-> (Response success -> IO ())

successful callback

-> (Response error -> IO ())

errorful callback

-> CONTENT_TYPE

content type

-> IO () 

Retrieve JSON via Fetch API

Basic GET of JSON using Fetch API, will be expanded upon.

See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API