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

redux-api-actions

v0.0.18

Published

redux-api-middleware's helper to support create api actions.

Downloads

35

Readme

redux-api-actions

Build Status

Utilities for redux-api-middleware, redux-actions and react-redux.

Table of Contents

Getting Started

Installation

redux-api-actions is available on npm.

npm install --save redux-api-actions

Usage

Before use this library

Defining the module

If you have something looks like this:

  • constants
const namespace = 'count';

export const NAMESPACE = namespace.toUpperCase();
export const TYPE_COUNT = NAMESPACE;
export const TYPE_COUNT_REQUEST = `${TYPE_COUNT}_REQUEST`;
export const TYPE_COUNT_SUCCESS = `${TYPE_COUNT}_SUCCESS`;
export const TYPE_COUNT_FAILURE = `${TYPE_COUNT}_FAILURE`;

export const endpoint = 'http://www.example.com/api/counter'
  • redux-actions
import { createActions, handleActions } from 'redux-actions';
import { TYPE_COUNT } from './constants';

const defaultState = { counter: 10 };
export const increment = createAction(TYPE_COUNT);

export const reducer = handleActions({
  [TYPE_COUNT]: state => state.update('counter', v => v + 1),
}, defaultState);
  • redux-api-middleware
import { handleActions } from 'redux-actions';
import { RSAA } from 'redux-api-middleware';
import {
  endpoint,
  TYPE_COUNT_REQUEST,
  TYPE_COUNT_SUCCESS,
  TYPE_COUNT_FAILURE,
} from './constants';

export const incrementRemote = amount => ({
  [RSAA]: {
    endpoint: `${endpoint}/${amount}`,
    method: 'GET',
    headers: { 'Content-Type': 'application/json' },
    credentials: 'same-origin',
    types: [
      TYPE_COUNT_REQUEST,
      TYPE_COUNT_SUCCESS,
      TYPE_COUNT_FAILURE,
    ],
  },
});

export const reducer = handleActions({
  [TYPE_COUNT_SUCCESS]: (state, { payload: { amount } }) => state.update('counter', v => amount),
});
  • react-redux
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { NAMESPACE } from './constants';

const reduxActions = { increment, incrementRemote };

const mapStateToProps = store => ({ store: store[NAMESPACE] });
const mapDispatchToProps = dispatch => ({ actions: bindActionCreators(reduxActions, dispatch) });
export const Container = connect(mapStateToProps, mapDispatchToProps)(Root);

Then, you can merge them into this gist:

  • redux-api-actions
import creator from 'redux-api-actions';
import { endpoint } from './constants';

const { Container, Reducer, NAMESPACE } = creator({
  namespace: 'count',
  state: { counter: 0 },
  component: Root,
  actions: {
    increment: {
      success: state => state.update('counter', v => v + 1),
    },
    incrementRemote: {
      endpoint,
      before: amount => ({ endpoint: `${endpoint}/${amount}` }),
      success: (state, { payload: { amount } }) => state.update('counter', v => amount),
    }
  },
});

Looks good, right?

Finally, merge your Reducer into your redux store as usual.

import { createStore, combineReducers } from 'redux';
const store = createStore(
  combineReducers({ ...Reducer }),
  initialState,
);

Documentation

Hey, I'm no good at Writting. So, please help me do this. And please help me add the test case.

License

MIT