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

auto-thunk

v1.0.4

Published

Enhanced thunk middleware for Redux.

Downloads

8

Readme

AutoThunk

Enhanced version of redux-thunk that provides a powerful and short syntax to write async action creators.

  • backward compatible, drop in replacement of redux-thunk
  • No dependencies

Motivation

Working with RESTful apis, async action creators are repetitive to write. In most cases, we just need to make a request and send an action passing the response data to the Redux store. AutoThunk provides a short syntax to quickly connect your endpoints with the store.

Installation

npm i --save auto-thunk

Usage

Configure and apply the middleware:

import autoThunkMiddleware from 'auto-thunk'

const autoThunk = autoThunkMiddleware({
  httpClient: axios.create(),
  errorHandler: error => { ... }, // Default error handler
  log: <myLogFunction>,
  track: <myTrackFunction>,
})

const store = createStore(<reducers>, <initialState>, applyMiddleware([autoThunk]))

Write your action creators

Syntax

const getFoos = data => ({
  // Action as a string or an object or an array.
  action: <action>,
  // Request parameters (If an object is passed, it will be passed directly to the httpClient)
  request: ['<http client method>', '<endpoint>', <body>],
  // Log function will be called with '<identifier>' as the first argument, and the response/error as the second argument.
  log: '<identifier>',
  // Track function will be called with the given argument.
  track: <track function argument>,
  // Automatically transform the body data to formData.
  bodyType: 'formData',
  // Override the default error handler if needed
  errorHandler: error => { ... }
})

Examples

Basic example
export const getFoos = () => ({
  action: 'ADD_FOOS',
  request: ['get', '/foos']
})

The equivalent with redux-thunk alone would be:

import httpClient from 'myHttpClientInstance'
export const getFoos = async () => {
  return (dispatch) => {
    try {
      const response = await httpClient.request({
        method: 'get',
        url: '/foos'
      })
      dispatch({
        type: 'ADD_FOOS',
        data: response.data
      })
    } catch (error) {
      // some logic for error handling
    }
  }
}
Other examples
export const deleteFoo = ({ id }) => ({
  action: { type: 'DELETE_FOO', data: id },
  request: ['delete', `/foos/${id}`]
})
// Dispatched action ==> { type: 'DELETE_FOO', data: <id>}


// Dispatching several actions:
export const updateFooColor = ({ color, name, id }) => ({
  action: [
    { type: 'SET_UPDATED_FOO', version: 'v2' }, // Dispatched action ==> { type: 'SET_UPDATED_FOO', data: <response.data>, version: 'v2'}
    { type: 'UPDATE_FOO_COLOR', data: { color } }, // Dispatched action ==> { type: 'SET_UPDATED_FOO', data: { color } }
  ],
  request: ['put', `/foos/${id}`, { color, name }]
})


// With logging and tracking and custom error handler:
export const createFoo = ({ name, color }) => ({
  action: 'ADD_FOO',
  request: ['post', '/foos', { name, color }],
  track: { event: 'CREATION_OF_FOO' }, // Track function will be called with { event: 'CREATION_OF_FOO' }
  log: 'createFoo', // Log function will be called with 'createFoo' as the first argument, and the response/error as the second argument.
  errorHandler: error => console.log(error)
})
// Dispatched action ==> { type: 'ADD_FOO', data: <response.data>}

// Using request as an object:
const postSomeStuff = ({name, cancelToken}) => ({
  request: {
    method: 'post',
    url: `/stuff`,
    data: { name },
    cancelToken
  }
})

// Making requests without dispatching any action:
export const getBars = () => ['get', '/bars']
export const getBar = ({id}) => ['get', `/bars/${id}`]