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

@gluedigital/network-action

v0.3.4

Published

A small wrapper for fetch requests on Redux apps

Downloads

42

Readme

Network Action

A simple wrapper around Fetch API, ironing out some of its kirks, and adding Redux action dispatching.

Note: this is a ES6-only module, and must be used in a ES6 environment, or transpiled as needed.

Usage demo

import networkAction from '@gluedigital/network-action'

export const someApiAction = (username, password) => networkAction({
  action: 'login',
  url: 'http://localhost:3000/oauth/token'
  body: { username, password }
})

Features

These are the main differences from using raw fetch:

  • A loading action will be dispatched when starting the request, and a success/error one when finished.
  • Automatic handling of posting JSON body, and defaults to parsing response as JSON.
  • If you have a 'user' reducer with a 'access_token' key, it will be added to each request.
  • When used with Universal Scripts on the server, it will passthrough any received cookies.

Options

The following options can be used:

| Key | Type | Meaning |--------------|--------|--------- | url | string | The absolute url that we want to fetch. | baseUrl | string | A prefix that will be added to the url before using it. Useful for default values. | action | string | Redux action type prefix. If action is 'foo', we will dispatch 'foo/loading' and one of 'foo/success' or 'foo/error'. | body | any | The body of the HTTP request. Can be a FormData, string, or JSON object. | token | string | An access_token to override the one retrieved from the store. | noReject | bool | On error responses, don't reject the promise (to prevent unhandled rejections). | processBody | func | Function to process the body before adding it to the request. | processRequest | func | Function to process the request before sending it. | processResponse | func | Function to process the response before handling it and dispatching the success action. It can reject it too by throwing. | meta | any | Any extra info you want to associate to this request. Will be dispatched on the loading/success/error actions. | fetchOptions | object | Extra raw options to pass to fetch().

Defaults

If you want to make multiple calls with some common settings, like when creating actions related with the same server, you can use this pattern:

import networkAction from '@gluedigital/network-action'

const fooNetworkAction = networkAction.withDefaults({
  baseUrl: 'https://example.com/foo'
  fetchOpts: { credentials: 'include' }
})

export const someFooApiAction = (username, password) => fooNetworkAction({
  action: 'login',
  url: '/oauth/token'
  body: { username, password }
})

The withDefaults helper adds the options passed to it as defaults for the next calls, and takes care to mix the fetchOpts properly.

Reducer

We provide a ready-made reducer for the actions dispatched by this library. You can use it like this:

import networkAction from '@gluedigital/network-action'

const myReducer = combineReducers({
  loadFoo: networkAction.reducer('loadFoo'),
  saveBar: networkAction.reducer('saveBar')
})

Additionally, networkAction.reducer may take a list of actions that will share the same state key (and, therefore, overwrite its previous value when one of the actions is dispatched).

import networkAction from '@gluedigital/network-action'

const myReducer = combineReducers({
  sharedFooBar: networkAction.reducer(['loadFoo', 'saveBar' ])
})

The stored value will be:

// If still loading:
{ isLoading: true }
// On success:
{ isSuccess: true, value: action.payload }
// On error:
{ isError: true, error: action.payload }

In case you want to store certain extra info after dispatching the action, you could do it by adding the meta object containing the info, just like in the example below:

import networkAction from '@gluedigital/network-action'

export const getImageById = (imageId) => networkAction({
  action: 'getImageById',
  url: 'http://localhost:3000/images',
  body: { imageId },
  meta: { imageId }
})

const fooApiActionReducer = combineReducers({
  image: networkAction.reducer('getImageById'),
})

The stored value would include the meta field with the original info provided to the action:

// If still loading:
{ isLoading: true, meta: { imageId: imageId } }
// On success:
{ isSuccess: true, value: action.payload, meta: { imageId: imageId } }
// On error:
{ isError: true, error: action.payload, meta: { imageId: imageId} }

There is another variant available at networkAction.keyedReducer which saves a response similar to the previous one, but for each different value of the key attribute. The key can be either an attribute name that matches a field on the meta object, or a function which returns a key derived from the whole action.

const myReducer = networkAction.keyedReducer('getTask', 'id')

const getTask = id => networkAction({
  action: 'getTask',
  url: 'http://localhost:3000/tasks/' + id,
  meta: { id }
}))

// Usage demo:
await dispatch(getTask(1))
await dispatch(getTask(2))
console.log(getState())
// {
//   1: { isSuccess: true, value: ... },
//   2: { isError: true, error: ... },
// }