JSPM

  • Created
  • Published
  • Downloads 194
  • Score
    100M100P100Q87487F
  • License MIT

redux middleware and api library

Package Exports

  • @launchpadlab/lp-redux-api
  • @launchpadlab/lp-redux-api/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@launchpadlab/lp-redux-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version

lp-redux-api

Action creators and reducers for making API requests with redux.

// Define an API action

import { createRequest } from '@launchpadlab/lp-redux-api'

const fetchUser = createRequest('FETCH_USER', (id) => ({
  url: '/users/' + id,
}))

// Dispatch the action from a component (mapDispatchToProps not shown)

function Component({ fetchUser }) {
  return <button onClick={() => fetchUser(5)}> Fetch user with id: 5 </button>
}

// Store the response in a reducer

import { handleActions } from 'redux-actions'
import { setOnSuccess } from '@launchpadlab/lp-redux-api'

const reducer = handleActions(
  {
    [fetchUser]: setOnSuccess('user'),
  },
  {}
)

// state.user will contain the response when the request completes.

The key functions in this library are:

  • createRequest(key, requestInfo) - this function allows you to define a redux action that makes an API request asynchronously. By default, API requests are made using lp-requests, although this is configurable.

  • setOnSuccess(path) | setOnFailure(path) - these functions allow you to easily store the responses of API actions in your redux store.

Additional usage information, as well as a list of other helpers this library provides, can be found in the documentation.

Documentation

Documentation and usage info can be found in docs.md.

Migration Guides

Contribution

This package follows the Opex NPM package guidelines. Please refer to the linked document for information on contributing, testing and versioning.