XhrHttpClient.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. // Copyright (c) .NET Foundation. All rights reserved.
  3. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  4. var __extends = (this && this.__extends) || (function () {
  5. var extendStatics = Object.setPrototypeOf ||
  6. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  7. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. var Errors_1 = require("./Errors");
  16. var HttpClient_1 = require("./HttpClient");
  17. var ILogger_1 = require("./ILogger");
  18. var XhrHttpClient = /** @class */ (function (_super) {
  19. __extends(XhrHttpClient, _super);
  20. function XhrHttpClient(logger) {
  21. var _this = _super.call(this) || this;
  22. _this.logger = logger;
  23. return _this;
  24. }
  25. /** @inheritDoc */
  26. XhrHttpClient.prototype.send = function (request) {
  27. var _this = this;
  28. // Check that abort was not signaled before calling send
  29. if (request.abortSignal && request.abortSignal.aborted) {
  30. return Promise.reject(new Errors_1.AbortError());
  31. }
  32. if (!request.method) {
  33. return Promise.reject(new Error("No method defined."));
  34. }
  35. if (!request.url) {
  36. return Promise.reject(new Error("No url defined."));
  37. }
  38. return new Promise(function (resolve, reject) {
  39. var xhr = new XMLHttpRequest();
  40. xhr.open(request.method, request.url, true);
  41. xhr.withCredentials = true;
  42. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  43. // Explicitly setting the Content-Type header for React Native on Android platform.
  44. xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
  45. var headers = request.headers;
  46. if (headers) {
  47. Object.keys(headers)
  48. .forEach(function (header) {
  49. xhr.setRequestHeader(header, headers[header]);
  50. });
  51. }
  52. if (request.responseType) {
  53. xhr.responseType = request.responseType;
  54. }
  55. if (request.abortSignal) {
  56. request.abortSignal.onabort = function () {
  57. xhr.abort();
  58. reject(new Errors_1.AbortError());
  59. };
  60. }
  61. if (request.timeout) {
  62. xhr.timeout = request.timeout;
  63. }
  64. xhr.onload = function () {
  65. if (request.abortSignal) {
  66. request.abortSignal.onabort = null;
  67. }
  68. if (xhr.status >= 200 && xhr.status < 300) {
  69. resolve(new HttpClient_1.HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText));
  70. }
  71. else {
  72. reject(new Errors_1.HttpError(xhr.statusText, xhr.status));
  73. }
  74. };
  75. xhr.onerror = function () {
  76. _this.logger.log(ILogger_1.LogLevel.Warning, "Error from HTTP request. " + xhr.status + ": " + xhr.statusText + ".");
  77. reject(new Errors_1.HttpError(xhr.statusText, xhr.status));
  78. };
  79. xhr.ontimeout = function () {
  80. _this.logger.log(ILogger_1.LogLevel.Warning, "Timeout from HTTP request.");
  81. reject(new Errors_1.TimeoutError());
  82. };
  83. xhr.send(request.content || "");
  84. });
  85. };
  86. return XhrHttpClient;
  87. }(HttpClient_1.HttpClient));
  88. exports.XhrHttpClient = XhrHttpClient;
  89. //# sourceMappingURL=XhrHttpClient.js.map