HTTP Status Codes

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 instead of this plugin. For instance:

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.

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

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

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;

Last updated