# fetch-middleware

> Middleware for Redux async actions

Latest version **1.1.0** (published 2017-05-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install fetch-middleware
pnpm add fetch-middleware
yarn add fetch-middleware
bun add fetch-middleware
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-05-03 |
| First published | 2017-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Junior Oliveira |
| Maintainers | arojunior |
| Keywords | redux, es6, middleware, async, requests, flux, side-effects |

## Links

- npm: https://www.npmjs.com/package/fetch-middleware
- Repository: https://github.com/arojunior/fetch-middleware
- Issues: https://github.com/arojunior/fetch-middleware/issues
- npm.io page: https://npm.io/package/fetch-middleware

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2017-05-03
- 1.0.4 — 2017-04-12
- 1.0.3 — 2017-03-27
- 1.0.2 — 2017-03-27
- 1.0.1 — 2017-03-27
- 1.0.0 — 2017-03-26

## README

# Redux Middleware for async actions

* FSA
* Promise based

#### Usage example

* **Actions creators**

```javascript
const githubFetching = createAction(GITHUB_FETCHING)
const githubError    = createAction(GITHUB_ERROR)
const githubSuccess  = createAction(GITHUB_SUCCESS)
```

* Create an action passing all action creators and the api call method

```javascript
export const getUserFromGithub = username => ({
  type : [
    githubFetching,
    githubSuccess,
    githubError
  ],
  payload : {
    data : () => axios.get(`https://api.github.com/users/${username}`)
  }
})
```
* **type** is an Array with actions creators
* **payload** must have **data** property and it must to be a function that returns a **Promise**

**Note:** you can pass more properties inside **payload** and it will be passed to success action.

Basic flux: loginSending -> data -> loginSuccess / loginError

The middleware will call loginSending before apiCall and loginSuccess when promise got resolved, if got an error, loginError will be called.

### Reducer / Action creators example (with redux-actions)

* **Actions types**

```javascript
export const GITHUB_FETCHING = 'modules/Github/FETCHING'
export const GITHUB_SUCCESS  = 'modules/Github/SUCCESS'
export const GITHUB_ERROR    = 'modules/Github/ERROR'
```

* **Reducer**

```javascript
import {handleActions} from 'redux-actions'

import {GITHUB_FETCHING, GITHUB_SUCCESS, GITHUB_ERROR} from './actions'

const initialState = {
  fetching : false,
  user     : null
}

const reducer = handleActions({
  [GITHUB_FETCHING]: (state, action) => ({
    ...state,
    sending     : true
  }),

  [GITHUB_SUCCESS]: (state, action) => ({
    sending     : false,
    user        : action.payload.data
  }),

  [GITHUB_ERROR]: (state, action) => ({
    ...state,
    sending     : false,
  })

}, initialState);

export default reducer
```

### Example without redux-actions

```javascript
const githubFetching = () => ({ type : 'GITHUB_FETCHING' })
const githubError    = () => ({ type : 'GITHUB_ERROR' })
const githubSuccess  = payload => ({
  type    : 'GITHUB_SUCCESS',
  payload : payload
})

const initialState = {
  fetching : false,
  user     : null
}

export const getUserFromGithub = username => ({
  type : [
    githubFetching,
    githubSuccess,
    githubError
  ],
  payload : {
      data : () => axios.get(`https://api.github.com/users/${username}`)
  }
})

export default (state = initialState, action) => {
  switch (action.type) {
    case 'GITHUB_FETCHING':
      return {...state,
              fetching     : true
              }
    case 'GITHUB_SUCCESS':
      return {...state,
              fetching     : false,
              user         : action.payload.data
              }
    case 'GITHUB_ERROR':
      return {...state,
              fetching       : false
            }
    default:
      return state
  }
}
```

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