1.0.3 • Published 7 years ago

redux-fetch-elegant v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

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
    }
  }
}
1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago