Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UserAgentInfo and Constantpaths issues #2888

Merged
merged 24 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/rlc-common/src/metadata/buildPackageFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import { getRelativePartFromSrcPath } from "../helpers/pathUtils.js";
interface PackageFileOptions {
exports?: Record<string, any>;
dependencies?: Record<string, string>;
clientContextPaths?: string[];
}

export function buildPackageFile(
model: RLCModel,
{ exports, dependencies }: PackageFileOptions = {}
{ exports, dependencies, clientContextPaths }: PackageFileOptions = {}
) {
const config: PackageCommonInfoConfig = {
description: getDescription(model),
Expand All @@ -46,7 +47,8 @@ export function buildPackageFile(
hasLro: hasPollingOperations(model),
monorepoPackageDirectory: model.options?.azureOutputDirectory,
specSource: model.options?.sourceFrom ?? "TypeSpec",
dependencies
dependencies,
clientContextPaths
};

if (isAzureMonorepoPackage(model)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
export interface AzureMonorepoInfoConfig extends AzurePackageInfoConfig {
monorepoPackageDirectory?: string;
clientFilePaths: string[];
clientContextPaths?: string[];
}

/**
Expand Down Expand Up @@ -57,18 +58,6 @@ export function getAzureMonorepoDependencies(config: AzureMonorepoInfoConfig) {
export function getAzureMonorepoPackageInfo(
config: AzureMonorepoInfoConfig
): Record<string, any> {
const metadata: Record<string, any> = {
constantPaths: []
};

addSwaggerMetadata(metadata, config.specSource);
for (const clientFilePath of config.clientFilePaths) {
metadata.constantPaths.push({
path: clientFilePath,
prefix: "userAgentInfo"
});
}

const commonPackageInfo = getPackageCommonInfo(config);

return {
Expand All @@ -83,7 +72,7 @@ export function getAzureMonorepoPackageInfo(
homepage: `https://github.com/Azure/azure-sdk-for-js/tree/main/${config.monorepoPackageDirectory}/README.md`
}),
prettier: "@azure/eslint-plugin-azure-sdk/prettier.json",
"//metadata": metadata
"//metadata": getMetadataInfo(config)
};
}

Expand Down Expand Up @@ -255,3 +244,21 @@ function getCjsScripts({ moduleKind }: AzureMonorepoInfoConfig) {
"unit-test:browser": "dev-tool run test:browser"
};
}

function getMetadataInfo(config: AzureMonorepoInfoConfig) {
const metadata: Record<string, any> = {
constantPaths: []
};
const paths = config.isModularLibrary
? config.clientContextPaths
: config.clientFilePaths;
addSwaggerMetadata(metadata, config.specSource);
for (const path of paths ?? []) {
metadata.constantPaths.push({
path: path,
prefix: "userAgentInfo"
});
}

return metadata;
}
3 changes: 2 additions & 1 deletion packages/rlc-common/test/integration/mockHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type TestModelConfig = {
hasLro?: boolean;
hasPaging?: boolean;
isModularLibrary?: boolean;
azureSdkForJs?: boolean;
};

export function createMockModel(config: TestModelConfig = {}): RLCModel {
Expand Down Expand Up @@ -48,7 +49,7 @@ export function createMockModel(config: TestModelConfig = {}): RLCModel {
generateSample: config.withSamples ?? false,
moduleKind: config.moduleKind,
sourceFrom: config.source ?? "TypeSpec",
isModularLibrary: config.isModularLibrary ?? false
isModularLibrary: config.isModularLibrary ?? false,
},
helperDetails: {
hasPaging: config.hasPaging ?? false,
Expand Down
26 changes: 26 additions & 0 deletions packages/rlc-common/test/integration/packageJson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,32 @@ describe("Package file generation", () => {
'dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore "src/**/*.{ts,cts,mts}" "test/**/*.{ts,cts,mts}" "*.{js,cjs,mjs,json}" '
);
});

it("[esm] should read clientContextPaths from config for modular", () => {
const model = createMockModel({
...baseConfig,
moduleKind: "esm",
isModularLibrary: true
});

const packageFileContent = buildPackageFile(model, {
clientContextPaths: ["src/api/chatCompletionsContext.ts"]
});
const packageFile = JSON.parse(packageFileContent?.content ?? "{}");
expect(packageFile).to.have.property("//metadata");
expect(packageFile["//metadata"]["constantPaths"][0]).to.have.property("path", "src/api/chatCompletionsContext.ts", "modular");
});

it("[esm] should read clientPath from config for rlc", () => {
const model = createMockModel({
...baseConfig,
moduleKind: "esm",
});
const packageFileContent = buildPackageFile(model);
const packageFile = JSON.parse(packageFileContent?.content ?? "{}");
expect(packageFile).to.have.property("//metadata");
expect(packageFile["//metadata"]["constantPaths"][0]).to.have.property("path", "src/msinternal/test.ts", "rlc");
});
});

describe("Azure flavor for standalone library", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export function createNetworkAnalytics(
): NetworkAnalyticsContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `https://management.azure.com`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-arm-networkanalytics/1.0.0-beta.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class NetworkAnalyticsClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createNetworkAnalytics(credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class AnomalyDetectorClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createAnomalyDetector(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export function createAnomalyDetector(
options.endpoint ??
options.baseUrl ??
`${endpointParam}/anomalydetector/${apiVersion}`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-ai-anomaly-detector/1.0.0-beta.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export function createBatch(
options: BatchClientOptionalParams = {},
): BatchContext {
const endpointUrl = options.endpoint ?? options.baseUrl ?? `${endpointParam}`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-batch/1.0.0-beta.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class BatchClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createBatch(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export function createChatProtocol(
options: ChatProtocolClientOptionalParams = {},
): ChatProtocolContext {
const endpointUrl = options.endpoint ?? options.baseUrl ?? `${endpointParam}`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-ai-chat-protocol/1.0.0-beta.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ChatProtocolClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createChatProtocol(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export function createContentSafety(
): ContentSafetyContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `${endpointParam}/contentsafety`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-ai-content-safety/1.0.0`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ContentSafetyClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createContentSafety(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export function createEventGrid(
options: EventGridClientOptionalParams = {},
): EventGridContext {
const endpointUrl = options.endpoint ?? options.baseUrl ?? `${endpointParam}`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-eventgrid/1.0.0-beta.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class EventGridClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createEventGrid(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export function createRadiologyInsights(
): RadiologyInsightsContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `${endpointParam}/health-insights`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-health-insights-radiologyinsights/1.0.0-beta.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class RadiologyInsightsClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createRadiologyInsights(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export function createFoo(
options: FooClientOptionalParams = {},
): FooContext {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-hierarchy-generic/1.0.0-beta.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class FooClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createFoo(endpointParam, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export function createLoadTestAdministration(
): LoadTestAdministrationContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `https://${endpointParam}`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-load-testing/1.0.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class LoadTestAdministrationClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createLoadTestAdministration(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export function createLoadTestRun(
): LoadTestRunContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `https://${endpointParam}`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-load-testing/1.0.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class LoadTestRunClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createLoadTestRun(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export function createTestProfileAdministration(
): TestProfileAdministrationContext {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `https://${endpointParam}`;

const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentInfo = `azsdk-js-load-testing/1.0.1`;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: "azsdk-js-api";
? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}`
: `azsdk-js-api ${userAgentInfo}`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class TestProfileAdministrationClient {
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-client`
: "azsdk-js-client";
: `azsdk-js-client`;
this._client = createTestProfileAdministration(endpointParam, credential, {
...options,
userAgentOptions: { userAgentPrefix },
Expand Down
Loading
Loading