Skip to content

taberh/redux-fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redux-fetch

Standard - JavaScript Style Guide Build Status

Simple and elegant isomorophic fetch for Redux middleware. Not complex api, just like writing a synchronization action.

Install

npm install --save redux-fetch-elegant

Example Usage

import Redux, {
  createStore,
  applyMiddleware,
  compose
} from 'redux'
import fetchMiddleware, {
  STATUS_REQUEST,
  STATUS_SUCCESS,
  STATUS_FAILURE,
  configureOptions
} from 'redux-fetch-elegant'

// you can configure global default options
configureOptions({
  credentials: 'same-origin',
  headers: {
    'xxx': 'xxx'
  }
})

// and configure common logic
fetchMiddleware.then((response) => response.json())

const composeArray = [
  applyMiddleware(fetchMiddleware)
]
const store = compose.apply(Redux, composeArray)(createStore)(reducer)

const FETCH_SOMETHING = 'fetch_something'

store.dispatch({
  type: FETCH_SOMETHING,
  endpoint: 'http://domain/path/',
  body: JSON.stringify({}),
  method: 'GET'
})

function reducer(state, action) {
  if (action.type === FETCH_SOMETHING) {
    switch (state.status) {
      case STATUS_REQUEST
        // fetch start
        break
      case STATUS_SUCCESS:
        // fetch success
        break
      case STATUS_FAILURE:
        // fetch error
        break
    }
  }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published