qwest
Version:
Ajax library with XHR2, promises and request limit
1 lines • 7.86 kB
JavaScript
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.qwest=e()}}(function(){var e;return function t(e,n,o){function r(s,a){if(!n[s]){if(!e[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(i)return i(s,!0);var p=new Error("Cannot find module '"+s+"'");throw p.code="MODULE_NOT_FOUND",p}var f=n[s]={exports:{}};e[s][0].call(f.exports,function(t){var n=e[s][1][t];return r(n?n:t)},f,f.exports,t,e,n,o)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<o.length;s++)r(o[s]);return r}({1:[function(t,n,o){!function(t){"use strict";var o=function(e){var t=function(e,t,n){n="function"==typeof n?n():null===n?"":void 0===n?"":n,e[e.length]=encodeURIComponent(t)+"="+encodeURIComponent(n)},n=function(e,o,r){var i,s,a;if("[object Array]"===Object.prototype.toString.call(o))for(i=0,s=o.length;i<s;i++)n(e+"["+("object"==typeof o[i]?i:"")+"]",o[i],r);else if(o&&"[object Object]"===o.toString())for(a in o)o.hasOwnProperty(a)&&(e?n(e+"["+a+"]",o[a],r,t):n(a,o[a],r,t));else if(e)t(r,e,o);else for(a in o)t(r,a,o[a]);return r};return n("",e,[]).join("&").replace(/%20/g,"+")};"object"==typeof n&&"object"==typeof n.exports?n.exports=o:"function"==typeof e&&e.amd?e([],function(){return o}):t.param=o}(this)},{}],2:[function(e,t,n){!function(e){function t(e){return"function"==typeof e}function n(e){return"object"==typeof e}function o(e){"undefined"!=typeof setImmediate?setImmediate(e):"undefined"!=typeof process&&process.nextTick?process.nextTick(e):setTimeout(e,0)}var r;e[0][e[1]]=function i(e){var s,a=[],u=[],p=function(e,t){return null==s&&null!=e&&(s=e,a=t,u.length&&o(function(){for(var e=0;e<u.length;e++)u[e]()})),s};return p.then=function(p,f){var c=i(e),l=function(){function e(o){var i,s=0;try{if(o&&(n(o)||t(o))&&t(i=o.then)){if(o===c)throw new TypeError;i.call(o,function(){s++||e.apply(r,arguments)},function(e){s++||c(!1,[e])})}else c(!0,arguments)}catch(a){s++||c(!1,[a])}}try{var o=s?p:f;t(o)?e(o.apply(r,a||[])):c(s,a)}catch(i){c(!1,[i])}};return null!=s?o(l):u.push(l),c},e&&(p=e(p)),p}}("undefined"==typeof t?[window,"pinkySwear"]:[t,"exports"])},{}],qwest:[function(e,t,n){t.exports=function(){var t="undefined"!=typeof window?window:self,n=e("pinkyswear"),o=e("jquery-param"),r={},i="json",s="post",a=null,u=0,p=[],f=t.XMLHttpRequest?function(){return new t.XMLHttpRequest}:function(){return new ActiveXObject("Microsoft.XMLHTTP")},c=""===f().responseType,l=function(e,l,d,y,h){e=e.toUpperCase(),d=void 0===d?null:d,y=y||{};for(var m in r)if(!(m in y))if("object"==typeof r[m]&&"object"==typeof y[m])for(var T in r[m])y[m][T]=r[m][T];else y[m]=r[m];var w,v,x,g,b,q=!1,j=!1,C=!1,O=0,D={},E={text:"*/*",xml:"text/xml",json:"application/json",post:"application/x-www-form-urlencoded",document:"text/html"},M={text:"*/*",xml:"application/xml; q=1.0, text/xml; q=0.8, */*; q=0.1",json:"application/json; q=1.0, text/*; q=0.8, */*; q=0.1"},S=!1,X=n(function(n){return n.abort=function(){C||(v&&4!=v.readyState&&v.abort(),S&&(--u,S=!1),C=!0)},n.send=function(){if(!S){if(u==a)return void p.push(n);if(C)return void(p.length&&p.shift().send());if(++u,S=!0,v=f(),w&&("withCredentials"in v||!t.XDomainRequest||(v=new XDomainRequest,j=!0,"GET"!=e&&"POST"!=e&&(e="POST"))),j?v.open(e,l):(v.open(e,l,y.async,y.user,y.password),c&&y.async&&(v.withCredentials=y.withCredentials)),!j)for(var o in D)D[o]&&v.setRequestHeader(o,D[o]);if(c&&"auto"!=y.responseType)try{v.responseType=y.responseType,q=v.responseType==y.responseType}catch(r){}c||j?(v.onload=L,v.onerror=R,j&&(v.onprogress=function(){})):v.onreadystatechange=function(){4==v.readyState&&L()},y.async?"timeout"in v?(v.timeout=y.timeout,v.ontimeout=k):x=setTimeout(k,y.timeout):j&&(v.ontimeout=function(){}),"auto"!=y.responseType&&"overrideMimeType"in v&&v.overrideMimeType(E[y.responseType]),h&&h(v),j?setTimeout(function(){v.send("GET"!=e?d:null)},0):v.send("GET"!=e?d:null)}},n}),L=function(){var e;if(S=!1,clearTimeout(x),p.length&&p.shift().send(),!C){--u;try{if(q){if("response"in v&&null===v.response)throw"The request response is empty";b=v.response}else{if(e=y.responseType,"auto"==e)if(j)e=i;else{var n=v.getResponseHeader("Content-Type")||"";e=n.indexOf(E.json)>-1?"json":n.indexOf(E.xml)>-1?"xml":"text"}switch(e){case"json":if(v.responseText.length)try{b="JSON"in t?JSON.parse(v.responseText):new Function("return ("+v.responseText+")")()}catch(o){throw"Error while parsing JSON body : "+o}break;case"xml":try{t.DOMParser?b=(new DOMParser).parseFromString(v.responseText,"text/xml"):(b=new ActiveXObject("Microsoft.XMLDOM"),b.async="false",b.loadXML(v.responseText))}catch(o){b=void 0}if(!b||!b.documentElement||b.getElementsByTagName("parsererror").length)throw"Invalid XML";break;default:b=v.responseText}}if("status"in v&&!/^2|1223/.test(v.status))throw v.status+" ("+v.statusText+")";X(!0,[v,b])}catch(o){X(!1,[o,v,b])}}},R=function(e){C||(e="string"==typeof e?e:"Connection aborted",X.abort(),X(!1,[new Error(e),v,null]))},k=function(){C||(y.attempts&&++O==y.attempts?R("Timeout ("+l+")"):(v.abort(),S=!1,X.send()))};if(y.async=!("async"in y)||!!y.async,y.cache="cache"in y&&!!y.cache,y.dataType="dataType"in y?y.dataType.toLowerCase():s,y.responseType="responseType"in y?y.responseType.toLowerCase():"auto",y.user=y.user||"",y.password=y.password||"",y.withCredentials=!!y.withCredentials,y.timeout="timeout"in y?parseInt(y.timeout,10):3e4,y.attempts="attempts"in y?parseInt(y.attempts,10):1,g=l.match(/\/\/(.+?)\//),w=g&&!!g[1]&&g[1]!=location.host,"ArrayBuffer"in t&&d instanceof ArrayBuffer?y.dataType="arraybuffer":"Blob"in t&&d instanceof Blob?y.dataType="blob":"Document"in t&&d instanceof Document?y.dataType="document":"FormData"in t&&d instanceof FormData&&(y.dataType="formdata"),null!==d)switch(y.dataType){case"json":d=JSON.stringify(d);break;case"post":case"queryString":d=o(d)}if(y.headers){var P=function(e,t,n){return t+n.toUpperCase()};for(g in y.headers)D[g.replace(/(^|-)([^-])/g,P)]=y.headers[g]}return"Content-Type"in D||"GET"==e||y.dataType in E&&E[y.dataType]&&(D["Content-Type"]=E[y.dataType]),D.Accept||(D.Accept=y.responseType in M?M[y.responseType]:"*/*"),w||"X-Requested-With"in D||(D["X-Requested-With"]="XMLHttpRequest"),y.cache||"Cache-Control"in D||(D["Cache-Control"]="no-cache"),"GET"!=e&&"queryString"!=y.dataType||!d||"string"!=typeof d||(l+=(/\?/.test(l)?"&":"?")+d),y.async&&X.send(),X},d=function(e){var t=[],o=0,r=[];return n(function(n){var i=-1,s=function(e){return function(s,a,u,p){var f=++i;return++o,t.push(l(e,n.base+s,a,u,p).then(function(e,t){r[f]=arguments,--o||n(!0,1==r.length?r[0]:[r])},function(){n(!1,arguments)})),n}};n.get=s("GET"),n.post=s("POST"),n.put=s("PUT"),n["delete"]=s("DELETE"),n["catch"]=function(e){return n.then(null,e)},n.complete=function(e){var t=function(){e()};return n.then(t,t)},n.map=function(e,t,n,o,r){return s(e.toUpperCase()).call(this,t,n,o,r)};for(var a in e)a in n||(n[a]=e[a]);return n.send=function(){for(var e=0,o=t.length;e<o;++e)t[e].send();return n},n.abort=function(){for(var e=0,o=t.length;e<o;++e)t[e].abort();return n},n})},y={base:"",get:function(){return d(y).get.apply(this,arguments)},post:function(){return d(y).post.apply(this,arguments)},put:function(){return d(y).put.apply(this,arguments)},"delete":function(){return d(y)["delete"].apply(this,arguments)},map:function(){return d(y).map.apply(this,arguments)},xhr2:c,limit:function(e){return a=e,y},setDefaultOptions:function(e){return r=e,y},setDefaultXdrResponseType:function(e){return i=e.toLowerCase(),y},setDefaultDataType:function(e){return s=e.toLowerCase(),y},getOpenRequests:function(){return u}};return y}()},{"jquery-param":1,pinkyswear:2}]},{},[1,2])("qwest")});