11.2.6 • Published 8 months ago

redux-request-middleware v11.2.6

Weekly downloads
10
License
MIT
Repository
github
Last release
8 months ago

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).

11.2.2

8 months ago

11.2.6

8 months ago

11.0.0

1 year ago

10.0.0

1 year ago

8.0.13

2 years ago

9.0.0-rc.6

2 years ago

9.0.0-rc.4

2 years ago

9.0.0-rc.2

2 years ago

9.0.0

2 years ago

8.0.5

2 years ago

8.0.4

2 years ago

8.0.6

2 years ago

7.8.0

2 years ago

8.0.12

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.2

2 years ago

7.6.6

3 years ago

7.6.5

3 years ago

7.6.7

3 years ago

7.6.2

3 years ago

7.6.1

3 years ago

7.6.0

3 years ago

7.3.2

4 years ago

7.2.7

4 years ago

7.0.14

5 years ago

7.0.0

5 years ago

6.17.10

5 years ago

6.10.0

5 years ago

6.9.0

5 years ago

6.3.0

6 years ago

6.2.3

6 years ago

6.2.2

6 years ago

6.1.10

6 years ago

6.1.7

6 years ago

6.1.6

6 years ago

6.1.5

6 years ago

6.1.3

6 years ago

6.0.1

6 years ago

6.0.0

6 years ago

5.0.3

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

0.14.0

7 years ago

0.13.5

7 years ago

0.13.4

7 years ago

0.13.3

8 years ago

0.13.2

8 years ago

0.13.1

8 years ago

0.12.7

8 years ago

0.12.6

8 years ago

0.12.5

8 years ago

0.13.0

8 years ago

0.12.4

8 years ago

0.12.3

8 years ago

0.12.2

8 years ago

0.12.1

8 years ago

0.12.0

8 years ago

0.11.3

8 years ago

0.11.1

8 years ago

0.11.0

8 years ago

0.10.1

8 years ago

0.10.0

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.7

8 years ago

0.4.6

8 years ago

0.4.5

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.0

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.1.0

9 years ago