Code Monkey home page Code Monkey logo

elm-web-data's Introduction

Elm Web Data

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).

elm-web-data's People

Contributors

ohanhi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.