Registry / http-networking / redux-api-middleware

redux-api-middleware

JSON →
library3.2.1jsnpmunverified

Redux-api-middleware is a Redux middleware designed to standardize and simplify API calls within a Redux application. It processes actions adhering to the "Redux Standard API-calling Action" (RSAA) specification, identified by a special `[RSAA]` property. When such an action is dispatched, the middleware intercepts it, makes an HTTP request using the `fetch` API, and then dispatches a sequence of Flux Standard Actions (FSA) representing the request's lifecycle: `REQUEST`, `SUCCESS`, and `FAILURE`. This declarative approach helps manage loading states, error handling, and data fetching consistency. The current stable version is 3.2.1, with a history of regular updates and significant breaking changes in major versions (v2.0.0, v3.0.0). It relies on a global `fetch` implementation, requiring polyfills in environments like Node.js.

npm install redux-api-middleware
INSTALL
IMPORT
SIG · REDUX-API-MIDDLEWA
R
redux-api-middleware
http-networkingjavascriptv3.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

apiMiddleware
import { apiMiddleware } from 'redux-api-middleware';
const { apiMiddleware } = require('redux-api-middleware');
This is the main middleware function to be applied to your Redux store using `applyMiddleware`.
RSAA
import { RSAA } from 'redux-api-middleware';
import { CALL_API } from 'redux-api-middleware';
`RSAA` is a string constant (`'[RSAA]'`) that identifies a Redux Standard API-calling Action. The `CALL_API` alias was deprecated in v2.0.0 and removed entirely in v3.0.0.
createAction
import { createAction } from 'redux-api-middleware';
import createAction from 'redux-api-middleware/lib/createAction';
A helper function for conveniently constructing RSAA actions. It simplifies defining the endpoint, method, and types for API calls.

This quickstart demonstrates how to set up `redux-api-middleware` in a Redux store, define an RSAA action for an API call, and dispatch it to manage fetching data and updating state.

