Skip to content

jinjor/elm-req

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

elm-req

An experimental alternative for elm/http.

Warning: Not battle tested yet. Use with care.

Note: If you use --debug, you'll need two extra dependency, elm/file and elm/bytes. This is a workaround for a known Map.! bug. Move those into "direct" by running elm install.

elm install elm/file
elm install elm/bytes

For more details, See this issue or this repo.

Motivation

  • Use Tasks more.
  • Use put patch and delete.
  • Build options one by one.
  • Get full information in requests/responses to describe errors.
  • Switch from Cmd to Task gradually (and vice versa).
  • Switch from Http.Error to custom error gradually.

Examples

Create a task that can return Http.Error.

getRepoCompatible : String -> String -> Task Http.Error Repo
getRepoCompatible userName repoName =
    Req.get ("https://api.github.com/repos/" ++ userName ++ "/" ++ repoName)
        |> Req.jsonCompatible repoDecoder

Create a task that can return Req.Error which contains the full information.

getRepo : String -> String -> Task (Req.Error String) Repo
getRepo userName repoName =
    Req.get ("https://api.github.com/repos/" ++ userName ++ "/" ++ repoName)
        |> Req.json repoDecoder
type alias Error a =
    { request : Req
    , problem : Problem a
    }

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

You can also decode error body using *WithError family.

getRepo : String -> String -> Task (Req.Error ErrorInfo) Repo
getRepo userName repoName =
    Req.get ("https://api.github.com/repos/" ++ userName ++ "/" ++ repoName)
        |> Req.jsonWithError
            { decoder = repoDecoder, errorDecoder = errorDecoder }

errorDecoder : Http.Metadata -> Decoder ErrorInfo
errorDecoder meta =
    D.map ErrorInfo
        (D.field "message" D.string)