DefaultHttpClient.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. import { AbortError } from "./Errors";
  4. import { FetchHttpClient } from "./FetchHttpClient";
  5. import { HttpClient } from "./HttpClient";
  6. import { Platform } from "./Utils";
  7. import { XhrHttpClient } from "./XhrHttpClient";
  8. /** Default implementation of {@link @microsoft/signalr.HttpClient}. */
  9. export class DefaultHttpClient extends HttpClient {
  10. /** Creates a new instance of the {@link @microsoft/signalr.DefaultHttpClient}, using the provided {@link @microsoft/signalr.ILogger} to log messages. */
  11. constructor(logger) {
  12. super();
  13. if (typeof fetch !== "undefined" || Platform.isNode) {
  14. this._httpClient = new FetchHttpClient(logger);
  15. }
  16. else if (typeof XMLHttpRequest !== "undefined") {
  17. this._httpClient = new XhrHttpClient(logger);
  18. }
  19. else {
  20. throw new Error("No usable HttpClient found.");
  21. }
  22. }
  23. /** @inheritDoc */
  24. send(request) {
  25. // Check that abort was not signaled before calling send
  26. if (request.abortSignal && request.abortSignal.aborted) {
  27. return Promise.reject(new AbortError());
  28. }
  29. if (!request.method) {
  30. return Promise.reject(new Error("No method defined."));
  31. }
  32. if (!request.url) {
  33. return Promise.reject(new Error("No url defined."));
  34. }
  35. return this._httpClient.send(request);
  36. }
  37. getCookieString(url) {
  38. return this._httpClient.getCookieString(url);
  39. }
  40. }
  41. //# sourceMappingURL=DefaultHttpClient.js.map