@bigfishtv/cockpit
Version:
150 lines (139 loc) • 5.43 kB
JavaScript
/**
* XHR API Utilities
* @module API/xhrUtils
*/
import axios, { get as axiosGet, post as axiosPost } from 'axios';
import { notifySuccess, notifyFailure } from '../actions/notifications';
var store = null;
var defaultGetConfig = {
url: 'about:blank',
params: {},
failureMessage: 'Failed to load %%%, try reloading!',
successMessage: 'Loaded %%% successfully!',
subject: 'data',
callback: function callback() {
return console.warn('[xhrUtils] no callback function provided for xhr get');
},
callbackError: function callbackError() {
return console.warn('[xhrUtils] no error callback function provided for xhr get');
},
quiet: false,
quietSuccess: true,
quietError: false
};
var defaultPostConfig = {
url: 'about:blank',
data: {},
failureMessage: 'Failed to save %%%!',
successMessage: 'Saved %%% successfully!',
subject: 'data',
callback: function callback() {
return console.warn('[xhrUtils] no callback function provided for xhr post');
},
callbackError: function callbackError() {
return console.warn('[xhrUtils] no error callback function provided for xhr post');
},
quiet: false,
quietSuccess: false,
quietError: false
/**
* Makes reference to redux store for dispatching notifications
* @param {Object} _store
*/
};export function init(_store) {
store = _store;
setCsrfToken(store.getState().csrfToken);
}
/**
* Sets the csrfToken for all axios requests
* @param {String} csrfToken
*/
export function setCsrfToken(csrfToken) {
axios.defaults.headers.common['X-CSRF-Token'] = csrfToken;
}
/**
* Add a new interceptor to the REQUEST stack
*
* @param {Function} fulfilled The function to handle `then` for a `Promise`
* @param {Function} rejected The function to handle `reject` for a `Promise`
*
* @return {Function} The function to eject the interceptor
*/
export function interceptRequest(fulfilled, rejected) {
var id = axios.interceptors.request.use(fulfilled, rejected);
return function () {
return axios.interceptors.request.eject(id);
};
}
/**
* Add a new interceptor to the RESPONSE stack
*
* @param {Function} fulfilled The function to handle `then` for a `Promise`
* @param {Function} rejected The function to handle `reject` for a `Promise`
*
* @return {Function} The function to eject the interceptor
*/
export function interceptResponse(fulfilled, rejected) {
var id = axios.interceptors.response.use(fulfilled, rejected);
return function () {
return axios.interceptors.response.eject(id);
};
}
/**
* Automagically does xhr get request based on config
* @param {Object} config
* @param {String} config.url
* @param {Object} config.params - params to be encoded in url
* @param {String} config.subject
* @param {String} config.failureMessage - use '%%%' to sub in subject e.g. 'Failed to load %%%, try reloading!'
* @param {String} config.successMessage - use '%%%' to sub in subject e.g. 'Loaded %%% successfully!'
* @param {Boolean} [config.quiet=false]
* @param {Boolean} [config.quietError=false]
* @param {Boolean} [config.quietSuccess=true]
* @param {Function} config.callback - success callback, provides arguments of response data and response.
* @param {Function} config.callbackError - error callback, provides response as only argument
*/
export function get(_config) {
var config = Object.assign({}, defaultGetConfig, _config);
axiosGet(config.url, { params: config.params }).then(function (response) {
if (!config.quiet && !config.quietSuccess) store.dispatch(notifySuccess(config.successMessage.replace('%%%', config.subject)));
setTimeout(function () {
return config.callback(response.data.data || response.data, response.data);
});
}).catch(function (response) {
if (!config.quiet && !config.quietError) store.dispatch(notifyFailure(config.failureMessage.replace('%%%', config.subject)));
setTimeout(function () {
return config.callbackError(response);
});
console.warn('Error getting data', response, store);
});
}
/**
* Automagically does xhr post request based on config
* @param {Object} config
* @param {String} config.url
* @param {Object} config.data - post data
* @param {String} config.subject
* @param {String} config.failureMessage - use '%%%' to sub in subject e.g. 'Failed to load %%%, try reloading!'
* @param {String} config.successMessage - use '%%%' to sub in subject e.g. 'Loaded %%% successfully!'
* @param {Boolean} [config.quiet=false]
* @param {Boolean} [config.quietError=false]
* @param {Boolean} [config.quietSuccess=true]
* @param {Function} config.callback - success callback, provides arguments of response data and response.
* @param {Function} config.callbackError - error callback, provides response as only argument
*/
export function post(_config) {
var config = Object.assign({}, defaultPostConfig, _config);
axiosPost(config.url, config.data).then(function (response) {
if (!config.quiet && !config.quietSuccess) store.dispatch(notifySuccess(config.successMessage.replace('%%%', config.subject)));
setTimeout(function () {
return config.callback(response.data.data, response.data);
});
}).catch(function (response) {
if (!config.quiet && !config.quietError) store.dispatch(notifyFailure(config.failureMessage.replace('%%%', config.subject)));
setTimeout(function () {
return config.callbackError(response);
});
console.warn('Error getting data', response, store);
});
}