UNPKG

@aspnet/signalr

Version:

ASP.NET Core SignalR Client

111 lines 4.87 kB
// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; import { HttpError, TimeoutError } from "./Errors"; import { LogLevel } from "./ILogger"; /** Represents an HTTP response. */ var HttpResponse = /** @class */ (function () { function HttpResponse(statusCode, statusText, content) { this.statusCode = statusCode; this.statusText = statusText; this.content = content; } return HttpResponse; }()); export { HttpResponse }; /** Abstraction over an HTTP client. * * This class provides an abstraction over an HTTP client so that a different implementation can be provided on different platforms. */ var HttpClient = /** @class */ (function () { function HttpClient() { } HttpClient.prototype.get = function (url, options) { return this.send(__assign({}, options, { method: "GET", url: url })); }; HttpClient.prototype.post = function (url, options) { return this.send(__assign({}, options, { method: "POST", url: url })); }; HttpClient.prototype.delete = function (url, options) { return this.send(__assign({}, options, { method: "DELETE", url: url })); }; return HttpClient; }()); export { HttpClient }; /** Default implementation of {@link @aspnet/signalr.HttpClient}. */ var DefaultHttpClient = /** @class */ (function (_super) { __extends(DefaultHttpClient, _super); /** Creates a new instance of the {@link @aspnet/signalr.DefaultHttpClient}, using the provided {@link @aspnet/signalr.ILogger} to log messages. */ function DefaultHttpClient(logger) { var _this = _super.call(this) || this; _this.logger = logger; return _this; } /** @inheritDoc */ DefaultHttpClient.prototype.send = function (request) { var _this = this; return new Promise(function (resolve, reject) { var xhr = new XMLHttpRequest(); xhr.open(request.method, request.url, true); xhr.withCredentials = true; xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); // Explicitly setting the Content-Type header for React Native on Android platform. xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); if (request.headers) { Object.keys(request.headers) .forEach(function (header) { return xhr.setRequestHeader(header, request.headers[header]); }); } if (request.responseType) { xhr.responseType = request.responseType; } if (request.abortSignal) { request.abortSignal.onabort = function () { xhr.abort(); }; } if (request.timeout) { xhr.timeout = request.timeout; } xhr.onload = function () { if (request.abortSignal) { request.abortSignal.onabort = null; } if (xhr.status >= 200 && xhr.status < 300) { resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText)); } else { reject(new HttpError(xhr.statusText, xhr.status)); } }; xhr.onerror = function () { _this.logger.log(LogLevel.Warning, "Error from HTTP request. " + xhr.status + ": " + xhr.statusText); reject(new HttpError(xhr.statusText, xhr.status)); }; xhr.ontimeout = function () { _this.logger.log(LogLevel.Warning, "Timeout from HTTP request."); reject(new TimeoutError()); }; xhr.send(request.content || ""); }); }; return DefaultHttpClient; }(HttpClient)); export { DefaultHttpClient }; //# sourceMappingURL=HttpClient.js.map