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-thunk-service-dispatcher

v0.1.3

Published

A redux-thunk action that simplifies async HTTP service integration w/ Redux stores

Downloads

1

Readme

redux-thunk-service-dispatcher

A redux-thunk action that simplifies async HTTP service integration w/ Redux stores

NPM JavaScript Style Guide

Install

npm install --save redux-thunk-service-dispatcher

Example Usage

Usage

Given fetch service thunks setup like so:

export const getResourceById = (
  id
) => async () => fetch(
  `http://api.myhost.com/resource/${id}`
)

With a Redux store setup like so:

import {createReducer} from "redux-create-reducer"
export const RESOURCE_SET_STATE = 'RESOURCE_SET_STATE'

// Reducer
const INIT_STATE = null
export const recourceReducer = createReducer(INIT_STATE,{
  [RESOURCE_SET_STATE]: (state, {item}) => item,
})

// Action
export const resourceItemSetState = item => ({
  type: RESOURCE_SET_STATE,
  item
})

With redux-thunk-service-dispatcher you can now, very simply set up your async action-thunks like so: 🎉🎉🎉

import serviceDispatcher from 'redux-thunk-service-dispatcher'

export const fetchItem = (
  id
) => async dispatch =>
  dispatch(
    serviceDispatcher(
      getResourceById(id),
      resourceItemSetState
    )
  )

That's it!

Your action-thunk will then return either the resource that was fetched, or else, if there's either a network-level error or your API returns an error response, the body of the response will automatically be parsed for you, and returned back to the caller as a new Error(errorMessage(s))!

This helps to keep your action-thunks DRY, helping keep your code base more manageable and allows your team to move quicker.

API

serviceDispatcher(
  asyncServiceMethod,
  reduxSetStateAction,
  [serviceDispatcherOptions]
)

serviceDispatcherOptions signature:

const serviceDispatchOptions = {
  responseBodyDataKey: 'data', // Key in response body that should be passed to state set action. Default is the whole parsed request body
  onServiceStartAction: () => dispatch => dispatch(startBusy()), // Action that should be dispatched before service call is dispatched.  Useful for starting busy states (i.e. sponners)
  onErrorAction: err => dispatch => dispatch(toastShow(TOAST_TYPE_ERROR, err.message)), // Action that should be dispatched if either a network or response error is detected
  onFinallyAction: () => dispatch => dispatch(stopBusy()), // Action to dispatch at the end of processing service dispatch
  generateReqError: resp => getRespError(resp) // Callback function for customizing how error object will be constructed if error is detected
}

const getRespError = async resp => {
  const respBody = await getResponseBody(resp)
  return new Error(
    respBody.errors ?
      Object.values(respBody.errors)
      .join(', ') : respBody
  )
}

License

MIT © RavenHursT