XHRFactory.js 721 B

1234567891011121314151617181920212223242526272829303132
  1. const XHRFactory = {
  2. config: {
  3. withCredentials: false,
  4. customHeaders: [
  5. { header: null, value: null }
  6. ]
  7. },
  8. createXMLHttpRequest: function () {
  9. let xhr = new XMLHttpRequest();
  10. if (this.config.customHeaders &&
  11. Array.isArray(this.config.customHeaders) &&
  12. this.config.customHeaders.length > 0) {
  13. let baseOpen = xhr.open;
  14. let customHeaders = this.config.customHeaders;
  15. xhr.open = function () {
  16. baseOpen.apply(this, [].slice.call(arguments));
  17. customHeaders.forEach(function (customHeader) {
  18. if (!!customHeader.header && !!customHeader.value) {
  19. xhr.setRequestHeader(customHeader.header, customHeader.value);
  20. }
  21. });
  22. };
  23. }
  24. return xhr;
  25. }
  26. };
  27. export {XHRFactory};