Giter VIP home page Giter VIP logo

bs-remotedata's Introduction

bs-remotedata

RemoteData and WebData to use with bs-fetch and bs-json for BuckleScript

Test status

RemoteData.t is a simple variant type that allows you to store a data that can have 4 potential states:

  • Success('a): your data has been successfully retrieved and stored
  • Failure('e): your data could not be retrieved
  • Loading: your data is beeing fetched
  • NotAsked: you didn't fetch your data yet

This type provides you many usefull functions to handle your data: map, andThen, withDefault, fromOption, fromResult, fromPromise, ...

The main goal for this type is to be used for HTTP requests. That's where WebData comes into play. WebData.t is a RemoteData.t type but with the error type specified as a WebData.error. The WebData module provides some usefull functions to fetch data from API and convert it to a WebData.t. You can even provide your own Json decoder to convert the result of your API call to a WebData.t of any type you want/need.

Example

type person = {
  age: int,
  name: string
};

module Decode = {
  let personDecoderExn = json =>
    Json.Decode.{
      age: json |> field("age", int),
      name: json |> field("name", string)
    };
    
  let personDecoder = json =>
    try (Belt.Result.Ok(personDecoderExn(json))) {
    | Json.Decode.DecodeError(err) => Belt.Result.Error(err)
    };    
};

/* At app launch say you set your data state to `NotAsked` */
let data: WebData.t(person) = RemoteData.NotAsked;

/* You received an event and you need to retrieve your data */
let data: WebData.t(person) = RemoteData.Loading;

Fetch.fetch("http://my-api")
|> WebData.fromResponse(Decode.personDecoder)
|> Js.Promise.then_(data => {
  /* Here your data is still a WebData.t(person) and will be
    either Success(person), or Failure(httpError) */
});

Installation

npm install --save bs-remotedata

Then add bs-remotedata to bs-dependencies in your bsconfig.json:

{
  ...
  "bs-dependencies": ["bs-remotedata"]
}

Documentation

For the moment, please see the interface files:

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.