x-http-client
Version:
An http client to make it easier to send requests (including JSONP requests) to the server.
1 lines • 15.9 kB
JavaScript
!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 s(i,u,c){function l(e,n){if(!u[e]){if(!i[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:{}};i[e][0].call(o.exports,function(n){return l(i[e][1][n]||n)},o,o.exports,s,i,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(38);function o(){this.cancelled=!1,this.callbacks=[]}o.prototype.cancel=function(){var n=this.callbacks,e=0,t=n.length;if(!1===this.cancelled)for(this.cancelled=!0;e<t;e+=1)try{n[e]()}catch(n){!function(n){setTimeout(function(){throw n})}(n)}},o.prototype.isCancelled=function(){return this.cancelled},o.prototype.registerCancelCallback=function(n){r(n)&&this.callbacks.push(n)},e.exports=o},{38:38}],2:[function(n,e,t){var r=n(43),o=n(40),s=n(38),i=n(39),u=n(36),c=n(35),l=n(33),a=n(34),p=n(32),f=n(25),h=n(28),d=n(27),R=n(26),E=n(9),y=n(3),v=n(6),x=n(10),O=n(4),m=n(7),C=n(11),T=n(5),b=n(8),w=n(1),_="0.0.1-alpha.5";function g(n,e,t){var r=d();i(n)&&o(r,n),s(e)&&(e(r),r=o({},r)),s(t)||(t=l),this.copyOptions=function(){return o({},r)},this.mergeOptions=function(n){var e=o({},r,n);return t(e),e}}g.prototype.send=function(n,e,t){var r=this.mergeOptions(n);return r.requestFunctionName="send",r.controller=null,new y(r,e,t)},g.prototype.fetch=function(n){var r=this.mergeOptions(n),o=r.controller;return r.requestFunctionName="fetch",new Promise(function(e,n){var t=new y(r,function(n){o&&o.isCancelled()||e(n)},n);o&&(o.isCancelled()?t.cancel():o.registerCancelCallback(function(){t.cancel()}))})},g.prototype.getJSONP=function(n,e,t){var r=this.mergeOptions(n);return r.requestFunctionName="getJSONP",r.controller=null,new v(r,e,t)},g.prototype.fetchJSONP=function(n){var r=this.mergeOptions(n),o=r.controller;return r.requestFunctionName="fetchJSONP",new Promise(function(e,n){var t=new v(r,function(n){o&&o.isCancelled()||e(n)},n);o&&(o.isCancelled()?t.cancel():o.registerCancelCallback(function(){t.cancel()}))})},g.prototype.createCancelController=R,g.createCancelController=R,g.version=_,g.prototype.version=_,h(g,"constants",o({},f)),h(g,"libs",{QS:r}),h(g,"classes",{CancelController:w,HttpClient:g,HttpRequest:y,HttpResponse:O,HttpResponseError:T,JSONPRequest:v,JSONPResponse:m,JSONPResponseError:b,Request:E,Response:x,ResponseError:C}),h(g,"functions",{template:a,merge:o,isAbsoluteURL:u,isFunction:s,isPlainObject:i,uuid:c,noop:l,inherits:p,createDefaultOptions:d}),e.exports=g},{1:1,10:10,11:11,25:25,26:26,27:27,28:28,3:3,32:32,33:33,34:34,35:35,36:36,38:38,39:39,4:4,40:40,43:43,5:5,6:6,7:7,8:8,9:9}],3:[function(n,e,t){var i=n(9),u=n(25),r=n(32),c=n(23),l=n(30),a=n(24),p=n(12),f=n(17),h=n(15),d=n(16),R=n(14);function o(n,e,t){var r,o,s;i.call(this,u.HTTP_REQUEST,n,e,t),l(n),r=this.xhr=n.createXHR.call(null,n),o=d(n),s=c(n),f(r,n),R(n.onXhrCreated,r,n),r.open(n.method||"GET",s,!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,i),e.exports=o},{12:12,14:14,15:15,16:16,17:17,23:23,24:24,25:25,30:30,32:32,9:9}],4:[function(n,e,t){var r=n(10),o=n(32),s=n(22);function i(n){r.call(this,n),s(this,n.options,"httpResponseParser")}o(i,r),e.exports=i},{10:10,22:22,32:32}],5:[function(n,e,t){var r=n(11),o=n(32),s=n(22);function i(n,e){r.call(this,n,e),s(this,e.options,"httpResponseErrorParser")}o(i,r),e.exports=i},{11:11,22:22,32:32}],6:[function(n,e,t){var u=n(9),c=n(25),r=n(32),l=n(30),a=n(24),p=n(18),f=n(19),h=n(21),d=n(20);function o(n,e,t){var r,o,s,i;u.call(this,c.JSONP_REQUEST,n,e,t),l(n),o=this.script=n.createScript.call(null,n),i=n.jsonpContainerNode.call(null,n),s=f(n),r=d(n,s),o.setAttribute("src",r),h(o,n),p(this,s),i.appendChild(o),a(n,this)}r(o,u),e.exports=o},{18:18,19:19,20:20,21:21,24:24,25:25,30:30,32:32,9:9}],7:[function(n,e,t){var r=n(10),o=n(32),s=n(22);function i(n){r.call(this,n),s(this,n.options,"jsonpResponseParser")}o(i,r),e.exports=i},{10:10,22:22,32:32}],8:[function(n,e,t){var r=n(11),o=n(32),s=n(22);function i(n,e){r.call(this,n,e),s(this,e.options,"jsonpResponseErrorParser")}o(r,i),e.exports=i},{11:11,22:22,32:32}],9:[function(n,e,t){var o=n(35);e.exports=function(n,e,t,r){this.errorCode=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}},{35:35}],10:[function(n,e,t){e.exports=function(n){this.request=n}},{}],11:[function(n,e,t){var r={ERR_ABORTED:"Request aborted",ERR_CANCELLED:"Request cancelled",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.errorCode=n,this.code=n,this.request=e,this.message=t}},{}],12:[function(n,e,t){var p=n(38),f=n(4),h=n(13),d=n(29),R=n(33),r=n(25),E=r.ERR_ABORTED,y=r.ERR_CANCELLED,v=r.ERR_NETWORK,x=r.ERR_RESPONSE,O=r.ERR_TIMEOUT;e.exports=function(e){var n=e.xhr,t=e.options,r=e.requestType,o=new f(e),s=t.isResponseOk,i=null,u=parseInt(t.timeout||0,10),c=function(){if(l(),n.abort)try{n.abort()}catch(n){}a(y)},l=function(){l=R,n.onabort=null,n.onerror=null,n.onreadystatechange=null,n.ontimeout=null,i&&(i(),i=null)},a=function(n){c=a=R,e.finished=!0,l(),d(n,o)};n.onabort=function(){a(E)},n.onerror=function(){a(v)},n.onreadystatechange=function(){4==+n.readyState&&(p(s)?s(r,o)?a(null):a(x):a(null))},e.cancel=function(){c()},!isNaN(u)&&0<u&&(i=h(n,u,function(){if(l(),n.abort)try{n.abort()}catch(n){}a(O)}))}},{13:13,25:25,29:29,33:33,38:38,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(38);e.exports=function(n,e,t){r(n)&&n(e,t)}},{38:38}],15:[function(n,e,t){var s=n(40),i=n(39),u=n(31);e.exports=function(n,e){var t,r,o=i(e.headers)?e.headers:{};for(t in e.noCache&&i(e.noCacheHeaders)&&(o=s(o,e.noCacheHeaders)),o)u.call(o,t)&&null!=(r=o[t])&&n.setRequestHeader(t,r);e.headers=o}},{31:31,39:39,40:40}],16:[function(n,e,t){var p=n(40),f=n(38),h=n(39),d=n(31);e.exports=function(n){var e,t,r,o,s,i=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]){i=c[o.key],s=o;break}s&&(h(s.headers)&&(a=p(a,s.headers)),o=s.processor,f(o)&&(i=o(i,n)))}return n.headers=a,i}},{31:31,38:38,39:39,40:40}],17:[function(n,e,t){var o=n(39),s=n(31);e.exports=function(n,e){var t,r=e.xhrProps;if(e.cors&&(n.withCredentials=!0),o(r))for(t in r)s.call(r,t)&&(n[t]=r[t])}},{31:31,39:39}],18:[function(n,e,t){var a=n(38),p=n(7),f=n(29),h=n(33),r=n(25),d=r.ERR_CANCELLED,R=r.ERR_NETWORK,E=r.ERR_RESPONSE,y=r.ERR_TIMEOUT;e.exports=function(e,t){var r=e.script,n=e.options,o=e.requestType,s=n.isResponseOk,i=new p(e),u=parseInt(n.timeout||0,10),c=null,l=function(n){l=h,e.finished=!0,window[t]=h,(r.onerror=null)!==c&&(clearTimeout(c),c=null),f(n,i)};window[t]=function(n){e.responseJSON=n,a(s)?s(o,i)?l(null):l(E):l(null)},r.onerror=function(){l(R)},e.cancel=function(){l(d)},!isNaN(u)&&0<u&&(c=setTimeout(function(){l(y)},u))}},{25:25,29:29,33:33,38:38,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(23);e.exports=function(n,e){var t=n.query,r=n.jsonp;return t||(t={},n.query=t),t[r]=e,o(n)}},{23:23}],21:[function(n,e,t){e.exports=function(n,e){e.cors&&n.setAttribute("crossorigin","use-credentials")}},{}],22:[function(n,e,t){var i=n(39),u=n(38),c=n(31);e.exports=function(n,e,t){var r,o,s=e[t];if(i(s))for(r in s)if(c.call(s,r)&&(o=s[r],u(o))){if(r in n)throw new Error('"'+r+'" cannot be a name of parser');n[r]=o}}},{31:31,38:38,39:39}],23:[function(n,e,t){var c=n(38),l=n(36),a=n(39);e.exports=function(n){var e,t=n.url+"",r=n.baseURL,o=n.model,s=n.query,i=n.compileURL,u=n.encodeQueryString;return l(t)||"string"==typeof r&&(t=r+t),a(o)&&c(i)&&(t=i(t,o,n)),a(s)&&c(u)&&(s=u(s,n),-1<(t=(e=t.split("#"))[0]).indexOf("?")?"&"===t.charAt(t.length-1)?t+=s:t=t+"&"+s:t=t+"?"+s,e[0]=t,t=e.join("#")),t}},{36:36,38:38,39:39}],24:[function(n,e,t){var r=n(38);e.exports=function(n,e){var t=n.onRequestCreated;r(t)&&t(e)}},{38:38}],25:[function(n,e,t){t.ERR_ABORTED="ERR_ABORTED",t.ERR_RESPONSE="ERR_RESPONSE",t.ERR_CANCELLED="ERR_CANCELLED",t.ERR_NETWORK="ERR_NETWORK",t.ERR_TIMEOUT="ERR_TIMEOUT",t.HTTP_REQUEST="HTTP_REQUEST",t.JSONP_REQUEST="JSONP_REQUEST"},{}],26:[function(n,e,t){var r=n(1);e.exports=function(){return new r}},{1:1}],27:[function(n,e,t){var r=n(43),o=n(25),s=n(34),i=n(35),u=o.HTTP_REQUEST;e.exports=function(){return{method:"GET",baseURL:null,url:null,model:null,query:null,headers:null,body:null,timeout:0,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.encode(n)}},json:{priority:2,headers:{"Content-Type":"application/json; charset=UTF-8"},processor:function(n,e){return JSON.stringify(n)}}},httpResponseParser:{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}},jsonpResponseParser:{json:function(){return this.request.responseJSON}},httpResponseErrorParser:null,jsonpResponseErrorParser: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_"+i()+"_"+(new Date).getTime()},compileURL:function(n,e,t){return s(n,e)},encodeQueryString:function(n,e){return r.encode(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}}},{25:25,34:34,35:35,43:43}],28:[function(n,e,t){e.exports=function(n,e,t){n.prototype.exports=n.exports=n.exports||{},n.exports[e]=t}},{}],29:[function(n,e,t){var E=n(38),y=n(5),v=n(8),x=n(25).HTTP_REQUEST;e.exports=function(n,e){var t=e.request,r=t.requestType,o=t.options,s=t.onsuccess,i=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===x?new y(n,t):new v(n,t),d=E(l)?l(r,p):p,E(u)&&(f=u(r,d,p)),f&&E(i)&&i(d)):(R=E(a)?a(r,e):e,E(c)&&(h=c(r,R,e)),h&&E(s)&&s(R))}},{25:25,38:38,5:5,8:8}],30:[function(n,e,t){var r=n(38);e.exports=function(n){r(n.handleOptions)&&n.handleOptions(n)}},{38:38}],31:[function(n,e,t){e.exports=Object.prototype.hasOwnProperty},{}],32:[function(n,e,t){e.exports=function(n,e){function t(){}t.prototype=e.prototype,n.prototype=new t,n.prototype.constructor=n}},{}],33:[function(n,e,t){e.exports=function(){}},{}],34:[function(n,e,t){var E=1,y=2;e.exports=function(e,n,t){var r=e+"",o=n||{},s=t||{},i=s.start||"{",u=s.end||"}",c=s.encode||encodeURIComponent,l=function(n,e){var t,r,o=[],s=0,i=n.length;for(o.push("var __o=[]"),o.push("with(__s){");s<i;++s)(r=n[s]).type===E?o.push("__o.push("+JSON.stringify(r.text)+")"):r.type===y&&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(e,n,t,r){function o(n){return e.charAt(n)}function s(n,e){for(var t=n.length;"\\"===o(u);)if("\\"===o(++u))e.push("\\"),++u;else{if(!R(n)){e.push("\\");break}e.push(n),u+=t}}function i(){f.length&&(p.push({type:E,text:f.join("")}),f=[])}var u=0,c=e.length,l=n.length,a=t.length,p=[],f=[],h=[],d=E,R=function(n){for(var e=0,t=u,r=n.length;e<r&&t<c;){if(n.charAt(e)!==o(t))return 0;++e,++t}return 1};for(;u<c;)d===E?(s(n,f),R(n)?(d=y,u+=l):(f.push(o(u)),++u)):d===y&&(s(t,h),R(t)?(d=E,u+=a,i(),p.push(r(h.join(""))),h=[]):(h.push(o(u)),++u));if(d!==y)return i(),p;throw new Error("Unexpected end")}(r,i,u,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:y,text:n,raw:r}}),c);try{return l(o)}catch(n){throw new Error("Compile Error:\n\n"+e+"\n\n"+n.message)}}},{}],35:[function(n,e,t){var r=0;e.exports=function(){return r+=1}},{}],36:[function(n,e,t){e.exports=function(n){return/^(?:[a-z][a-z0-9\-\.\+]*:)?\/\//i.test(n)}},{}],37:[function(n,e,t){var r=Object.prototype.toString;e.exports=function(n){return"[object Array]"===r.call(n)}},{}],38:[function(n,e,t){var r=Object.prototype.toString;e.exports=function(n){return"[object Function]"===r.call(n)}},{}],39:[function(n,e,t){var r=Object.prototype.toString;e.exports=function(n){return!!n&&(("undefined"==typeof window||n!==window)&&(("undefined"==typeof global||n!==global)&&"[object Object]"===r.call(n)))}},{}],40:[function(n,e,t){var o=n(37),s=n(39),i=Object.prototype.hasOwnProperty,u=Array.prototype.slice;function c(n,e){var t,r;if(n&&(o(e)||s(e)))for(t in e)i.call(e,t)&&void 0!==(r=e[t])&&(s(r)?(s(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},{37:37,39:39}],41:[function(n,e,t){var f=n(44).isArray,h=function(n,e,t,r){var o,s,i,u,c,l,a,p=/\[([^\[]*?)?\]$/;if(p.test(t)){if(o=RegExp.$1,i=e[s=t.replace(p,"")],u=""===o,c=/(^0$)|(^[1-9]\d*$)/.test(o)||u,i){if(!c&&f(i))for(a=(l=i).length,i={};a--;)void 0!==i[a]&&(i[a]=l[a])}else i=c?[]:{};u&&f(i)?i.push(r):i[o]=r,e[s]=i,h(n,e,s,i)}else n[t]=r};t.decode=function(n){var e,t,r,o,s,i,u={},c={};if(n&&"string"==typeof n)for(t=0,r=(e=n.split("&")).length;t<r;)o=e[t].split("="),s=decodeURIComponent(o[0]),i="string"==typeof(i=o[1])?decodeURIComponent(i):null,h(u,c,s,i),t+=1;return u}},{44:44}],42:[function(n,e,t){var r=n(44),l=r.isArray,a=r.isObject,p=Object.prototype.hasOwnProperty,f=function(n,e,t,r){var o,s,i,u,c;if(a(e))for(o in e)p.call(e,o)&&(u=e[o],f(c=n+"["+o+"]",u,t,r));else if(l(e))for(s=0,i=e.length;s<i;)u=e[s],c=r||l(u)||a(u)?n+"["+s+"]":n+"[]",f(c,u,t,r),s+=1;else n=encodeURIComponent(n),u=null===e?n:(void 0===e?e="":"string"!=typeof e&&(e=""+e),n+"="+encodeURIComponent(e)),t.push(u)};t.encode=function(n,e){var t,r=[];if(e=!!e,a(n))for(t in n)p.call(n,t)&&f(t,n[t],r,e);return r.join("&")}},{44:44}],43:[function(n,e,t){var r=n(42).encode,o=n(41).decode;t.encode=r,t.decode=o,t.version="1.1.2"},{41:41,42:42}],44:[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)});