Why I published easy-redux-http-call
Every API call in a Redux app needs the same boilerplate. I turned it into an npm package that takes it from 15–20 minutes to under one.
I published easy-redux-http-call to npm in November 2022. It started as a complaint, not a plan.
The problem
In a Redux app, every API call needs the same scaffolding. Three action types, a thunk that dispatches them, reducer cases to store the result, and a loading flag and an error to keep in sync. Here is a trimmed version for a single endpoint:
// 1. action types
const GET_EMPLOYEES_START = "GET_EMPLOYEES_START";
const GET_EMPLOYEES_SUCCESS = "GET_EMPLOYEES_SUCCESS";
const GET_EMPLOYEES_ERROR = "GET_EMPLOYEES_ERROR";
// 2. thunk
export const getEmployees = () => async (dispatch) => {
dispatch({ type: GET_EMPLOYEES_START });
try {
const res = await fetch(`${BASE_URL}/employees`);
dispatch({ type: GET_EMPLOYEES_SUCCESS, payload: await res.json() });
} catch (error) {
dispatch({ type: GET_EMPLOYEES_ERROR, error });
}
};
// 3. reducer cases
case GET_EMPLOYEES_START:
return { ...state, loading: true };
case GET_EMPLOYEES_SUCCESS:
return { ...state, loading: false, data: action.payload };
case GET_EMPLOYEES_ERROR:
return { ...state, loading: false, error: action.error };
None of it is hard, and that is exactly the problem. It is the same code every time, with only the names changed. Writing and wiring it for one endpoint took me 15 to 20 minutes. An app with thirty endpoints spends a full working day typing code that carries no decisions.
The idea
Describe an API call by the few facts that actually differ (where it goes, which method, and where the result should live in the store) and generate everything else.
import { CreateBase } from "easy-redux-http-call";
const api = new CreateBase({
store,
baseApiUrl: "<https://api.example.com>",
});
export const employees = api.createInstance({
endpoint: "/employees",
method: "GET",
reducer: "user",
reduxKey: "employeeList",
action: "GET_EMPLOYEES",
});
That is the whole definition. From it the package generates the start, success, error, update and delete actions (named like user_START_employeeList), and employees.call() makes the request and dispatches them. Binding it to your existing reducer is one line:
const userReducer = bindReducer(baseUserReducer, { key: "user" });
Your existing actions keep working. The package only handles the actions it generated.
Reading the state in a component
import { useInstance } from "easy-redux-http-call";
function Employees() {
const { loading, data, error } = useInstance(employees);
// ...
}
Loading, data and error come from the store, with no selector to write and no flags to maintain.
What else it covers
- Dynamic endpoints. Pass
urlParamstocall()to fill a path like/user/{id}, along withparamsfor the body andheaders. - Lists.
createListInstanceaddsupdateActionanddeleteAction, so a record can be changed or removed in the store without a new reducer case. They can be called straight from a socket event. - Middleware. Hooks for
beforeStart,afterSuccess,afterUpdateandafterDeletecover the things every project eventually needs: pagination, notifications and filtering results. - Your own HTTP client.
httpFnlets you use fetch, Axios or anything else.
What it did for me
For a typical endpoint, the work went from 15 to 20 minutes down to under a minute.
The second benefit is consistency. Every call in the project has the same shape and the same state, so any developer can read any of them at a glance, with no per-endpoint quirks to learn.
The package has had 46 releases on npm. It is small and solves one irritating problem. If you work in a Redux codebase and recognise that boilerplate, it is here: easy-redux-http-call.