@microsoft/kiota-http-fetchlibrary
Version:
Kiota request adapter implementation with fetch
62 lines • 2.77 kB
JavaScript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { trace } from "@opentelemetry/api";
import { getObservabilityOptionsFromRequest } from "../observabilityOptions.js";
import { UrlReplaceHandlerOptions, UrlReplaceHandlerOptionsKey } from "./options/urlReplaceHandlerOptions.js";
/**
* Replaces url placeholders with values from the request option.
*/
export class UrlReplaceHandler {
/**
*
* Creates a new instance of the UrlReplaceHandler class
* @param handlerOptions The options for the url replace handler.
* @returns An instance of the UrlReplaceHandler class
*/
constructor(handlerOptions = new UrlReplaceHandlerOptions()) {
this.handlerOptions = handlerOptions;
if (!handlerOptions) {
throw new Error("handlerOptions cannot be undefined");
}
}
/**
* @inheritdoc
*/
execute(url, requestInit, requestOptions) {
let currentOptions = this.handlerOptions;
if (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions[UrlReplaceHandlerOptionsKey]) {
currentOptions = requestOptions[UrlReplaceHandlerOptionsKey];
}
const obsOptions = getObservabilityOptionsFromRequest(requestOptions);
if (obsOptions) {
return trace.getTracer(obsOptions.getTracerInstrumentationName()).startActiveSpan("urlReplaceHandler - execute", (span) => {
try {
span.setAttribute("com.microsoft.kiota.handler.urlReplace.enable", currentOptions.enabled);
return this.replaceTokensInUrl(currentOptions, url, requestInit, requestOptions);
}
finally {
span.end();
}
});
}
return this.replaceTokensInUrl(currentOptions, url, requestInit, requestOptions);
}
replaceTokensInUrl(options, url, requestInit, requestOptions) {
var _a;
if (options.enabled) {
Object.keys(options.urlReplacements).forEach((replacementKey) => {
url = url.replace(replacementKey, options.urlReplacements[replacementKey]);
});
}
const response = (_a = this.next) === null || _a === void 0 ? void 0 : _a.execute(url, requestInit, requestOptions);
if (!response) {
throw new Error("Response is undefined");
}
return response;
}
}
//# sourceMappingURL=urlReplaceHandler.js.map