# redux-service-middleware

> A Redux middleware for easy service integration

Latest version **2.1.0** (published 2018-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-service-middleware
pnpm add redux-service-middleware
yarn add redux-service-middleware
bun add redux-service-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 | 2.1.0 |
| Published | 2018-10-26 |
| First published | 2018-06-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 65.5 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Varghese John |
| Maintainers | varghese88 |
| Keywords | redux, middleware, middlewares, promise, promises, axios, async, async functions, webservice, api |

## Links

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

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.18.0

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2018-10-26
- 2.0.4 — 2018-07-11
- 2.0.3 — 2018-07-11
- 2.0.2 — 2018-07-11
- 2.0.1 — 2018-06-28
- 2.0.0 — 2018-06-28
- 1.0.2 — 2018-06-28
- 1.0.1 — 2018-06-28
- 1.0.0 — 2018-06-28

## README

# Redux-Service-Middleware

Redux Service Middleware enables simple to integrate webservice call easily through [Redux](http://redux.js.org). 

First we need to define the service configuration

Here is the sample

```js
const config = {
    baseConfig:{
        baseURL: 'https://example.com',
        headers:{'content-type':'application/json'},
        timeout: 10000,
    },
    serviceConfig:{
        POST_DATA:{
            method:'GET',
            url:'/posts'
        }
    }
}
```
- baseConfig - define the basic web service configuartion
- serviceConfig - collection of object in which each object define redux action as key and the web service details as value

> Additional features 
 - Third party dependency injection
 - validate the action before dispatch 
 - async dispatch( similar to thunk)
 - promise payload
 - dependency injection on validate, process and payload 

Middleware usage

Defining the middleware
```js
const store = createStore(
    reducer,applyMiddleware(dataService({config, dependencies})));
})
```
Define the redux action
```js
example 1

const getPostData = () =>({
    type:'POST_DATA',
    validate({dispatch, accept, reject }){
        if(check){
            accept();
        } else {
            reject();
        }
    },
    process({dispatch}){
        dispatch({ type:"TEST_DATA"})
    },
    complete({ dispatch, resposne, error }){
        console.log(response)
    }
    payload:{
        userId:45666
    }
})

example 2
Dependency Injection on process, validate and payload

const getPostData = () =>({
    type:'POST_DATA',
    validate({dispatch, accept, reject, uuid }){
        if(check){
            accept();
        } else {
            reject();
        }
    },
    process({dispatch, uuid }){
        dispatch({ type:"TEST_DATA", payload:{uuid}})
    },
    payload({ uuid }){
         return {uuid};
    }
})
```
- Internaly this middleware uses axios as httpclient
- Currently it supports **REST** GET and POST method
- Pass parms - **GET**/ body - **POST** or **PUT** in payload as object


Given a single action with an async payload, the middleware transforms the action to a separate a pending action and a separate fulfilled/rejected action, representing the states of the async action.

Varghese John:
- [GitHub](https://github.com/varghese88)

## License

[Code licensed with the MIT License (MIT)](/LICENSE).

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