https://github.com/vasa-develop/redux-cross-fetch
A Redux middleware for API requests
https://github.com/vasa-develop/redux-cross-fetch
Last synced: 7 months ago
JSON representation
A Redux middleware for API requests
- Host: GitHub
- URL: https://github.com/vasa-develop/redux-cross-fetch
- Owner: vasa-develop
- Created: 2019-03-25T17:39:44.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-08T19:29:33.000Z (almost 7 years ago)
- Last Synced: 2025-03-18T13:29:41.317Z (about 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
*Add `redux-cross-fetch` middleware to `createStore()`*
## store.js
```
import { createStore, applyMiddleware } from 'redux'
import rootReducer from '../reducers'
import apiMiddleWare from 'redux-cross-fetch'
const store = createStore(rootReducer, applyMiddleware(apiMiddleWare))
export default store
```
The middleware takes following params:
```
@param {url}: [REQUIRED] the request URL
@param {method}: [REQUIRED] HTTP method
@param {headers}: [OPTIONAL] headers sent with the HTTP request
@param {body}: [OPTIONAL] body sent with the HTTP request
@param {success}: [REQUIRED] action creator for successful response
Expects a payload for Server Response
@param {failure}: [REQUIRED] action creator for failed response
Expects a payload for Server Error
```
## actions.js
*Create actions with `type` including the string `API_REQUEST` to fire the middleware.*
```
export const testAction = () => ({
type: "API_REQUEST",
payload: {
request: {
url: "https://api.github.com/users/vasa-develop",
method: "GET"
},
success: apiSuccess,
failure: apiFailure
}
})
export const apiSuccess = (response) => ({
type: "API_SUCCESS",
payload: {
response: response
}
})
export const apiFailure = (error) => ({
type: "API_FAILURE",
payload: {
error: error
}
})
```