UNPKG

rx-dom-ajax

Version:
3 lines 5.72 kB
/* Copyright (c) Microsoft, Inc. All rights reserved. See License.txt in the project root for license information.*/ (function(a){function b(a){return a&&a.Object===Object?a:null}var c={"function":!0,object:!0},d=c[typeof exports]&&exports&&!exports.nodeType?exports:null,e=c[typeof module]&&module&&!module.nodeType?module:null,f=b(d&&e&&"object"==typeof global&&global),g=b(c[typeof self]&&self),h=b(c[typeof window]&&window),i=(e&&e.exports===d?d:null,b(c[typeof this]&&this)),j=f||h!==(i&&i.window)&&h||g||i||Function("return this")();"function"==typeof define&&define.amd?define(["rx"],function(b,c){return a(j,c,b)}):"object"==typeof module&&module&&module.exports===d?module.exports=a(j,module.exports,require("rx")):j.Rx=a(j,{},j.Rx)}).call(this,function(a,b,c,d){function e(){if(a.XMLHttpRequest)return new a.XMLHttpRequest;var b;try{for(var c=["Msxml2.XMLHTTP","Microsoft.XMLHTTP","Msxml2.XMLHTTP.4.0"],d=0;3>d;d++)try{if(b=c[d],new a.ActiveXObject(b))break}catch(e){}return new a.ActiveXObject(b)}catch(e){throw new Error("XMLHttpRequest is not supported by your browser")}}function f(){var b=new a.XMLHttpRequest;if("withCredentials"in b)return b.withCredentials=!0,b;if(a.XDomainRequest)return new XDomainRequest;throw new Error("CORS is not supported by your browser")}function g(a,b,c){var d="response"in b?b.response:b.responseText;return d="json"===c.responseType?JSON.parse(d):d,{response:d,status:b.status,responseType:b.responseType,xhr:b,originalEvent:a}}function h(a,b,c){return{type:c,status:b.status,xhr:b,originalEvent:a}}var i=(c.Observable,c.ObservableBase),j=c.DOM||(c.DOM={}),k={}.hasOwnProperty,l=c.internals.inherits,m=function(b){function c(a){this._settings=a,b.call(this)}function d(a,b){this._state=a,this._xhr=b,this.isDisposed=!1}return l(c,b),c.prototype.subscribeCore=function(b){var c,e={isDone:!1},f=this._settings,g=f.normalizeError,h=f.normalizeSuccess,i=function(a,c){var d=1223===a.status?204:a.status;d>=200&&300>=d||0===d||""===d?(b.onNext(h(c,a,f)),b.onCompleted()):b.onError(f.normalizeError(c,a,"error")),e.isDone=!0};try{c=f.createXHR()}catch(j){return b.onError(j)}try{f.user?c.open(f.method,f.url,f.async,f.user,f.password):c.open(f.method,f.url,f.async);var l=f.headers;for(var m in l)k.call(l,m)&&c.setRequestHeader(m,l[m]);c.timeout=f.timeout,c.ontimeout=function(a){f.progressObserver&&f.progressObserver.onError(a),b.onError(g(a,c,"timeout"))},c.upload||!("withCredentials"in c)&&a.XDomainRequest?(c.onload=function(a){f.progressObserver&&(f.progressObserver.onNext(a),f.progressObserver.onCompleted()),i(c,a)},f.progressObserver&&(c.onprogress=function(a){f.progressObserver.onNext(a)}),c.onerror=function(a){f.progressObserver&&f.progressObserver.onError(a),b.onError(g(a,c,"error")),e.isDone=!0},c.onabort=function(a){f.progressObserver&&f.progressObserver.onError(a),b.onError(g(a,c,"abort")),e.isDone=!0}):c.onreadystatechange=function(a){4===c.readyState&&i(c,a)};var n=f.headers["Content-Type"]||f.headers["Content-type"]||f.headers["content-type"];if(f.hasContent&&"application/x-www-form-urlencoded"===n&&"string"!=typeof f.body){var o=[];for(var p in f.body)k.call(f.body,p)&&o.push(p+"="+f.body[p]);f.body=o.join("&")}c.send(f.hasContent&&f.body||null)}catch(q){b.onError(q)}return new d(e,c)},d.prototype.dispose=function(){this.isDisposed||(this.isDisposed=!0,this._state.isDone||4===this._xhr.readyState||this._xhr.abort())},c}(i),n=j.ajax=function(a){var b={method:"GET",crossDomain:!1,async:!0,headers:{},responseType:"text",timeout:0,createXHR:function(){return this.crossDomain?f():e()},normalizeError:h,normalizeSuccess:g};if("string"==typeof a)b.url=a;else for(var c in a)k.call(a,c)&&(b[c]=a[c]);return b.crossDomain||b.headers["X-Requested-With"]||(b.headers["X-Requested-With"]="XMLHttpRequest"),b.hasContent=b.body!==d,new m(b)};j.post=function(a,b){var c;return"string"==typeof a?c={url:a,body:b,method:"POST"}:"object"==typeof a&&(c=a,c.method="POST"),n(c)},j.get=function(a){var b;return"string"==typeof a?b={url:a}:"object"==typeof a&&(b=a),n(b)},j.getJSON=function(b){if(!a.JSON&&"function"!=typeof a.JSON.parse)throw new TypeError("JSON is not supported in your runtime.");return n({url:b,responseType:"json"}).map(function(a){return a.response})};var o=function(){var b="document"in a&&a.document.createElement("div");return function(a){b.appendChild(a),b.innerHTML=""}}(),p=function(b){function c(a){this._settings=a,b.call(this)}function d(a){this._script=a,this.isDisposed=!1}return l(c,b),c.id=0,c.prototype.subscribeCore=function(b){var e={jsonp:"JSONPCallback",async:!0,jsonpCallback:"rxjsjsonpCallbackscallback_"+(c.id++).toString(36)};if("string"==typeof this._settings)e.url=this._settings;else for(var f in this._settings)k.call(this._settings,f)&&(e[f]=this._settings[f]);var g=a.document.createElement("script");g.type="text/javascript",g.async=e.async,g.src=e.url.replace(e.jsonp,e.jsonpCallback),a[e.jsonpCallback]=function(b){a[e.jsonpCallback].called=!0,a[e.jsonpCallback].data=b};var h=function(c){"load"!==c.type||a[e.jsonpCallback].called||(c={type:"error"});var d="error"===c.type?400:200,f=a[e.jsonpCallback].data;200===d?(b.onNext({status:d,responseType:"jsonp",response:f,originalEvent:c}),b.onCompleted()):b.onError({type:"error",status:d,originalEvent:c})};g.onload=g.onreadystatechanged=g.onerror=h;var i=a.document.getElementsByTagName("head")[0]||a.document.documentElement;return i.insertBefore(g,i.firstChild),new d(g)},d.prototype.dispose=function(){this.isDisposed||(this.isDisposed=!0,this._script.onload=this._script.onreadystatechanged=this._script.onerror=null,o(this._script),this._script=null)},c}(i);return j.jsonpRequest=function(a){return new p(a)},c}); //# sourceMappingURL=rx.dom.ajax.map