userAgentPolicy.js 2.7 KB

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