Options
All
  • Public
  • Public/Protected
  • All
Menu

@roebuk/remote-data

Remote Data

GitHub Workflow Status David npm bundle size (scoped)

Tools for fetching data from remote sources (incl. HTTP). For a detailed write up of why Remote Data helps, read this post.

Installation

$ npm install @roebuk/remote-data

Via a script tag https://unpkg.com/@roebuk/remote-data

Documentation

API Docs

Basic Example

import * as RemoteData from '@roebuk/remote-data';

// Set the initial state
var remoteUsers = RemoteData.NotAsked();

// An interaction starts off the request
remoteUsers = RemoteData.Loading();

// Once the request is complete,
// it will either be in a `Success` or `Failure` state.
remoteUsers = await fetch('/api/users')
.then(res => res.json())
.then(users => RemoteData.Success(users))
.catch(err => RemoteData.Failure(err));


// "Pattern match" on the RemoteData type and extract the current state.
// The return value of the functions should all be of the same type.
RemoteData.match({
notAsked: () => 'Not Requested the data'
loading: () => 'Loading...'
success: users => `Loaded ${users.length} users`,
failed: err => `Something when wrong. Details: ${err.message}`
}, remoteUsers);

React Example

React & Remote Data on StackBlitz

Building & Testing

npm ci
npm run build
npm t

Generated using TypeDoc