Redux middleware to dispatch Superagent or BackboneORM requests
JavaScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src Improve error handling a little Apr 28, 2017
test Add retry option Aug 16, 2016
.babelrc back to babel 5 May 24, 2016
.eslintrc initial commit Sep 29, 2015
.gitignore Fix error status being clobbered Dec 11, 2016
.npmignore Added requestLogger Jun 2, 2016
LICENSE license Dec 14, 2015
changelog.md Changes Dec 15, 2016
package.json 0.14.0 Apr 28, 2017
readme.md Update readme.md May 18, 2018

readme.md

This package has moved to the Frameworkstein repository


Request middleware for Redux

Works like redux promise middleware. Resolves request objects from superagent or BackboneORM models. Can work with anything with a similar callback style api.

requestMiddleware, responseParserMiddleware

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
// Superagent
import request from superagent

dispatch({
  type: 'GET_SOMETHING',
  request: request.get('/something'),
  callback: err => console.log('This will be called when the request completes. Useful for navigating after a request returns (login, etc). Errors should not be handled here - an error action is sent, work with that.'),
})


// BackboneORM
import Task from './models/task'

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


// Callback
import Task from './models/task'

dispatch({
  type: 'GET_TASKS',
  request: callback => loadSomeThingsManually(callback),
})

requestModifierMiddleware

Appends a user id to any superagent request or BackboneORM cursor function it finds on an action.

It's designed to work alongside redux-request-middleware which will perform the request and dispatch the relevant (sub)-actions.

It must be included before redux-request-middleware 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:

export function setValue(request, value) {
  if (_.isObject(request._cursor)) {
    _.merge(request._cursor, value)
  }
  if (_.isFunction(request.query)) {
    request.query(value)
  }
  return request
}
Usage

This example creates middleware that adds a $user_id param with the current user's id to requests

const requestModifierMiddleware = createRequestModifierMiddleware({
  getValue: store => {
    const {auth} = store.getState()
    const value = {}
    if (auth.get('user')) value.$user_id = auth.get('user').get('id')
    return value
  },
})

i.e. if you have a user with id 1234, all modified requests will now look like /api/some_model/?$user_id=1234

requestLoggerMiddleware

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