UNPKG

@ts-common/azure-js-dev-tools

Version:

Developer dependencies for TypeScript related projects

288 lines 11 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for * license information. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.FakeHttpClient = exports.NodeHttpClient = exports.getDefaultHttpClient = exports.HttpHeaders = exports.getHeaderKey = void 0; var tslib_1 = require("tslib"); var http = tslib_1.__importStar(require("http")); var https = tslib_1.__importStar(require("https")); var arrays_1 = require("./arrays"); var url_1 = require("./url"); /** * A collection of HttpHeaders that can be sent with a HTTP request. */ function getHeaderKey(headerName) { return headerName.toLowerCase(); } exports.getHeaderKey = getHeaderKey; /** * A collection of HTTP header key/value pairs. */ var HttpHeaders = /** @class */ (function () { function HttpHeaders(rawHeaders) { this._headersMap = {}; if (rawHeaders) { for (var headerName in rawHeaders) { this.set(headerName, rawHeaders[headerName]); } } } /** * Set a header in this collection with the provided name and value. The name is * case-insensitive. * @param headerName The name of the header to set. This value is case-insensitive. * @param headerValue The value of the header to set. */ HttpHeaders.prototype.set = function (headerName, headerValue) { this._headersMap[getHeaderKey(headerName)] = { name: headerName, value: headerValue.toString() }; }; /** * Get the header value for the provided header name, or undefined if no header exists in this * collection with the provided name. * @param headerName The name of the header. */ HttpHeaders.prototype.get = function (headerName) { var header = this._headersMap[getHeaderKey(headerName)]; return !header ? undefined : header.value; }; /** * Get whether or not this header collection contains a header entry for the provided header name. */ HttpHeaders.prototype.contains = function (headerName) { return !!this._headersMap[getHeaderKey(headerName)]; }; /** * Remove the header with the provided headerName. Return whether or not the header existed and * was removed. * @param headerName The name of the header to remove. */ HttpHeaders.prototype.remove = function (headerName) { var result = this.contains(headerName); delete this._headersMap[getHeaderKey(headerName)]; return result; }; /** * Get the headers that are contained this collection as an object. */ HttpHeaders.prototype.rawHeaders = function () { var result = {}; for (var headerKey in this._headersMap) { var header = this._headersMap[headerKey]; result[header.name.toLowerCase()] = header.value; } return result; }; /** * Get the headers that are contained in this collection as an array. */ HttpHeaders.prototype.headersArray = function () { var headers = []; for (var headerKey in this._headersMap) { headers.push(this._headersMap[headerKey]); } return headers; }; /** * Get the header names that are contained in this collection. */ HttpHeaders.prototype.headerNames = function () { var headerNames = []; var headers = this.headersArray(); for (var i = 0; i < headers.length; ++i) { headerNames.push(headers[i].name); } return headerNames; }; /** * Get the header names that are contained in this collection. */ HttpHeaders.prototype.headerValues = function () { var headerValues = []; var headers = this.headersArray(); for (var i = 0; i < headers.length; ++i) { headerValues.push(headers[i].value); } return headerValues; }; /** * Get the JSON object representation of this HTTP header collection. */ HttpHeaders.prototype.toJson = function () { return this.rawHeaders(); }; /** * Get the string representation of this HTTP header collection. */ HttpHeaders.prototype.toString = function () { return JSON.stringify(this.toJson()); }; /** * Create a deep clone/copy of this HttpHeaders collection. */ HttpHeaders.prototype.clone = function () { return new HttpHeaders(this.rawHeaders()); }; return HttpHeaders; }()); exports.HttpHeaders = HttpHeaders; /** * Get an instance of the default HttpClient. */ function getDefaultHttpClient() { return new NodeHttpClient(); } exports.getDefaultHttpClient = getDefaultHttpClient; function sendNodeHttpClientRequest(request) { var requestUrl = request.url instanceof url_1.URLBuilder ? request.url : url_1.URLBuilder.parse(request.url); var requestHeaders; if (request.headers instanceof HttpHeaders) { requestHeaders = request.headers.rawHeaders(); } else if (request.headers) { requestHeaders = request.headers; } var protocol = requestUrl.getScheme() || "http"; var requestOptions = { method: request.method, headers: requestHeaders, protocol: protocol + ":", host: requestUrl.getHost(), port: requestUrl.getPort(), path: (requestUrl.getPath() || "") + (requestUrl.getQuery() || "") }; return new Promise(function (resolve, reject) { try { var clientRequest = (protocol === "http" ? http : https).request(requestOptions, function (response) { try { response.setEncoding("utf8"); var responseBody_1 = ""; response.on("data", function (chunk) { responseBody_1 += chunk; }); response.on("end", function () { var e_1, _a; var responseHeaders = new HttpHeaders(); try { for (var _b = tslib_1.__values(Object.keys(response.headers)), _c = _b.next(); !_c.done; _c = _b.next()) { var responseHeaderName = _c.value; var responseHeaderValue = response.headers[responseHeaderName]; if (typeof responseHeaderValue === "string") { responseHeaders.set(responseHeaderName, responseHeaderValue); } else if (Array.isArray(responseHeaderValue)) { responseHeaders.set(responseHeaderName, responseHeaderValue.join(",")); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } resolve({ request: request, statusCode: response.statusCode, headers: responseHeaders, body: responseBody_1 }); }); response.on("error", function (error) { reject(error); }); } catch (error) { reject(error); } }); if (request.body) { clientRequest.write(request.body, function (error) { if (error) { reject(error); } }); } clientRequest.on("error", function (error) { reject(error); }); clientRequest.end(function () { }); } catch (error) { reject(error); } }); } /** * An HTTP client that uses the built-in Node.js http module. */ var NodeHttpClient = /** @class */ (function () { function NodeHttpClient(options) { if (options === void 0) { options = {}; } this.handleRedirects = options.handleRedirects == undefined ? true : options.handleRedirects; } NodeHttpClient.prototype.sendRequest = function (request) { return tslib_1.__awaiter(this, void 0, void 0, function () { var response; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sendNodeHttpClientRequest(request)]; case 1: response = _a.sent(); _a.label = 2; case 2: if (!(this.handleRedirects && 300 <= response.statusCode && response.statusCode < 400 && response.headers.contains("location"))) return [3 /*break*/, 4]; request.url = response.headers.get("location"); return [4 /*yield*/, sendNodeHttpClientRequest(request)]; case 3: response = _a.sent(); return [3 /*break*/, 2]; case 4: return [2 /*return*/, response]; } }); }); }; return NodeHttpClient; }()); exports.NodeHttpClient = NodeHttpClient; /** * A fake HttpClient that can registered pre-determined HttpResponses for HttpRequests. */ var FakeHttpClient = /** @class */ (function () { function FakeHttpClient() { this.fakeResponses = []; } FakeHttpClient.prototype.add = function (requestMethod, requestUrl, responseStatusCode, responseHeaders, responseBody) { this.fakeResponses.push({ request: { method: requestMethod, url: requestUrl }, statusCode: responseStatusCode || 200, headers: responseHeaders || new HttpHeaders(), body: responseBody, }); return this; }; FakeHttpClient.prototype.sendRequest = function (request) { var result = arrays_1.last(this.fakeResponses, function (fakeResponse) { var fakeRequest = fakeResponse.request; return fakeRequest.method === request.method && fakeRequest.url.toString() === request.url.toString(); }); if (!result) { result = { request: request, statusCode: 404, headers: new HttpHeaders(), }; } return Promise.resolve(result); }; return FakeHttpClient; }()); exports.FakeHttpClient = FakeHttpClient; //# sourceMappingURL=http.js.map