XhrHttpClient.js 3.7 KB

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