Skip to content

Commit

Permalink
[apimanagement] Update to use snippets (#32475)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR

- @azure/arm-apimanagement
- @azure/api-management-custom-widgets-scaffolder
- @azure/api-management-custom-widgets-tools

### Issues associated with this PR

- #32416

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

Updates all `apimanagement` packages to use snippets.

### 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 9, 2025
1 parent 52eecea commit 1093f4a
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class UI {
constructor(opts) {
var _a;
this.width = opts.width;
this.wrap = (_a = opts.wrap) !== null && _a !== void 0 ? _a : true;
this.wrap = (_a = opts.wrap) !== null && _a !== undefined ? _a : true;
this.rows = [];
}
span(...args) {
Expand Down Expand Up @@ -523,8 +523,8 @@ let mixin;
function cliui(opts, _mixin) {
mixin = _mixin;
return new UI({
width: (opts === null || opts === void 0 ? void 0 : opts.width) || getWindowWidth(),
wrap: opts === null || opts === void 0 ? void 0 : opts.wrap
width: (opts === null || opts === undefined ? undefined : opts.width) || getWindowWidth(),
wrap: opts === null || opts === undefined ? undefined : opts.wrap
});
}

Expand Down Expand Up @@ -875,7 +875,7 @@ const buildGetConfig = (gray, red) => {
}
else {
gray("Retrieved from the command parameters");
Object.entries(configPartial).forEach(([key, value]) => { var _a; return value != null && gray(`${(_a = fieldIdToName[key]) !== null && _a !== void 0 ? _a : key}: ${value}`); });
Object.entries(configPartial).forEach(([key, value]) => { var _a; return value != null && gray(`${(_a = fieldIdToName[key]) !== null && _a !== undefined ? _a : key}: ${value}`); });
return configPartial;
}
};
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 --no-test-proxy",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe } from "vitest";

describe("snippets", () => {});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "npm run build:test && dev-tool run test:vitest --no-test-proxy --browser",
"unit-test:node": "dev-tool run test:vitest --no-test-proxy",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe } from "vitest";

describe("snippets", () => {});
35 changes: 22 additions & 13 deletions sdk/apimanagement/arm-apimanagement/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,32 @@ 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://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).

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

const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new ApiManagementClient(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 ApiManagementClient(credential, subscriptionId);
```

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

```ts snippet:ReadmeSampleCreateClient_Browser
import { InteractiveBrowserCredential } from "@azure/identity";
import { ApiManagementClient } from "@azure/arm-apimanagement";

const subscriptionId = "00000000-0000-0000-0000-000000000000";
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new ApiManagementClient(credential, subscriptionId);
```

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

## Key concepts
Expand All @@ -80,8 +88,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
4 changes: 2 additions & 2 deletions sdk/apimanagement/arm-apimanagement/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@azure/core-lro": "^2.7.2",
"@azure/core-paging": "^1.6.2",
"@azure/core-rest-pipeline": "^1.18.0",
"tslib": "^2.2.0"
"tslib": "^2.8.1"
},
"keywords": [
"node",
Expand Down Expand Up @@ -75,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
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
*/

import { createTestCredential } from "@azure-tools/test-credential";
import {
delay,
env,
isPlaybackMode,
Recorder,
RecorderStartOptions,
} from "@azure-tools/test-recorder";
import type { RecorderStartOptions } from "@azure-tools/test-recorder";
import { delay, env, isPlaybackMode, Recorder } from "@azure-tools/test-recorder";
import { ApiManagementClient } from "../src/apiManagementClient.js";
import { describe, it, assert, beforeEach, afterEach } from "vitest";

Expand Down Expand Up @@ -85,7 +80,7 @@ describe("Apimanagement test", () => {

it("apiManagementService listByResourceGroup test", async () => {
const resArray = new Array();
for await (let item of client.apiManagementService.listByResourceGroup(resourceGroupName)) {
for await (const item of client.apiManagementService.listByResourceGroup(resourceGroupName)) {
resArray.push(item);
}
assert.equal(resArray.length, 1);
Expand All @@ -96,8 +91,8 @@ describe("Apimanagement test", () => {
while (count < 20) {
count++;
const res = await client.apiManagementService.get(resourceGroupName, serviceName);
if (res.provisioningState == "Succeeded") {
const res = await client.apiManagementService.beginUpdateAndWait(
if (res.provisioningState === "Succeeded") {
const updateResult = await client.apiManagementService.beginUpdateAndWait(
resourceGroupName,
serviceName,
{
Expand All @@ -107,7 +102,7 @@ describe("Apimanagement test", () => {
},
testPollingOptions,
);
assert.equal(res.type, "Microsoft.ApiManagement/service");
assert.equal(updateResult.type, "Microsoft.ApiManagement/service");
break;
} else {
// The resource is activating
Expand All @@ -122,7 +117,7 @@ describe("Apimanagement test", () => {

it("backend list test", async () => {
const resArray = new Array();
for await (let item of client.backend.listByService(resourceGroupName, serviceName, {
for await (const item of client.backend.listByService(resourceGroupName, serviceName, {
top: 1,
})) {
resArray.push(item);
Expand All @@ -132,7 +127,7 @@ describe("Apimanagement test", () => {

it("backend delete test", async () => {
const resArray = new Array();
for await (let item of client.backend.listByService(resourceGroupName, serviceName)) {
for await (const item of client.backend.listByService(resourceGroupName, serviceName)) {
resArray.push(item);
}
assert.equal(resArray.length, 0);
Expand All @@ -143,9 +138,11 @@ describe("Apimanagement test", () => {
while (count < 20) {
count++;
const res = await client.apiManagementService.get(resourceGroupName, serviceName);
if (res.provisioningState == "Succeeded") {
if (res.provisioningState === "Succeeded") {
const resArray = new Array();
for await (let item of client.apiManagementService.listByResourceGroup(resourceGroupName)) {
for await (const item of client.apiManagementService.listByResourceGroup(
resourceGroupName,
)) {
resArray.push(item);
}
assert.equal(resArray.length, 0);
Expand Down
27 changes: 27 additions & 0 deletions sdk/apimanagement/arm-apimanagement/test/snippets.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

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

describe("snippets", () => {
it("ReadmeSampleCreateClient_Node", async () => {
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new ApiManagementClient(new DefaultAzureCredential(), subscriptionId);
});

it("ReadmeSampleCreateClient_Browser", async () => {
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new ApiManagementClient(credential, subscriptionId);
});

it("SetLogLevel", async () => {
setLogLevel("info");
});
});

0 comments on commit 1093f4a

Please sign in to comment.