# redux-req

> A redux middleware for handling HTTP requests

Latest version **0.3.0** (published 2016-11-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-req
pnpm add redux-req
yarn add redux-req
bun add redux-req
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2016-11-22 |
| First published | 2016-05-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sheng |
| Maintainers | jedirandy |

## Links

- npm: https://www.npmjs.com/package/redux-req
- Repository: https://github.com/jedirandy/redux-req
- Homepage: https://github.com/jedirandy/redux-req#readme
- Issues: https://github.com/jedirandy/redux-req/issues
- npm.io page: https://npm.io/package/redux-req

## Recent versions

- 0.3.0 (latest) — 2016-11-22
- 0.2.0 — 2016-06-16
- 0.1.3 — 2016-06-16
- 0.1.2 — 2016-05-20
- 0.1.1 — 2016-05-20
- 0.1.0 — 2016-05-20

## README

# redux-request
:beginner: A redux middleware for handling HTTP requests

It's based on plain old XHR, though not as fancy as fetch, it simply does the job and there's no need for extra dependencies.

[![Build Status](https://travis-ci.org/jedirandy/redux-req.svg?branch=master)](https://travis-ci.org/jedirandy/redux-req)
[![dependencies](https://david-dm.org/jedirandy/redux-req.svg)](https://david-dm.org/jedirandy/redux-req)
[![npm module](https://badge.fury.io/js/redux-req.svg)](https://www.npmjs.org/package/redux-req)
[![codecov.io](https://codecov.io/github/jedirandy/redux-req/coverage.svg?branch=master)](https://codecov.io/github/jedirandy/redux-req?branch=master)

## Install
```bash
npm install redux-req
```

## A quick example

```javascript
import { ACTION_TYPE } from 'redux-request';

// dispatch an API request
dispatch({
  type: ACTION_TYPE,
  resourceName: 'users'
  url: '/api/users',
  method: 'GET',
  requestType: 'REQUEST_API',
  receiveType: 'RECEIVE_API'
});

```
once a request is dispatched, the middleware will dispatch an action of the specified request type, you can make a reducer to handle this action, for instance, update the loading state for the resource.

when the request is completed, the middleware will dispatch an event of the specified receive type, similarly, a reducer can be used to handle the response, with a result of either failure or success.

## Usage
To dispatch a request middleware aware action, the action should have the following properties:

| prop | type | description |
| -----| ---- | ----------- |
| type | string | **required** should be the type imported (**ACTION_TYPE**) from the middleware |
| resourceName | string | the name of the resource for which you want to send a request, this is necessary for reducers to identify the request|
| url | string | the url of the resource |
| method | string | the method of the request |
| payload | any | the request payload |
| beforeSend | function | a function passed with the XHR object, so that some extra work can be done before sending the request |

## Reducer enhancer

To simplify the creation of reducer that can interpret the actions emitted by the middleware, a reducer enhancer is also delivered with the package, here's an example:

```javascript
import { enhanceReducer } from 'redux-req';

const enhanced = enhanceReducer(a_reducer);
```

then the state will be injected with the following properties according to the request result.

```javascript
{
    isFetching: boolean,
    error: any // payload from the failed request, null if there's no error
}
```

## License
MIT

---
_Source: https://npm.io/package/redux-req · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
