jmeas/redux-resource

View on GitHub
docs/extras/http-status-codes-plugin.md

Summary

Maintainability
Test Coverage
# HTTP Status Codes Plugin

### Documentation

Add this plugin to keep track of status codes of your HTTP Requests on
resource metadata. This is useful because status codes give you more
detail information about your in-flight requests.

Note that you can simply use [request objects](../requests/request-objects.md)
instead of this plugin. For instance:

```js
dispatch({
  type: 'READ_RESOURCES_FAILED',
  resourceType: 'books',
  requestKey: 'searchBooks',
  requestProperties: {
    statusCode: 404
  }
});

// => resource object:
//
// {
//   requestKey: 'searchBooks',
//   status: 'FAILED',
//   statusCode: 404
// }
```

This plugin is only useful when you specifically want to track the status on
resource metadata.

### Usage

First, you need to register this plugin when you call
[`resourceReducer`](../api-reference/resource-reducer.md).

```js
import { resourceReducer } from 'redux-resource';
import { httpStatusCodes } from 'redux-resource-plugins';

const reducer = resourceReducer('books', {
  plugins: [httpStatusCodes]
});
```

This plugin doesn't come with any custom action types. Instead, it changes the
way the state is tranformed with the built-in CRUD
[action types](../api-reference/action-types.md). Any time that you pass a
`statusCode` in an action with one of those types, then the code will be stored
in your state tree.

Passing the status code looks like the following:

```js
import { actionTypes } from 'redux-resource';
import store from './store';

store.dispatch({
  type: actionTypes.READ_RESOURCES_FAILED,
  resourceType: 'books',
  resources: [10],
  statusCode: 404
});
```

If you're using the
[Redux Resource XHR](redux-resource-xhr.md)
library, then you don't need to do anything differently: request status
codes are already included in the actions dispatched from that library.

Within your resource metadata, the status code will be available at one of four
keys, depending on the CRUD operation being performed:

- `createStatusCode`
- `readStatusCode`
- `updateStatusCode`
- `deleteStatusCode`

On a request object, the code is just available under `statusCode`.

```js
import store from './store';

const state = store.getState();

// Access the status codes of some resource meta
const bookStatusCode = state.books.meta[24].readStatusCode;

// Access the status code from a request object
const searchStatusCode = state.books.requests.search.statusCode;
```