UNPKG

1.71 kBPlain TextView Raw
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
4import { AbortError } from "./Errors";
5import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
6import { ILogger } from "./ILogger";
7import { NodeHttpClient } from "./NodeHttpClient";
8import { XhrHttpClient } from "./XhrHttpClient";
9
10/** Default implementation of {@link @microsoft/signalr.HttpClient}. */
11export class DefaultHttpClient extends HttpClient {
12 private readonly httpClient: HttpClient;
13
14 /** Creates a new instance of the {@link @microsoft/signalr.DefaultHttpClient}, using the provided {@link @microsoft/signalr.ILogger} to log messages. */
15 public constructor(logger: ILogger) {
16 super();
17
18 if (typeof XMLHttpRequest !== "undefined") {
19 this.httpClient = new XhrHttpClient(logger);
20 } else {
21 this.httpClient = new NodeHttpClient(logger);
22 }
23 }
24
25 /** @inheritDoc */
26 public send(request: HttpRequest): Promise<HttpResponse> {
27 // Check that abort was not signaled before calling send
28 if (request.abortSignal && request.abortSignal.aborted) {
29 return Promise.reject(new AbortError());
30 }
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
39 return this.httpClient.send(request);
40 }
41
42 public getCookieString(url: string): string {
43 return this.httpClient.getCookieString(url);
44 }
45}