Skip to content

Commit

Permalink
[advisor] Update @azure/arm-advisor to use snippets (#32415)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR

- @azure/arm-advisor

### Issues associated with this PR

- #32416

### Describe the problem that is addressed by this PR

Adds snippets for the README which are type checked.

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
mpodwysocki authored Jan 3, 2025
1 parent e81ed7e commit de423d6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
32 changes: 20 additions & 12 deletions sdk/advisor/arm-advisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,27 @@ Set the values of the client ID, tenant ID, and client secret of the AAD applica

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).

```javascript
const { AdvisorManagementClient } = require("@azure/arm-advisor");
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 snippet:AdvisorManagementClientAuth_Node
import { AdvisorManagementClient } from "@azure/arm-advisor";
import { DefaultAzureCredential } from "@azure/identity";

const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new AdvisorManagementClient(new DefaultAzureCredential(), subscriptionId);
```

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

// 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 AdvisorManagementClient(credential, subscriptionId);
```ts snippet:AdvisorManagementClientAuth_Browser
import { InteractiveBrowserCredential } from "@azure/identity";
import { AdvisorManagementClient } from "@azure/arm-advisor";

const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new AdvisorManagementClient(credential, subscriptionId);
```

### JavaScript Bundle
Expand All @@ -80,8 +87,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 snippet:setLogLevel
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
```

Expand Down
10 changes: 4 additions & 6 deletions sdk/advisor/arm-advisor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"azure",
"typescript",
"browser",
"isomorphic"
"isomorphic",
"cloud"
],
"license": "MIT",
"main": "./dist/commonjs/index.js",
Expand All @@ -39,10 +40,7 @@
"typescript": "~5.7.2",
"vitest": "^2.1.8"
},
"repository": {
"type": "git",
"url": "https://github.com/Azure/azure-sdk-for-js.git"
},
"repository": "github:Azure/azure-sdk-for-js",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
Expand Down Expand Up @@ -77,7 +75,7 @@
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "echo skipped",
"unit-test:node": "dev-tool run test:vitest",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand Down
26 changes: 26 additions & 0 deletions sdk/advisor/arm-advisor/test/snippets.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { DefaultAzureCredential, InteractiveBrowserCredential } from "@azure/identity";
import { AdvisorManagementClient } from "@azure/arm-advisor";
import { setLogLevel } from "@azure/logger";
import { describe, it } from "vitest";

describe("snippets", function () {
it("AdvisorManagementClientAuth_Node", async function () {
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new AdvisorManagementClient(new DefaultAzureCredential(), subscriptionId);
});

it("AdvisorManagementClientAuth_Browser", async function () {
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new AdvisorManagementClient(credential, subscriptionId);
});

it("setLogLevel", async function () {
setLogLevel("info");
});
});

0 comments on commit de423d6

Please sign in to comment.