diff --git a/sdk/cosmosdb/cosmos/review/cosmos.api.md b/sdk/cosmosdb/cosmos/review/cosmos.api.md index 454f36a290fc..66f50c3873ce 100644 --- a/sdk/cosmosdb/cosmos/review/cosmos.api.md +++ b/sdk/cosmosdb/cosmos/review/cosmos.api.md @@ -433,6 +433,7 @@ export const Constants: { ContentEncoding: string; CharacterSet: string; UserAgent: string; + CustomUserAgent: string; IfModifiedSince: string; IfMatch: string; IfNoneMatch: string; @@ -706,6 +707,7 @@ export interface CosmosClientOptions { aadCredentials?: TokenCredential; agent?: Agent; connectionPolicy?: ConnectionPolicy; + connectionString?: string; consistencyLevel?: keyof typeof ConsistencyLevel; // Warning: (ae-forgotten-export) The symbol "CosmosHeaders_2" needs to be exported by the entry point index.d.ts // @@ -713,7 +715,7 @@ export interface CosmosClientOptions { defaultHeaders?: CosmosHeaders_2; // (undocumented) diagnosticLevel?: CosmosDbDiagnosticLevel; - endpoint: string; + endpoint?: string; httpClient?: HttpClient; key?: string; permissionFeed?: PermissionDefinition[]; diff --git a/sdk/cosmosdb/cosmos/src/ClientContext.ts b/sdk/cosmosdb/cosmos/src/ClientContext.ts index 16a4bcd22ce5..b04fa11f070e 100644 --- a/sdk/cosmosdb/cosmos/src/ClientContext.ts +++ b/sdk/cosmosdb/cosmos/src/ClientContext.ts @@ -38,6 +38,7 @@ import type { DiagnosticFormatter } from "./diagnostics/DiagnosticFormatter"; import { DefaultDiagnosticFormatter } from "./diagnostics/DiagnosticFormatter"; import { CosmosDbDiagnosticLevel } from "./diagnostics/CosmosDbDiagnosticLevel"; import { randomUUID } from "@azure/core-util"; +import { getUserAgent } from "./common/platform"; const logger: AzureLogger = createClientLogger("ClientContext"); @@ -978,4 +979,14 @@ export class ClientContext { public getClientConfig(): ClientConfigDiagnostic { return this.clientConfig; } + + /** + * @internal + */ + public refreshUserAgent(hostFramework: string): void { + const updatedUserAgent = getUserAgent(this.cosmosClientOptions.userAgentSuffix, hostFramework); + this.cosmosClientOptions.defaultHeaders[Constants.HttpHeaders.UserAgent] = updatedUserAgent; + this.cosmosClientOptions.defaultHeaders[Constants.HttpHeaders.CustomUserAgent] = + updatedUserAgent; + } } diff --git a/sdk/cosmosdb/cosmos/src/CosmosClient.ts b/sdk/cosmosdb/cosmos/src/CosmosClient.ts index f72c337654e0..313df662b1a9 100644 --- a/sdk/cosmosdb/cosmos/src/CosmosClient.ts +++ b/sdk/cosmosdb/cosmos/src/CosmosClient.ts @@ -70,6 +70,10 @@ export class CosmosClient { constructor(optionsOrConnectionString: string | CosmosClientOptions) { if (typeof optionsOrConnectionString === "string") { optionsOrConnectionString = parseConnectionString(optionsOrConnectionString); + } else if (optionsOrConnectionString.connectionString) { + const { endpoint, key } = parseConnectionString(optionsOrConnectionString.connectionString); + optionsOrConnectionString.endpoint = endpoint; + optionsOrConnectionString.key = key; } const endpoint = checkURL(optionsOrConnectionString.endpoint); @@ -95,9 +99,9 @@ export class CosmosClient { optionsOrConnectionString.consistencyLevel; } - optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.UserAgent] = getUserAgent( - optionsOrConnectionString.userAgentSuffix, - ); + const userAgent = getUserAgent(optionsOrConnectionString.userAgentSuffix); + optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.UserAgent] = userAgent; + optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.CustomUserAgent] = userAgent; const globalEndpointManager = new GlobalEndpointManager( optionsOrConnectionString, @@ -272,4 +276,13 @@ export class CosmosClient { this.endpointRefresher.unref(); } } + + /** + * Update the host framework. If provided host framework will be used to generate the defualt SDK user agent. + * @param hostFramework - A custom string. + * @internal + */ + public async updateHostFramework(hostFramework: string): Promise { + this.clientContext.refreshUserAgent(hostFramework); + } } diff --git a/sdk/cosmosdb/cosmos/src/CosmosClientOptions.ts b/sdk/cosmosdb/cosmos/src/CosmosClientOptions.ts index 02de08505ea9..3ba5b807dc1d 100644 --- a/sdk/cosmosdb/cosmos/src/CosmosClientOptions.ts +++ b/sdk/cosmosdb/cosmos/src/CosmosClientOptions.ts @@ -20,7 +20,7 @@ export interface Agent { export interface CosmosClientOptions { /** The service endpoint to use to create the client. */ - endpoint: string; + endpoint?: string; /** The account master or readonly key */ key?: string; /** An object that contains resources tokens. @@ -60,4 +60,6 @@ export interface CosmosClientOptions { diagnosticLevel?: CosmosDbDiagnosticLevel; /** @internal */ plugins?: PluginConfig[]; + /** An optional parameter that represents the connection string. Your database connection string can be found in the Azure Portal. */ + connectionString?: string; } diff --git a/sdk/cosmosdb/cosmos/src/common/constants.ts b/sdk/cosmosdb/cosmos/src/common/constants.ts index 75328fa7909a..75bf0afd76a9 100644 --- a/sdk/cosmosdb/cosmos/src/common/constants.ts +++ b/sdk/cosmosdb/cosmos/src/common/constants.ts @@ -22,6 +22,7 @@ export const Constants = { ContentEncoding: "Content-Encoding", CharacterSet: "CharacterSet", UserAgent: "User-Agent", + CustomUserAgent: "x-ms-useragent", IfModifiedSince: "If-Modified-Since", IfMatch: "If-Match", IfNoneMatch: "If-None-Match", diff --git a/sdk/cosmosdb/cosmos/src/common/platform.ts b/sdk/cosmosdb/cosmos/src/common/platform.ts index d7756ff2afab..28334b9dbfa3 100644 --- a/sdk/cosmosdb/cosmos/src/common/platform.ts +++ b/sdk/cosmosdb/cosmos/src/common/platform.ts @@ -6,11 +6,15 @@ import { Constants } from "./constants"; /** * @hidden */ -export function getUserAgent(suffix?: string): string { - const ua = `${userAgentDetails()} ${Constants.SDKName}/${Constants.SDKVersion}`; +export function getUserAgent(suffix?: string, hostFramework?: string): string { + let ua = `${userAgentDetails()} ${Constants.SDKName}/${Constants.SDKVersion}`; + if (hostFramework) { + ua = ua + " " + hostFramework; + } if (suffix) { - return ua + " " + suffix; + ua = ua + " " + suffix; } + return ua; }