Skip to content

Commit

Permalink
[identity] Migrate rest of identity projects to use snippets extracti…
Browse files Browse the repository at this point in the history
…on (#33201)

### Packages impacted by this PR

- @azure/identity-cache-persistence
- @azure/identity-vscode

### Issues associated with this PR

- #32416

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

Updates all remaining projects under `identity` to use snippets
extraction.

### 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 Mar 3, 2025
1 parent fabbcb1 commit 3352cfc
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 116 deletions.
15 changes: 10 additions & 5 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions sdk/identity/identity-cache-persistence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ If this is your first time using `@azure/identity` or Microsoft Entra ID, we rec

As of `@azure/identity` version 2.0.0, the Identity client library for JavaScript includes a plugin API. This package (`@azure/identity-cache-persistence`) exports a plugin object that you must pass as an argument to the top-level `useIdentityPlugin` function from the `@azure/identity` package. Enable token cache persistence in your program as follows:

```ts snippet:getting_started
```ts snippet:ReadmeSampleUsePlugin
import { useIdentityPlugin } from "@azure/identity";
import { cachePersistencePlugin } from "@azure/identity-cache-persistence";

Expand All @@ -44,7 +44,7 @@ After calling `useIdentityPlugin`, the persistent token cache plugin is register

Once the plugin is registered, you can enable token cache persistence by passing `tokenCachePersistenceOptions` with an `enabled` property set to `true` to a credential constructor. In the following example, we use the `DeviceCodeCredential`, since persistent caching of its tokens allows you to skip the interactive device-code authentication flow if a cached token is available.

```ts snippet:device_code_credential_example
```ts snippet:ReadmeSampleDeviceCodeCredential
import { DeviceCodeCredential } from "@azure/identity";

const credential = new DeviceCodeCredential({
Expand All @@ -66,7 +66,7 @@ console.log((await credential.getToken(scope)).token.substring(0, 10), "...");

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`:

```ts snippet:logging
```ts snippet:SetLogLevel
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
Expand Down
17 changes: 9 additions & 8 deletions sdk/identity/identity-cache-persistence/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "echo skipped",
"unit-test:node": "echo skipped",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"files": [
"dist/",
Expand Down Expand Up @@ -71,16 +71,17 @@
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/logger": "^1.1.4",
"@types/node": "^18.0.0",
"@vitest/browser": "^3.0.3",
"@vitest/coverage-istanbul": "^3.0.3",
"@vitest/browser": "^3.0.6",
"@vitest/coverage-istanbul": "^3.0.6",
"dotenv": "^16.0.0",
"eslint": "^9.9.0",
"playwright": "^1.49.0",
"playwright": "^1.50.1",
"typescript": "~5.7.2",
"vitest": "^3.0.3"
"vitest": "^3.0.6"
},
"type": "module",
"tshy": {
"project": "./tsconfig.src.json",
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
Expand All @@ -93,8 +94,7 @@
"browser",
"react-native"
],
"selfLink": false,
"project": "./tsconfig.src.json"
"selfLink": false
},
"browser": "./dist/browser/index.js",
"exports": {
Expand All @@ -117,5 +117,6 @@
"default": "./dist/commonjs/index.js"
}
}
}
},
"react-native": "./dist/react-native/index.js"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import { useIdentityPlugin, DeviceCodeCredential } from "@azure/identity";
import { cachePersistencePlugin } from "@azure/identity-cache-persistence";
useIdentityPlugin(cachePersistencePlugin);

import dotenv from "dotenv";
dotenv.config();
import "dotenv/config";

async function main() {
async function main(): Promise<void> {
const credential = new DeviceCodeCredential({
// This property must be provided, with `enabled` set to true to enable
// persistent token caching.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
/**
* @summary Authenticates using persistent token caching with Interactive Browser Credential
*/
import {
InteractiveBrowserCredential,
useIdentityPlugin,
AuthenticationRecord,
} from "@azure/identity";
import type { AuthenticationRecord } from "@azure/identity";
import { InteractiveBrowserCredential, useIdentityPlugin } from "@azure/identity";
import { cachePersistencePlugin } from "@azure/identity-cache-persistence";
import path from "path";
import fs from "fs";
import dotenv from "dotenv";
import path from "node:path";
import fs from "node:fs";
import "dotenv/config";

useIdentityPlugin(cachePersistencePlugin);

dotenv.config();
// The app registration client ID in the Microsoft Entra tenant
const clientId = "APP-REGISTRATION-CLIENT-ID";
// The tenant ID in Microsoft Entra ID
Expand All @@ -26,7 +21,7 @@ const loginHint = `EMAIL-ADDRESS`;
// The file path to save the authentication record
const authenticationRecordFilePath = path.resolve(__dirname, "authenticationRecord.json");

async function main() {
async function main(): Promise<void> {
try {
const fileContents = await fs.promises.readFile(authenticationRecordFilePath, "utf8");
const authenticationRecord = JSON.parse(fileContents);
Expand Down
2 changes: 1 addition & 1 deletion sdk/identity/identity-cache-persistence/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface AzurePluginContext {
*
* Example:
*
* ```ts snippet:device_code_credential_example
* ```ts snippet:ReadmeSampleDeviceCodeCredential
* import { DeviceCodeCredential } from "@azure/identity";
*
* const credential = new DeviceCodeCredential({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("ClientCertificateCredential (internal)", () => {
);
});

afterEach(async function () {
afterEach(async () => {
await cleanup();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("ClientSecretCredential (internal)", () => {
"acquireTokenByClientCredential",
);
});
afterEach(async function () {
afterEach(async () => {
await cleanup();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("DeviceCodeCredential (internal)", () => {
doGetTokenSpy = vi.spyOn(PublicClientApplication.prototype, "acquireTokenByDeviceCode");
});

afterEach(async function () {
afterEach(async () => {
await cleanup();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("UsernamePasswordCredential (internal)", () => {
doGetTokenSpy = vi.spyOn(PublicClientApplication.prototype, "acquireTokenByUsernamePassword");
});

afterEach(async function () {
afterEach(async () => {
await cleanup();
});

Expand Down
8 changes: 4 additions & 4 deletions sdk/identity/identity-cache-persistence/test/snippets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import { DeviceCodeCredential, useIdentityPlugin } from "@azure/identity";
import { cachePersistencePlugin } from "@azure/identity-cache-persistence";
import { setLogLevel } from "@azure/logger";
import { describe, it, assert } from "vitest";
import { describe, it } from "vitest";

describe("snippets", () => {
it("getting_started", () => {
it("ReadmeSampleUsePlugin", () => {
useIdentityPlugin(cachePersistencePlugin);
});

it("device_code_credential_example", async () => {
it("ReadmeSampleDeviceCodeCredential", async () => {
const credential = new DeviceCodeCredential({
tokenCachePersistenceOptions: {
enabled: true,
Expand All @@ -25,7 +25,7 @@ describe("snippets", () => {
console.log((await credential.getToken(scope)).token.substring(0, 10), "...");
});

it("logging", async () => {
it("SetLogLevel", async () => {
setLogLevel("info");
});
});
17 changes: 10 additions & 7 deletions sdk/identity/identity-cache-persistence/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"references": [
{ "path": "./tsconfig.src.json" },
{ "path": "./tsconfig.samples.json" },
{ "path": "./tsconfig.test.json" }
],
"compilerOptions": {
"esModuleInterop": true
}
{
"path": "./tsconfig.src.json"
},
{
"path": "./tsconfig.samples.json"
},
{
"path": "./tsconfig.test.json"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export default mergeConfig(
viteConfig,
defineConfig({
test: {
include: [
"dist-test/browser/test/**/*.spec.js",
],
include: ["dist-test/browser/test/**/*.spec.js",],
testTimeout: 1200000,
hookTimeout: 1200000,
},
}),
);
12 changes: 11 additions & 1 deletion sdk/identity/identity-cache-persistence/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { defineConfig, mergeConfig } from "vitest/config";
import viteConfig from "../../../vitest.shared.config.ts";

export default viteConfig;
export default mergeConfig(
viteConfig,
defineConfig({
test: {
testTimeout: 1200000,
hookTimeout: 1200000,
},
}),
);
12 changes: 12 additions & 0 deletions sdk/identity/identity-cache-persistence/vitest.esm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { mergeConfig } from "vitest/config";
import vitestConfig from "./vitest.config.ts";
import vitestEsmConfig from "../../../vitest.esm.shared.config.ts";

export default mergeConfig(
vitestConfig,
vitestEsmConfig
);
Loading

0 comments on commit 3352cfc

Please sign in to comment.