UNPKG

2.79 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3import { __extends } from "tslib";
4import { HttpHeaders } from "../httpHeaders";
5import { Constants } from "../util/constants";
6import { getDefaultUserAgentKey, getPlatformSpecificData } from "./msRestUserAgentPolicy";
7import { BaseRequestPolicy, } from "./requestPolicy";
8function getRuntimeInfo() {
9 var msRestRuntime = {
10 key: "ms-rest-js",
11 value: Constants.msRestVersion,
12 };
13 return [msRestRuntime];
14}
15function getUserAgentString(telemetryInfo, keySeparator, valueSeparator) {
16 if (keySeparator === void 0) { keySeparator = " "; }
17 if (valueSeparator === void 0) { valueSeparator = "/"; }
18 return telemetryInfo
19 .map(function (info) {
20 var value = info.value ? "" + valueSeparator + info.value : "";
21 return "" + info.key + value;
22 })
23 .join(keySeparator);
24}
25export var getDefaultUserAgentHeaderName = getDefaultUserAgentKey;
26export function getDefaultUserAgentValue() {
27 var runtimeInfo = getRuntimeInfo();
28 var platformSpecificData = getPlatformSpecificData();
29 var userAgent = getUserAgentString(runtimeInfo.concat(platformSpecificData));
30 return userAgent;
31}
32export function userAgentPolicy(userAgentData) {
33 var key = !userAgentData || userAgentData.key == undefined ? getDefaultUserAgentKey() : userAgentData.key;
34 var value = !userAgentData || userAgentData.value == undefined
35 ? getDefaultUserAgentValue()
36 : userAgentData.value;
37 return {
38 create: function (nextPolicy, options) {
39 return new UserAgentPolicy(nextPolicy, options, key, value);
40 },
41 };
42}
43var UserAgentPolicy = /** @class */ (function (_super) {
44 __extends(UserAgentPolicy, _super);
45 function UserAgentPolicy(_nextPolicy, _options, headerKey, headerValue) {
46 var _this = _super.call(this, _nextPolicy, _options) || this;
47 _this._nextPolicy = _nextPolicy;
48 _this._options = _options;
49 _this.headerKey = headerKey;
50 _this.headerValue = headerValue;
51 return _this;
52 }
53 UserAgentPolicy.prototype.sendRequest = function (request) {
54 this.addUserAgentHeader(request);
55 return this._nextPolicy.sendRequest(request);
56 };
57 UserAgentPolicy.prototype.addUserAgentHeader = function (request) {
58 if (!request.headers) {
59 request.headers = new HttpHeaders();
60 }
61 if (!request.headers.get(this.headerKey) && this.headerValue) {
62 request.headers.set(this.headerKey, this.headerValue);
63 }
64 };
65 return UserAgentPolicy;
66}(BaseRequestPolicy));
67export { UserAgentPolicy };
68//# sourceMappingURL=userAgentPolicy.js.map
\No newline at end of file