js-xajax
Version:
XHR tool wrapper, convenient use and global management.
2 lines (1 loc) • 5.94 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var t,e=function(){return e=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},e.apply(this,arguments)};function n(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{a(r.next(t))}catch(t){i(t)}}function u(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,u)}a((r=r.apply(t,e||[])).next())}))}function r(t,e){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,r=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=e.call(t,s)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}exports.HttpMethod=void 0,(t=exports.HttpMethod||(exports.HttpMethod={})).GET="GET",t.POST="POST",t.PUT="PUT",t.PATCH="PATCH",t.DELETE="DELETE",t.OPTIONS="OPTIONS",t.get="GET",t.post="POST",t.put="PUT",t.patch="PATCH",t.delete="DELETE",t.options="OPTIONS";var o={READY_STATE_CHANGE:"readystatechange",LOAD_START:"loadstart",PROGRESS:"progress",ABORT:"abort",ERROR:"error",LOAD:"load",TIMEOUT:"timeout",LOAD_END:"loadend"},i=function(){function t(t){return this._defaultConfig={dump:JSON.stringify,load:JSON.parse,xmlHttpRequest:function(){return new XMLHttpRequest},timeout:3e4,baseUrl:"",withCredentials:!1},this._timeoutPromise=function(t,e){return new Promise((function(n,r){setTimeout((function(){null==e||e.abort()}),t)}))},this._res=function(t,e){return{status:t.status,statusText:t.statusText,response:t.response,data:e,xhr:t}},this._options=e(e({},this._defaultConfig),t),this}return t.prototype.request=function(t){return n(this,void 0,void 0,(function(){var n,i,s,u,a=this;return r(this,(function(r){return n=e(e({},this._options),t),u=new Promise((function(r,u){var l,d,c,p;if(s=n.xmlHttpRequest(),void 0===n.url)throw new Error("No URL defined");var h=n.url.includes("://")?n.url:(n.baseUrl+n.url).replace(/([^\:])\/\//g,"$1/");t.params&&(h+="".concat(h.includes("?")?"&":"?").concat(new URLSearchParams(n.params).toString())),s.open(n.method,h,!0),s.withCredentials=null!==(l=n.withCredentials)&&void 0!==l&&l,s.addEventListener(o.LOAD,(function(){if(s.status>=200&&s.status<300){var t=void 0;s.responseText&&(console.log({responseText:s.responseText}),t=!0===n.raw?s.responseText:n.load(s.responseText)),r(a._res(s,t))}else u(a._res(s))})),s.addEventListener(o.ABORT,(function(){return u(a._res(s))})),s.addEventListener(o.ERROR,(function(){return u(a._res(s))})),s.addEventListener(o.TIMEOUT,(function(){return u(a._res(s))}));var f=e({Accept:"application/json","Content-Type":"application/json"},null!==(d=n.headers)&&void 0!==d?d:{});for(var v in null===(c=null==n?void 0:n.setRequestHeaders)||void 0===c||c.call(a,f),f)({}).hasOwnProperty.call(f,v)&&s.setRequestHeader(v,f[v]);if(n.eventsHandler)for(var v in n.eventsHandler)({}).hasOwnProperty.call(n.eventsHandler,v)&&s.addEventListener(v,n.eventsHandler[v].bind(null,s),!1);var T="object"!=typeof n.data||n.raw?n.data:n.dump(n.data);i=e(e({xhr:s},t),{fullUrl:h,headers:f}),null===(p=null==n?void 0:n.requestHandler)||void 0===p||p.call(a,i),s.send(void 0!==T?T:null)})),[2,Promise.race([this._timeoutPromise(n.timeout,s),u]).then((function(t){var r;return null===(r=null==n?void 0:n.responseHandler)||void 0===r||r.call(a,e({result:t},null!=i?i:{})),t})).catch((function(t){var r;return null===(r=null==n?void 0:n.errorHandler)||void 0===r?void 0:r.call(a,e({error:t},null!=i?i:{}))})).finally((function(){var t;null===(t=null==n?void 0:n.requestFinally)||void 0===t||t.call(a)}))]}))}))},t.prototype.get=function(t,e,o){return void 0===o&&(o={}),n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,this.request({url:t,params:e,method:exports.HttpMethod.GET,headers:o})]}))}))},t.prototype.post=function(t,e,o){return void 0===o&&(o={}),n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,this.request({url:t,data:e,method:exports.HttpMethod.POST,headers:o})]}))}))},t.prototype.patch=function(t,e,o){return void 0===o&&(o={}),n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,this.request({url:t,data:e,method:exports.HttpMethod.PATCH,headers:o})]}))}))},t.prototype.put=function(t,e,o){return void 0===o&&(o={}),n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,this.request({url:t,data:e,method:exports.HttpMethod.PUT,headers:o})]}))}))},t.prototype.delete=function(t,e,o){return void 0===o&&(o={}),n(this,void 0,void 0,(function(){return r(this,(function(n){return[2,this.request({url:t,data:e,method:exports.HttpMethod.DELETE,headers:o})]}))}))},t.prototype.getBaseURL=function(){return this._options.baseUrl},t.prototype.setBaseURL=function(t){return this._options.baseUrl=null!=t?t:"",this},t.prototype.setRequestTimeout=function(t){return this._options.timeout=t,this},t.prototype.getInstance=function(){return this},t.prototype.create=function(e){return new t(e)},t}(),s=new i;exports.EVENTS=o,exports.XAjax=s,exports.XAjaxClass=i,exports.default=s;