This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
DEPRECATED in favor of `ohanhi/remotedata-http`
version 2.0.3
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 2.0.3
Committed At 2017-04-17 20:07:44 UTC
elm-lang/http 1.0.0 <= v < 2.0.0 1.0.0
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

README

Elm Web Data

DEPRECATED in favor of ohanhi/remotedata-http


This package is intended as a quick and easy abstraction for dealing with backend data in Elm using the JSON format. As of now, there is very little configuration you can do.

If you like the pattern but need to for example set some more request headers, don't be afraid to simply copy the source code into your project and modify it there. I am willing to introduce some configuration though, so if you have ideas, please post them in the GitHub Issues.

WebData is a type representing the state of some remote JSON data. It looks like this:

type WebData a
    = NotAsked
    | Loading
    | Failure (Error String)
    | Success a

Hats off to Kris Jenkins for coining this pattern in elm-exts.

Example usage

Say we have a component called Cat, which requests some auxiliary data from the backend. Usually, we can define the command to do so just like this:

import WebData exposing (..)
import WebData.Http

type Msg
    = ReceiveCatData (WebData Cat)
-- ...

fetchData : Int -> Cmd Msg
fetchData catId =
    WebData.Http.get
      ("/api/cats/" ++ toString catId)
      catDecoder
      ReceiveCatData

Commands are usually what we need, but sometimes we need tasks for more fine-grained flow control. We can do that just as easily:

create : Cat -> Task Error (WebData Cat)
create catData =
    WebData.Http.postTask
      "/api/cats/"
      catDecoder
      (encode catData)

There is a function to make a command or a task for each of the HTTP verbs.

  • Commands: get, getWithCache, post, put, patch, delete
  • Tasks: getTask, getWithCacheTask, postTask, putTask, patchTask, deleteTask

License

This is made by Ossi Hanhinen and licensed under BSD (3-clause).