1.0.9 • Published 5 years ago

redux-fast-axios v1.0.9

Weekly downloads
5
License
ISC
Repository
github
Last release
5 years ago

redux-fast-axios

Simple redux middleware to make easy axios requests with redux flow

Description

Clean and easy way to make fast axios request

Installation

npm -i --save redux-axios
yarn add redux-axios

Usage

//store.js
import { createStore, applyMiddleware } from 'redux'
import createReduxFastAxiosMiddleware from "redux-fast-axios"
import rootReducer from './rootReducer'
 
const reduxAxiosMiddleware = createReduxFastAxiosMiddleware(
    {
      timeout:1000,
      defaultHeaders:[{type:'Content-Type',value:'application/json'},{type:'x-access-token',value:localStorage.getItem('token')}],
      baseUrl:process.env.NODE_ENV === `development` ? 'http://localhost/api' : 'http://some_prod_url/',
      digestAxiosResponse:(response,onSuccess,onFailure)=>{
          if(response.data.success){
              onSuccess(response.data.success.data)
          }
          else{
              onFailure(response.data.error.message)
          }
      }
    }
)
 
const store = createStore(
  rootReducer,
  applyMiddleware(reduxAxiosMiddleware)
)


//actions.js : 
import {reduxFastAxiosAction} from "redux-fast-axios"

const doReduxAction = ()=>(dispatch,getState)=>{
    
    dispatch(reduxFastAxiosAction({
        url:'some-url', // will be http://localhost/api/some-url //
        method:'POSt',
        data:{user:''},
        type:"SOME_START_TYPE",
        onSuccess:(data)=>{
            //do something with the callback from digestAxiosResponse
            dispatch({type:'SOME_SUCCESS_TYPE',payload:{data}})
        },
        onFailure:(failureMessage)=>{
            //do something with the callback from digestAxiosResponse
            dispatch({type:'SOME_FAILURE_TYPE',payload:{failureMessage}})
        }
    }))
    
}

Config for createReduxFastAxiosMiddleware

PropDescriptionDefault
baseUrlbase url for axios''
timeouttimeout for axios request0
defaultHeadersdefault headers array shape of {type,value} for axios[]
digestAxiosResponsecustom function to digest the axios response to decide what to return to onSuccess or onFailure callbacksfunction

Future Updates

support more axios configuration

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago