UNPKG

@bigfishtv/cockpit

Version:

140 lines (128 loc) 5.17 kB
/** * XHR API Utilities * @module API/xhrUtils */ import axios, { get as axiosGet, post as axiosPost } from 'axios' import { notifySuccess, notifyFailure } from '../actions/notifications' let store = null const defaultGetConfig = { url: 'about:blank', params: {}, failureMessage: 'Failed to load %%%, try reloading!', successMessage: 'Loaded %%% successfully!', subject: 'data', callback: () => console.warn('[xhrUtils] no callback function provided for xhr get'), callbackError: () => console.warn('[xhrUtils] no error callback function provided for xhr get'), quiet: false, quietSuccess: true, quietError: false, } const defaultPostConfig = { url: 'about:blank', data: {}, failureMessage: 'Failed to save %%%!', successMessage: 'Saved %%% successfully!', subject: 'data', callback: () => console.warn('[xhrUtils] no callback function provided for xhr post'), callbackError: () => 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) { const id = axios.interceptors.request.use(fulfilled, rejected) 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) { const id = axios.interceptors.response.use(fulfilled, rejected) 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) { const config = Object.assign({}, defaultGetConfig, _config) axiosGet(config.url, { params: config.params }) .then(response => { if (!config.quiet && !config.quietSuccess) store.dispatch(notifySuccess(config.successMessage.replace('%%%', config.subject))) setTimeout(() => config.callback(response.data.data || response.data, response.data)) }) .catch(response => { if (!config.quiet && !config.quietError) store.dispatch(notifyFailure(config.failureMessage.replace('%%%', config.subject))) setTimeout(() => 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) { const config = Object.assign({}, defaultPostConfig, _config) axiosPost(config.url, config.data) .then(response => { if (!config.quiet && !config.quietSuccess) store.dispatch(notifySuccess(config.successMessage.replace('%%%', config.subject))) setTimeout(() => config.callback(response.data.data, response.data)) }) .catch(response => { if (!config.quiet && !config.quietError) store.dispatch(notifyFailure(config.failureMessage.replace('%%%', config.subject))) setTimeout(() => config.callbackError(response)) console.warn('Error getting data', response, store) }) }