Skip to content

Commit

Permalink
Deprecate baseurl and use endpoint instead for ts-http-runtime (#28850)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazrael2119 authored Mar 13, 2024
1 parent 75526c0 commit d561c63
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
7 changes: 4 additions & 3 deletions sdk/core/ts-http-runtime/review/ts-http-runtime.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface AccessToken {
}

// @public
export function addCredentialPipelinePolicy(pipeline: Pipeline, baseUrl: string, options?: AddCredentialPipelinePolicyOptions): void;
export function addCredentialPipelinePolicy(pipeline: Pipeline, endpoint: string, options?: AddCredentialPipelinePolicyOptions): void;

// @public
export interface AddCredentialPipelinePolicyOptions {
Expand Down Expand Up @@ -129,6 +129,7 @@ export type ClientOptions = PipelineOptions & {
apiKeyHeaderName?: string;
};
baseUrl?: string;
endpoint?: string;
apiVersion?: string;
allowInsecureConnection?: boolean;
additionalPolicies?: AdditionalPolicyConfig[];
Expand Down Expand Up @@ -262,10 +263,10 @@ export interface FullOperationResponse extends PipelineResponse {
}

// @public
export function getClient(baseUrl: string, options?: ClientOptions): Client;
export function getClient(endpoint: string, options?: ClientOptions): Client;

// @public
export function getClient(baseUrl: string, credentials?: TokenCredential | KeyCredential, options?: ClientOptions): Client;
export function getClient(endpoint: string, credentials?: TokenCredential | KeyCredential, options?: ClientOptions): Client;

// @public
export function getDefaultProxySettings(proxyUrl?: string): ProxySettings | undefined;
Expand Down
8 changes: 4 additions & 4 deletions sdk/core/ts-http-runtime/src/client/clientHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface AddCredentialPipelinePolicyOptions {
*/
export function addCredentialPipelinePolicy(
pipeline: Pipeline,
baseUrl: string,
endpoint: string,
options: AddCredentialPipelinePolicyOptions = {},
): void {
const { credential, clientOptions } = options;
Expand All @@ -44,7 +44,7 @@ export function addCredentialPipelinePolicy(
if (isTokenCredential(credential)) {
const tokenPolicy = bearerTokenAuthenticationPolicy({
credential,
scopes: clientOptions?.credentials?.scopes ?? `${baseUrl}/.default`,
scopes: clientOptions?.credentials?.scopes ?? `${endpoint}/.default`,
});
pipeline.addPolicy(tokenPolicy);
} else if (isKeyCredential(credential)) {
Expand All @@ -63,15 +63,15 @@ export function addCredentialPipelinePolicy(
* Creates a default rest pipeline to re-use accross Rest Level Clients
*/
export function createDefaultPipeline(
baseUrl: string,
endpoint: string,
credential?: TokenCredential | KeyCredential,
options: ClientOptions = {},
): Pipeline {
const pipeline = createPipelineFromOptions(options);

pipeline.addPolicy(apiVersionPolicy(options));

addCredentialPipelinePolicy(pipeline, baseUrl, { credential, clientOptions: options });
addCredentialPipelinePolicy(pipeline, endpoint, { credential, clientOptions: options });
return pipeline;
}

Expand Down
5 changes: 5 additions & 0 deletions sdk/core/ts-http-runtime/src/client/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,13 @@ export type ClientOptions = PipelineOptions & {
};
/**
* Base url for the client
* @deprecated This property is deprecated and will be removed soon, please use endpoint instead
*/
baseUrl?: string;
/**
* Endpoint for the client
*/
endpoint?: string;
/**
* Options for setting a custom apiVersion.
*/
Expand Down
14 changes: 7 additions & 7 deletions sdk/core/ts-http-runtime/src/client/getClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ import { PipelineOptions } from "../createPipelineFromOptions.js";

/**
* Creates a client with a default pipeline
* @param baseUrl - Base endpoint for the client
* @param endpoint - Base endpoint for the client
* @param options - Client options
*/
export function getClient(baseUrl: string, options?: ClientOptions): Client;
export function getClient(endpoint: string, options?: ClientOptions): Client;
/**
* Creates a client with a default pipeline
* @param baseUrl - Base endpoint for the client
* @param endpoint - Base endpoint for the client
* @param credentials - Credentials to authenticate the requests
* @param options - Client options
*/
export function getClient(
baseUrl: string,
endpoint: string,
credentials?: TokenCredential | KeyCredential,
options?: ClientOptions,
): Client;
export function getClient(
baseUrl: string,
endpoint: string,
credentialsOrPipelineOptions?: (TokenCredential | KeyCredential) | ClientOptions,
clientOptions: ClientOptions = {},
): Client {
Expand All @@ -49,7 +49,7 @@ export function getClient(
}
}

const pipeline = createDefaultPipeline(baseUrl, credentials, clientOptions);
const pipeline = createDefaultPipeline(endpoint, credentials, clientOptions);
if (clientOptions.additionalPolicies?.length) {
for (const { policy, position } of clientOptions.additionalPolicies) {
// Sign happens after Retry and is commonly needed to occur
Expand All @@ -64,7 +64,7 @@ export function getClient(
const { allowInsecureConnection, httpClient } = clientOptions;
const client = (path: string, ...args: Array<any>) => {
const getUrl = (requestOptions: RequestParameters): string =>
buildRequestUrl(baseUrl, path, args, { allowInsecureConnection, ...requestOptions });
buildRequestUrl(endpoint, path, args, { allowInsecureConnection, ...requestOptions });

return {
get: (requestOptions: RequestParameters = {}): StreamableMethod => {
Expand Down
18 changes: 9 additions & 9 deletions sdk/core/ts-http-runtime/src/client/urlHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import { RequestParameters } from "./common.js";

/**
* Builds the request url, filling in query and path parameters
* @param baseUrl - base url which can be a template url
* @param routePath - path to append to the baseUrl
* @param endpoint - base url which can be a template url
* @param routePath - path to append to the endpoint
* @param pathParameters - values of the path parameters
* @param options - request parameters including query parameters
* @returns a full url with path and query parameters
*/
export function buildRequestUrl(
baseUrl: string,
endpoint: string,
routePath: string,
pathParameters: string[],
options: RequestParameters = {},
): string {
if (routePath.startsWith("https://") || routePath.startsWith("http://")) {
return routePath;
}
baseUrl = buildBaseUrl(baseUrl, options);
endpoint = buildBaseUrl(endpoint, options);
routePath = buildRoutePath(routePath, pathParameters, options);
const requestUrl = appendQueryParams(`${baseUrl}/${routePath}`, options);
const requestUrl = appendQueryParams(`${endpoint}/${routePath}`, options);
const url = new URL(requestUrl);

return (
Expand Down Expand Up @@ -71,9 +71,9 @@ function skipQueryParameterEncoding(url: URL): URL {
return url;
}

export function buildBaseUrl(baseUrl: string, options: RequestParameters): string {
export function buildBaseUrl(endpoint: string, options: RequestParameters): string {
if (!options.pathParameters) {
return baseUrl;
return endpoint;
}
const pathParams = options.pathParameters;
for (const [key, param] of Object.entries(pathParams)) {
Expand All @@ -87,9 +87,9 @@ export function buildBaseUrl(baseUrl: string, options: RequestParameters): strin
if (!options.skipUrlEncoding) {
value = encodeURIComponent(param);
}
baseUrl = replaceAll(baseUrl, `{${key}}`, value) ?? "";
endpoint = replaceAll(endpoint, `{${key}}`, value) ?? "";
}
return baseUrl;
return endpoint;
}

function buildRoutePath(
Expand Down

0 comments on commit d561c63

Please sign in to comment.