import { createStore, applyMiddleware, combineReducers } from 'redux'; import { apiMiddleware, RSAA, createAction } from 'redux-api-middleware'; // A simple reducer to handle API states const initialState = { data: null, loading: false, error: null, }; function apiReducer(state = initialState, action: any) { switch (action.type) { case 'FETCH_USER_REQUEST': return { ...state, loading: true, error: null }; case 'FETCH_USER_SUCCESS': return { ...state, loading: false, data: action.payload }; case 'FETCH_USER_FAILURE': return { ...state, loading: false, error: action.payload }; default: return state; } } const rootReducer = combineReducers({ api: apiReducer, }); // Configure the Redux store with the API middleware // Ensure 'fetch' is available globally (e.g., polyfilled for Node.js) const store = createStore(rootReducer, applyMiddleware(apiMiddleware)); // Define action types for the API call lifecycle const FETCH_USER_REQUEST = 'FETCH_USER_REQUEST'; const FETCH_USER_SUCCESS = 'FETCH_USER_SUCCESS'; const FETCH_USER_FAILURE = 'FETCH_USER_FAILURE'; // Create and dispatch an RSAA action to fetch a user const fetchUser = (userId: string) => ({ [RSAA]: { endpoint: `https://jsonplaceholder.typicode.com/users/${userId}`, method: 'GET', types: [ FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE, ], }, }); console.log('Dispatching action to fetch user 1...'); store.dispatch(fetchUser('1')); // Example using the createAction helper (available since v3.2.0 for RSAA actions) const fetchUserWithCreator = (userId: string) => createAction({ endpoint: `https://jsonplaceholder.typicode.com/users/${userId}`, method: 'GET', types: [FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE], }); console.log('Dispatching action to fetch user 2 using createAction...'); store.dispatch(fetchUserWithCreator('2')); // In a real application, you would connect components to the Redux store // to react to these state changes. For demonstration, we'll log the state. setTimeout(() => { console.log('Current state after API calls:', store.getState()); }, 1000); // Wait for the async fetches to potentially complete
Debug
Known issues
breakingThe `CALL_API` alias for the RSAA action key was completely removed. Actions using `[CALL_API]` will no longer be processed by the middleware.
fix
Update all API-calling actions to use `[RSAA]` as the top-level key instead of `[CALL_API]`.
affects: >=3.0.0
breakingError handling for failed `fetch` requests changed significantly. Previously, a failed `fetch` would dispatch a `REQUEST` FSA followed by another `REQUEST` FSA with an error flag. Now, it dispatches a `REQUEST` FSA followed by a `FAILURE` FSA.
fix
Refactor reducers to explicitly handle `FAILURE` action types for API errors, rather than relying on an error flag within a `REQUEST` action.
affects: >=3.0.0
breakingThe `CALL_API` symbol was replaced with the `RSAA` string as the top-level key for API-calling actions. While `CALL_API` was aliased to `RSAA` in v2.0.0, this alias was removed in v3.0.0.
fix
Migrate all existing API-calling actions from using `[CALL_API]` to `[RSAA]` for forward compatibility and to avoid issues in v3+.
affects: >=2.0.0
breakingStarting from v2.0.0, `redux-api-middleware` no longer bundles its own `fetch` implementation and explicitly depends on a global `fetch` being available in the runtime environment.
fix
Ensure `fetch` is polyfilled in your application's entry point for environments that do not natively support it (e.g., `whatwg-fetch` for browsers, `node-fetch` or `isomorphic-fetch` for Node.js). Alternatively, provide a custom `fetch` implementation via `createMiddleware` or directly in the RSAA action's `fetch` property.
affects: >=2.0.0
gotchaAsynchronous properties within an RSAA action object (e.g., for `endpoint`, `body`, `headers`) are only officially supported since version 3.1.0. In older versions, these properties were expected to be synchronous.
fix
Upgrade to `v3.1.0` or later to utilize asynchronous RSAA properties. For older versions, ensure all RSAA properties are synchronous values or functions that return synchronous values.
affects: <3.1.0
Errors
Common errors & fixes
TypeError: fetch is not a function
The runtime environment (e.g., Node.js, older browsers) does not have the global `fetch` API available, and no polyfill has been provided. `redux-api-middleware` relies on this global function since v2.0.0.
fix
Install and import a `fetch` polyfill. For Node.js, `npm install node-fetch` then `import 'node-fetch/polyfill';` (or `require('node-fetch/polyfill');`). For older browsers, `npm install whatwg-fetch` then `import 'whatwg-fetch';`.
Property '[CALL_API]' does not exist on type '{ ... }' (TypeScript error)
Attempting to use the `CALL_API` symbol as the action key in TypeScript, which was replaced by `RSAA` and removed in v3.0.0.
fix
Change the action key from `[CALL_API]` to `[RSAA]` in your Redux Standard API-calling Actions.
Middleware must not return a Promise on actions without RSAA properties
This error can occur if a non-RSAA action is dispatched and the middleware's internal logic (potentially an older version or specific setup) incorrectly processes it as if it were an API call, leading to an unexpected Promise return. This was specifically addressed in v2.2.0.
fix
Ensure that only actions intended for `redux-api-middleware` contain the `[RSAA]` property. If dispatching regular actions, confirm they do not inadvertently match the RSAA pattern. Consider upgrading to `v2.2.0` or later if you encounter this with valid non-RSAA actions.
Upgrade
Version history
3.2.1latest on npm
Audit
Dependencies
reduxrequiredCore library for Redux state management. `redux-api-middleware` is designed to be used within a Redux store's middleware chain.
whatwg-fetchoptional`redux-api-middleware` depends on a global `fetch` API. `whatwg-fetch` is a common polyfill for browser environments that lack native `fetch` support. This dependency became mandatory from v2.0.0 onwards.
node-fetchoptional`redux-api-middleware` depends on a global `fetch` API. `node-fetch` is a common polyfill for Node.js environments where `fetch` is not natively available. This dependency became mandatory from v2.0.0 onwards.
Agent activity
4 hits · last 30 days
node
4
Resources
redux-api-middleware — npm install redux-api-middleware · libregistry