зеркало из https://github.com/Azure/ms-rest-js.git
Add new httpPipeline and tests without modifying existing pipeline
This commit is contained in:
Родитель
6f6a594404
Коммит
5a1a53aa6c
|
@ -37,68 +37,62 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var httpHeaders_1 = require("./httpHeaders");
|
||||
/**
|
||||
* Provides the fetch() method based on the environment.
|
||||
* @returns {fetch} fetch - The fetch() method available in the environment to make requests
|
||||
*/
|
||||
function getFetch() {
|
||||
// using window.Fetch in Edge gives a TypeMismatchError
|
||||
// (https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8546263/).
|
||||
// Hence we will be using the fetch-ponyfill for Edge.
|
||||
return (window && window.fetch && window.navigator && window.navigator.userAgent && window.navigator.userAgent.indexOf("Edge/") === -1)
|
||||
? window.fetch.bind(window)
|
||||
: require("fetch-ponyfill")({ useCookie: true }).fetch;
|
||||
return require("fetch-ponyfill")({ useCookie: true }).fetch;
|
||||
}
|
||||
/**
|
||||
* The cached fetch method that will be used to send HTTP requests.
|
||||
*/
|
||||
var myFetch;
|
||||
var fetch;
|
||||
/**
|
||||
* A HttpClient implementation that uses fetch to send HTTP requests.
|
||||
* @param request The request to send.
|
||||
*/
|
||||
var FetchHttpClient = /** @class */ (function () {
|
||||
function FetchHttpClient() {
|
||||
if (!myFetch) {
|
||||
myFetch = getFetch();
|
||||
}
|
||||
}
|
||||
FetchHttpClient.prototype.send = function (request) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var result, fetchResponse_1, fetchResponseHeaders, responseHeaders, headerName, headerValue, response, err_1;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
_a.trys.push([0, 2, , 3]);
|
||||
return [4 /*yield*/, myFetch(request.url.toString(), request)];
|
||||
case 1:
|
||||
fetchResponse_1 = _a.sent();
|
||||
fetchResponseHeaders = fetchResponse_1.headers;
|
||||
responseHeaders = {};
|
||||
for (headerName in fetchResponseHeaders.keys()) {
|
||||
headerValue = fetchResponseHeaders.get(headerName);
|
||||
if (headerValue) {
|
||||
responseHeaders[headerName] = headerValue;
|
||||
}
|
||||
}
|
||||
response = {
|
||||
request: request,
|
||||
statusCode: fetchResponse_1.status,
|
||||
headers: responseHeaders,
|
||||
bodyAsText: function () { return fetchResponse_1.text(); },
|
||||
bodyAsJson: function () { return fetchResponse_1.json(); }
|
||||
};
|
||||
result = Promise.resolve(response);
|
||||
return [3 /*break*/, 3];
|
||||
case 2:
|
||||
err_1 = _a.sent();
|
||||
result = Promise.reject(err_1);
|
||||
return [3 /*break*/, 3];
|
||||
case 3: return [2 /*return*/, result];
|
||||
}
|
||||
});
|
||||
function fetchHttpClient(request) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var result, fetchRequestOptions, fetchResponse_1, responseHeaders_1, fetchResponseHeaders, response, err_1;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
if (!fetch) {
|
||||
fetch = getFetch();
|
||||
}
|
||||
_a.label = 1;
|
||||
case 1:
|
||||
_a.trys.push([1, 3, , 4]);
|
||||
fetchRequestOptions = {
|
||||
method: request.httpMethod,
|
||||
headers: request.headers.toJson(),
|
||||
body: request.body
|
||||
};
|
||||
return [4 /*yield*/, fetch(request.url, fetchRequestOptions)];
|
||||
case 2:
|
||||
fetchResponse_1 = _a.sent();
|
||||
responseHeaders_1 = new httpHeaders_1.HttpHeaders();
|
||||
fetchResponseHeaders = fetchResponse_1.headers;
|
||||
fetchResponseHeaders.forEach(function (headerValue, headerName) { responseHeaders_1.set(headerName, headerValue); });
|
||||
response = {
|
||||
request: request,
|
||||
statusCode: fetchResponse_1.status,
|
||||
headers: responseHeaders_1,
|
||||
bodyAsText: function () { return fetchResponse_1.text(); },
|
||||
bodyAsJson: function () { return fetchResponse_1.json(); }
|
||||
};
|
||||
result = Promise.resolve(response);
|
||||
return [3 /*break*/, 4];
|
||||
case 3:
|
||||
err_1 = _a.sent();
|
||||
result = Promise.reject(err_1);
|
||||
return [3 /*break*/, 4];
|
||||
case 4: return [2 /*return*/, result];
|
||||
}
|
||||
});
|
||||
};
|
||||
return FetchHttpClient;
|
||||
}());
|
||||
exports.FetchHttpClient = FetchHttpClient;
|
||||
});
|
||||
}
|
||||
exports.fetchHttpClient = fetchHttpClient;
|
||||
//# sourceMappingURL=fetchHttpClient.js.map
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"fetchHttpClient.js","sourceRoot":"","sources":["../../lib/fetchHttpClient.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAS/F;;;GAGG;AACH;IACI,uDAAuD;IACvD,mFAAmF;IACnF,sDAAsD;IACtD,MAAM,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QACnI,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC;QAC3B,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC;AAC/D,CAAC;AAED;;GAEG;AACH,IAAI,OAAoB,CAAC;AAEzB;;GAEG;AACH;IACI;QACI,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;YACX,OAAO,GAAG,QAAQ,EAAE,CAAC;QACzB,CAAC;IACL,CAAC;IAEK,8BAAI,GAAV,UAAW,OAAoB;;;;;;;wBAIS,qBAAM,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,EAAA;;wBAAxE,kBAA0B,SAA8C;wBAExE,oBA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AAC,KAAK,CAAC,CAAC,CAAC;wBACT,KAAK,GAAG,QAAQ,EAAE,CAAC;oBACvB,CAAC;;;;oBAKS,mBAAmB,GAAgB;wBACrC,MAAM,EAAE,OAAO,CAAC,UAAU;wBAC1B,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE;wBACjC,IAAI,EAAE,OAAO,CAAC,IAAI;qBACrB,CAAC;oBAE8B,qBAAM,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,CAAC,EAAA;;oBAAvE,kBAA0B,SAA6C;oBAEvE,oBAAkB,IAAI,yBAAW,EAAE,CAAC;oBACpC,oBAAoB,GAAY,eAAa,CAAC,OAAO,CAAC;oBAC5D,oBAAoB,CAAC,OAAO,CAAC,UAAC,WAAmB,EAAE,UAAkB,IAAO,iBAAe,CAAC,GAAG,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAEvH,QAAQ,GAAiB;wBAC3B,OAAO,EAAE,OAAO;wBAChB,UAAU,EAAE,eAAa,CAAC,MAAM;wBAChC,OAAO,EAAE,iBAAe;wBACxB,UAAU,EAAE,cAAM,OAAA,eAAa,CAAC,IAAI,EAAE,EAApB,CAAoB;wBACtC,UAAU,EAAE,cAAM,OAAA,eAAa,CAAC,IAAI,EAAE,EAApB,CAAoB;qBACzC,CAAC;oBAEF,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;;;;oBAEnC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,KAAG,CAAC,CAAC;;wBAGjC,sBAAO,MAAM,EAAC;;;;CACjB;AAlCD,0CAkCC"}
|
|
@ -0,0 +1,18 @@
|
|||
"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 });
|
||||
/**
|
||||
* An adapter type that adapts a HttpClient to look like a RequestPolicy.
|
||||
*/
|
||||
var HttpClientToRequestPolicyAdapter = /** @class */ (function () {
|
||||
function HttpClientToRequestPolicyAdapter(_httpClient) {
|
||||
this._httpClient = _httpClient;
|
||||
}
|
||||
HttpClientToRequestPolicyAdapter.prototype.send = function (request) {
|
||||
return this._httpClient(request);
|
||||
};
|
||||
return HttpClientToRequestPolicyAdapter;
|
||||
}());
|
||||
exports.HttpClientToRequestPolicyAdapter = HttpClientToRequestPolicyAdapter;
|
||||
//# sourceMappingURL=httpClientToRequestPolicyAdapter.js.map
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"httpClientToRequestPolicyAdapter.js","sourceRoot":"","sources":["../../lib/httpClientToRequestPolicyAdapter.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAO/F;;GAEG;AACH;IACI,0CAAoB,WAAuB;QAAvB,gBAAW,GAAX,WAAW,CAAY;IAC3C,CAAC;IAED,+CAAI,GAAJ,UAAK,OAAoB;QACrB,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;IACL,uCAAC;AAAD,CAAC,AAPD,IAOC;AAPY,4EAAgC"}
|
|
@ -2,4 +2,86 @@
|
|||
// 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 });
|
||||
/**
|
||||
* A collection of HttpHeaders that can be sent with a HTTP request.
|
||||
*/
|
||||
function getHeaderKey(headerName) {
|
||||
return headerName.toLowerCase();
|
||||
}
|
||||
/**
|
||||
* 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 the headers that are contained in this collection.
|
||||
*/
|
||||
HttpHeaders.prototype.headers = 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.headers();
|
||||
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.headers();
|
||||
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 () {
|
||||
var result = {};
|
||||
var headers = this.headers();
|
||||
for (var i = 0; i < headers.length; ++i) {
|
||||
result[headers[i].name] = headers[i].value;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
return HttpHeaders;
|
||||
}());
|
||||
exports.HttpHeaders = HttpHeaders;
|
||||
//# sourceMappingURL=httpHeaders.js.map
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"httpHeaders.js","sourceRoot":"","sources":["../../lib/httpHeaders.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F"}
|
||||
{"version":3,"file":"httpHeaders.js","sourceRoot":"","sources":["../../lib/httpHeaders.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAE/F;;GAEG;AACH,sBAAsB,UAAkB;IACpC,MAAM,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC;AACpC,CAAC;AAsBD;;GAEG;AACH;IAGI,qBAAY,UAA2B;QACnC,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;YACb,GAAG,CAAC,CAAC,IAAM,UAAU,IAAI,UAAU,CAAC,CAAC,CAAC;gBAClC,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC;YACjD,CAAC;QACL,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACI,yBAAG,GAAV,UAAW,UAAkB,EAAE,WAA4B;QACvD,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,CAAC,QAAQ,EAAE,EAAE,CAAC;IACrG,CAAC;IAED;;;;OAIG;IACI,yBAAG,GAAV,UAAW,UAAkB;QACzB,IAAM,MAAM,GAAe,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC;QACtE,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;IAC9C,CAAC;IAED;;OAEG;IACI,6BAAO,GAAd;QACI,IAAM,OAAO,GAAiB,EAAE,CAAC;QACjC,GAAG,CAAC,CAAC,IAAM,SAAS,IAAI,IAAI,CAAC,WAAW,CAAC,C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CAAC;QAED,MAAM,CAAC,MAAM,CAAC;IAClB,CAAC;IACL,kBAAC;AAAD,CAAC,AAhFD,IAgFC;AAhFY,kCAAW"}
|
|
@ -3,11 +3,12 @@
|
|||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fetchHttpClient_1 = require("./fetchHttpClient");
|
||||
var httpClientToRequestPolicyAdapter_1 = require("./httpClientToRequestPolicyAdapter");
|
||||
var requestPolicyOptions_1 = require("./requestPolicyOptions");
|
||||
var defaultHttpClient;
|
||||
function getDefaultHttpClient() {
|
||||
if (!defaultHttpClient) {
|
||||
defaultHttpClient = new fetchHttpClient_1.FetchHttpClient();
|
||||
defaultHttpClient = fetchHttpClient_1.fetchHttpClient;
|
||||
}
|
||||
return defaultHttpClient;
|
||||
}
|
||||
|
@ -34,15 +35,15 @@ var HttpPipeline = /** @class */ (function () {
|
|||
* @return A Promise that resolves to the HttpResponse from the targeted server.
|
||||
*/
|
||||
HttpPipeline.prototype.send = function (request) {
|
||||
var firstRequestPolicy = this._httpClient;
|
||||
var requestPolicyChainHead = new httpClientToRequestPolicyAdapter_1.HttpClientToRequestPolicyAdapter(this._httpClient);
|
||||
if (this._requestPolicyFactories) {
|
||||
var requestPolicyFactoriesLength = this._requestPolicyFactories.length;
|
||||
for (var i = requestPolicyFactoriesLength - 1; i >= 0; --i) {
|
||||
var requestPolicyFactory = this._requestPolicyFactories[i];
|
||||
firstRequestPolicy = requestPolicyFactory.create(firstRequestPolicy, this._requestPolicyOptions);
|
||||
requestPolicyChainHead = requestPolicyFactory(requestPolicyChainHead, this._requestPolicyOptions);
|
||||
}
|
||||
}
|
||||
return firstRequestPolicy.send(request);
|
||||
return requestPolicyChainHead.send(request);
|
||||
};
|
||||
return HttpPipeline;
|
||||
}());
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"httpPipeline.js","sourceRoot":"","sources":["../../lib/httpPipeline.ts"],"names":[],"mappings":";AA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AAI,sBAAsB,GAAkB,IAAI,mEAAgC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QACnG,EAAE,CAAC,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC,CAAC;YAC/B,IAAM,4BAA4B,GAAW,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC;YACjF,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,4BAA4B,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;gBACzD,IAAM,oBAAoB,GAAyB,IAAI,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC;gBACnF,sBAAsB,GAAG,oBAAoB,CAAC,sBAAsB,EAAE,IAAI,CAAC,qBAAqB,CAAC,CAAC;YACtG,CAAC;QACL,CAAC;QACD,MAAM,CAAC,sBAAsB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC;IACL,mBAAC;AAAD,CAAC,AAjCD,IAiCC;AAjCY,oCAAY"}
|
|
@ -2,19 +2,20 @@
|
|||
// 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 });
|
||||
var httpHeaders_1 = require("./httpHeaders");
|
||||
/**
|
||||
* An individual HTTP request that can be sent with a HttpClient.
|
||||
*/
|
||||
var HttpRequest = /** @class */ (function () {
|
||||
function HttpRequest(_httpMethod, _url, _headers, _body) {
|
||||
function HttpRequest(_httpMethod, _url, headers, _body) {
|
||||
this._httpMethod = _httpMethod;
|
||||
this._url = _url;
|
||||
this._headers = _headers;
|
||||
this._body = _body;
|
||||
if (!this._url) {
|
||||
var urlString = (this._url === undefined || this._url === null ? this._url : "\"" + this._url + "\"");
|
||||
throw new Error(urlString + " is not a valid URL for a HttpRequest.");
|
||||
}
|
||||
this._headers = (headers instanceof httpHeaders_1.HttpHeaders ? headers : new httpHeaders_1.HttpHeaders(headers));
|
||||
}
|
||||
Object.defineProperty(HttpRequest.prototype, "httpMethod", {
|
||||
/**
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"httpRequest.js","sourceRoot":"","sources":["../../lib/httpRequest.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAK/F;;GAEG;AACH;IACI,qBAAoB,WAAuB,EAAU,IAAY,EAAU,QAAqB,EAAU,KAAc;QAApG,gBAAW,GAAX,WAAW,CAAY;QAAU,SAAI,GAAJ,IAAI,CAAQ;QAAU,aAAQ,GAAR,QAAQ,CAAa;QAAU,UAAK,GAAL,KAAK,CAAS;QACpH,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;YACb,IAAM,SAAS,GAAW,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,OAAI,IAAI,CAAC,IAAI,OAAG,CAAC,CAAC;YACzG,MAAM,IAAI,KAAK,CAAI,SAAS,2CAAwC,CAAC,CAAC;QAC1E,CAAC;IACL,CAAC;IAKD,sBAAW,mCAAU;QAHrB;;WAEG;aACH;YACI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;QAC5B,CAAC;;;OAAA;IAKD,sBAAW,4BAAG;QAHd;;WAEG;aACH;YACI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;QACrB,CAAC;;;OAAA;IAKD,sBAAW,gCAAO;QAHlB;;WAEG;aACH;YACI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;QACzB,CAAC;;;OAAA;IAKD,sBAAW,6BAAI;QAHf;;WAEG;aACH;YACI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;QACtB,CAAC;;;OAAA;IACL,kBAAC;AAAD,CAAC,AAnCD,IAmCC;AAnCY,kCAAW"}
|
||||
{"version":3,"file":"httpRequest.js","sourceRoot":"","sources":["../../lib/httpRequest.ts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microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. 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 __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var requestPolicy_1 = require("../requestPolicy");
|
||||
var httpPipelineLogLevel_1 = require("../httpPipelineLogLevel");
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates UserAgentRequestPolicies.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
*/
|
||||
function userAgentRequestPolicyFactory(userAgent) {
|
||||
return function (nextPolicy, options) {
|
||||
return new UserAgentRequestPolicy(userAgent, nextPolicy, options);
|
||||
};
|
||||
}
|
||||
exports.userAgentRequestPolicyFactory = userAgentRequestPolicyFactory;
|
||||
var UserAgentRequestPolicy = /** @class */ (function (_super) {
|
||||
__extends(UserAgentRequestPolicy, _super);
|
||||
function UserAgentRequestPolicy(_userAgent, nextPolicy, options) {
|
||||
var _this = _super.call(this, nextPolicy, options) || this;
|
||||
_this._userAgent = _userAgent;
|
||||
return _this;
|
||||
}
|
||||
UserAgentRequestPolicy.prototype.send = function (request) {
|
||||
if (this.shouldLog(httpPipelineLogLevel_1.HttpPipelineLogLevel.INFO)) {
|
||||
this.log(httpPipelineLogLevel_1.HttpPipelineLogLevel.INFO, "Set \"User-Agent\" header to \"" + this._userAgent + "\".");
|
||||
}
|
||||
request.headers.set("User-Agent", this._userAgent);
|
||||
return this.nextPolicy.send(request);
|
||||
};
|
||||
return UserAgentRequestPolicy;
|
||||
}(requestPolicy_1.BaseRequestPolicy));
|
||||
//# sourceMappingURL=userAgentRequestPolicyFactory.js.map
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"userAgentRequestPolicyFactory.js","sourceRoot":"","sources":["../../../lib/policies/userAgentRequestPolicyFactory.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;;;;;;;;;;;AAE/F,kDAAoE;AAKpE,gEAA+D;AAE/D;;;GAGG;AACH,uCAA8C,SAAiB;IAC3D,MAAM,CAAC,UAAC,UAAyB,EAAE,OAA6B;QAC5D,MAAM,CAAC,IAAI,sBAAsB,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IACtE,CAAC,CAAC;AACN,CAAC;AAJD,sEAIC;AAED;IAAqC,0CAAiB;IAClD,gCAAoB,UAAkB,EAAE,UAAyB,EAAE,OAA6B;QAAhG,YACI,kBAAM,UAAU,EAAE,OAAO,CAAC,SAC7B;QAFmB,gBAAU,GAAV,UAAU,CAAQ;;IAEtC,CAAC;IAED,qCAAI,GAAJ,UAAK,OAAoB;QACrB,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,2CAAoB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAC5C,IAAI,CAAC,GAAG,CAAC,2CAAoB,CAAC,IAAI,EAAE,oCAA+B,IAAI,CAAC,UAAU,QAAI,CAAC,CAAC;QAC5F,CAAC;QACD,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QACnD,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;IACL,6BAAC;AAAD,CAAC,AAZD,CAAqC,iCAAiB,GAYrD"}
|
|
@ -2,4 +2,42 @@
|
|||
// 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 });
|
||||
/**
|
||||
* A base class implementation of RequestPolicy.
|
||||
*/
|
||||
var BaseRequestPolicy = /** @class */ (function () {
|
||||
function BaseRequestPolicy(_nextPolicy, _options) {
|
||||
this._nextPolicy = _nextPolicy;
|
||||
this._options = _options;
|
||||
}
|
||||
Object.defineProperty(BaseRequestPolicy.prototype, "nextPolicy", {
|
||||
/**
|
||||
* The next RequestPolicy in the HttpPipeline.
|
||||
*/
|
||||
get: function () {
|
||||
return this._nextPolicy;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
/**
|
||||
* Get whether or not a log with the provided log level should be logged.
|
||||
* @param logLevel The log level of the log that will be logged.
|
||||
* @returns Whether or not a log with the provided log level should be logged.
|
||||
*/
|
||||
BaseRequestPolicy.prototype.shouldLog = function (logLevel) {
|
||||
return this._options.shouldLog(logLevel);
|
||||
};
|
||||
/**
|
||||
* Attempt to log the provided message to the provided logger. If no logger was provided or if
|
||||
* the log level does not meat the logger's threshold, then nothing will be logged.
|
||||
* @param logLevel The log level of this log.
|
||||
* @param message The message of this log.
|
||||
*/
|
||||
BaseRequestPolicy.prototype.log = function (logLevel, message) {
|
||||
this._options.log(logLevel, message);
|
||||
};
|
||||
return BaseRequestPolicy;
|
||||
}());
|
||||
exports.BaseRequestPolicy = BaseRequestPolicy;
|
||||
//# sourceMappingURL=requestPolicy.js.map
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"requestPolicy.js","sourceRoot":"","sources":["../../lib/requestPolicy.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F"}
|
||||
{"version":3,"file":"requestPolicy.js","sourceRoot":"","sources":["../../lib/requestPolicy.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAoB/F;;GAEG;AACH;IACI,2BAAoB,WAA0B,EAAU,QAA8B;QAAlE,gBAAW,GAAX,WAAW,CAAe;QAAU,aAAQ,GAAR,QAAQ,CAAsB;IACtF,CAAC;IAOD,sBAAc,yCAAU;QAHxB;;WAEG;aACH;YACI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;QAC5B,CAAC;;;OAAA;IAED;;;;OAIG;IACO,qCAAS,GAAnB,UAAoB,QAA8B;QAC9C,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;IAC7C,CAAC;IAED;;;;;OAKG;IACO,+BAAG,GAAb,UAAc,QAA8B,EAAE,OAAe;QACzD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IACL,wBAAC;AAAD,CAAC,AA/BD,IA+BC;AA/BqB,8CAAiB"}
|
|
@ -0,0 +1,31 @@
|
|||
"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 });
|
||||
var fetchHttpClient_1 = require("../lib/fetchHttpClient");
|
||||
var httpRequest_1 = require("../lib/httpRequest");
|
||||
var assert = require("assert");
|
||||
describe("fetchHttpClient", function () {
|
||||
it("should send HTTP requests", function () {
|
||||
var request = new httpRequest_1.HttpRequest("GET", "http://www.example.com", {});
|
||||
return fetchHttpClient_1.fetchHttpClient(request)
|
||||
.then(function (response) {
|
||||
assert.deepStrictEqual(response.request, request);
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert(response.headers);
|
||||
assert.strictEqual(response.headers.get("accept-ranges"), "bytes");
|
||||
assert.strictEqual(response.headers.get("connection"), "close");
|
||||
assert.strictEqual(response.headers.get("content-encoding"), "gzip");
|
||||
assert.strictEqual(response.headers.get("content-length"), "606");
|
||||
assert.strictEqual(response.headers.get("content-type"), "text/html");
|
||||
assert.strictEqual(response.headers.get("vary"), "Accept-Encoding");
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then(function (responseBody) {
|
||||
assert(responseBody);
|
||||
var expectedResponseBody = "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n";
|
||||
assert.strictEqual(responseBody, expectedResponseBody);
|
||||
});
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=fetchHttpClientTests.js.map
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"fetchHttpClientTests.js","sourceRoot":"","sources":["../../test/fetchHttpClientTests.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAE/F,0DAAyD;AACzD,kDAAiD;AAEjD,+BAAiC;AAEjC,QAAQ,CAAC,iBAAiB,EAAE;IACxB,EAAE,CAAC,2BAA2B,EAAE;QAC5B,IAAM,OAAO,GAAG,IAAI,yBAAW,CAAC,KAAK,EAAE,wBAAwB,EAAE,EAAE,CAAC,CAAC;QACrE,MAAM,CAAC,iCAAe,CAAC,OAAO,CAAC;aAC1B,IAAI,CAAC,UAAC,QAAsB;YACzB,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAClD,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;YAC7C,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YACzB,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC,CAAC;YACnE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC,CAAC;YAChE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC,CAAC;YACrE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,EAAE,KAAK,CAAC,CAAC;YAClE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,WAAW,CAAC,CAAC;YACtE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,iBAAiB,CAAC,CAAC;YAEpE,MAAM,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;QACjC,CAAC,CAAC;aACD,IAAI,CAAC,UAAC,YAAqB;YACxB,MAAM,CAAC,YAAY,CAAC,CAAC;YAErB,IAAM,oBAAoB,GAC1C,4zCAkDC,CAAC;YACc,MAAM,CAAC,WAAW,CAAC,YAAY,EAAE,oBAAoB,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;IACX,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@ -1,24 +1,35 @@
|
|||
"use strict";
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. 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 __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var inMemoryHttpClient_1 = require("./inMemoryHttpClient");
|
||||
var inMemoryHttpResponse_1 = require("./inMemoryHttpResponse");
|
||||
var requestPolicy_1 = require("../lib/requestPolicy");
|
||||
var httpPipeline_1 = require("../lib/httpPipeline");
|
||||
var httpRequest_1 = require("../lib/httpRequest");
|
||||
var inMemoryHttpResponse_1 = require("./inMemoryHttpResponse");
|
||||
var userAgentRequestPolicyFactory_1 = require("../lib/policies/userAgentRequestPolicyFactory");
|
||||
var assert = require("assert");
|
||||
describe("HttpPipeline", function () {
|
||||
it("should send requests when no request policies are assigned", function () {
|
||||
var httpClient = new inMemoryHttpClient_1.InMemoryHttpClient(function (request) {
|
||||
var httpClient = function (request) {
|
||||
return Promise.resolve(new inMemoryHttpResponse_1.InMemoryHttpResponse(request, 200, {}, "hello"));
|
||||
});
|
||||
};
|
||||
var httpPipeline = new httpPipeline_1.HttpPipeline([], { httpClient: httpClient });
|
||||
var httpRequest = new httpRequest_1.HttpRequest("GET", "http://www.example.com", {});
|
||||
return httpPipeline.send(httpRequest)
|
||||
.then(function (response) {
|
||||
assert.deepStrictEqual(response.request, httpRequest);
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert.deepStrictEqual(response.headers, {});
|
||||
assert.deepStrictEqual(response.headers.toJson(), {});
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then(function (responseBodyAsText) {
|
||||
|
@ -26,20 +37,54 @@ describe("HttpPipeline", function () {
|
|||
});
|
||||
});
|
||||
it("should send requests when request-modifying request policies are assigned", function () {
|
||||
var httpClient = new inMemoryHttpClient_1.InMemoryHttpClient(function (request) {
|
||||
return Promise.resolve(new inMemoryHttpResponse_1.InMemoryHttpResponse(request, 200, {}, "hello"));
|
||||
});
|
||||
var httpPipeline = new httpPipeline_1.HttpPipeline([], { httpClient: httpClient });
|
||||
var httpClient = function (request) {
|
||||
assert.deepStrictEqual(request.headers.toJson(), { "User-Agent": "my user agent string" });
|
||||
return Promise.resolve(new inMemoryHttpResponse_1.InMemoryHttpResponse(request, 200, {}, "hello2"));
|
||||
};
|
||||
var httpPipeline = new httpPipeline_1.HttpPipeline([userAgentRequestPolicyFactory_1.userAgentRequestPolicyFactory("my user agent string")], { httpClient: httpClient });
|
||||
var httpRequest = new httpRequest_1.HttpRequest("GET", "http://www.example.com", {});
|
||||
return httpPipeline.send(httpRequest)
|
||||
.then(function (response) {
|
||||
assert.deepStrictEqual(response.request, httpRequest);
|
||||
assert.deepStrictEqual(response.request.headers.toJson(), { "User-Agent": "my user agent string" });
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert.deepStrictEqual(response.headers, {});
|
||||
assert.deepStrictEqual(response.headers.toJson(), {});
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then(function (responseBodyAsText) {
|
||||
assert.strictEqual("hello", responseBodyAsText);
|
||||
assert.strictEqual("hello2", responseBodyAsText);
|
||||
});
|
||||
});
|
||||
it("should send requests when response-modifying request policies are assigned", function () {
|
||||
var httpClient = function (request) {
|
||||
assert.deepStrictEqual(request.headers.toJson(), {});
|
||||
return Promise.resolve(new inMemoryHttpResponse_1.InMemoryHttpResponse(request, 200, {}, "hello3"));
|
||||
};
|
||||
var ResponseModifyingRequestPolicy = /** @class */ (function (_super) {
|
||||
__extends(ResponseModifyingRequestPolicy, _super);
|
||||
function ResponseModifyingRequestPolicy() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
ResponseModifyingRequestPolicy.prototype.send = function (request) {
|
||||
return this.nextPolicy.send(request).then(function (response) {
|
||||
response.headers.set("My-Header", "My-Value");
|
||||
return response;
|
||||
});
|
||||
};
|
||||
return ResponseModifyingRequestPolicy;
|
||||
}(requestPolicy_1.BaseRequestPolicy));
|
||||
var httpPipeline = new httpPipeline_1.HttpPipeline([function (nextPolicy, options) { return new ResponseModifyingRequestPolicy(nextPolicy, options); }], { httpClient: httpClient });
|
||||
var httpRequest = new httpRequest_1.HttpRequest("GET", "http://www.example.com", {});
|
||||
return httpPipeline.send(httpRequest)
|
||||
.then(function (response) {
|
||||
assert.deepStrictEqual(response.request, httpRequest);
|
||||
assert.deepStrictEqual(response.request.headers.toJson(), {});
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert.deepStrictEqual(response.headers.toJson(), { "My-Header": "My-Value" });
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then(function (responseBodyAsText) {
|
||||
assert.strictEqual("hello3", responseBodyAsText);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"httpPipelineTests.js","sourceRoot":"","sources":["../../test/httpPipelineTests.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAE/F,2DAA0D;AAC1D,+DAA8D;AAC9D,oDAAmD;AACnD,kDAAiD;AAEjD,+BAAiC;AAEjC,QAAQ,CAAC,cAAc,EAAE;IACrB,EAAE,CAAC,4DAA4D,EAAE;QAC7D,IAAM,UAAU,GAAG,IAAI,uCAAkB,CAAC,UAAC,OAAoB;YAC3D,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,2CAAoB,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC;QAChF,CAAC,CAAC,CAAA;QACF,IAAM,YAAY,GAAG,IAAI,2BAAY,CAAC,EAAE,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;QAEtE,IAAM,WAAW,GAAG,IAAI,yBAAW,CAAC,KAAK,EAAE,wBAAwB,EAAE,EAAE,CAAC,CAAC;QACzE,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,WAAW,CAAC;aAChC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CAAC,CAAC;YACtD,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;YAC9D,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;YAC7C,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,EAAC,WAAW,EAAE,UAAU,EAAC,CAAC,CAAC;YAC7E,MAAM,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;QACjC,CAAC,CAAC;aACD,IAAI,CAAC,UAAC,kBAAsC;YACzC,MAAM,CAAC,WAAW,CAAC,QAAQ,EAAE,kBAAkB,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;IACX,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@ -20,28 +20,28 @@ describe("HttpRequest", function () {
|
|||
var httpRequest = new httpRequest_1.HttpRequest("GET", "www.example.com", {});
|
||||
assert.strictEqual(httpRequest.httpMethod, "GET");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, {});
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), {});
|
||||
assert.strictEqual(httpRequest.body, undefined);
|
||||
});
|
||||
it("should return a valid POST HttpRequest when the body is undefined", function () {
|
||||
var httpRequest = new httpRequest_1.HttpRequest("POST", "www.example.com", {});
|
||||
assert.strictEqual(httpRequest.httpMethod, "POST");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, {});
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), {});
|
||||
assert.strictEqual(httpRequest.body, undefined);
|
||||
});
|
||||
it("should return a valid POST HttpRequest when the body is \"\"", function () {
|
||||
var httpRequest = new httpRequest_1.HttpRequest("POST", "www.example.com", {}, "");
|
||||
assert.strictEqual(httpRequest.httpMethod, "POST");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, {});
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), {});
|
||||
assert.strictEqual(httpRequest.body, "");
|
||||
});
|
||||
it("should return a valid POST HttpRequest when the body is \"hello\"", function () {
|
||||
var httpRequest = new httpRequest_1.HttpRequest("POST", "www.example.com", { "Content-Length": 5 }, "hello");
|
||||
var httpRequest = new httpRequest_1.HttpRequest("POST", "www.example.com", { "Content-Length": "5" }, "hello");
|
||||
assert.strictEqual(httpRequest.httpMethod, "POST");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, { "Content-Length": 5 });
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), { "Content-Length": "5" });
|
||||
assert.strictEqual(httpRequest.body, "hello");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"httpRequestTests.js","sourceRoot":"","sources":["../../test/httpRequestTests.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAE/F,kDAAiD;AACjD,+BAAiC;AAEjC,QAAQ,CAAC,aAAa,EAAE;IACpB,QAAQ,CAAC,aAAa,EAAE;QACpB,EAAE,CAAC,4CAA0C,EAAE;YAC3C,IAAI,CAAC;gBACD,IAAI,yBAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;gBAC/B,MAAM,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;YAChD,CAAC;YAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;gBACb,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;gBAC7B,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,EAAE,4CAA0C,CAAC,CAAC;YAClF,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,2EAAyE,EAAE;YAC1E,IAAM,WAA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,EAAE,EAAE,CAAC,CAAC;YACzD,MAAM,CAAC,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC7C,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,mEAAiE,EAAE;YAClE,IAAM,WAAW,GAAgB,IAAI,yBAAW,CAAC,MAAM,EAAE,iBAAiB,EAAE,EAAC,gBAAgB,EAAE,GAAG,EAAC,EAAE,OAAO,CAAC,CAAC;YAC9G,MAAM,CAAC,WAAW,CAAC,WAAW,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;YACnD,MAAM,CAAC,WAAW,CAAC,WAAW,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC;YACvD,MAAM,CAAC,eAAe,CAAC,WAAW,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,EAAC,gBAAgB,EAAE,GAAG,EAAC,CAAC,CAAC;YAC9E,MAAM,CAAC,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@ -2,12 +2,13 @@
|
|||
// 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 });
|
||||
var httpHeaders_1 = require("../lib/httpHeaders");
|
||||
var InMemoryHttpResponse = /** @class */ (function () {
|
||||
function InMemoryHttpResponse(_request, _statusCode, _headers, _bodyText) {
|
||||
function InMemoryHttpResponse(_request, _statusCode, headers, _bodyText) {
|
||||
this._request = _request;
|
||||
this._statusCode = _statusCode;
|
||||
this._headers = _headers;
|
||||
this._bodyText = _bodyText;
|
||||
this._headers = (headers instanceof httpHeaders_1.HttpHeaders ? headers : new httpHeaders_1.HttpHeaders(headers));
|
||||
}
|
||||
Object.defineProperty(InMemoryHttpResponse.prototype, "request", {
|
||||
get: function () {
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"inMemoryHttpResponse.js","sourceRoot":"","sources":["../../test/inMemoryHttpResponse.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAM/F;IACI,8BAAoB,QAAqB,EAAU,WAAmB,EAAU,QAAqB,EAAU,SAAkB;QAA7G,aAAQ,GAAR,QAAQ,CAAa;QAAU,gBAAW,GAAX,WAAW,CAAQ;QAAU,aAAQ,GAAR,QAAQ,CAAa;QAAU,cAAS,GAAT,SAAS,CAAS;IACjI,CAAC;IAED,sBAAW,yCAAO;aAAlB;YACI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;QACzB,CAAC;;;OAAA;IAED,sBAAW,4CAAU;aAArB;YACI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;QAC5B,CAAC;;;OAAA;IAED,sBAAW,yCAAO;aAAlB;YACI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;QACzB,CAAC;;;OAAA;IAED,yCAAU,GAAV;QACI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3C,CAAC;IACD,yCAAU,GAAV;QACI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IACpF,CAAC;IACL,2BAAC;AAAD,CAAC,AAtBD,IAsBC;AAtBY,oDAAoB"}
|
||||
{"version":3,"file":"inMemoryHttpResponse.js","sourceRoot":"","sources":["../../test/inMemoryHttpResponse.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,+FAA+F;;AAI/F,kDAAiE;AAEjE;IAGI,8BAAoB,QAAqB,EAAU,WAAmB,EAAE,OAAqC,EAAU,SAAkB;QAArH,aAAQ,GAAR,QAAQ,CAAa;QAAU,gBAAW,GAAX,WAAW,CAAQ;QAAiD,cAAS,GAAT,SAAS,CAAS;QACrI,IAAI,CAAC,QAAQ,GAAG,CAAC,OAAO,YAAY,yBAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,yBAAW,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1F,CAAC;IAED,sBAAW,yCAAO;aAAlB;YACI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;QACzB,CAAC;;;OAAA;IAED,sBAAW,4CAAU;aAArB;YACI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;QAC5B,CAAC;;;OAAA;IAED,sBAAW,yCAAO;aAAlB;YACI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;QACzB,CAAC;;;OAAA;IAED,yCAAU,GAAV;QACI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3C,CAAC;IACD,yCAAU,GAAV;QACI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IACpF,CAAC;IACL,2BAAC;AAAD,CAAC,AAzBD,IAyBC;AAzBY,oDAAoB"}
|
|
@ -1,69 +1,60 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { HttpClient } from "./httpClient";
|
||||
import { HttpHeaders } from "./httpHeaders";
|
||||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
import { HttpHeaders } from "./httpHeaders";
|
||||
|
||||
type FetchMethod = (url: string, options: any) => Response;
|
||||
type FetchMethod = (url: string, options: RequestInit) => Response;
|
||||
|
||||
/**
|
||||
* Provides the fetch() method based on the environment.
|
||||
* @returns {fetch} fetch - The fetch() method available in the environment to make requests
|
||||
*/
|
||||
function getFetch(): FetchMethod {
|
||||
// using window.Fetch in Edge gives a TypeMismatchError
|
||||
// (https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8546263/).
|
||||
// Hence we will be using the fetch-ponyfill for Edge.
|
||||
return (window && window.fetch && window.navigator && window.navigator.userAgent && window.navigator.userAgent.indexOf("Edge/") === -1)
|
||||
? window.fetch.bind(window)
|
||||
: require("fetch-ponyfill")({ useCookie: true }).fetch;
|
||||
return require("fetch-ponyfill")({ useCookie: true }).fetch;
|
||||
}
|
||||
|
||||
/**
|
||||
* The cached fetch method that will be used to send HTTP requests.
|
||||
*/
|
||||
let myFetch: FetchMethod;
|
||||
let fetch: FetchMethod;
|
||||
|
||||
/**
|
||||
* A HttpClient implementation that uses fetch to send HTTP requests.
|
||||
* @param request The request to send.
|
||||
*/
|
||||
export class FetchHttpClient implements HttpClient {
|
||||
constructor() {
|
||||
if (!myFetch) {
|
||||
myFetch = getFetch();
|
||||
}
|
||||
export async function fetchHttpClient(request: HttpRequest): Promise<HttpResponse> {
|
||||
if (!fetch) {
|
||||
fetch = getFetch();
|
||||
}
|
||||
|
||||
async send(request: HttpRequest): Promise<HttpResponse> {
|
||||
let result: Promise<HttpResponse>;
|
||||
let result: Promise<HttpResponse>;
|
||||
|
||||
try {
|
||||
const fetchResponse: Response = await myFetch(request.url.toString(), request);
|
||||
try {
|
||||
const fetchRequestOptions: RequestInit = {
|
||||
method: request.httpMethod,
|
||||
headers: request.headers.toJson(),
|
||||
body: request.body
|
||||
};
|
||||
|
||||
const fetchResponseHeaders: Headers = fetchResponse.headers;
|
||||
const responseHeaders: HttpHeaders = {};
|
||||
for (const headerName in fetchResponseHeaders.keys()) {
|
||||
const headerValue: string | null = fetchResponseHeaders.get(headerName);
|
||||
if (headerValue) {
|
||||
responseHeaders[headerName] = headerValue;
|
||||
}
|
||||
}
|
||||
const fetchResponse: Response = await fetch(request.url, fetchRequestOptions);
|
||||
|
||||
const response: HttpResponse = {
|
||||
request: request,
|
||||
statusCode: fetchResponse.status,
|
||||
headers: responseHeaders,
|
||||
bodyAsText: () => fetchResponse.text(),
|
||||
bodyAsJson: () => fetchResponse.json()
|
||||
};
|
||||
const responseHeaders = new HttpHeaders();
|
||||
const fetchResponseHeaders: Headers = fetchResponse.headers;
|
||||
fetchResponseHeaders.forEach((headerValue: string, headerName: string) => { responseHeaders.set(headerName, headerValue); });
|
||||
|
||||
result = Promise.resolve(response);
|
||||
} catch (err) {
|
||||
result = Promise.reject(err);
|
||||
}
|
||||
const response: HttpResponse = {
|
||||
request: request,
|
||||
statusCode: fetchResponse.status,
|
||||
headers: responseHeaders,
|
||||
bodyAsText: () => fetchResponse.text(),
|
||||
bodyAsJson: () => fetchResponse.json()
|
||||
};
|
||||
|
||||
return result;
|
||||
result = Promise.resolve(response);
|
||||
} catch (err) {
|
||||
result = Promise.reject(err);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
|
@ -1,17 +1,11 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
|
||||
/**
|
||||
* A interface that can send HttpRequests and receive promised HttpResponses.
|
||||
* A function that can send HttpRequests and receive promised HttpResponses.
|
||||
* @param request The HTTP request to send.
|
||||
* @returns A Promise that resolves to the HttpResponse from the targeted server.
|
||||
*/
|
||||
export interface HttpClient {
|
||||
/**
|
||||
* Send the provided HttpRequest request.
|
||||
* @param request The HTTP request to send.
|
||||
* @return A Promise that resolves to the HttpResponse from the targeted server.
|
||||
*/
|
||||
send(request: HttpRequest): Promise<HttpResponse>;
|
||||
}
|
||||
export type HttpClient = (request: HttpRequest) => Promise<HttpResponse>;
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
import { HttpClient } from "./httpClient";
|
||||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
import { RequestPolicy } from "./requestPolicy";
|
||||
|
||||
/**
|
||||
* An adapter type that adapts a HttpClient to look like a RequestPolicy.
|
||||
*/
|
||||
export class HttpClientToRequestPolicyAdapter implements RequestPolicy {
|
||||
constructor(private _httpClient: HttpClient) {
|
||||
}
|
||||
|
||||
send(request: HttpRequest): Promise<HttpResponse> {
|
||||
return this._httpClient(request);
|
||||
}
|
||||
}
|
|
@ -4,4 +4,111 @@
|
|||
/**
|
||||
* A collection of HttpHeaders that can be sent with a HTTP request.
|
||||
*/
|
||||
export type HttpHeaders = { [headerName: string]: string | number };
|
||||
function getHeaderKey(headerName: string) {
|
||||
return headerName.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* An individual header within a HttpHeaders collection.
|
||||
*/
|
||||
export interface HttpHeader {
|
||||
/**
|
||||
* The name of the header.
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The value of the header.
|
||||
*/
|
||||
value: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A HttpHeaders collection represented as a simple JSON object.
|
||||
*/
|
||||
export type RawHttpHeaders = { [headerName: string]: string };
|
||||
|
||||
/**
|
||||
* A collection of HTTP header key/value pairs.
|
||||
*/
|
||||
export class HttpHeaders {
|
||||
private _headersMap: { [headerKey: string]: HttpHeader };
|
||||
|
||||
constructor(rawHeaders?: RawHttpHeaders) {
|
||||
this._headersMap = {};
|
||||
if (rawHeaders) {
|
||||
for (const 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.
|
||||
*/
|
||||
public set(headerName: string, headerValue: string | number): void {
|
||||
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.
|
||||
*/
|
||||
public get(headerName: string): string | undefined {
|
||||
const header: HttpHeader = this._headersMap[getHeaderKey(headerName)];
|
||||
return !header ? undefined : header.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the headers that are contained in this collection.
|
||||
*/
|
||||
public headers(): HttpHeader[] {
|
||||
const headers: HttpHeader[] = [];
|
||||
for (const headerKey in this._headersMap) {
|
||||
headers.push(this._headersMap[headerKey]);
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the header names that are contained in this collection.
|
||||
*/
|
||||
public headerNames(): string[] {
|
||||
const headerNames: string[] = [];
|
||||
const headers: HttpHeader[] = this.headers();
|
||||
for (let i = 0; i < headers.length; ++i) {
|
||||
headerNames.push(headers[i].name);
|
||||
}
|
||||
return headerNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the header names that are contained in this collection.
|
||||
*/
|
||||
public headerValues(): string[] {
|
||||
const headerValues: string[] = [];
|
||||
const headers: HttpHeader[] = this.headers();
|
||||
for (let i = 0; i < headers.length; ++i) {
|
||||
headerValues.push(headers[i].value);
|
||||
}
|
||||
return headerValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSON object representation of this HTTP header collection.
|
||||
*/
|
||||
public toJson(): RawHttpHeaders {
|
||||
const result: RawHttpHeaders = {};
|
||||
|
||||
const headers: HttpHeader[] = this.headers();
|
||||
for (let i = 0; i < headers.length; ++i) {
|
||||
result[headers[i].name] = headers[i].value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,20 +1,20 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { fetchHttpClient } from "./fetchHttpClient";
|
||||
import { HttpClient } from "./httpClient";
|
||||
import { HttpClientToRequestPolicyAdapter } from "./httpClientToRequestPolicyAdapter";
|
||||
import { HttpPipelineOptions } from "./httpPipelineOptions";
|
||||
import { RequestPolicy } from "./requestPolicy";
|
||||
import { RequestPolicyFactory } from "./requestPolicyFactory";
|
||||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
import { HttpClient } from "./httpClient";
|
||||
import { FetchHttpClient } from "./fetchHttpClient";
|
||||
import { RequestPolicy } from "./requestPolicy";
|
||||
import { RequestPolicyFactory } from "./requestPolicyFactory";
|
||||
import { RequestPolicyOptions } from "./requestPolicyOptions";
|
||||
|
||||
let defaultHttpClient: HttpClient;
|
||||
|
||||
function getDefaultHttpClient(): HttpClient {
|
||||
if (!defaultHttpClient) {
|
||||
defaultHttpClient = new FetchHttpClient();
|
||||
defaultHttpClient = fetchHttpClient;
|
||||
}
|
||||
return defaultHttpClient;
|
||||
}
|
||||
|
@ -46,14 +46,14 @@ export class HttpPipeline {
|
|||
* @return A Promise that resolves to the HttpResponse from the targeted server.
|
||||
*/
|
||||
public send(request: HttpRequest): Promise<HttpResponse> {
|
||||
let firstRequestPolicy: RequestPolicy = this._httpClient;
|
||||
let requestPolicyChainHead: RequestPolicy = new HttpClientToRequestPolicyAdapter(this._httpClient);
|
||||
if (this._requestPolicyFactories) {
|
||||
const requestPolicyFactoriesLength: number = this._requestPolicyFactories.length;
|
||||
for (let i = requestPolicyFactoriesLength - 1; i >= 0; --i) {
|
||||
const requestPolicyFactory: RequestPolicyFactory = this._requestPolicyFactories[i];
|
||||
firstRequestPolicy = requestPolicyFactory.create(firstRequestPolicy, this._requestPolicyOptions);
|
||||
requestPolicyChainHead = requestPolicyFactory(requestPolicyChainHead, this._requestPolicyOptions);
|
||||
}
|
||||
}
|
||||
return firstRequestPolicy.send(request);
|
||||
return requestPolicyChainHead.send(request);
|
||||
}
|
||||
}
|
|
@ -1,18 +1,21 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { HttpHeaders } from "./httpHeaders";
|
||||
import { HttpHeaders, RawHttpHeaders } from "./httpHeaders";
|
||||
import { HttpMethod } from "./httpMethod";
|
||||
|
||||
/**
|
||||
* An individual HTTP request that can be sent with a HttpClient.
|
||||
*/
|
||||
export class HttpRequest {
|
||||
constructor(private _httpMethod: HttpMethod, private _url: string, private _headers: HttpHeaders, private _body?: string) {
|
||||
private readonly _headers: HttpHeaders;
|
||||
|
||||
constructor(private _httpMethod: HttpMethod, private _url: string, headers: HttpHeaders | RawHttpHeaders, private _body?: string) {
|
||||
if (!this._url) {
|
||||
const urlString: string = (this._url === undefined || this._url === null ? this._url : `"${this._url}"`);
|
||||
throw new Error(`${urlString} is not a valid URL for a HttpRequest.`);
|
||||
}
|
||||
|
||||
this._headers = (headers instanceof HttpHeaders ? headers : new HttpHeaders(headers));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,22 +1,20 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { RequestPolicy, BaseRequestPolicy } from "../requestPolicy";
|
||||
import { RequestPolicyFactory } from "../requestPolicyFactory";
|
||||
import { RequestPolicyOptions } from "../requestPolicyOptions";
|
||||
import { HttpPipelineLogLevel } from "../httpPipelineLogLevel";
|
||||
import { HttpRequest } from "../httpRequest";
|
||||
import { HttpResponse } from "../httpResponse";
|
||||
import { HttpPipelineLogLevel } from "../httpPipelineLogLevel";
|
||||
import { BaseRequestPolicy, RequestPolicy } from "../requestPolicy";
|
||||
import { RequestPolicyFactory } from "../requestPolicyFactory";
|
||||
import { RequestPolicyOptions } from "../requestPolicyOptions";
|
||||
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
export class UserAgentRequestPolicyFactory implements RequestPolicyFactory {
|
||||
constructor(private _userAgent: string) {
|
||||
}
|
||||
|
||||
public create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): RequestPolicy {
|
||||
return new UserAgentRequestPolicy(this._userAgent, nextPolicy, options);
|
||||
}
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates UserAgentRequestPolicies.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
*/
|
||||
export function userAgentRequestPolicyFactory(userAgent: string): RequestPolicyFactory {
|
||||
return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
|
||||
return new UserAgentRequestPolicy(userAgent, nextPolicy, options);
|
||||
};
|
||||
}
|
||||
|
||||
class UserAgentRequestPolicy extends BaseRequestPolicy {
|
||||
|
@ -25,8 +23,10 @@ class UserAgentRequestPolicy extends BaseRequestPolicy {
|
|||
}
|
||||
|
||||
send(request: HttpRequest): Promise<HttpResponse> {
|
||||
this.log(HttpPipelineLogLevel.INFO, `Set "User-Agent" header to "${this._userAgent}".`)
|
||||
request.headers['User-Agent'] = this._userAgent;
|
||||
if (this.shouldLog(HttpPipelineLogLevel.INFO)) {
|
||||
this.log(HttpPipelineLogLevel.INFO, `Set "User-Agent" header to "${this._userAgent}".`);
|
||||
}
|
||||
request.headers.set("User-Agent", this._userAgent);
|
||||
return this.nextPolicy.send(request);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { RequestPolicy } from "./requestPolicy";
|
||||
import { RequestPolicyOptions } from "./requestPolicyOptions";
|
||||
|
||||
|
@ -8,12 +7,7 @@ import { RequestPolicyOptions } from "./requestPolicyOptions";
|
|||
* Factory to create a RequestPolicy. RequestPolicies are instantiated per-request
|
||||
* so that they can contain instance state specific to that request/response exchange,
|
||||
* for example, the number of retries attempted so far in a counter.
|
||||
* @param nextPolicy The RequestPolicy that the created RequestPolicy will hand HttpRequests to and receive HttpResponses from.
|
||||
* @param options The set of RequestPolicyOptions that will apply to the created RequestPolicy.
|
||||
*/
|
||||
export interface RequestPolicyFactory {
|
||||
/**
|
||||
* Create a RequestPolicy.
|
||||
* @param nextPolicy The RequestPolicy that the created RequestPolicy will hand HttpRequests to and receive HttpResponses from.
|
||||
* @param options The set of RequestPolicyOptions that will apply to the created RequestPolicy.
|
||||
*/
|
||||
create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): RequestPolicy;
|
||||
}
|
||||
export type RequestPolicyFactory = (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => RequestPolicy;
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -3959,12 +3959,6 @@
|
|||
"urix": "0.1.0"
|
||||
}
|
||||
},
|
||||
"source-map-url": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz",
|
||||
"integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=",
|
||||
"dev": true
|
||||
},
|
||||
"source-map-support": {
|
||||
"version": "0.5.4",
|
||||
"resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.4.tgz",
|
||||
|
@ -3982,6 +3976,12 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"source-map-url": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz",
|
||||
"integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=",
|
||||
"dev": true
|
||||
},
|
||||
"spdx-correct": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.0.0.tgz",
|
||||
|
|
|
@ -73,4 +73,4 @@
|
|||
"build": "npm -s run-script tsc && webpack && npm -s run-script uglify",
|
||||
"tslint": "tslint -p . -c tslint.json --exclude test/**/*.ts"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
import * as assert from "assert";
|
||||
import { fetchHttpClient } from "../lib/fetchHttpClient";
|
||||
import { HttpRequest } from "../lib/httpRequest";
|
||||
import { HttpResponse } from "../lib/httpResponse";
|
||||
|
||||
describe("fetchHttpClient", () => {
|
||||
it("should send HTTP requests", () => {
|
||||
const request = new HttpRequest("GET", "http://www.example.com", {});
|
||||
return fetchHttpClient(request)
|
||||
.then((response: HttpResponse) => {
|
||||
assert.deepStrictEqual(response.request, request);
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert(response.headers);
|
||||
assert.strictEqual(response.headers.get("accept-ranges"), "bytes");
|
||||
assert.strictEqual(response.headers.get("connection"), "close");
|
||||
assert.strictEqual(response.headers.get("content-encoding"), "gzip");
|
||||
assert.strictEqual(response.headers.get("content-length"), "606");
|
||||
assert.strictEqual(response.headers.get("content-type"), "text/html");
|
||||
assert.strictEqual(response.headers.get("vary"), "Accept-Encoding");
|
||||
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then((responseBody?: string) => {
|
||||
assert(responseBody);
|
||||
|
||||
const expectedResponseBody: string =
|
||||
`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Example Domain</title>
|
||||
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<style type="text/css">
|
||||
body {
|
||||
background-color: #f0f0f2;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||
|
||||
}
|
||||
div {
|
||||
width: 600px;
|
||||
margin: 5em auto;
|
||||
padding: 50px;
|
||||
background-color: #fff;
|
||||
border-radius: 1em;
|
||||
}
|
||||
a:link, a:visited {
|
||||
color: #38488f;
|
||||
text-decoration: none;
|
||||
}
|
||||
@media (max-width: 700px) {
|
||||
body {
|
||||
background-color: #fff;
|
||||
}
|
||||
div {
|
||||
width: auto;
|
||||
margin: 0 auto;
|
||||
border-radius: 0;
|
||||
padding: 1em;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div>
|
||||
<h1>Example Domain</h1>
|
||||
<p>This domain is established to be used for illustrative examples in documents. You may use this
|
||||
domain in examples without prior coordination or asking for permission.</p>
|
||||
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
assert.strictEqual(responseBody, expectedResponseBody);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,19 +1,19 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { InMemoryHttpClient } from "./inMemoryHttpClient";
|
||||
import { InMemoryHttpResponse } from "./inMemoryHttpResponse";
|
||||
import * as assert from "assert";
|
||||
import { HttpClient } from "../lib/httpClient";
|
||||
import { HttpPipeline } from "../lib/httpPipeline";
|
||||
import { HttpRequest } from "../lib/httpRequest";
|
||||
import { HttpResponse } from "../lib/httpResponse";
|
||||
import * as assert from "assert";
|
||||
import { UserAgentRequestPolicyFactory } from "../lib/policies/userAgentRequestPolicyFactory";
|
||||
import { userAgentRequestPolicyFactory } from "../lib/policies/userAgentRequestPolicyFactory";
|
||||
import { BaseRequestPolicy } from "../lib/requestPolicy";
|
||||
import { InMemoryHttpResponse } from "./inMemoryHttpResponse";
|
||||
|
||||
describe("HttpPipeline", () => {
|
||||
it("should send requests when no request policies are assigned", () => {
|
||||
const httpClient = new InMemoryHttpClient((request: HttpRequest) => {
|
||||
const httpClient: HttpClient = (request: HttpRequest) => {
|
||||
return Promise.resolve(new InMemoryHttpResponse(request, 200, {}, "hello"));
|
||||
});
|
||||
};
|
||||
|
||||
const httpPipeline = new HttpPipeline([], { httpClient: httpClient });
|
||||
|
||||
|
@ -22,7 +22,7 @@ describe("HttpPipeline", () => {
|
|||
.then((response: HttpResponse) => {
|
||||
assert.deepStrictEqual(response.request, httpRequest);
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert.deepStrictEqual(response.headers, {});
|
||||
assert.deepStrictEqual(response.headers.toJson(), {});
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then((responseBodyAsText: string | undefined) => {
|
||||
|
@ -31,26 +31,59 @@ describe("HttpPipeline", () => {
|
|||
});
|
||||
|
||||
it("should send requests when request-modifying request policies are assigned", () => {
|
||||
const httpClient = new InMemoryHttpClient((request: HttpRequest) => {
|
||||
assert.deepStrictEqual(request.headers, { "User-Agent": "my user agent string" });
|
||||
const httpClient: HttpClient = (request: HttpRequest) => {
|
||||
assert.deepStrictEqual(request.headers.toJson(), { "User-Agent": "my user agent string" });
|
||||
return Promise.resolve(new InMemoryHttpResponse(request, 200, {}, "hello2"));
|
||||
});
|
||||
};
|
||||
|
||||
const httpPipeline = new HttpPipeline(
|
||||
[ new UserAgentRequestPolicyFactory("my user agent string") ],
|
||||
[ userAgentRequestPolicyFactory("my user agent string") ],
|
||||
{ httpClient: httpClient });
|
||||
|
||||
const httpRequest = new HttpRequest("GET", "http://www.example.com", {});
|
||||
return httpPipeline.send(httpRequest)
|
||||
.then((response: HttpResponse) => {
|
||||
assert.deepStrictEqual(response.request, httpRequest);
|
||||
assert.deepStrictEqual(response.request.headers, { "User-Agent": "my user agent string" });
|
||||
assert.deepStrictEqual(response.request.headers.toJson(), { "User-Agent": "my user agent string" });
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert.deepStrictEqual(response.headers, {});
|
||||
assert.deepStrictEqual(response.headers.toJson(), {});
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then((responseBodyAsText: string | undefined) => {
|
||||
assert.strictEqual("hello2", responseBodyAsText);
|
||||
});
|
||||
});
|
||||
|
||||
it("should send requests when response-modifying request policies are assigned", () => {
|
||||
const httpClient: HttpClient = (request: HttpRequest) => {
|
||||
assert.deepStrictEqual(request.headers.toJson(), {});
|
||||
return Promise.resolve(new InMemoryHttpResponse(request, 200, {}, "hello3"));
|
||||
};
|
||||
|
||||
class ResponseModifyingRequestPolicy extends BaseRequestPolicy {
|
||||
send(request: HttpRequest): Promise<HttpResponse> {
|
||||
return this.nextPolicy.send(request).then((response: HttpResponse) => {
|
||||
response.headers.set("My-Header", "My-Value");
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const httpPipeline = new HttpPipeline(
|
||||
[ (nextPolicy, options) => new ResponseModifyingRequestPolicy(nextPolicy, options) ],
|
||||
{ httpClient: httpClient });
|
||||
|
||||
const httpRequest = new HttpRequest("GET", "http://www.example.com", {});
|
||||
return httpPipeline.send(httpRequest)
|
||||
.then((response: HttpResponse) => {
|
||||
assert.deepStrictEqual(response.request, httpRequest);
|
||||
assert.deepStrictEqual(response.request.headers.toJson(), {});
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert.deepStrictEqual(response.headers.toJson(), {"My-Header": "My-Value"});
|
||||
return response.bodyAsText();
|
||||
})
|
||||
.then((responseBodyAsText: string | undefined) => {
|
||||
assert.strictEqual("hello3", responseBodyAsText);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,8 +1,7 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { HttpRequest } from "../lib/httpRequest";
|
||||
import * as assert from "assert";
|
||||
import { HttpRequest } from "../lib/httpRequest";
|
||||
|
||||
describe("HttpRequest", () => {
|
||||
describe("constructor", () => {
|
||||
|
@ -20,7 +19,7 @@ describe("HttpRequest", () => {
|
|||
const httpRequest: HttpRequest = new HttpRequest("GET", "www.example.com", {});
|
||||
assert.strictEqual(httpRequest.httpMethod, "GET");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, {});
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), {});
|
||||
assert.strictEqual(httpRequest.body, undefined);
|
||||
});
|
||||
|
||||
|
@ -28,7 +27,7 @@ describe("HttpRequest", () => {
|
|||
const httpRequest: HttpRequest = new HttpRequest("POST", "www.example.com", {});
|
||||
assert.strictEqual(httpRequest.httpMethod, "POST");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, {});
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), {});
|
||||
assert.strictEqual(httpRequest.body, undefined);
|
||||
});
|
||||
|
||||
|
@ -36,15 +35,15 @@ describe("HttpRequest", () => {
|
|||
const httpRequest: HttpRequest = new HttpRequest("POST", "www.example.com", {}, "");
|
||||
assert.strictEqual(httpRequest.httpMethod, "POST");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, {});
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), {});
|
||||
assert.strictEqual(httpRequest.body, "");
|
||||
});
|
||||
|
||||
it(`should return a valid POST HttpRequest when the body is "hello"`, () => {
|
||||
const httpRequest: HttpRequest = new HttpRequest("POST", "www.example.com", {"Content-Length": 5}, "hello");
|
||||
const httpRequest: HttpRequest = new HttpRequest("POST", "www.example.com", {"Content-Length": "5"}, "hello");
|
||||
assert.strictEqual(httpRequest.httpMethod, "POST");
|
||||
assert.strictEqual(httpRequest.url, "www.example.com");
|
||||
assert.deepStrictEqual(httpRequest.headers, {"Content-Length": 5});
|
||||
assert.deepStrictEqual(httpRequest.headers.toJson(), {"Content-Length": "5"});
|
||||
assert.strictEqual(httpRequest.body, "hello");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { HttpClient } from "../lib/httpClient";
|
||||
import { HttpRequest } from "../lib/httpRequest";
|
||||
import { HttpResponse } from "../lib/httpResponse";
|
||||
|
||||
/**
|
||||
* A simple HttpClient implementation that uses a provided function to respond to HttpRequests.
|
||||
*/
|
||||
export class InMemoryHttpClient implements HttpClient {
|
||||
constructor(private _requestHandler: (request: HttpRequest) => Promise<HttpResponse>) {
|
||||
}
|
||||
|
||||
send(request: HttpRequest): Promise<HttpResponse> {
|
||||
return this._requestHandler(request);
|
||||
}
|
||||
}
|
|
@ -1,12 +1,14 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { HttpResponse } from "../lib/httpResponse";
|
||||
import { HttpHeaders, RawHttpHeaders } from "../lib/httpHeaders";
|
||||
import { HttpRequest } from "../lib/httpRequest";
|
||||
import { HttpHeaders } from "../lib/httpHeaders";
|
||||
import { HttpResponse } from "../lib/httpResponse";
|
||||
|
||||
export class InMemoryHttpResponse implements HttpResponse {
|
||||
constructor(private _request: HttpRequest, private _statusCode: number, private _headers: HttpHeaders, private _bodyText?: string) {
|
||||
private readonly _headers: HttpHeaders;
|
||||
|
||||
constructor(private _request: HttpRequest, private _statusCode: number, headers: HttpHeaders | RawHttpHeaders, private _bodyText?: string) {
|
||||
this._headers = (headers instanceof HttpHeaders ? headers : new HttpHeaders(headers));
|
||||
}
|
||||
|
||||
public get request(): HttpRequest {
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
import { HttpClient } from "./httpClient";
|
||||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
/**
|
||||
* A HttpClient implementation that uses fetch to send HTTP requests.
|
||||
* @param request The request to send.
|
||||
*/
|
||||
export declare class FetchHttpClient implements HttpClient {
|
||||
constructor();
|
||||
send(request: HttpRequest): Promise<HttpResponse>;
|
||||
}
|
||||
export declare function fetchHttpClient(request: HttpRequest): Promise<HttpResponse>;
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
/**
|
||||
* A interface that can send HttpRequests and receive promised HttpResponses.
|
||||
* A function that can send HttpRequests and receive promised HttpResponses.
|
||||
* @param request The HTTP request to send.
|
||||
* @returns A Promise that resolves to the HttpResponse from the targeted server.
|
||||
*/
|
||||
export interface HttpClient {
|
||||
/**
|
||||
* Send the provided HttpRequest request.
|
||||
* @param request The HTTP request to send.
|
||||
* @return A Promise that resolves to the HttpResponse from the targeted server.
|
||||
*/
|
||||
send(request: HttpRequest): Promise<HttpResponse>;
|
||||
}
|
||||
export declare type HttpClient = (request: HttpRequest) => Promise<HttpResponse>;
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
import { HttpClient } from "./httpClient";
|
||||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
import { RequestPolicy } from "./requestPolicy";
|
||||
/**
|
||||
* An adapter type that adapts a HttpClient to look like a RequestPolicy.
|
||||
*/
|
||||
export declare class HttpClientToRequestPolicyAdapter implements RequestPolicy {
|
||||
private _httpClient;
|
||||
constructor(_httpClient: HttpClient);
|
||||
send(request: HttpRequest): Promise<HttpResponse>;
|
||||
}
|
|
@ -1,6 +1,55 @@
|
|||
/**
|
||||
* A collection of HttpHeaders that can be sent with a HTTP request.
|
||||
* An individual header within a HttpHeaders collection.
|
||||
*/
|
||||
export declare type HttpHeaders = {
|
||||
[headerName: string]: string | number;
|
||||
export interface HttpHeader {
|
||||
/**
|
||||
* The name of the header.
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* The value of the header.
|
||||
*/
|
||||
value: string;
|
||||
}
|
||||
/**
|
||||
* A HttpHeaders collection represented as a simple JSON object.
|
||||
*/
|
||||
export declare type RawHttpHeaders = {
|
||||
[headerName: string]: string;
|
||||
};
|
||||
/**
|
||||
* A collection of HTTP header key/value pairs.
|
||||
*/
|
||||
export declare class HttpHeaders {
|
||||
private _headersMap;
|
||||
constructor(rawHeaders?: RawHttpHeaders);
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
set(headerName: string, headerValue: string | number): void;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
get(headerName: string): string | undefined;
|
||||
/**
|
||||
* Get the headers that are contained in this collection.
|
||||
*/
|
||||
headers(): HttpHeader[];
|
||||
/**
|
||||
* Get the header names that are contained in this collection.
|
||||
*/
|
||||
headerNames(): string[];
|
||||
/**
|
||||
* Get the header names that are contained in this collection.
|
||||
*/
|
||||
headerValues(): string[];
|
||||
/**
|
||||
* Get the JSON object representation of this HTTP header collection.
|
||||
*/
|
||||
toJson(): RawHttpHeaders;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { HttpPipelineOptions } from "./httpPipelineOptions";
|
||||
import { RequestPolicyFactory } from "./requestPolicyFactory";
|
||||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
import { RequestPolicyFactory } from "./requestPolicyFactory";
|
||||
/**
|
||||
* A collection of RequestPolicies that will be applied to a HTTP request before it is sent and will
|
||||
* be applied to a HTTP response when it is received.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { HttpHeaders } from "./httpHeaders";
|
||||
import { HttpHeaders, RawHttpHeaders } from "./httpHeaders";
|
||||
import { HttpMethod } from "./httpMethod";
|
||||
/**
|
||||
* An individual HTTP request that can be sent with a HttpClient.
|
||||
|
@ -6,9 +6,9 @@ import { HttpMethod } from "./httpMethod";
|
|||
export declare class HttpRequest {
|
||||
private _httpMethod;
|
||||
private _url;
|
||||
private _headers;
|
||||
private _body;
|
||||
constructor(_httpMethod: HttpMethod, _url: string, _headers: HttpHeaders, _body?: string | undefined);
|
||||
private readonly _headers;
|
||||
constructor(_httpMethod: HttpMethod, _url: string, headers: HttpHeaders | RawHttpHeaders, _body?: string | undefined);
|
||||
/**
|
||||
* Get the HTTP method that this request will use.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
import { RequestPolicyFactory } from "../requestPolicyFactory";
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates UserAgentRequestPolicies.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
*/
|
||||
export declare function userAgentRequestPolicyFactory(userAgent: string): RequestPolicyFactory;
|
|
@ -1,5 +1,7 @@
|
|||
import { HttpRequest } from "./httpRequest";
|
||||
import { HttpResponse } from "./httpResponse";
|
||||
import { RequestPolicyOptions } from "./requestPolicyOptions";
|
||||
import { HttpPipelineLogLevel } from "./httpPipelineLogLevel";
|
||||
/**
|
||||
* Uses the decorator pattern to add custom behavior when an HTTP request is made.
|
||||
* e.g. add header, user agent, timeout, retry, etc.
|
||||
|
@ -12,3 +14,29 @@ export interface RequestPolicy {
|
|||
*/
|
||||
send(request: HttpRequest): Promise<HttpResponse>;
|
||||
}
|
||||
/**
|
||||
* A base class implementation of RequestPolicy.
|
||||
*/
|
||||
export declare abstract class BaseRequestPolicy implements RequestPolicy {
|
||||
private _nextPolicy;
|
||||
private _options;
|
||||
constructor(_nextPolicy: RequestPolicy, _options: RequestPolicyOptions);
|
||||
abstract send(request: HttpRequest): Promise<HttpResponse>;
|
||||
/**
|
||||
* The next RequestPolicy in the HttpPipeline.
|
||||
*/
|
||||
protected readonly nextPolicy: RequestPolicy;
|
||||
/**
|
||||
* Get whether or not a log with the provided log level should be logged.
|
||||
* @param logLevel The log level of the log that will be logged.
|
||||
* @returns Whether or not a log with the provided log level should be logged.
|
||||
*/
|
||||
protected shouldLog(logLevel: HttpPipelineLogLevel): boolean;
|
||||
/**
|
||||
* Attempt to log the provided message to the provided logger. If no logger was provided or if
|
||||
* the log level does not meat the logger's threshold, then nothing will be logged.
|
||||
* @param logLevel The log level of this log.
|
||||
* @param message The message of this log.
|
||||
*/
|
||||
protected log(logLevel: HttpPipelineLogLevel, message: string): void;
|
||||
}
|
||||
|
|
|
@ -4,12 +4,7 @@ import { RequestPolicyOptions } from "./requestPolicyOptions";
|
|||
* Factory to create a RequestPolicy. RequestPolicies are instantiated per-request
|
||||
* so that they can contain instance state specific to that request/response exchange,
|
||||
* for example, the number of retries attempted so far in a counter.
|
||||
* @param nextPolicy The RequestPolicy that the created RequestPolicy will hand HttpRequests to and receive HttpResponses from.
|
||||
* @param options The set of RequestPolicyOptions that will apply to the created RequestPolicy.
|
||||
*/
|
||||
export interface RequestPolicyFactory {
|
||||
/**
|
||||
* Create a RequestPolicy.
|
||||
* @param nextPolicy The RequestPolicy that the created RequestPolicy will hand HttpRequests to and receive HttpResponses from.
|
||||
* @param options The set of RequestPolicyOptions that will apply to the created RequestPolicy.
|
||||
*/
|
||||
create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): RequestPolicy;
|
||||
}
|
||||
export declare type RequestPolicyFactory = (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => RequestPolicy;
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
export {};
|
|
@ -1,12 +1,12 @@
|
|||
import { HttpResponse } from "../lib/httpResponse";
|
||||
import { HttpRequest } from "../lib/httpRequest";
|
||||
import { HttpHeaders } from "../lib/httpHeaders";
|
||||
import { HttpHeaders, RawHttpHeaders } from "../lib/httpHeaders";
|
||||
export declare class InMemoryHttpResponse implements HttpResponse {
|
||||
private _request;
|
||||
private _statusCode;
|
||||
private _headers;
|
||||
private _bodyText;
|
||||
constructor(_request: HttpRequest, _statusCode: number, _headers: HttpHeaders, _bodyText?: string | undefined);
|
||||
private readonly _headers;
|
||||
constructor(_request: HttpRequest, _statusCode: number, headers: HttpHeaders | RawHttpHeaders, _bodyText?: string | undefined);
|
||||
readonly request: HttpRequest;
|
||||
readonly statusCode: number;
|
||||
readonly headers: HttpHeaders;
|
||||
|
|
Загрузка…
Ссылка в новой задаче