Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AjaxObservable.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var root_1 = require("../../util/root");
  17. var tryCatch_1 = require("../../util/tryCatch");
  18. var errorObject_1 = require("../../util/errorObject");
  19. var Observable_1 = require("../../Observable");
  20. var Subscriber_1 = require("../../Subscriber");
  21. var map_1 = require("../../operators/map");
  22. function getCORSRequest() {
  23. if (root_1.root.XMLHttpRequest) {
  24. return new root_1.root.XMLHttpRequest();
  25. }
  26. else if (!!root_1.root.XDomainRequest) {
  27. return new root_1.root.XDomainRequest();
  28. }
  29. else {
  30. throw new Error('CORS is not supported by your browser');
  31. }
  32. }
  33. function getXMLHttpRequest() {
  34. if (root_1.root.XMLHttpRequest) {
  35. return new root_1.root.XMLHttpRequest();
  36. }
  37. else {
  38. var progId = void 0;
  39. try {
  40. var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
  41. for (var i = 0; i < 3; i++) {
  42. try {
  43. progId = progIds[i];
  44. if (new root_1.root.ActiveXObject(progId)) {
  45. break;
  46. }
  47. }
  48. catch (e) {
  49. }
  50. }
  51. return new root_1.root.ActiveXObject(progId);
  52. }
  53. catch (e) {
  54. throw new Error('XMLHttpRequest is not supported by your browser');
  55. }
  56. }
  57. }
  58. function ajaxGet(url, headers) {
  59. if (headers === void 0) { headers = null; }
  60. return new AjaxObservable({ method: 'GET', url: url, headers: headers });
  61. }
  62. exports.ajaxGet = ajaxGet;
  63. function ajaxPost(url, body, headers) {
  64. return new AjaxObservable({ method: 'POST', url: url, body: body, headers: headers });
  65. }
  66. exports.ajaxPost = ajaxPost;
  67. function ajaxDelete(url, headers) {
  68. return new AjaxObservable({ method: 'DELETE', url: url, headers: headers });
  69. }
  70. exports.ajaxDelete = ajaxDelete;
  71. function ajaxPut(url, body, headers) {
  72. return new AjaxObservable({ method: 'PUT', url: url, body: body, headers: headers });
  73. }
  74. exports.ajaxPut = ajaxPut;
  75. function ajaxPatch(url, body, headers) {
  76. return new AjaxObservable({ method: 'PATCH', url: url, body: body, headers: headers });
  77. }
  78. exports.ajaxPatch = ajaxPatch;
  79. var mapResponse = map_1.map(function (x, index) { return x.response; });
  80. function ajaxGetJSON(url, headers) {
  81. return mapResponse(new AjaxObservable({
  82. method: 'GET',
  83. url: url,
  84. responseType: 'json',
  85. headers: headers
  86. }));
  87. }
  88. exports.ajaxGetJSON = ajaxGetJSON;
  89. var AjaxObservable = (function (_super) {
  90. __extends(AjaxObservable, _super);
  91. function AjaxObservable(urlOrRequest) {
  92. var _this = _super.call(this) || this;
  93. var request = {
  94. async: true,
  95. createXHR: function () {
  96. return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
  97. },
  98. crossDomain: true,
  99. withCredentials: false,
  100. headers: {},
  101. method: 'GET',
  102. responseType: 'json',
  103. timeout: 0
  104. };
  105. if (typeof urlOrRequest === 'string') {
  106. request.url = urlOrRequest;
  107. }
  108. else {
  109. for (var prop in urlOrRequest) {
  110. if (urlOrRequest.hasOwnProperty(prop)) {
  111. request[prop] = urlOrRequest[prop];
  112. }
  113. }
  114. }
  115. _this.request = request;
  116. return _this;
  117. }
  118. AjaxObservable.prototype._subscribe = function (subscriber) {
  119. return new AjaxSubscriber(subscriber, this.request);
  120. };
  121. AjaxObservable.create = (function () {
  122. var create = function (urlOrRequest) {
  123. return new AjaxObservable(urlOrRequest);
  124. };
  125. create.get = ajaxGet;
  126. create.post = ajaxPost;
  127. create.delete = ajaxDelete;
  128. create.put = ajaxPut;
  129. create.patch = ajaxPatch;
  130. create.getJSON = ajaxGetJSON;
  131. return create;
  132. })();
  133. return AjaxObservable;
  134. }(Observable_1.Observable));
  135. exports.AjaxObservable = AjaxObservable;
  136. var AjaxSubscriber = (function (_super) {
  137. __extends(AjaxSubscriber, _super);
  138. function AjaxSubscriber(destination, request) {
  139. var _this = _super.call(this, destination) || this;
  140. _this.request = request;
  141. _this.done = false;
  142. var headers = request.headers = request.headers || {};
  143. if (!request.crossDomain && !headers['X-Requested-With']) {
  144. headers['X-Requested-With'] = 'XMLHttpRequest';
  145. }
  146. if (!('Content-Type' in headers) && !(root_1.root.FormData && request.body instanceof root_1.root.FormData) && typeof request.body !== 'undefined') {
  147. headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
  148. }
  149. request.body = _this.serializeBody(request.body, request.headers['Content-Type']);
  150. _this.send();
  151. return _this;
  152. }
  153. AjaxSubscriber.prototype.next = function (e) {
  154. this.done = true;
  155. var _a = this, xhr = _a.xhr, request = _a.request, destination = _a.destination;
  156. var response = new AjaxResponse(e, xhr, request);
  157. if (response.response === errorObject_1.errorObject) {
  158. destination.error(errorObject_1.errorObject.e);
  159. }
  160. else {
  161. destination.next(response);
  162. }
  163. };
  164. AjaxSubscriber.prototype.send = function () {
  165. var _a = this, request = _a.request, _b = _a.request, user = _b.user, method = _b.method, url = _b.url, async = _b.async, password = _b.password, headers = _b.headers, body = _b.body;
  166. var createXHR = request.createXHR;
  167. var xhr = tryCatch_1.tryCatch(createXHR).call(request);
  168. if (xhr === errorObject_1.errorObject) {
  169. this.error(errorObject_1.errorObject.e);
  170. }
  171. else {
  172. this.xhr = xhr;
  173. this.setupEvents(xhr, request);
  174. var result = void 0;
  175. if (user) {
  176. result = tryCatch_1.tryCatch(xhr.open).call(xhr, method, url, async, user, password);
  177. }
  178. else {
  179. result = tryCatch_1.tryCatch(xhr.open).call(xhr, method, url, async);
  180. }
  181. if (result === errorObject_1.errorObject) {
  182. this.error(errorObject_1.errorObject.e);
  183. return null;
  184. }
  185. if (async) {
  186. xhr.timeout = request.timeout;
  187. xhr.responseType = request.responseType;
  188. }
  189. if ('withCredentials' in xhr) {
  190. xhr.withCredentials = !!request.withCredentials;
  191. }
  192. this.setHeaders(xhr, headers);
  193. result = body ? tryCatch_1.tryCatch(xhr.send).call(xhr, body) : tryCatch_1.tryCatch(xhr.send).call(xhr);
  194. if (result === errorObject_1.errorObject) {
  195. this.error(errorObject_1.errorObject.e);
  196. return null;
  197. }
  198. }
  199. return xhr;
  200. };
  201. AjaxSubscriber.prototype.serializeBody = function (body, contentType) {
  202. if (!body || typeof body === 'string') {
  203. return body;
  204. }
  205. else if (root_1.root.FormData && body instanceof root_1.root.FormData) {
  206. return body;
  207. }
  208. if (contentType) {
  209. var splitIndex = contentType.indexOf(';');
  210. if (splitIndex !== -1) {
  211. contentType = contentType.substring(0, splitIndex);
  212. }
  213. }
  214. switch (contentType) {
  215. case 'application/x-www-form-urlencoded':
  216. return Object.keys(body).map(function (key) { return encodeURIComponent(key) + "=" + encodeURIComponent(body[key]); }).join('&');
  217. case 'application/json':
  218. return JSON.stringify(body);
  219. default:
  220. return body;
  221. }
  222. };
  223. AjaxSubscriber.prototype.setHeaders = function (xhr, headers) {
  224. for (var key in headers) {
  225. if (headers.hasOwnProperty(key)) {
  226. xhr.setRequestHeader(key, headers[key]);
  227. }
  228. }
  229. };
  230. AjaxSubscriber.prototype.setupEvents = function (xhr, request) {
  231. var progressSubscriber = request.progressSubscriber;
  232. function xhrTimeout(e) {
  233. var _a = xhrTimeout, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
  234. if (progressSubscriber) {
  235. progressSubscriber.error(e);
  236. }
  237. var ajaxTimeoutError = new exports.AjaxTimeoutError(this, request);
  238. if (ajaxTimeoutError.response === errorObject_1.errorObject) {
  239. subscriber.error(errorObject_1.errorObject.e);
  240. }
  241. else {
  242. subscriber.error(ajaxTimeoutError);
  243. }
  244. }
  245. xhr.ontimeout = xhrTimeout;
  246. xhrTimeout.request = request;
  247. xhrTimeout.subscriber = this;
  248. xhrTimeout.progressSubscriber = progressSubscriber;
  249. if (xhr.upload && 'withCredentials' in xhr) {
  250. if (progressSubscriber) {
  251. var xhrProgress_1;
  252. xhrProgress_1 = function (e) {
  253. var progressSubscriber = xhrProgress_1.progressSubscriber;
  254. progressSubscriber.next(e);
  255. };
  256. if (root_1.root.XDomainRequest) {
  257. xhr.onprogress = xhrProgress_1;
  258. }
  259. else {
  260. xhr.upload.onprogress = xhrProgress_1;
  261. }
  262. xhrProgress_1.progressSubscriber = progressSubscriber;
  263. }
  264. var xhrError_1;
  265. xhrError_1 = function (e) {
  266. var _a = xhrError_1, progressSubscriber = _a.progressSubscriber, subscriber = _a.subscriber, request = _a.request;
  267. if (progressSubscriber) {
  268. progressSubscriber.error(e);
  269. }
  270. var ajaxError = new exports.AjaxError('ajax error', this, request);
  271. if (ajaxError.response === errorObject_1.errorObject) {
  272. subscriber.error(errorObject_1.errorObject.e);
  273. }
  274. else {
  275. subscriber.error(ajaxError);
  276. }
  277. };
  278. xhr.onerror = xhrError_1;
  279. xhrError_1.request = request;
  280. xhrError_1.subscriber = this;
  281. xhrError_1.progressSubscriber = progressSubscriber;
  282. }
  283. function xhrReadyStateChange(e) {
  284. return;
  285. }
  286. xhr.onreadystatechange = xhrReadyStateChange;
  287. xhrReadyStateChange.subscriber = this;
  288. xhrReadyStateChange.progressSubscriber = progressSubscriber;
  289. xhrReadyStateChange.request = request;
  290. function xhrLoad(e) {
  291. var _a = xhrLoad, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
  292. if (this.readyState === 4) {
  293. var status_1 = this.status === 1223 ? 204 : this.status;
  294. var response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
  295. if (status_1 === 0) {
  296. status_1 = response ? 200 : 0;
  297. }
  298. if (status_1 < 400) {
  299. if (progressSubscriber) {
  300. progressSubscriber.complete();
  301. }
  302. subscriber.next(e);
  303. subscriber.complete();
  304. }
  305. else {
  306. if (progressSubscriber) {
  307. progressSubscriber.error(e);
  308. }
  309. var ajaxError = new exports.AjaxError('ajax error ' + status_1, this, request);
  310. if (ajaxError.response === errorObject_1.errorObject) {
  311. subscriber.error(errorObject_1.errorObject.e);
  312. }
  313. else {
  314. subscriber.error(ajaxError);
  315. }
  316. }
  317. }
  318. }
  319. xhr.onload = xhrLoad;
  320. xhrLoad.subscriber = this;
  321. xhrLoad.progressSubscriber = progressSubscriber;
  322. xhrLoad.request = request;
  323. };
  324. AjaxSubscriber.prototype.unsubscribe = function () {
  325. var _a = this, done = _a.done, xhr = _a.xhr;
  326. if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
  327. xhr.abort();
  328. }
  329. _super.prototype.unsubscribe.call(this);
  330. };
  331. return AjaxSubscriber;
  332. }(Subscriber_1.Subscriber));
  333. exports.AjaxSubscriber = AjaxSubscriber;
  334. var AjaxResponse = (function () {
  335. function AjaxResponse(originalEvent, xhr, request) {
  336. this.originalEvent = originalEvent;
  337. this.xhr = xhr;
  338. this.request = request;
  339. this.status = xhr.status;
  340. this.responseType = xhr.responseType || request.responseType;
  341. this.response = parseXhrResponse(this.responseType, xhr);
  342. }
  343. return AjaxResponse;
  344. }());
  345. exports.AjaxResponse = AjaxResponse;
  346. function AjaxErrorImpl(message, xhr, request) {
  347. Error.call(this);
  348. this.message = message;
  349. this.name = 'AjaxError';
  350. this.xhr = xhr;
  351. this.request = request;
  352. this.status = xhr.status;
  353. this.responseType = xhr.responseType || request.responseType;
  354. this.response = parseXhrResponse(this.responseType, xhr);
  355. return this;
  356. }
  357. AjaxErrorImpl.prototype = Object.create(Error.prototype);
  358. exports.AjaxError = AjaxErrorImpl;
  359. function parseJson(xhr) {
  360. if ('response' in xhr) {
  361. return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
  362. }
  363. else {
  364. return JSON.parse(xhr.responseText || 'null');
  365. }
  366. }
  367. function parseXhrResponse(responseType, xhr) {
  368. switch (responseType) {
  369. case 'json':
  370. return tryCatch_1.tryCatch(parseJson)(xhr);
  371. case 'xml':
  372. return xhr.responseXML;
  373. case 'text':
  374. default:
  375. return ('response' in xhr) ? xhr.response : xhr.responseText;
  376. }
  377. }
  378. function AjaxTimeoutErrorImpl(xhr, request) {
  379. exports.AjaxError.call(this, 'ajax timeout', xhr, request);
  380. this.name = 'AjaxTimeoutError';
  381. return this;
  382. }
  383. exports.AjaxTimeoutError = AjaxTimeoutErrorImpl;
  384. //# sourceMappingURL=AjaxObservable.js.map