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-request-middleware

v11.2.6

Published

Redux middleware to dispatch stein-orm or stein-fetch requests

Downloads

89

Readme

Request middleware for Redux

Middleware for redux that provides a convention for working with async requests. Similar in philosophy and behavior to redux-pack which has a much nicer and more detailed writeup on why you might want to use a library like this.

requestMiddleware, responseParserMiddleware

requestMiddleware looks for a request property on redux actions and attempts to resolve it. request can be a promise, fetch request or BackboneORM cursor object.

When a request is found the middleware will terminate the current action and dispatch the following (with <TYPE> being the original type property of the action) <TYPE>_START is sent before resolving the request. <TYPE>_SUCCESS is sent when the request has successfully resolved. The response is added to the current action as res. <TYPE>_ERROR is sent when the request has failed. The response is added to the current action as res.

The middleware returns a promise which will get passed back to the dispatcher of the action. It is resolved when the request completes (after sending the *_SUCCESS or *_ERROR action). Alternatively you may add a callback property to an action which will be called at this time. Either way, the callback/promise is given the current action as a param.

responseParserMiddleware operates on data added to an action by requestMiddleware. It parses raw json from responses and adds the following properties to the current action:

{
  models:       // a map of models in the form {id: <model>}
  modelList:    // always an array of models regardless of whether the response was an array or single object
  model:        // always a single model, equaivalent to models[0]
  status:       // status code of the request
  error:        // best guess at an error object if something went wrong
}
Usage
  1. Add to your middleware
import { createStore, applyMiddleware } from 'redux'
import { requestMiddleware, responseParserMiddleware } from 'redux-request-middleware'
import rootReducer from './reducers'

const store = createStore(
  rootReducer,
  applyMiddleware(requestMiddleware, responseParserMiddleware),
)
  1. Add a request property to your actions that contains the request you want to dispatch

// stein-fetch or any function returning a promise
dispatch({
  type: 'GET_TASKS',
  request: fetch('/api/tasks'),
})

// stein-orm
import Task from './models/Task'

dispatch({
  type: 'GET_TASKS',
  request: Task.cursor({active: true}),
})


requestModifierMiddleware
-------------------------

Functions to modify request actions before sending them.

Designed to work alongside `requestMiddleware` which will perform the request and dispatch the relevant (sub)-actions.

It must be included *before* `requestMiddleware` when combining middleware, otherwise the requests will be sent before it has a chance to alter the query.


#### Options

 - getValue(store):              A function that takes a store and returns a value object to append to the request. You need to supply this.

 - getRequest(action):           Return a request from an action, defaults to returning action.request

 - setValue(request, value):     A function that appends `value` to the request somehow. By default it's this:

```javascript
export function setValue(request, value) {
  if (_.isFunction(request.setHeaders)) {
    request.setHeaders(value.headers)
  }
  return request
}
Usage

This example creates middleware that adds a header with the current organisation id to requests

const requestModifierMiddleware = createRequestModifierMiddleware({
  getValue: store => {
    const { auth } = store.getState()
    const value = {
      headers: {},
    }
    if (auth.get('organisation')) value.headers['x-organisation-id'] = auth.get('organisation').get('id')
    return value
  },
})

requestLoggerMiddleware

Auto logs all requests to the console. Add to your redux middleware to have each request logged (useful for react-native debugging).