Last active
May 19, 2016 20:54
-
-
Save mindjuice/29586b87a2de1726b87e7ed329ff6318 to your computer and use it in GitHub Desktop.
Simple HTTP requests code using promises. Integrates nicely with Redux actions when using redux-thunk middleware. `httpRequests.js` has the `get`, `put`, `post` and `del` functions and `authActions.js` gives examples of calling `post()` and `get()`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { get, post } from '../../../utils/httpRequests'; | |
export const AUTH_LOGIN = 'AUTH_LOGIN'; | |
export const AUTH_LOGOUT = 'AUTH_LOGOUT'; | |
export const login = (email, password) => { | |
const params = { username: email, password }; | |
return dispatch => post('/api/login', AUTH_LOGIN, params, dispatch); | |
}; | |
export const logout = () => dispatch => get('/api/logout', AUTH_LOGOUT, undefined, dispatch); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import request from 'superagent'; | |
import superAgentAsPromised from 'superagent-as-promised'; | |
superAgentAsPromised(request); | |
const DEFAULT_TIMEOUT = 3000; | |
function dispatchRequest(req, actionCreator, actionPayload, dispatch) { | |
// First tell the reducers that we are starting a request | |
dispatch(actionCreator(actionPayload, true)); | |
return req.then(res => { | |
dispatch(actionCreator({ ...actionPayload, data: res.body })); | |
}) | |
.catch(err => { | |
dispatch(actionCreator(new Error(err))); | |
}); | |
} | |
export function get(url, actionCreator, actionPayload, dispatch, timeout = DEFAULT_TIMEOUT) { | |
const req = request | |
.get(url) | |
.timeout(timeout); | |
return dispatchRequest(req, actionCreator, actionPayload, dispatch); | |
} | |
export function put(url, actionCreator, actionPayload, dispatch, timeout = DEFAULT_TIMEOUT) { | |
const req = request | |
.put(url) | |
.send(actionPayload) | |
.timeout(timeout); | |
return dispatchRequest(req, actionCreator, actionPayload, dispatch); | |
} | |
export function post(url, actionCreator, actionPayload, dispatch, timeout = DEFAULT_TIMEOUT) { | |
const req = request | |
.post(url) | |
.type('application/json') | |
.type('form') | |
.set('X-Requested-With', 'XMLHttpRequest') | |
.set('Cache-Control', 'no-cache,no-store,must-revalidate,max-age=-1') | |
.send(actionPayload) | |
.timeout(timeout); | |
return dispatchRequest(req, actionCreator, actionPayload, dispatch); | |
} | |
export function del(url, actionCreator, actionPayload, dispatch, timeout = DEFAULT_TIMEOUT) { | |
const req = request | |
.del(url) | |
.send(actionPayload) | |
.timeout(timeout); | |
return dispatchRequest(req, actionCreator, actionPayload, dispatch); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment