DefaultHttpClient.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. // Licensed to the .NET Foundation under one or more agreements.
  3. // The .NET Foundation licenses this file to you under the MIT license.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. exports.DefaultHttpClient = void 0;
  6. const Errors_1 = require("./Errors");
  7. const FetchHttpClient_1 = require("./FetchHttpClient");
  8. const HttpClient_1 = require("./HttpClient");
  9. const Utils_1 = require("./Utils");
  10. const XhrHttpClient_1 = require("./XhrHttpClient");
  11. /** Default implementation of {@link @microsoft/signalr.HttpClient}. */
  12. class DefaultHttpClient extends HttpClient_1.HttpClient {
  13. /** Creates a new instance of the {@link @microsoft/signalr.DefaultHttpClient}, using the provided {@link @microsoft/signalr.ILogger} to log messages. */
  14. constructor(logger) {
  15. super();
  16. if (typeof fetch !== "undefined" || Utils_1.Platform.isNode) {
  17. this._httpClient = new FetchHttpClient_1.FetchHttpClient(logger);
  18. }
  19. else if (typeof XMLHttpRequest !== "undefined") {
  20. this._httpClient = new XhrHttpClient_1.XhrHttpClient(logger);
  21. }
  22. else {
  23. throw new Error("No usable HttpClient found.");
  24. }
  25. }
  26. /** @inheritDoc */
  27. send(request) {
  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 this._httpClient.send(request);
  39. }
  40. getCookieString(url) {
  41. return this._httpClient.getCookieString(url);
  42. }
  43. }
  44. exports.DefaultHttpClient = DefaultHttpClient;
  45. //# sourceMappingURL=DefaultHttpClient.js.map