UNPKG

@bigfishtv/cockpit

Version:

165 lines (149 loc) 5.86 kB
'use strict'; exports.__esModule = true; exports.init = init; exports.setCsrfToken = setCsrfToken; exports.interceptRequest = interceptRequest; exports.interceptResponse = interceptResponse; exports.get = get; exports.post = post; var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios); var _notifications = require('../actions/notifications'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * XHR API Utilities * @module API/xhrUtils */ 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 */ };function init(_store) { store = _store; setCsrfToken(store.getState().csrfToken); } /** * Sets the csrfToken for all axios requests * @param {String} csrfToken */ function setCsrfToken(csrfToken) { _axios2.default.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 */ function interceptRequest(fulfilled, rejected) { var id = _axios2.default.interceptors.request.use(fulfilled, rejected); return function () { return _axios2.default.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 */ function interceptResponse(fulfilled, rejected) { var id = _axios2.default.interceptors.response.use(fulfilled, rejected); return function () { return _axios2.default.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 */ function get(_config) { var config = Object.assign({}, defaultGetConfig, _config); (0, _axios.get)(config.url, { params: config.params }).then(function (response) { if (!config.quiet && !config.quietSuccess) store.dispatch((0, _notifications.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((0, _notifications.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 */ function post(_config) { var config = Object.assign({}, defaultPostConfig, _config); (0, _axios.post)(config.url, config.data).then(function (response) { if (!config.quiet && !config.quietSuccess) store.dispatch((0, _notifications.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((0, _notifications.notifyFailure)(config.failureMessage.replace('%%%', config.subject))); setTimeout(function () { return config.callbackError(response); }); console.warn('Error getting data', response, store); }); }