@microsoft/kiota-http-fetchlibrary
Version:
Kiota request adapter implementation with fetch
57 lines • 2.78 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 { appendRequestHeader, getRequestHeader } from "../utils/headersUtil.js";
import { UserAgentHandlerOptions, UserAgentHandlerOptionsKey } from "./options/userAgentHandlerOptions.js";
const USER_AGENT_HEADER_KEY = "User-Agent";
export class UserAgentHandler {
/**
* To create an instance of UserAgentHandler
* @param _options - The options for the middleware
*/
constructor(_options = new UserAgentHandlerOptions()) {
this._options = _options;
}
/** @inheritdoc */
execute(url, requestInit, requestOptions) {
const obsOptions = getObservabilityOptionsFromRequest(requestOptions);
if (obsOptions) {
return trace.getTracer(obsOptions.getTracerInstrumentationName()).startActiveSpan("userAgentHandler - execute", (span) => {
try {
span.setAttribute("com.microsoft.kiota.handler.useragent.enable", true);
return this.addValue(url, requestInit, requestOptions);
}
finally {
span.end();
}
});
}
else {
return this.addValue(url, requestInit, requestOptions);
}
}
async addValue(url, requestInit, requestOptions) {
var _a;
let currentOptions = this._options;
if (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions[UserAgentHandlerOptionsKey]) {
currentOptions = requestOptions[UserAgentHandlerOptionsKey];
}
if (currentOptions.enable) {
const additionalValue = `${currentOptions.productName}/${currentOptions.productVersion}`;
const currentValue = getRequestHeader(requestInit, USER_AGENT_HEADER_KEY);
if (!(currentValue === null || currentValue === void 0 ? void 0 : currentValue.includes(additionalValue))) {
appendRequestHeader(requestInit, USER_AGENT_HEADER_KEY, additionalValue, " ");
}
}
const response = await ((_a = this.next) === null || _a === void 0 ? void 0 : _a.execute(url, requestInit, requestOptions));
if (!response)
throw new Error("No response returned by the next middleware");
return response;
}
}
//# sourceMappingURL=userAgentHandler.js.map