Created
November 14, 2017 10:57
-
-
Save mateuszwrobel/0e1d5b19ac77b560a63211419ebf60e3 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 * as $ from 'jquery'; | |
import * as _ from 'underscore'; | |
import moment from 'moment'; | |
import alertify from 'alertify.js'; | |
import NProgress from 'nprogress'; | |
require('nprogress/nprogress.css'); | |
require('./http.css'); | |
import config from '../config'; | |
import {hashHistory, browserHistory} from 'react-router'; | |
let history = hashHistory; | |
if (config.browserHistory) { | |
history = browserHistory; | |
} | |
const error401 = (response) => { | |
alertify.error(`Error <b>${response.status}</b> : ${response.statusText} <br> Session expired, please login again.`); | |
history.replace('/logout'); | |
}; | |
const errorHandlerHttp = (promise) => { | |
promise.fail((response)=> { | |
if(response.status === 0) return; //Ignore error code 0 | |
if(response.status === 401) { | |
error401(response); | |
return; | |
} | |
alertify.error(`Error <b>${response.status}</b> : ${response.statusText}`); | |
}); | |
return promise; | |
}; | |
export const authRedirectionHandler = (promise) => { | |
promise.fail((response)=> { | |
if(response.status === 401) { | |
error401(response); | |
} | |
}); | |
return promise; | |
}; | |
let _progressRequests = []; | |
export const progressHttp = (promise) => { | |
function _checkAllDone() { | |
const pending = _progressRequests.filter((p) => { | |
return p.readyState === 3; | |
}); | |
if (pending.length === 0) { | |
NProgress.done(); | |
_progressRequests = []; | |
} | |
} | |
NProgress.start(); | |
_progressRequests.push(promise); | |
promise.always(()=> { | |
_checkAllDone(); | |
}); | |
return promise; | |
}; | |
export const jsonHttp = (options) => { | |
options.contentType = 'application/json; charset=UTF-8'; | |
options.dataType = 'json'; | |
return options; | |
}; | |
export const textHttp = (options) => { | |
options.contentType = 'text/plain; charset=UTF-8'; | |
return options; | |
}; | |
export const jsonPayloadHttp = (options) => { | |
let newData = _.mapObject(options.data, (val) => { | |
return moment.isMoment(val) ? val.utc().format(config.dateRESTFormat) : val; | |
}); | |
options.processData = false; | |
options.data = JSON.stringify(newData); | |
return options; | |
}; | |
export const ajax = $.ajax; | |
export const progressAjax = _.compose(progressHttp, ajax); | |
export const defaultAjax = _.compose(progressHttp, errorHandlerHttp, ajax); | |
export const textAjax = _.compose(progressHttp, errorHandlerHttp, ajax, textHttp); | |
export const jsonAjax = _.compose(progressHttp, errorHandlerHttp, ajax, jsonHttp); | |
export const jsonPayloadAjax = _.compose(progressHttp, errorHandlerHttp, ajax, jsonPayloadHttp, jsonHttp); | |
export const jsonNoErrorAjax = _.compose(progressHttp, authRedirectionHandler, ajax, jsonPayloadHttp, jsonHttp); | |
export const parallelRequests = $.when; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment