| Copyright | (C) 2016-2025 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
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
- getJSON :: (FromJSON body, FromJSVal error) => MisoString -> [(MisoString, MisoString)] -> (Response body -> action) -> (Response error -> action) -> Effect parent model action
- postJSON :: (FromJSVal error, ToJSON body) => MisoString -> body -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- putJSON :: (FromJSVal error, ToJSON body) => MisoString -> body -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- getText :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response MisoString -> action) -> (Response error -> action) -> Effect parent model action
- postText :: FromJSVal error => MisoString -> MisoString -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- putText :: FromJSVal error => MisoString -> MisoString -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- getBlob :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response Blob -> action) -> (Response error -> action) -> Effect parent model action
- postBlob :: FromJSVal error => MisoString -> Blob -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- putBlob :: FromJSVal error => MisoString -> Blob -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- getFormData :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response FormData -> action) -> (Response error -> action) -> Effect parent model action
- postFormData :: FromJSVal error => MisoString -> FormData -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- putFormData :: FromJSVal error => MisoString -> FormData -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- getUint8Array :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response Uint8Array -> action) -> (Response error -> action) -> Effect parent model action
- postUint8Array :: FromJSVal error => MisoString -> Uint8Array -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- putUint8Array :: FromJSVal error => MisoString -> Uint8Array -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- postImage :: FromJSVal error => MisoString -> Image -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- putImage :: FromJSVal error => MisoString -> Image -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- getArrayBuffer :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response ArrayBuffer -> action) -> (Response error -> action) -> Effect parent model action
- postArrayBuffer :: FromJSVal error => MisoString -> ArrayBuffer -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- putArrayBuffer :: FromJSVal error => MisoString -> ArrayBuffer -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent model action
- accept :: MisoString
- contentType :: MisoString
- applicationJSON :: MisoString
- textPlain :: MisoString
- formData :: MisoString
- type Body = JSVal
- data Response body = Response {
- status :: Maybe Int
- headers :: Map MisoString MisoString
- errorMessage :: Maybe MisoString
- body :: body
- data CONTENT_TYPE
- fetch :: (FromJSVal success, FromJSVal error) => MisoString -> MisoString -> Maybe JSVal -> [(MisoString, MisoString)] -> (Response success -> JSM ()) -> (Response error -> JSM ()) -> CONTENT_TYPE -> JSM ()
JSON
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 returned from a fetch request
Constructors
| Response | |
Fields
| |
data CONTENT_TYPE Source #
List of possible content types that are available for use with the fetch API
Instances
| Show CONTENT_TYPE Source # | |
Defined in Miso.FFI.Internal Methods showsPrec :: Int -> CONTENT_TYPE -> ShowS # show :: CONTENT_TYPE -> String # showList :: [CONTENT_TYPE] -> ShowS # | |
| Eq CONTENT_TYPE Source # | |
Defined in Miso.FFI.Internal | |
| ToJSVal CONTENT_TYPE Source # | |
Defined in Miso.FFI.Internal Methods toJSVal :: CONTENT_TYPE -> JSM JSVal Source # toJSValListOf :: [CONTENT_TYPE] -> JSM JSVal Source # | |
Internal
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