From 6ebba013d3b299f280fb6155d43d2e73bc38cc0f Mon Sep 17 00:00:00 2001 From: Jeremy Meng Date: Wed, 27 Apr 2022 21:37:06 +0000 Subject: [PATCH] [core] improve user agent information for React Native --- .../src/policies/msRestUserAgentPolicy.native.ts | 5 +++-- sdk/core/core-rest-pipeline/CHANGELOG.md | 2 ++ .../src/util/userAgentPlatform.native.ts | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/sdk/core/core-http/src/policies/msRestUserAgentPolicy.native.ts b/sdk/core/core-http/src/policies/msRestUserAgentPolicy.native.ts index f44d1862ae1f..d45607518cd8 100644 --- a/sdk/core/core-http/src/policies/msRestUserAgentPolicy.native.ts +++ b/sdk/core/core-http/src/policies/msRestUserAgentPolicy.native.ts @@ -13,14 +13,15 @@ export function getDefaultUserAgentKey(): string { } export function getPlatformSpecificData(): TelemetryInfo[] { + const { major, minor, patch } = Platform.constants.reactNativeVersion; const runtimeInfo = { key: "react-native", - value: Platform.reactNativeVersion, + value: `${major}.${minor}.${patch}`, }; const osInfo = { key: "OS", - value: `${Platform.OS}-${String(Platform.Version)}`, + value: `${Platform.OS}-${Platform.Version}`, }; return [runtimeInfo, osInfo]; diff --git a/sdk/core/core-rest-pipeline/CHANGELOG.md b/sdk/core/core-rest-pipeline/CHANGELOG.md index 68f407f9314b..02fd75304189 100644 --- a/sdk/core/core-rest-pipeline/CHANGELOG.md +++ b/sdk/core/core-rest-pipeline/CHANGELOG.md @@ -8,6 +8,8 @@ - Exposed type guard for RestError called `isRestError` for typesafe exception handling. +- Improve user agent information for React-Native. + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/core/core-rest-pipeline/src/util/userAgentPlatform.native.ts b/sdk/core/core-rest-pipeline/src/util/userAgentPlatform.native.ts index 23fbb519cbbd..51dcfba51942 100644 --- a/sdk/core/core-rest-pipeline/src/util/userAgentPlatform.native.ts +++ b/sdk/core/core-rest-pipeline/src/util/userAgentPlatform.native.ts @@ -4,6 +4,7 @@ /* * NOTE: When moving this file, please update "react-native" section in package.json. */ +const { Platform } = require("react-native"); // eslint-disable-line import/no-extraneous-dependencies, @typescript-eslint/no-require-imports /** * @internal @@ -16,8 +17,7 @@ export function getHeaderName(): string { * @internal */ export function setPlatformSpecificData(map: Map): void { - // TODO: Investigate using `import { Platform } from "react-native"` to get "OS" and "Version". - // This may bring in a lot of overhead if we have to use this package directly, perhaps we can shim - // types. - map.set("OS", `react-native`); + const { major, minor, patch } = Platform.constants.reactNativeVersion; + map.set("react-native", `${major}.${minor}.${patch}`); + map.set("OS", `${Platform.OS}-${Platform.Version}`); }