request-monitor
Version:
1 lines • 5.13 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.requestMonitor=e():t.requestMonitor=e()}(window,function(){return function(t){var e={};function n(o){if(e[o])return e[o].exports;var r=e[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(o,r,function(e){return t[e]}.bind(null,r));return o},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r={},i=1;function s(t){return window._requestMonitorIsLoad?null:(window._requestMonitorIsLoad=!0,function(t){var e=XMLHttpRequest.prototype.open,n=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.open=function(){for(var t=arguments.length,n=Array(t),o=0;o<t;o++)n[o]=arguments[o];var r=n[0],i=n[1];this._monitor={__type:"xhr"},e.apply(this,n),Object.assign(this._monitor,{method:r,url:i})},XMLHttpRequest.prototype.send=function(){for(var e=this,r=arguments.length,i=Array(r),s=0;s<r;s++)i[s]=arguments[s];n.apply(this,i),this._monitor.startTime=(new Date).getTime(),this._monitor.timeout=this.timeout||0;try{i.length>0&&"string"==typeof i[0]&&(this._monitor.params=i[0])}catch(t){}this.addEventListener("load",function(){var e=this.getResponseHeader("Content-Type");e=(e=e||"").toLowerCase();if(!(this.responseType&&["blob","arraybuffer","moz-chunked-arraybuffer","ms-stream"].indexOf(this.responseType)>-1)){if(-1!==e.indexOf("application/json")){this._monitor.responseText=this.responseText;try{this._monitor.responseJson=JSON.parse(this.responseText)}catch(t){}}else-1!==e.indexOf("text/plain")&&(this._monitor.responseText=this.responseText);this._monitor.responseHeaders=function(t){try{var e=t.getAllResponseHeaders();if(!e)return;if("object"===(void 0===e?"undefined":o(e)))return e;var n={},e=e.split(/[\r\n]/).forEach(function(t){var e=t.indexOf(":"),o=t.substr(0,e),r=t.substr(e+2);o&&(n[o]=r)});return n}catch(t){return{}}}(this),this._monitor.responseStatus=this.status,this._monitor.responseStatusText=this.statusText,t(this._monitor)}},!1),this.addEventListener("error",function(){e._monitor.responseStatusText="Xhr Network request exception",e._monitor.responseStatus=-1,t(e._monitor)}),this.addEventListener("timeout",function(){t(e._monitor)})}}(t),function(t){if(window.fetch){var e=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=function(t){var e=void 0;try{t.body&&"string"==typeof t.body&&(e=t.body)}catch(t){}return e}(r),s={__type:"fetch",url:e,method:r.method||"GET",params:i,startTime:(new Date).getTime(),timeout:r.timeout||0},u=o(e,r).then(function(e){var o=e.text;return s.responseStatus=e.status,s.responseHeaders=function(t){try{var e=t.headers.entries(),n={},o=!0,r=!1,i=void 0;try{for(var s,u=e[Symbol.iterator]();!(o=(s=u.next()).done);o=!0){var c=s.value;n[c[0]]=c[1]}}catch(t){r=!0,i=t}finally{try{!o&&u.return&&u.return()}finally{if(r)throw i}}return n}catch(t){return console.warn(t),{}}}(e),s.responseStatusText=e.statusText,e.json=function(){return new n(function(n,r){o.call(e).then(function(e){try{var o=JSON.parse(e);s.responseText=e,s.responseJson=o,t(s),n(o)}catch(n){n.type="invalid-json",s.responseText=e,t(s),r(n)}}).catch(function(t){r(t)})})},e.text=function(){return new n(function(n,r){o.call(e).then(function(e){s.responseText=e,t(s),n(e)}).catch(function(t){r(t)})})},e});return new n(function(e,n){return u.then(function(t){e(t)}).catch(function(e){s.responseStatus=e.status||-1,s.responseText=e.message||"Fetch Network request exception",t(s),n(e)}),u})},n=window.fetch.Promise||Promise,o=window.fetch;e.Promise=n,Object.defineProperty(e,"Promise",{set:function(t){o.Promise=t},get:function(){return o.Promise}}),window.fetch=e}}(t),!0)}function u(t){!function(t){var e=(new Date).getTime();t.requestTime=e-t.startTime,t.endTime=e}(t);try{Object.keys(t).forEach(function(e){void 0===t[e]&&delete t[e]}),Object.keys(r).forEach(function(e){setTimeout(function(){try{r[e](t)}catch(t){console.error(t)}})})}catch(t){console.error(t)}}function c(t){if("function"!=typeof(t=t||function(t){return t}))throw new Error("The listener Type must be function");var e=i++;return r[e]=t,s(u),{cancel:function(){delete r[e]}}}t.exports=c,c.handleDefaultApi=s}])});