UNPKG

x-http-client

Version:

An http client to simplify sending requests (HTTP & JSONP) in the browser.

1 lines 14.9 kB
!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).HttpClient=n()}}(function(){return function i(s,u,c){function l(e,n){if(!u[e]){if(!s[e]){var t="function"==typeof require&&require;if(!n&&t)return t(e,!0);if(a)return a(e,!0);var r=new Error("Cannot find module '"+e+"'");throw r.code="MODULE_NOT_FOUND",r}var o=u[e]={exports:{}};s[e][0].call(o.exports,function(n){return l(s[e][1][n]||n)},o,o.exports,i,s,u,c)}return u[e].exports}for(var a="function"==typeof require&&require,n=0;n<c.length;n++)l(c[n]);return l}({1:[function(n,e,t){var r=n(36);function o(){this.canceled=!1,this.callbacks=[]}o.prototype.cancel=function(){var n=this.callbacks,e=0,t=n.length;if(!1===this.canceled)for(this.canceled=!0;e<t;e+=1)try{n[e]()}catch(n){!function(n){setTimeout(function(){throw n})}(n)}},o.prototype.isCanceled=function(){return this.canceled},o.prototype.registerCancelCallback=function(n){r(n)&&this.callbacks.push(n)},e.exports=o},{36:36}],2:[function(n,e,t){var o=n(38),i=n(36),s=n(37),u=n(31),r=n(24),c=n(26),l=n(25),a=n(9),p=n(3),f=n(6),h=n(10),d=n(4),R=n(7),E=n(11),x=n(5),y=n(8),v=n(1);function O(n,e,t){var r=c();s(n)&&o(r,n),i(e)&&(e(r),r=o({},r)),i(t)||(t=u),this.copyOptions=function(){return o({},r)},this.mergeOptions=function(n){var e=o({},r,n);return t(e),e}}O.prototype.send=function(n,e,t){var r=this.mergeOptions(n);return r.requestFunctionName="send",r.controller=null,new p(r,e,t)},O.prototype.fetch=function(n){var r=this.mergeOptions(n),o=r.controller;return r.requestFunctionName="fetch",new Promise(function(e,n){var t=new p(r,function(n){o&&o.isCanceled()||e(n)},n);o&&(o.isCanceled()?t.cancel():o.registerCancelCallback(function(){t.cancel()}))})},O.prototype.getJSONP=function(n,e,t){var r=this.mergeOptions(n);return r.requestFunctionName="getJSONP",r.controller=null,new f(r,e,t)},O.prototype.fetchJSONP=function(n){var r=this.mergeOptions(n),o=r.controller;return r.requestFunctionName="fetchJSONP",new Promise(function(e,n){var t=new f(r,function(n){o&&o.isCanceled()||e(n)},n);o&&(o.isCanceled()?t.cancel():o.registerCancelCallback(function(){t.cancel()}))})},O.prototype.createCancelController=l,O.createCancelController=l,O.version=O.prototype.version="0.0.1-alpha.8",O.exports=O.prototype.exports=o({CancelController:v,HttpClient:O,HttpRequest:p,HttpResponse:d,HttpResponseError:x,JSONPRequest:f,JSONPResponse:R,JSONPResponseError:y,Request:a,Response:h,ResponseError:E,createDefaultOptions:c},r),e.exports=O},{1:1,10:10,11:11,24:24,25:25,26:26,3:3,31:31,36:36,37:37,38:38,4:4,5:5,6:6,7:7,8:8,9:9}],3:[function(n,e,t){var s=n(9),u=n(24),r=n(30),c=n(22),l=n(28),a=n(23),p=n(12),f=n(17),h=n(15),d=n(16),R=n(14);function o(n,e,t){var r,o,i;s.call(this,u.HTTP_REQUEST,n,e,t),l(n),r=this.xhr=n.createXHR.call(null,n),o=d(n),i=c(n),f(r,n),R(n.onXhrCreated,r,n),r.open(n.method||"GET",i,!0,n.username,n.password),p(this),R(n.onXhrOpened,r,n),h(r,n),r.send(o),R(n.onXhrSent,r,n),a(n,this)}r(o,s),e.exports=o},{12:12,14:14,15:15,16:16,17:17,22:22,23:23,24:24,28:28,30:30,9:9}],4:[function(n,e,t){var r=n(10),o=n(30),i=n(21);function s(n){r.call(this,n),i(this,n.options,"httpResponseMixin")}o(s,r),e.exports=s},{10:10,21:21,30:30}],5:[function(n,e,t){var r=n(11),o=n(30),i=n(21);function s(n,e){r.call(this,n,e),i(this,e.options,"httpResponseErrorMixin")}o(s,r),e.exports=s},{11:11,21:21,30:30}],6:[function(n,e,t){var u=n(9),c=n(24),r=n(30),l=n(28),a=n(23),p=n(18),f=n(19),h=n(20);function o(n,e,t){var r,o,i,s;u.call(this,c.JSONP_REQUEST,n,e,t),l(n),o=this.script=n.createScript.call(null,n),s=n.jsonpContainerNode.call(null,n),i=f(n),r=h(n,i),o.setAttribute("src",r),p(this,i),s.appendChild(o),a(n,this)}r(o,u),e.exports=o},{18:18,19:19,20:20,23:23,24:24,28:28,30:30,9:9}],7:[function(n,e,t){var r=n(10),o=n(30),i=n(21);function s(n){r.call(this,n),i(this,n.options,"jsonpResponseMixin")}o(s,r),e.exports=s},{10:10,21:21,30:30}],8:[function(n,e,t){var r=n(11),o=n(30),i=n(21);function s(n,e){r.call(this,n,e),i(this,e.options,"jsonpResponseErrorMixin")}o(r,s),e.exports=s},{11:11,21:21,30:30}],9:[function(n,e,t){var o=n(33);e.exports=function(n,e,t,r){this.error=null,this.xhr=null,this.script=null,this.finished=!1,this.responseJSON=null,this.requestId=o(),this.requestType=n,this.options=e,this.requestFunctionName=e.requestFunctionName,this.controller=e.controller||null,this.onsuccess=t||null,this.onerror=r||null,e.requestType=n}},{33:33}],10:[function(n,e,t){e.exports=function(n){this.request=n}},{}],11:[function(n,e,t){var r={ERR_ABORTED:"Request aborted",ERR_CANCELED:"Request canceled",ERR_NETWORK:"Network error",ERR_RESPONSE:"Response error",ERR_TIMEOUT:"Request timeout"};e.exports=function(n,e){var t;r[n=n||"ERR_UNKNOWN"]&&(t=r[n]),t=t||"Unknown error "+n,e.error=n,this.code=n,this.request=e,this.message=t}},{}],12:[function(n,e,t){var p=n(36),f=n(4),h=n(13),d=n(27),R=n(31),r=n(24),E=r.ERR_ABORTED,x=r.ERR_CANCELED,y=r.ERR_NETWORK,v=r.ERR_RESPONSE,O=r.ERR_TIMEOUT;e.exports=function(e){var r=e.xhr,n=e.options,o=e.requestType,i=new f(e),s=n.isResponseOk,t=null,u=parseInt(n.timeout,10)||0,c=function(){if(l(),r.abort)try{r.abort()}catch(n){}a(x)},l=function(){l=R,r.onabort=null,r.onerror=null,r.onreadystatechange=null,r.ontimeout=null,t&&(t(),t=null)},a=function(n){c=a=R,e.finished=!0,l(),d(n,i)};r.onabort=function(){a(E)},r.onerror=function(){a(y)},r.onreadystatechange=function(){var n,e,t;4==+r.readyState&&(e=+(n=r).status,"string"!=typeof(t=n.responseURL)&&(t=""),0==e&&0!==t.indexOf("file:")?a(E):p(s)?s(o,i)?a(null):a(v):a(null))},e.cancel=function(){c()},0<u&&(t=h(r,u,function(){if(l(),r.abort)try{r.abort()}catch(n){}a(O)}))}},{13:13,24:24,27:27,31:31,36:36,4:4}],13:[function(n,e,t){e.exports=function(n,e,t){var r=null;return"timeout"in n&&"ontimeout"in n?(n.timeout=e,n.ontimeout=t):r=setTimeout(t,e),function(){n&&(null===r?n.ontimeout=null:clearTimeout(r),t=n=null)}}},{}],14:[function(n,e,t){var r=n(36);e.exports=function(n,e,t){r(n)&&n(e,t)}},{36:36}],15:[function(n,e,t){var i=n(38),s=n(37),u=n(29);e.exports=function(n,e){var t,r,o=s(e.headers)?e.headers:{};for(t in e.noCache&&s(e.noCacheHeaders)&&(o=i(o,e.noCacheHeaders)),o)u.call(o,t)&&null!=(r=o[t])&&n.setRequestHeader(t,r);e.headers=o}},{29:29,37:37,38:38}],16:[function(n,e,t){var p=n(38),f=n(36),h=n(37),d=n(29);e.exports=function(n){var e,t,r,o,i,s=null,u=[],c=n.body,l=n.httpRequestBodyProcessor,a=h(n.headers)?n.headers:{};if(h(c)&&h(l)){for(r in l)d.call(l,r)&&(o=l[r],h(o)&&u.push({key:r,headers:o.headers,priority:o.priority,processor:o.processor}));for(u.sort(function(n,e){return e.priority-n.priority}),e=0,t=u.length;e<t;e+=1)if(void 0!==c[(o=u[e]).key]){s=c[o.key],i=o;break}i&&(h(i.headers)&&(a=p({},i.headers,a)),o=i.processor,f(o)&&(s=o(s,n)))}return n.headers=a,s}},{29:29,36:36,37:37,38:38}],17:[function(n,e,t){var o=n(37),i=n(29);e.exports=function(n,e){var t,r=e.xhrProps;if(e.cors&&(n.withCredentials=!0),o(r))for(t in r)i.call(r,t)&&(n[t]=r[t])}},{29:29,37:37}],18:[function(n,e,t){var a=n(36),p=n(7),f=n(27),h=n(31),r=n(24),d=r.ERR_CANCELED,R=r.ERR_NETWORK,E=r.ERR_RESPONSE,x=r.ERR_TIMEOUT;e.exports=function(e,t){var r=e.script,n=e.options,o=e.requestType,i=n.isResponseOk,s=new p(e),u=parseInt(n.timeout,10)||0,c=null;u<=0&&(u=parseInt(n.jsonpDefaultTimeout,10)||0);var l=function(n){l=h,e.finished=!0,window[t]=h,(r.onerror=null)!==c&&(clearTimeout(c),c=null),f(n,s)};window[t]=function(n){e.responseJSON=n,a(i)?i(o,s)?l(null):l(E):l(null)},r.onerror=function(){l(R)},e.cancel=function(){l(d)},0<u&&(c=setTimeout(function(){l(x)},u))}},{24:24,27:27,31:31,36:36,7:7}],19:[function(n,e,t){e.exports=function(n){for(var e;(e=n.jsonpCallbackName.call(null,n))in window;);return window[e]=null,e}},{}],20:[function(n,e,t){var o=n(22);e.exports=function(n,e){var t=n.query,r=n.jsonp;return t||(t={},n.query=t),t[r]=e,o(n)}},{22:22}],21:[function(n,e,t){var s=n(37),u=n(36),c=n(29);e.exports=function(n,e,t){var r,o,i=e[t];if(s(i))for(r in i)if(c.call(i,r)&&(o=i[r],u(o))){if(r in n)throw new Error('mixin name conflict "'+r+'"');n[r]=o}}},{29:29,36:36,37:37}],22:[function(n,e,t){var c=n(36),l=n(34),a=n(37);e.exports=function(n){var e,t=n.url,r=n.baseURL,o=n.model,i=n.query,s=n.compileURL,u=n.encodeQueryString;return null==t&&(t=""),l(t=""+t)||(null==r&&(r=""),t=r+t),a(o)&&c(s)&&(t=s(t,o,n)),a(i)&&c(u)&&(i=u(i,n),-1<(t=(e=t.split("#"))[0]).indexOf("?")?"&"===t.charAt(t.length-1)?t+=i:t=t+"&"+i:t=t+"?"+i,e[0]=t,t=e.join("#")),t}},{34:34,36:36,37:37}],23:[function(n,e,t){var r=n(36);e.exports=function(n,e){var t=n.onRequestCreated;r(t)&&t(e)}},{36:36}],24:[function(n,e,t){t.ERR_ABORTED="ERR_ABORTED",t.ERR_RESPONSE="ERR_RESPONSE",t.ERR_CANCELED="ERR_CANCELED",t.ERR_NETWORK="ERR_NETWORK",t.ERR_TIMEOUT="ERR_TIMEOUT",t.HTTP_REQUEST="HTTP_REQUEST",t.JSONP_REQUEST="JSONP_REQUEST"},{}],25:[function(n,e,t){var r=n(1);e.exports=function(){return new r}},{1:1}],26:[function(n,e,t){var r=n(39),o=n(24),i=n(32),s=n(33),u=o.HTTP_REQUEST;e.exports=function(){return{method:"GET",baseURL:"",url:"",model:null,query:null,headers:null,body:null,timeout:0,jsonpDefaultTimeout:6e4,cors:!1,noCache:!1,noCacheHeaders:{Pragma:"no-cache","Cache-Control":"no-cache, no-store, must-revalidate"},jsonp:"callback",settings:{},controller:null,requestFunctionName:null,requestType:null,xhrProps:null,username:null,password:null,httpRequestBodyProcessor:{raw:{priority:0,headers:null,processor:null},form:{priority:1,headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},processor:function(n,e){return r(n)}},json:{priority:2,headers:{"Content-Type":"application/json; charset=UTF-8"},processor:function(n,e){return JSON.stringify(n)}}},httpResponseMixin:{json:function(){var n=this.request.xhr.responseText;return n?JSON.parse(n):null},text:function(){return this.request.xhr.responseText},status:function(){return this.request.xhr.status}},jsonpResponseMixin:{json:function(){return this.request.responseJSON}},httpResponseErrorMixin:null,jsonpResponseErrorMixin:null,handleOptions:null,createXHR:function(n){return new XMLHttpRequest},createScript:function(n){var e=document.createElement("script");return e.setAttribute("type","text/javascript"),e.setAttribute("charset","utf-8"),e},jsonpContainerNode:function(n){return document.head||document.getElementsByName("head")[0]},jsonpCallbackName:function(n){return"jsonp_"+s()+"_"+(new Date).getTime()},compileURL:function(n,e,t){return i(n,e)},encodeQueryString:function(n,e){return r(n)},onXhrCreated:null,onXhrOpened:null,onXhrSent:null,onRequestCreated:null,isResponseOk:function(n,e){var t;return n!==u||(200<=(t=+e.request.xhr.status)&&t<300||304==t)},transformError:null,transformResponse:null,shouldCallErrorCallback:null,shouldCallSuccessCallback:null}}},{24:24,32:32,33:33,39:39}],27:[function(n,e,t){var E=n(36),x=n(5),y=n(8),v=n(24).HTTP_REQUEST;e.exports=function(n,e){var t=e.request,r=t.requestType,o=t.options,i=t.onsuccess,s=t.onerror,u=o.shouldCallErrorCallback,c=o.shouldCallSuccessCallback,l=o.transformError,a=o.transformResponse,p=null,f=!0,h=!0,d=null,R=null;n?(p=r===v?new x(n,t):new y(n,t),d=E(l)?l(r,p):p,E(u)&&(f=u(r,d,p)),f&&E(s)&&s(d)):(R=E(a)?a(r,e):e,E(c)&&(h=c(r,R,e)),h&&E(i)&&i(R))}},{24:24,36:36,5:5,8:8}],28:[function(n,e,t){var r=n(36);e.exports=function(n){r(n.handleOptions)&&n.handleOptions(n)}},{36:36}],29:[function(n,e,t){e.exports=Object.prototype.hasOwnProperty},{}],30:[function(n,e,t){e.exports=function(n,e){function t(){}t.prototype=e.prototype,n.prototype=new t,n.prototype.constructor=n}},{}],31:[function(n,e,t){e.exports=function(){}},{}],32:[function(n,e,t){var E=1,x=2;e.exports=function(e,n,t){var r=n||{},o=t||{},i=o.encode||encodeURIComponent,s=function(n,e){var t,r,o=[],i=0,s=n.length;for(o.push("var __o=[]"),o.push("with(__s){");i<s;++i)(r=n[i]).type===E?o.push("__o.push("+JSON.stringify(r.text)+")"):r.type===x&&r.text&&(r.raw?o.push("__o.push("+r.text+")"):o.push("__o.push(__e("+r.text+"))"));return o.push("}"),o.push('return __o.join("")'),t=new Function("__s","__e",o.join("\n")),function(n){return t(n,function(n){return null==n?"":e(n+"")})}}(function(n,e,t,r){var o,i=n,s=h(e),u=h(t),c="Unexpected end",l=E,a=[],p=[],f=[];function h(n){var e=n.replace(/[\\|{}()[\].*+?^$]/g,function(n){return"\\"+n});return new RegExp("(\\\\*)"+e)}function d(){f.push({type:E,text:a.join("")}),a=[]}function R(n,e,t){var r=n[1]||"",o=r.length;return o%2==0?(o&&t.push(r.substr(o/2)),!0):(1<o&&t.push(r.substr((o+1)/2)),t.push(e),!1)}for(;i.length;)if(l===E)if(o=s.exec(i)){if(a.push(i.substr(0,o.index)),i=i.substr(o.index+o[0].length),R(o,e,a)&&(d(),l=x,!i))throw new Error(c)}else a.push(i),d(),i="";else{if(!(o=u.exec(i)))throw new Error(c);p.push(i.substr(0,o.index)),i=i.substr(o.index+o[0].length),R(o,t,p)&&(f.push(r(p.join(""))),p=[],l=E)}return f}(null==e?"":e+"",o.openingTag||"{",o.closingTag||"}",function(n){var e=n.charAt(0),t=n.charAt(1),r=!1;return"-"===e&&" "===t&&(r=!0,n=n.substr(2)),n=n.replace(/^\s+|\s+$/g,""),{type:x,text:n,raw:r}}),i);try{return s(r)}catch(n){throw new Error("Compile Error:\n\n"+e+"\n\n"+n.message)}}},{}],33:[function(n,e,t){var r=0;e.exports=function(){return r+=1}},{}],34:[function(n,e,t){e.exports=function(n){return/^(?:[a-z][a-z0-9\-\.\+]*:)?\/\//i.test(n)}},{}],35:[function(n,e,t){var r=Object.prototype.toString;e.exports=function(n){return"[object Array]"===r.call(n)}},{}],36:[function(n,e,t){var r=Object.prototype.toString;e.exports=function(n){return"[object Function]"===r.call(n)}},{}],37:[function(n,e,t){var r=Object.prototype.toString,o=Object.getPrototypeOf;o=o||function(n){return n.__proto__},e.exports=function(n){var e;return"[object Object]"===r.call(n)&&(!(e=o(n))||e===o({}))}},{}],38:[function(n,e,t){var o=n(35),i=n(37),s=Object.prototype.hasOwnProperty,u=Array.prototype.slice;function c(n,e){var t,r;if(n&&(o(e)||i(e)))for(t in e)s.call(e,t)&&void 0!==(r=e[t])&&(i(r)?(i(n[t])||(n[t]={}),l(n[t],r)):o(r)?(o(n[t])||(n[t]=[]),l(n[t],r)):n[t]=r);return n}function l(n,e){var t=0,r=arguments.length-1;for(e=u.call(arguments,1),t=0;t<r;t+=1)c(n,e[t]);return n}e.exports=l},{35:35,37:37}],39:[function(n,e,t){var r=n(40),c=r.isArray,l=r.isObject,a=Object.prototype.hasOwnProperty;function p(n,e,t,r){var o,i,s,u;if(l(e))for(o in e)a.call(e,o)&&p(n+"["+o+"]",u=e[o],t,r);else if(c(e))for(i=0,s=e.length;i<s;)u=e[i],p(r||c(u)||l(u)?n+"["+i+"]":n+"[]",u,t,r),i+=1;else n=encodeURIComponent(n),u=null===e?n:(void 0===e?e="":"string"!=typeof e&&(e=""+e),n+"="+encodeURIComponent(e)),t.push(u)}e.exports=function(n,e){var t,r=[];if(e=!!e,l(n))for(t in n)a.call(n,t)&&p(t,n[t],r,e);return r.join("&")}},{40:40}],40:[function(n,e,t){var r=Object.prototype.toString;t.isArray=function(n){return"[object Array]"===r.call(n)},t.isObject=function(n){return"[object Object]"===r.call(n)}},{}]},{},[2])(2)});