зеркало из https://github.com/Azure/ms-rest-js.git
Add redirectPolicy
This commit is contained in:
Родитель
0059913eba
Коммит
81b70421a1
|
@ -7,36 +7,23 @@ 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) {
|
||||
this._httpMethod = _httpMethod;
|
||||
this._url = _url;
|
||||
/**
|
||||
* Create a new HTTP request using the provided properties.
|
||||
* @param _httpMethod The HTTP method that will be used to send this request.
|
||||
* @param url The URL that this request will be sent to.
|
||||
* @param headers The HTTP headers to include in this request.
|
||||
* @param _body The body of this HTTP request.
|
||||
*/
|
||||
function HttpRequest(httpMethod, url, headers, _body) {
|
||||
this.httpMethod = httpMethod;
|
||||
this.url = url;
|
||||
this._body = _body;
|
||||
if (!this._url) {
|
||||
var urlString = (this._url === undefined || this._url === null ? this._url : "\"" + this._url + "\"");
|
||||
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", {
|
||||
/**
|
||||
* Get the HTTP method that this request will use.
|
||||
*/
|
||||
get: function () {
|
||||
return this._httpMethod;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(HttpRequest.prototype, "url", {
|
||||
/**
|
||||
* Get the URL that this request will be sent to.
|
||||
*/
|
||||
get: function () {
|
||||
return this._url;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(HttpRequest.prototype, "headers", {
|
||||
/**
|
||||
* Get the HTTP headers that will be sent with this request.
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,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IAAI,CAAC,IAAI,CAAC,CAAC;IACvF,CAAC;IACL,kBAAC;AAAD,CAAC,AAvCD,IAuCC;AAvCY,kCAAW"}
|
|
@ -50,8 +50,8 @@ var inMemoryHttpResponse_1 = require("../inMemoryHttpResponse");
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates UserAgentPolicies.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
* Get a RequestPolicyFactory that creates LogPolicies.
|
||||
* @param logFunction The function to use to log messages.
|
||||
*/
|
||||
function logPolicy(logFunction) {
|
||||
return function (nextPolicy, options) {
|
||||
|
|
|
@ -17,7 +17,7 @@ var requestPolicy_1 = require("../requestPolicy");
|
|||
var constants_1 = require("../util/constants");
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates adds the ms-rest user agent to outgoing requests.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
* @param userAgentInfo The string[] of userAgent details to apply to each outgoing request.
|
||||
*/
|
||||
function msRestNodeJsUserAgentPolicy(userAgentInfo) {
|
||||
return function (nextPolicy, options) {
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
"use strict";
|
||||
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [0, t.value];
|
||||
switch (op[0]) {
|
||||
case 0: case 1: t = op; break;
|
||||
case 4: _.label++; return { value: op[1], done: false };
|
||||
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||
default:
|
||||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||
if (t[2]) _.ops.pop();
|
||||
_.trys.pop(); continue;
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||
}
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
var parse = require("url-parse");
|
||||
var httpMethod_1 = require("../httpMethod");
|
||||
var requestPolicy_1 = require("../requestPolicy");
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates RedirectPolicies.
|
||||
* @param maximumRedirections The maximum number of redirections to take before failing.
|
||||
*/
|
||||
function redirectPolicy(maximumRedirections) {
|
||||
return function (nextPolicy, options) {
|
||||
return new RedirectPolicy(maximumRedirections, nextPolicy, options);
|
||||
};
|
||||
}
|
||||
exports.redirectPolicy = redirectPolicy;
|
||||
var RedirectPolicy = /** @class */ (function (_super) {
|
||||
__extends(RedirectPolicy, _super);
|
||||
function RedirectPolicy(_maximumRedirections, nextPolicy, options) {
|
||||
var _this = _super.call(this, nextPolicy, options) || this;
|
||||
_this._maximumRedirections = _maximumRedirections;
|
||||
return _this;
|
||||
}
|
||||
RedirectPolicy.prototype.send = function (request) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var redirections, response;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
request = request.clone();
|
||||
redirections = 0;
|
||||
_a.label = 1;
|
||||
case 1:
|
||||
if (!true) return [3 /*break*/, 3];
|
||||
return [4 /*yield*/, this._nextPolicy.send(request.clone())];
|
||||
case 2:
|
||||
response = _a.sent();
|
||||
if (response && response.headers && response.headers.get("location") &&
|
||||
(response.statusCode === 300 || response.statusCode === 307 || (response.statusCode === 303 && request.httpMethod === httpMethod_1.HttpMethod.POST)) &&
|
||||
(!this._maximumRedirections || redirections < this._maximumRedirections)) {
|
||||
++redirections;
|
||||
request.url = parse(response.headers.get("location"), parse(request.url)).href;
|
||||
// POST request with Status code 303 should be converted into a
|
||||
// redirected GET request if the redirect url is present in the location header
|
||||
if (response.statusCode === 303) {
|
||||
request.httpMethod = httpMethod_1.HttpMethod.GET;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [3 /*break*/, 3];
|
||||
}
|
||||
return [3 /*break*/, 1];
|
||||
case 3: return [2 /*return*/, response];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
return RedirectPolicy;
|
||||
}(requestPolicy_1.BaseRequestPolicy));
|
||||
//# sourceMappingURL=redirectPolicy.js.map
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"redirectPolicy.js","sourceRoot":"","sources":["../../../lib/policies/redirectPolicy.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAA4D;AAC5D,+FAA+F;AAC/F,iCAAmC;AACnC,4CAA2C;AAG3C,kDAAoE;AAIpE;;;GAGG;AACH,wBAA+B,mBAA2B;IACtD,OAAO,UAAC,UAAyB,EAAE,OAA6B;QAC5D,OAAO,IAAI,cAAc,CAAC,mBAAmB,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IACxE,CAAC,CAAC;AACN,CAAC;AAJD,wCAIC;AAED;IAA6B,kCAAiB;IAC1C,wBAA6B,oBAA4B,EAAE,UAAyB,EAAE,OAA6B;QAAnH,YACI,kBAAM,UAAU,EAAE,OAAO,CAAC,SAC7B;QAF4B,0BAAoB,GAApB,oBAAoB,CAAQ;;IAEzD,CAAC;IAEK,6BAAI,GAAV,UAAW,OAAoB;;;;;;wBAC3B,OAAO,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;wBAEtB,YAAY,GAAG,CAAC,CAAC;;;6BAEd,IAAI;wBACI,qBAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,EAAA;;wBAAvD,QAAQ,GAAG,SAA4C,CAAC;wBAExD,IAAI,QAAQ,IAAI,QAAQ,CAAC,OAAO,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;4BAChE,CAAC,QAAQ,CAAC,UAAU,KAAK,GAAG,IAAI,QAAQ,CAAC,UAAU,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,UAAU,KAAK,GAAG,IAAI,OAAO,CAAC,UAAU,KAAK,uBAAU,CAAC,IAAI,CAAC,CAAC;4BACvI,CAAC,CAAC,IAAI,CAAC,oBAAoB,IAAI,YAAY,GAAG,IAAI,CAAC,oBAAoB,CAAC,EAAE;4BAE1E,EAAE,YAAY,CAAC;4BAEf,OAAO,CAAC,GAAG,GAAG,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAE,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;4BAEhF,+DAA+D;4BAC/D,+EAA+E;4BAC/E,IAAI,QAAQ,CAAC,UAAU,KAAK,GAAG,EAAE;gCAC7B,OAAO,CAAC,UAAU,GAAG,uBAAU,CAAC,GAAG,CAAC;6BACvC;yBACJ;6BAAM;4BACH,wBAAM;yBACT;;4BAGL,sBAAO,QAAQ,EAAC;;;;KACnB;IACL,qBAAC;AAAD,CAAC,AAjCD,CAA6B,iCAAiB,GAiC7C"}
|
|
@ -68,7 +68,7 @@ describe("logPolicy", function () {
|
|||
assert.deepStrictEqual(response.statusCode, 200);
|
||||
assert.deepStrictEqual(response.headers, new httpHeaders_1.HttpHeaders());
|
||||
assert.deepStrictEqual(logs, [
|
||||
">> Request: {\n \"_httpMethod\": \"GET\",\n \"_url\": \"https://spam.com\",\n \"_headers\": {\n \"_headersMap\": {}\n }\n}",
|
||||
">> Request: {\n \"httpMethod\": \"GET\",\n \"url\": \"https://spam.com\",\n \"_headers\": {\n \"_headersMap\": {}\n }\n}",
|
||||
">> Response Status Code: 200",
|
||||
">> Response Body: undefined"
|
||||
]);
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"logPolicyTests.js","sourceRoot":"","sources":["../../../test/policies/logPolicyTests.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,iBAqCG;;AAvCH,4DAA4D;AAC5D,+FAA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Ae,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,yBAAW,CAAC,uBAAU,CAAC,GAAG,EAAE,kBAAkB,EAAE,EAAE,CAAC,CAAC,CAAC;oBAClG,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;oBACjD,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,yBAAW,EAAE,CAAC,CAAC;oBAE5D,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE;wBACzB,iIAAiI;wBACjI,8BAA8B;wBAC9B,6BAA6B;qBAChC,CAAC,CAAC;;;;SACN,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@ -9,29 +9,22 @@ import { HttpMethod } from "./httpMethod";
|
|||
export class HttpRequest {
|
||||
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}"`);
|
||||
/**
|
||||
* Create a new HTTP request using the provided properties.
|
||||
* @param _httpMethod The HTTP method that will be used to send this request.
|
||||
* @param url The URL that this request will be sent to.
|
||||
* @param headers The HTTP headers to include in this request.
|
||||
* @param _body The body of this HTTP request.
|
||||
*/
|
||||
constructor(public httpMethod: HttpMethod, public 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the HTTP method that this request will use.
|
||||
*/
|
||||
public get httpMethod(): HttpMethod {
|
||||
return this._httpMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL that this request will be sent to.
|
||||
*/
|
||||
public get url(): string {
|
||||
return this._url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the HTTP headers that will be sent with this request.
|
||||
*/
|
||||
|
|
|
@ -9,8 +9,8 @@ import { InMemoryHttpResponse } from "../inMemoryHttpResponse";
|
|||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates UserAgentPolicies.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
* Get a RequestPolicyFactory that creates LogPolicies.
|
||||
* @param logFunction The function to use to log messages.
|
||||
*/
|
||||
export function logPolicy(logFunction: (message: string) => void): RequestPolicyFactory {
|
||||
return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
|
||||
|
|
|
@ -10,7 +10,7 @@ import { Constants } from "../util/constants";
|
|||
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates adds the ms-rest user agent to outgoing requests.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
* @param userAgentInfo The string[] of userAgent details to apply to each outgoing request.
|
||||
*/
|
||||
export function msRestNodeJsUserAgentPolicy(userAgentInfo: string[]): RequestPolicyFactory {
|
||||
return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
import * as parse from "url-parse";
|
||||
import { HttpMethod } from "../httpMethod";
|
||||
import { HttpRequest } from "../httpRequest";
|
||||
import { HttpResponse } from "../httpResponse";
|
||||
import { BaseRequestPolicy, RequestPolicy } from "../requestPolicy";
|
||||
import { RequestPolicyFactory } from "../requestPolicyFactory";
|
||||
import { RequestPolicyOptions } from "../requestPolicyOptions";
|
||||
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates RedirectPolicies.
|
||||
* @param maximumRedirections The maximum number of redirections to take before failing.
|
||||
*/
|
||||
export function redirectPolicy(maximumRedirections: number): RequestPolicyFactory {
|
||||
return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
|
||||
return new RedirectPolicy(maximumRedirections, nextPolicy, options);
|
||||
};
|
||||
}
|
||||
|
||||
class RedirectPolicy extends BaseRequestPolicy {
|
||||
constructor(private readonly _maximumRedirections: number, nextPolicy: RequestPolicy, options: RequestPolicyOptions) {
|
||||
super(nextPolicy, options);
|
||||
}
|
||||
|
||||
async send(request: HttpRequest): Promise<HttpResponse> {
|
||||
request = request.clone();
|
||||
|
||||
let redirections = 0;
|
||||
let response: HttpResponse;
|
||||
while (true) {
|
||||
response = await this._nextPolicy.send(request.clone());
|
||||
|
||||
if (response && response.headers && response.headers.get("location") &&
|
||||
(response.statusCode === 300 || response.statusCode === 307 || (response.statusCode === 303 && request.httpMethod === HttpMethod.POST)) &&
|
||||
(!this._maximumRedirections || redirections < this._maximumRedirections)) {
|
||||
|
||||
++redirections;
|
||||
|
||||
request.url = parse(response.headers.get("location")!, parse(request.url)).href;
|
||||
|
||||
// POST request with Status code 303 should be converted into a
|
||||
// redirected GET request if the redirect url is present in the location header
|
||||
if (response.statusCode === 303) {
|
||||
request.httpMethod = HttpMethod.GET;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
|
@ -32,7 +32,7 @@ describe("logPolicy", () => {
|
|||
assert.deepStrictEqual(response.headers, new HttpHeaders());
|
||||
|
||||
assert.deepStrictEqual(logs, [
|
||||
">> Request: {\n \"_httpMethod\": \"GET\",\n \"_url\": \"https://spam.com\",\n \"_headers\": {\n \"_headersMap\": {}\n }\n}",
|
||||
">> Request: {\n \"httpMethod\": \"GET\",\n \"url\": \"https://spam.com\",\n \"_headers\": {\n \"_headersMap\": {}\n }\n}",
|
||||
">> Response Status Code: 200",
|
||||
">> Response Body: undefined"
|
||||
]);
|
||||
|
|
|
@ -4,19 +4,18 @@ import { HttpMethod } from "./httpMethod";
|
|||
* An individual HTTP request that can be sent with a HttpClient.
|
||||
*/
|
||||
export declare class HttpRequest {
|
||||
private _httpMethod;
|
||||
private _url;
|
||||
httpMethod: HttpMethod;
|
||||
url: string;
|
||||
private _body;
|
||||
private readonly _headers;
|
||||
constructor(_httpMethod: HttpMethod, _url: string, headers: HttpHeaders | RawHttpHeaders, _body?: string | undefined);
|
||||
/**
|
||||
* Get the HTTP method that this request will use.
|
||||
* Create a new HTTP request using the provided properties.
|
||||
* @param _httpMethod The HTTP method that will be used to send this request.
|
||||
* @param url The URL that this request will be sent to.
|
||||
* @param headers The HTTP headers to include in this request.
|
||||
* @param _body The body of this HTTP request.
|
||||
*/
|
||||
readonly httpMethod: HttpMethod;
|
||||
/**
|
||||
* Get the URL that this request will be sent to.
|
||||
*/
|
||||
readonly url: string;
|
||||
constructor(httpMethod: HttpMethod, url: string, headers: HttpHeaders | RawHttpHeaders, _body?: string | undefined);
|
||||
/**
|
||||
* Get the HTTP headers that will be sent with this request.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { RequestPolicyFactory } from "../requestPolicyFactory";
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates UserAgentPolicies.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
* Get a RequestPolicyFactory that creates LogPolicies.
|
||||
* @param logFunction The function to use to log messages.
|
||||
*/
|
||||
export declare function logPolicy(logFunction: (message: string) => void): RequestPolicyFactory;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { RequestPolicyFactory } from "../requestPolicyFactory";
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates adds the ms-rest user agent to outgoing requests.
|
||||
* @param userAgent The userAgent string to apply to each outgoing request.
|
||||
* @param userAgentInfo The string[] of userAgent details to apply to each outgoing request.
|
||||
*/
|
||||
export declare function msRestNodeJsUserAgentPolicy(userAgentInfo: string[]): RequestPolicyFactory;
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
import { RequestPolicyFactory } from "../requestPolicyFactory";
|
||||
/**
|
||||
* Get a RequestPolicyFactory that creates RedirectPolicies.
|
||||
* @param maximumRedirections The maximum number of redirections to take before failing.
|
||||
*/
|
||||
export declare function redirectPolicy(maximumRedirections: number): RequestPolicyFactory;
|
Загрузка…
Ссылка в новой задаче