bluejax
Version:
jQuery AJAX wrapped in Bluebird promises.
8 lines (7 loc) • 3.26 kB
JavaScript
/**
* @author Louis-Dominique Dubeau
* @license MPL 2.0
* @copyright 2016 Louis-Dominique Dubeau
*/
!function(r,t){"use strict";"function"==typeof define&&define.amd?define(["bluejax.try","jquery","bluebird"],t):"object"==typeof module&&module.exports?module.exports=t(require("bluejax.try"),require("jquery"),require("bluebird")):r.bluejax=t(r.bluejax["try"],jQuery,Promise)}(this,function(r,t,e){"use strict";function n(r,t){r.prototype=Object.create(t.prototype),r.prototype.constructor=r}function o(r,t){try{Object.defineProperty(r,"name",{value:t})}catch(e){if(!(e instanceof TypeError))throw e}r.prototype.name=t}function i(r,t,e,n){if(this.jqXHR=r,this.textStatus=t,this.errorThrown=e,Error.captureStackTrace)Error.captureStackTrace(this,this.constructor);else{var o=new Error;if(o.stack)this.stack=o.stack;else try{throw o}catch(i){this.stack=i.stack}}var a="Ajax operation failed";e?a+=": "+e+" ("+r.status+")":t&&(a+=": "+t),a+=".",n&&(a+=" Called with: "+JSON.stringify(n)),this.message=a}function a(r,t,e,n){var o=k[t];return o||(o=k[0!==r.status?"http":"ajax"]),new o(r,t,e,n)}function u(r,t){i.call(this),this.message=r,this.originalError=t}function c(r){u.call(this,"your browser is offline",r)}function s(r){u.call(this,"the server appears to be down",r)}function f(r){u.call(this,"the network appears to be down",r)}function l(r){return r+=r.indexOf("?")<0?"?":"&_=",r+Date.now()}function h(r){return"/"===r[r.length-1]&&r.indexOf("?")<0&&(r+="favicon.ico"),r}function p(r,t){var n=t.knownServers;if(!n||0===n.length)throw new s(r);return e.all(n.map(function(r){return d({url:l(h(r)),timeout:1e3}).reflect()})).filter(function(r){return r.isFulfilled()}).then(function(t){if(0===t.length)throw new f(r);throw new s(r)})}function w(r,t){if("onLine"in navigator&&!navigator.onLine)throw new c(r);var e,n=t.serverURL;return e=n?d({url:l(h(n))})["catch"](function(){return p(r,t)}):p(r,t),e.then(function(){throw r})}function v(r){return!(r instanceof g.HttpError||r instanceof g.ParserError||r instanceof g.AbortError)}function y(t,n,o,i){var u,c=new e(function(e,c){function s(r,t,n){e(i.verboseResults?[r,t,n]:r)}function f(r,n,o){var u=a(r,n,o,i.verboseExceptions?t:null);if(v(u)){var s=i.diagnose;s&&s.on?e(w(u,s)):c(u)}else c(u)}u=r.perform.call(this,n,o,i),u.fail(f).done(s)});return{xhr:u,promise:c}}function E(e,n,o){var i=n?[e,n]:[e],a=n||e,u=r.extractBluejaxOptions(i),c=t.extend({},o,u[0]),s=u[1];return y(i,a,s,c)}function x(r,t,e){return E(r,t,e).promise}function d(r,t){return x(r,t)}function j(r,t){return E(r,t)}function b(r,t){return function(e,n){var o=E(e,n,r);return t?o[t]:o}}n(i,Error),o(i,"GeneralAjaxError");for(var m=["timeout","abort","parsererror","ajax","http"],k={},g={},O=0;O<m.length;++O){var S,q=m[O];S="parsererror"!==q?q[0].toUpperCase()+q.slice(1)+"Error":"ParserError";var D=function(){i.apply(this,arguments)};k[q]=D,g[S]=D,n(D,i),o(D,S)}g.ConnectivityError=u,n(u,i),o(u,"ConnectivityError"),g.BrowserOfflineError=c,n(c,u),o(c,"BrowserOfflineError"),g.ServerDownError=s,n(s,u),o(s,"ServerDownError"),g.NetworkDownError=f,n(f,u),o(f,"NetworkDownError");var A={"try":r,ajax:d,ajax$:j,GeneralAjaxError:i,make:b};A.version="1.0.0";for(var C in g)A[C]=g[C];return A});
//# sourceMappingURL=bluejax.min.js.map