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

resource-store-redux

v0.4.5

Published

Resource store for async resources

Downloads

19

Readme

resource-store-redux

Build Status Test Coverage

An async generic resource store for redux

This is a resource store implementation for handling redux state shape for async resources. The async resources can be fetched from http or websocket or even just promises. It contains only the reducer, action implementation. It doesn't have any saga, thunk library. This library is going to stay generic.

It's a remake of this library but without the saga stuff and contains improvements with the use of typesafe-actions library.

Notice: This package doesn't have functionality to perform request but it handles the state shape changes of async resources

Notice: There will be a separate package based on thunk middleware that maps these actions into axios or fetch requests

The state shape is the following;

export interface Resource {
  readonly isBusy: boolean;
  readonly error?: Error;
  readonly data?: any;
}

export interface ResourceStoreState {
  [key: string]: Resource;
}

The actions that can be applied to state shape is the following;

export enum ResourceActionTypes {
  RESOURCE_REQUEST = "@RESOURCE/REQUEST",
  RESOURCE_SUCCESS = "@RESOURCE/SUCCESS",
  RESOURCE_FAILURE = "@RESOURCE/FAILURE",
  RESOURCE_CANCEL = "@RESOURCE/CANCEL",
}

The key can be described as the resource key. Here's a sample of meaningful resource keys;

enum ResourceKeys {
    CreateUser = "createUser"
    ListUsers = "listUsers"
    DeleteUser = "deleteUser"
    UpdateUser = "updateUser"
}

To wire the reducer into the root reducer;

import { combineReducers } from 'redux'
import { resourceStore } from 'resource-store-redux'

const resourceStore = resourceStore({
    keys: Object.values(ResourceKeys)
})

export default combineReducers({
    resources: resourceStore.reducer,
    // And your other reducers
})

After this, the initial state would be generated for each resource key

To dispatch actions in your container definition;

import { resourceActions } from 'resource-store-redux'

const mapDispatchToProps = dispatch => ({

  onStuffHappens: () => {
    // request starts
    dispatch(resourceActions.request(ResourceKeys.CreateUser, requestParams)),

    // request ended with 20x
    dispatch(resourceActions.success(ResourceKeys.CreateUser, data))

    // request fails
    dispatch(resourceActions.failure(ResourceKeys.CreateUser, error))

    // cancel request
    dispatch(resourceActions.cancel(ResourceKeys.CreateUser, error))

    // Notice: These actions doesn't make any request but just handles the redux state of requests
  }
})

There are also builtin selectors for resources for mapping your state to props;

import { getResource, getData, isBusy, getError, hasError } from 'resource-store-redux'

const resource = getResource(state, ResourceKeys.CreateUser)
// returns the complete state shape for resource

const data =  getData(state, ResourceKeys.CreateUser)
// returns response body of request

const isResourceBusy = isBusy(state, ResourceKeys.CreateUser)
// returns if the resource key is busy (between request and response)

const error = getError(state, ResourceKeys.CreateUser)
// returns the error on the resource key

const hasResourceError = hasError(state, ResourceKeys.CreateUser)
// returns if the resource key has error

Roadmap

  • [ ] Get rid of tslint, integrate eslint