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

Miso.Fetch

Description

Module for interacting with the Fetch API https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API manually.

Refer to the miso README if you want to automatically interact with a Servant API.

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 model action 

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

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 model action 

POST request that uses JSON encoded data

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 model action 

PUT request that uses JSON encoded data

Text

getText Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response MisoString -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

GET request that uses Text encoded data

postText Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> MisoString

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

POST request that uses Text encoded data

putText Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> MisoString

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

PUT request that uses Text encoded data

Blob

getBlob Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response Blob -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

GET request that uses binary encoded data

postBlob Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Blob

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

POST request that uses binary encoded data

putBlob Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Blob

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

PUT request that uses binary encoded data

FormData

getFormData Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response FormData -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

GET request that uses FormData

postFormData Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> FormData

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

POST request that uses FormData

putFormData Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> FormData

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

PUT request that uses FormData

Uint8Array

getUint8Array Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response Uint8Array -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

GET request that retrieves a byte array

postUint8Array Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Uint8Array

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

POST request that sends a byte array

putUint8Array Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Uint8Array

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

PUT request that sends a byte array

Image

postImage Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Image

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

POST request that sends an Image

putImage Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> Image

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

PUT request that sends an Image

ArrayBuffer

getArrayBuffer Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> [(MisoString, MisoString)]

headers_

-> (Response ArrayBuffer -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

GET request that uses ArrayBuffer

postArrayBuffer Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> ArrayBuffer

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

POST request that uses ArrayBuffer

putArrayBuffer Source #

Arguments

:: FromJSVal error 
=> MisoString

url

-> ArrayBuffer

Body

-> [(MisoString, MisoString)]

headers_

-> (Response () -> action)

successful callback

-> (Response error -> action)

errorful callback

-> Effect parent model action 

PUT request that uses ArrayBuffer

Header helpers

accept :: MisoString Source #

Value for specifying Accept in an HTTP header

contentType :: MisoString Source #

Value for specifying "Content-Type" in an HTTP header

applicationJSON :: MisoString Source #

Value for specifying "application/json" in an HTTP header

textPlain :: MisoString Source #

Value for specifying "text/plain" in an HTTP header

formData :: MisoString Source #

Value for specifying "multipart/form-data" in an HTTP header

Types

type Body = JSVal Source #

Type synonym for 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

Internal

fetch Source #

Arguments

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

url

-> MisoString

method

-> Maybe JSVal

body

-> [(MisoString, MisoString)]

headers

-> (Response success -> JSM ())

successful callback

-> (Response error -> JSM ())

errorful callback

-> CONTENT_TYPE

content type

-> JSM () 

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