jinjor/elm-req/2.1.0

NOTE: the latest version is 3.0.0

Req

An experimental alternative for elm/http.

See more details in elm/http.

Types

type alias Req =
{ method : String
, url : String
, headers : List ( String, String )
, body : Body
, timeout : Maybe Float
, allowCookiesFromOtherDomains : Bool
}

Request

type Body
= EmptyBody
| StringBody String String
| JsonBody Value
| FileBody File
| BytesBody String Bytes
| MultipartBody (List Part)

Body

type Part
= StringPart String String
| FilePart String File
| BytesPart String String Bytes

Part

type alias Error a =
{ request : Req
, error : Problem a
}

Req with Error

type Problem a
= BadUrl String
| Timeout
| NetworkError
| BadStatus Metadata a
| BadBody Metadata String

Similar to Http.Error but have more informarion

Methods

init : String -> String -> Req

Arbitrary method

get : String -> Req

GET

post : String -> Req

POST

put : String -> Req

PUT

patch : String -> Req

PATCH

delete : String -> Req

DELETE

Body

withStringBody : String -> String -> Req -> Req

Add text with mime type.

withJsonBody : Value -> Req -> Req

Add JSON for POST etc.

withFileBody : File -> Req -> Req

Add file to upload

withBytesBody : String -> Bytes -> Req -> Req

Add bytes with mime type.

withMultipartBody : List Part -> Req -> Req

Add multipart body.

stringPart : String -> String -> Part

String part (key and value)

filePart : String -> File -> Part

String part (key and file)

bytesPart : String -> String -> Bytes -> Part

Bytes part (key and mime and bytes)

Options

withHeader : String -> String -> Req -> Req

Add header

withTimeout : Float -> Req -> Req

Add timeout

allowCookiesFromOtherDomains : Bool -> Req -> Req

Allow cookies from other domains.

Task

stringCompatible : Req -> Task Error String

Make a task that returns a string or Http.Error

string : Req -> Task (Error String) String

Make a task that returns a string or Req.Error

stringWithError : (Metadata -> Decoder e) -> Req -> Task (Error e) String

Make a task that returns a string or Req.Error with custom error. Decoding errors will be a BadBody error.

Note: This function assumes the error is always a JSON. If you are not sure, use string instead and decode the body later.

jsonCompatible : Decoder a -> Req -> Task Error a

Make a task that returns an arbitrary data or Http.Error

json : Decoder a -> Req -> Task (Error String) a

Make a task that returns an arbitrary data or Req.Error

{ decoder : Decoder a
, errorDecoder : Metadata -> Decoder e
}
-> Req
-> Task (Error e) a

Make a task that returns an arbitrary data or Req.Error with custom error. Decoding errors will be a BadBody error.

Note: This function assumes the error is always a JSON. If you are not sure, use json instead and decode the body later.

bytesCompatible : Decoder a -> Req -> Task Error a

Make a task that returns an arbitrary data or Http.Error.

bytes : Decoder a -> Req -> Task (Error String) a

Make a task that returns an arbitrary data or Req.Error.

{ decoder : Decoder a
, errorDecoder : Metadata -> Decoder e
}
-> Req
-> Task (Error e) a

Make a task that returns an arbitrary data or Req.Error with custom error.

whatever : msg -> Req -> Task x msg

Ignore the result.

{ onGoodStatus : Metadata -> String -> Result e a
, onBadStatus : Metadata -> String -> Result e a
}
-> Req
-> Task (Error e) a

Create a task from String body.

{ onGoodStatus : Metadata -> Bytes -> Result e a
, onBadStatus : Metadata -> Bytes -> Result e a
}
-> Req
-> Task (Error e) a

Create a task from Bytes body.

toTask : Resolver x a -> Req -> Task x a

Create a task with existing Http.Resolver.

Tracking

trackStringCompatible : String -> (Result Error String -> msg) -> Req -> Cmd msg

Send a request for tracking, expecting a string or Http.Error.

trackString : String -> (Result (Error String) String -> msg) -> Req -> Cmd msg

Send a request for tracking, expecting a string or Req.Error.

String
-> (Result (Error e) String -> msg)
-> (Metadata -> Decoder e)
-> Req
-> Cmd msg

Send a request for tracking, expecting a string or Req.Error with custom error. Decoding errors will be a BadBody error.

Note: This function assumes the error is always a JSON. If you are not sure, use trackString instead and decode the body later.

trackJsonCompatible : String -> (Result Error a -> msg) -> Decoder a -> Req -> Cmd msg

Send a request for tracking, expecting an arbitrary data or Http.Error.

trackJson : String -> (Result (Error String) a -> msg) -> Decoder a -> Req -> Cmd msg

Send a request for tracking, expecting an arbitrary data or Req.Error.

String
-> (Result (Error e) a -> msg)
-> { decoder : Decoder a
, errorDecoder : Metadata -> Decoder e
}
-> Req
-> Cmd msg

Send a request for tracking, expecting an arbitrary data or Req.Error with custom error. Decoding errors will be a BadBody error.

Note: This function assumes the error is always a JSON. If you are not sure, use trackJson instead and decode the body later.

trackBytesCompatible : String -> (Result Error a -> msg) -> Decoder a -> Req -> Cmd msg

Send a request for tracking, expecting an arbitrary data or Http.Error.

trackBytes : String -> (Result (Error String) a -> msg) -> Decoder a -> Req -> Cmd msg

Send a request for tracking, expecting an arbitrary data or Req.Error.

String
-> (Result (Error e) a -> msg)
-> { decoder : Decoder a
, errorDecoder : Metadata -> Decoder e
}
-> Req
-> Cmd msg

Send a request for tracking, expecting an arbitrary data or Req.Error with custom error.

trackWhatever : String -> (Result x () -> msg) -> Req -> Cmd msg

Track something but ignore the result.

track : String -> Expect msg -> Req -> Cmd msg

Make a Cmd with tracker using existing Http.Expect.