npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reduxful

v1.5.1

Published

Redux state from RESTful services

Downloads

84

Readme

Reduxful

Version npm GitHub Workflow Status Coverage Status

Client-side state is often related to data requested from RESTful web services. Reduxful aims to reduce the boilerplate for managing requested data in Redux state by generating actions, reducers, and selectors for you.

Installation

Install reduxful along with redux-thunk middleware.

yarn add reduxful redux-thunk

or

npm install --save reduxful redux-thunk

Documentation

Usage

Describe an API

// doodad-api.js

const apiDesc = {
  getDoodad: {
    url: 'http://api.my-service.com/doodads/:id'
  },
  getDoodadList: {
    url: 'http://api.my-service.com/doodads'
  }
};

In its purest form, an API description is an object of keys following the verbNoun convention by which to name a resource. The values associated with these are the request description, which at a minimum requires an url.

These resource names are also the names generated for the actionCreators and selectors.

Make a Request Adapter

Before we can call our endpoints, we need to set up a Request Adapter to use our AJAX or Fetch library of choice. A convenience function is available to make an adapter for the Fetch API.

// my-request-adapter.js

import fetch from 'cross-fetch';
import { makeFetchAdapter } from 'reduxful';

export default makeFetchAdapter(fetch);

In this example, we are using cross-fetch which allows universal fetching on both server and browser. Any other library can be used, as long as an adapter is implemented for adjusting params and returning the expected Promise.

Setup Reduxful Instance

To generate Redux tooling around an API Description, pass it along with the name you want your Redux state property to be. Also, include the request adapter in the api config argument.

// doodad-api.js

import Reduxful from 'reduxful';
import requestAdapter from './my-request-adapter';

const apiConfig = { requestAdapter };
const doodadApi = new Reduxful('doodadApi', apiDesc, apiConfig);

export default doodadApi;

The variable that you assign the Reduxful instance to has the actions, reducers, and selectors need for working with Redux.

Attach to Store

The first thing to using the Reduxful instance with Redux is to attach your reducers to a Redux store.

// store.js

import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import doodadApi from './doodad-api';

const rootReducer = combineReducers(doodadApi.reducers);
const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

export default store;

Reduxful depends on the redux-thunk middleware and uses it for the actionCreators.

Dispatch Actions

The Reduxful instance has an actionCreators property (or actions as an alias) from which you can dispatch action with the Redux store.

import doodadApi from './doodad-api';
import store from './store';

store.dispatch(doodadApi.actionCreators.getDoodadList());

store.dispatch(doodadApi.actionCreators.getDoodad({ id: '123' }));

Our list resource description does not require any path or query params. However, our single resource does require an id as a path param, so we this in the params object as the first argument to the actionCreator.

Select from State

The Reduxful instance has a selectors property by which you can easily select resources from the Redux state.

import doodadApi from './doodad-api';
import store from './store';

const doodadList = doodadApi.selectors.getDoodadList(store.getState());

const doodad = doodadApi.selectors.getDoodad(store.getState(), { id: '123' });

To select the list resource, we only need to pass the store's state. For our single resource, we also pass the same params used in the actionCreator. Params are used to key a resource in the redux state, which allows multiple requests to the same endpoint with different details to be managed.

Resources

Resources are the objects selected from Redux state and which track a request and resulting data from its response, with properties indicating status: isUpdating, isLoaded, and hasError.

import doodadApi from './doodad-api';
import store from './store';

function outputResultsExample(resource) {
  if (!resource) {
    return 'No resource found.';
  } else if (resource.isUpdating && !(resource.isLoaded || resource.hasError)) {
    return 'Loading...';
  } else if (resource.isLoaded) {
    return resource.value;
  } else if (resource.hasError) {
    return resource.error;
  }
}

const doodadList = doodadApi.selectors.getDoodadList(store.getState());

outputResultsExample(doodadList);

In this example, except for the first request, we always return the value or error data even if an update is occurring. When a first request is made, or for any subsequent requests, isUpdating will be true. After a response, the resource will have either isLoaded or hasError set to true.

If an action has not been dispatch, there will be no resource selected from state. In the situation, the utility functions isUpdating, isLoaded, and hasError can be used for checking status safely against an undefined resource.

From here

Continue learning about Reduxful's more advanced setups.