Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 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
Original file line number Diff line number Diff line change
Expand Up @@ -62,50 +62,62 @@ You will also need to **register a new AAD application and grant access to {{ se
For more information about how to create an Azure AD Application check out [this guide](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).

{{#if azureArm}}
```javascript
const { {{ clientClassName }} } = require("{{ clientPackageName }}");
const { DefaultAzureCredential } = require("@azure/identity");
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential. See https://aka.ms/azsdk/js/identity/examples for more details.
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Node{{/if}}
import { {{ clientClassName }} } from "{{ clientPackageName }}";
import { DefaultAzureCredential } from "@azure/identity";

{{#if hasClientSubscriptionId}}
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new {{ clientClassName }}(new DefaultAzureCredential(), subscriptionId);

// For client-side applications running in the browser, use this code instead:
// const credential = new InteractiveBrowserCredential({
// tenantId: "<YOUR_TENANT_ID>",
// clientId: "<YOUR_CLIENT_ID>"
// });
// const client = new {{ clientClassName }}(credential, subscriptionId);
```
{{else}}
const client = new {{ clientClassName }}(new DefaultAzureCredential());
```
{{/if}}

// For client-side applications running in the browser, use this code instead:
// const credential = new InteractiveBrowserCredential({
// tenantId: "<YOUR_TENANT_ID>",
// clientId: "<YOUR_CLIENT_ID>"
// });
// const client = new {{ clientClassName }}(credential);
For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Browser{{/if}}
import { InteractiveBrowserCredential } from "@azure/identity";
import { {{ clientClassName }} } from "{{ clientPackageName }}";

const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>"
});
{{#if hasClientSubscriptionId}}
const client = new {{ clientClassName }}(credential, subscriptionId);
```
{{else}}
const client = new {{ clientClassName }}(credential);
```
{{/if}}
{{else}}
```javascript
const { {{ clientClassName }} } = require("{{ clientPackageName }}");
const { DefaultAzureCredential } = require("@azure/identity");
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential. See https://aka.ms/azsdk/js/identity/examples for more details.
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Node{{/if}}
import { InteractiveBrowserCredential } from "@azure/identity";
import { {{ clientClassName }} } from "{{ clientPackageName }}";

const client = new {{ clientClassName }}("<endpoint>", new DefaultAzureCredential());
// For client-side applications running in the browser, use this code instead:
// const credential = new InteractiveBrowserCredential({
// tenantId: "<YOUR_TENANT_ID>",
// clientId: "<YOUR_CLIENT_ID>"
// });
// const client = new {{ clientClassName }}("<endpoint>", credential);
```

For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.

```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Browser{{/if}}
import { InteractiveBrowserCredential } from "@azure/identity";
import { {{ clientClassName }} } from "{{ clientPackageName }}";

const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>"
});
const client = new {{ clientClassName }}("<endpoint>", credential);
```
{{/if}}
{{/if}}{{/if}}

### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

Expand All @@ -122,8 +134,9 @@ To use this client library in the browser, first you need to use a bundler. For

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:

```javascript
const { setLogLevel } = require("@azure/logger");
```ts {{#if azureSdkForJs}}snippet:SetLogLevel{{/if}}
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function regularAutorestPackage(
"npm run clean && tsc && dev-tool run bundle && npm run minify && dev-tool run vendored mkdirp ./review && npm run extract-api";
packageInfo.scripts["clean"] = "dev-tool run vendored rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz *.log";
packageInfo.scripts["extract-api"] = "dev-tool run extract-api";
packageInfo.scripts["update-snippets"] = "echo skipped";
packageInfo.scripts["update-snippets"] = "dev-tool run update-snippets";
packageInfo.scripts["minify"] = `dev-tool run vendored uglifyjs -c -m --comments --source-map "content='./dist/index.js.map'" -o ./dist/index.min.js ./dist/index.js`;
} else {
packageInfo.devDependencies["@rollup/plugin-commonjs"] = "^24.0.0";
Expand All @@ -201,6 +201,7 @@ function regularAutorestPackage(
if (generateTest) {
packageInfo.module = `./dist-esm/src/index.js`;
packageInfo.devDependencies["@azure/identity"] = "^4.2.1";
packageInfo.devDependencies["@azure/logger"] = "^1.1.4";
packageInfo.devDependencies["@azure-tools/test-recorder"] = "^3.0.0";
packageInfo.devDependencies["@azure-tools/test-credential"] = "^1.1.0";
packageInfo.devDependencies["mocha"] = "^11.0.2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ interface Metadata {
hasMultiClients?: boolean;
/** Indicates if we have a client-level subscription id paramter */
hasClientSubscriptionId?: boolean;
/** Indicates if the package is generted to azure-sdk-for-js repo */
azureSdkForJs?: boolean;
}

/**
Expand All @@ -83,7 +85,8 @@ function createMetadata(
productDocLink,
dependencyInfo,
multiClient,
batch
batch,
azureSdkForJs
} = getAutorestOptions();
const { addCredentials } = getSecurityInfoFromModel(codeModel.security);

Expand Down Expand Up @@ -149,8 +152,8 @@ function createMetadata(
: packageSourceURL && `${packageSourceURL}/samples`,
impressionURL: azureHuh
? packageParentDirectoryName &&
packageDirectoryName &&
`https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2F${packageParentDirectoryName}%2F${packageDirectoryName}%2FREADME.png`
packageDirectoryName &&
`https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2F${packageParentDirectoryName}%2F${packageDirectoryName}%2FREADME.png`
: undefined,
clientDescriptiveName: `${serviceName} client`,
description: codeModel.info?.description,
Expand All @@ -168,7 +171,8 @@ function createMetadata(
dependencyDescription: dependencyInfo?.description,
dependencyLink: dependencyInfo?.link,
hasMultiClients: multiClient && batch && batch.length > 1,
hasClientSubscriptionId: hasClientSubscriptionId(clientDetails?.samples)
hasClientSubscriptionId: hasClientSubscriptionId(clientDetails?.samples),
azureSdkForJs: azureSdkForJs
};
}

Expand All @@ -192,7 +196,7 @@ export function generateReadmeFile(
});
}

function hasClientSubscriptionId(samples?: SampleGroup[]) {
export function hasClientSubscriptionId(samples?: SampleGroup[]) {
if (!samples || samples.length === 0) {
// have the subscription id parameter in constructor if no samples
return true;
Expand All @@ -204,7 +208,7 @@ function hasClientSubscriptionId(samples?: SampleGroup[]) {
});
}

function getClientAndServiceName(
export function getClientAndServiceName(
codeModelLanguage: Languages,
codeModelInfo: Info
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { {{ clientClassName }} } from "../src/index.js";
import { DefaultAzureCredential, InteractiveBrowserCredential } from "@azure/identity";
import { setLogLevel } from "@azure/logger";
import { describe, it } from "vitest";

describe("snippets", () => {
it("ReadmeSampleCreateClient_Node", async () => {
{{#if azureArm}}
{{#if hasClientSubscriptionId}}
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new {{ clientClassName }}(new DefaultAzureCredential(), subscriptionId);
{{else}}
const client = new {{ clientClassName }}(new DefaultAzureCredential());
{{/if}}
{{else}}
const client = new {{ clientClassName }}("<endpoint>", new DefaultAzureCredential());
{{/if}}
});

it("ReadmeSampleCreateClient_Browser", async () => {
{{#if azureArm}}
{{#if hasClientSubscriptionId}}
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new {{ clientClassName }}(credential, subscriptionId);
{{else}}
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new {{ clientClassName }}(credential);
{{/if}}
{{else}}
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new {{ clientClassName }}("<endpoint>", credential);
{{/if}}
});

it("SetLogLevel", async () => {
setLogLevel("info");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Project } from "ts-morph";
import * as path from "path";
import * as fs from "fs";
import * as hbs from "handlebars";
import { CodeModel } from "@autorest/codemodel";
import { ClientDetails } from "../../models/clientDetails";
import { getAutorestOptions } from "../../autorestSession";
import { getClientAndServiceName, hasClientSubscriptionId } from "../static/readmeFileGenerator";

export function generateSnippetsFile(
codeModel: CodeModel,
project: Project,
clientDetails?: ClientDetails
) {
const { generateTest, azureSdkForJs, azureArm } = getAutorestOptions();
if (!generateTest || !azureSdkForJs) {
return;
}
const { clientClassName } = getClientAndServiceName(
codeModel.language,
codeModel.info
);
const metadata = {
clientClassName: clientClassName,
azureArm: azureArm,
hasClientSubscriptionId: hasClientSubscriptionId(clientDetails?.samples)
};
const file = fs.readFileSync(path.join(__dirname, "snippets.ts.hbs"), {
encoding: "utf-8"
});
const readmeFileContents = hbs.compile(file, { noEscape: true });
project.createSourceFile("test/snippets.spec.ts", readmeFileContents(metadata), {
overwrite: true
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
buildSerializeHelper,
buildLogger,
buildSamples,
updatePackageFile
updatePackageFile,
buildSnippets
} from "@azure-tools/rlc-common";
import {
generateFileByBuilder,
Expand Down Expand Up @@ -87,6 +88,8 @@ export async function generateRestLevelClient() {
generateFileByBuilder(project, buildRecordedClientFile, rlcModels);
// buildSampleTest
generateFileByBuilder(project, buildSampleTest, rlcModels);
// buildSnippets
generateFileByBuilder(project, buildSnippets, rlcModels);
}

// buildResponseTypes
Expand Down
2 changes: 2 additions & 0 deletions packages/autorest.typescript/src/typescriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { generatePagingFiles } from "./generators/pagingHelperGenerator";
import { generateTracingFile } from "./generators/tracingFileGenerator";
import { getAutorestOptions } from "./autorestSession";
import { conflictResolver } from "./conflictResolver";
import { generateSnippetsFile } from "./generators/test/snippetsGenerator";

const prettierTypeScriptOptions: prettier.Options = {
parser: "typescript",
Expand Down Expand Up @@ -82,6 +83,7 @@ export async function generateTypeScriptLibrary(
generateLicenseFile(project);
generateReadmeFile(codeModel, project, clientDetails);
if (generateTest) {
generateSnippetsFile(codeModel, project, clientDetails);
generateSampleTestFile(project);
}
generateTsConfig(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP




### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"rollup-plugin-sourcemaps": "^0.6.3",
"uglify-js": "^3.4.9",
"@azure/identity": "^4.2.1",
"@azure/logger": "^1.1.4",
"@azure-tools/test-recorder": "^3.0.0",
"@azure-tools/test-credential": "^1.1.0",
"mocha": "^11.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP




### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"integration-test": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test:browser": "echo skipped",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP




### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"integration-test": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test:browser": "echo skipped",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP




### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"integration-test": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test:browser": "echo skipped",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP




### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"integration-test": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test:browser": "echo skipped",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP




### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

Expand Down
Loading