0.3.20 • Published 6 years ago

redux-axios-reducers v0.3.20

Weekly downloads
10
License
MIT
Repository
github
Last release
6 years ago

redux-axios-reducers

npm version travis build

Redux reducers for fetching data with axios HTTP client

Installation

npm install --save redux-axios-reducers

Dependencies

redux-axios-reducers depends on redux, redux-thunk.

How to use?

Configure and connect an Axios reducer

import {createStore, combineReducers} from 'redux';
import {AxiosReducer} from redux-axios-reducer;

const usersReducer = new AxiosReducer({name: 'users'});

const client = axios.create({
  baseURL:'http://localhost:8080/api',
  responseType: 'json'
});

const reducers = combineReducers({

    // You must to call reducer.configure method for provide reduce function into Redux
    users: usersReducer.configure({axios: client});

    // application reducers
    ...
});

const store = createStore(reducers)

Dispatch actions

A reducer instance has methods which generate async (redux-thunk) actions. The actions do HTTP request.

// HTTP GET /api/users
loadUsers = () => usersReducer.get()

// HTTP GET /api/users?status=active
loadUsersWithFilter = (status) => usersReducer.get({
    // See Axios documentation for params
    params: {
        status: 'active'
    }
})

// HTTP PUT /api/users/1
updateUsers = (id, data) => usersReducer.put({
    url: 'users/' + id,
    data: data
})

// Redux-thunk middleware
loadUsers = () => (dispatch) => {
    dispatch(usersReducer.get())
    .then( () => {
        // do something else
    })
}

// Redux-thunk middleware, chain methods
loadUsersAndComments = () => (dispatch) => {
    Promise.all([
        dispatch(usersReducer.get()),
        dispatch(commentsReducer.get())
    ])
    .then( () => {
        // do something else
    })
}

Reduce actions

A reducer is generating TYPES based on given name:

    reducerInstance.TYPES
    // {
    //     FETCHING: `ASYNC/${name.toUpperCase()}/FETCHING`
    //     FETCH_FAIL: `ASYNC/${name.toUpperCase()}/FETCH_FAIL`
    //     FETCH_SUCCESS: `ASYNC/${name.toUpperCase()}/SUCCESS`
    //     
    // }

So you can use it in your own reducers like usersReducer.TYPES.FETCH_SUCCESS

Reducers methods

AxiosReducer/AxiosRestReducer

methoddescription
get/fetchHTTP GET accepts AxiosParams
postHTTP POST accepts AxiosParams
putHTTP PUT accepts AxiosParams
patchHTTP PATCH accepts AxiosParams
removeHTTP DELETE accepts AxiosParams

License

This project is licensed under the MIT license, Copyright (c) 2017 Kirill Klenov. For more information see LICENSE.md.

Acknowledgements

Dan Abramov for Redux Matt Zabriskie for Axios. A Promise based HTTP client for the browser and node.js

0.3.20

6 years ago

0.3.19

6 years ago

0.3.18

6 years ago

0.3.17

6 years ago

0.3.16

6 years ago

0.3.15

6 years ago

0.3.14

6 years ago

0.3.13

6 years ago

0.3.12

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.2

6 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago