Last active
August 5, 2016 23:32
-
-
Save steezeburger/afae806e3c0186066fde43802032112d to your computer and use it in GitHub Desktop.
This file contains 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 superagent from 'superagent'; | |
import { Promise } from 'bluebird'; | |
import config from '../config'; | |
const methods = ['get', 'post', 'put', 'patch', 'del']; | |
function formatUrl(path) { | |
const adjustedPath = path[0] !== '/' ? '/' + path : path; | |
return config.apiHost + adjustedPath; | |
} | |
/** | |
* This client uses superagent to make ajax requests - | |
* All verb methods return a promise | |
*/ | |
export default class ApiClient { | |
constructor() { | |
methods.forEach((method) => | |
this[method] = (path, { params, data, token } = {}) => new Promise((resolve, reject) => { | |
const request = superagent[method](formatUrl(path)); | |
if(params) { | |
request.query(params); | |
} | |
if(token) { | |
request.set('Authorization', `Bearer ${token}`); | |
} | |
if(data) { | |
request.send(data); | |
} | |
request.end((err, { body } = {}) => { | |
// print error before rejecting - non production | |
if(err) { | |
console.log('%c network error ', 'background: red; color: white', formatUrl(path), params, data, err); | |
} | |
err ? reject(body || err) : resolve(body); | |
}); | |
})); | |
} | |
// hack - https://phabricator.babeljs.io/T2455 | |
empty() { | |
} | |
} |
This file contains 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
/** | |
* | |
* This middleware allows us to do some cool stuff with Promises in our action creators | |
* | |
* The types array contains the actions that will be dispatched for | |
* request, receiving a proper response, and receiving an error response, respectively. | |
* | |
* @param client | |
* @returns {function()} | |
*/ | |
export default function clientMiddleware(client) { | |
return ({ dispatch, getState }) => { | |
return next => action => { | |
if(typeof action === 'function') { | |
return action(dispatch, getState); | |
} | |
const { promise, types, ...rest } = action; // eslint-disable-line no-redeclare | |
if(!promise) { | |
return next(action); | |
} | |
const [REQUEST, SUCCESS, FAILURE] = types; | |
next({ ...rest, type: REQUEST }); | |
const actionPromise = promise(client); | |
actionPromise.then( | |
(result) => next({ ...rest, result, type: SUCCESS }), | |
(error) => next({ ...rest, error, type: FAILURE }) | |
).catch((error) => { | |
console.error('MIDDLEWARE ERROR:', error); | |
next({ ...rest, error, type: FAILURE }); | |
}); | |
return actionPromise; | |
}; | |
}; | |
} |
This file contains 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
export function fetchModuleValues(id) { | |
return { | |
types: [FETCH_MODULE_VALUES, FETCH_MODULE_VALUES_SUCCESS, FETCH_MODULE_VALUES_FAIL], | |
promise: (client) => client.get(`/modules/${id}/values`, { | |
token: fetchToken() | |
}) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment