@microsoft/kiota-http-fetchlibrary
Version:
Kiota request adapter implementation with fetch
95 lines • 4.55 kB
JavaScript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
export const RedirectHandlerOptionKey = "RedirectHandlerOption";
/**
* MiddlewareOptions
* A class representing RedirectHandlerOptions
*/
export class RedirectHandlerOptions {
/**
*
* To create an instance of RedirectHandlerOptions
* @param [options] - The redirect handler options instance
* @returns An instance of RedirectHandlerOptions
* @throws Error if maxRedirects is more than 20 or less than 0
* @example const options = new RedirectHandlerOptions({ maxRedirects: 5 });
*/
constructor(options = {}) {
var _a, _b, _c;
if (options.maxRedirects && options.maxRedirects > RedirectHandlerOptions.MAX_MAX_REDIRECTS) {
const error = new Error(`MaxRedirects should not be more than ${RedirectHandlerOptions.MAX_MAX_REDIRECTS}`);
error.name = "MaxLimitExceeded";
throw error;
}
if (options.maxRedirects !== undefined && options.maxRedirects < 0) {
const error = new Error(`MaxRedirects should not be negative`);
error.name = "MinExpectationNotMet";
throw error;
}
this.maxRedirects = (_a = options.maxRedirects) !== null && _a !== void 0 ? _a : RedirectHandlerOptions.DEFAULT_MAX_REDIRECTS;
this.shouldRedirect = (_b = options.shouldRedirect) !== null && _b !== void 0 ? _b : RedirectHandlerOptions.defaultShouldRetry;
this.scrubSensitiveHeaders = (_c = options.scrubSensitiveHeaders) !== null && _c !== void 0 ? _c : RedirectHandlerOptions.defaultScrubSensitiveHeaders;
}
getKey() {
return RedirectHandlerOptionKey;
}
}
/**
* A member holding default max redirects value
*/
RedirectHandlerOptions.DEFAULT_MAX_REDIRECTS = 5;
/**
* A member holding maximum max redirects value
*/
RedirectHandlerOptions.MAX_MAX_REDIRECTS = 20;
/**
*
* A member holding default shouldRedirect callback
* @returns true
*/
RedirectHandlerOptions.defaultShouldRetry = () => true;
/**
* The default implementation for scrubbing sensitive headers during redirects.
* This method removes Authorization and Cookie headers when the host or scheme changes.
* Note: Proxy-Authorization handling is not applicable in Fetch API as proxy configuration
* is handled at a lower level by the browser/runtime and is not accessible to JavaScript.
* @param headers - The headers object to modify
* @param originalUrl - The original request URL
* @param newUrl - The new redirect URL
*/
RedirectHandlerOptions.defaultScrubSensitiveHeaders = (headers, originalUrl, newUrl) => {
if (!headers || !originalUrl || !newUrl) {
return;
}
try {
const originalUri = new URL(originalUrl);
const newUri = new URL(newUrl);
// Remove Authorization, Cookie, and Proxy-Authorization headers if the request's scheme or host changes.
// Header keys must be matched case-insensitively because FetchRequestAdapter.getRequestFromRequestInformation
// lower-cases every header key before the headers object reaches this middleware, so PascalCase
// property deletes such as `delete headers.Authorization` would otherwise be a no-op.
const isDifferentHostOrScheme = originalUri.host.toLowerCase() !== newUri.host.toLowerCase() || originalUri.protocol.toLowerCase() !== newUri.protocol.toLowerCase();
if (isDifferentHostOrScheme) {
for (const key of Object.keys(headers)) {
const lower = key.toLowerCase();
if (lower === "authorization" || lower === "cookie" || lower === "proxy-authorization") {
delete headers[key];
}
}
}
}
catch (_a) {
// If URL parsing fails, don't modify headers
// This handles cases where invalid URLs are passed
return;
}
// Note: Proxy-Authorization is not handled here as proxy configuration in Fetch API
// is managed by the browser/runtime and not accessible to JavaScript code.
// In environments where this matters (e.g., Node.js with custom agents), the proxy
// configuration should be managed at the HTTP client level.
};
//# sourceMappingURL=redirectHandlerOptions.js.map