Skip to content
Open
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
11 changes: 11 additions & 0 deletions common/tools/dev-tool/src/util/samples/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ export async function createTsconfig(projectInfo: ProjectInfo): Promise<string>
delete tsconfig.compilerOptions.inlineSources;
delete tsconfig.compilerOptions.sourceMap;
delete tsconfig.compilerOptions.verbatimModuleSyntax;
// `allowImportingTsExtensions` is enabled in `samples-dev` so that local samples
// can `import "./foo.ts"` and be executed directly with Node's TypeScript loader.
// Published samples are compiled with `tsc` and emit `.js` files, so the `.ts`
// specifiers are rewritten back to `.js` during publishing — strip this flag.
delete tsconfig.compilerOptions.allowImportingTsExtensions;
tsconfig.include = ["./src"];
tsconfig.compilerOptions.outDir = "./dist";
tsconfig.compilerOptions.rootDir = "./src";
Expand Down Expand Up @@ -439,6 +444,12 @@ export async function makeSamplesFactory(
.replace(/\/\*\*(\s+\*)*/s, `/**\n *`)
// Finally remove empty doc comments.
.replace(/\s*\/\*\*(\s+\*)*\/\s*/s, "\n\n")
// Rewrite relative imports/requires that reference `.ts` files to `.js`,
// since published samples are consumed as JavaScript (or compiled with tsc
// using `nodenext` resolution, which also expects `.js` specifiers).
// The `.ts` form is used in samples-dev sources so that `dev-tool samples run`
// can load them directly via Node's TypeScript loader.
.replace(/(["'])(\.\.?\/[^"']*?)\.ts\1/g, "$1$2.js$1")
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Azure Template client library samples for JavaScript

These sample programs show how to use the JavaScript client libraries for Azure Template in some common scenarios.

| **File Name** | **Description** |
| ----------------------- | -------------------------------------------------------------------------------------------------------------- |
| [useUtils.js][useutils] | verifies that relative imports written with a `.ts` extension are rewritten to `.js` in the published samples. |

## Prerequisites

The sample programs are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).

You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs:

- [Azure App Configuration][createinstance_azureappconfiguration]

Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.

Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package].

## Setup

To run the samples using the published version of the package:

1. Install the dependencies using `npm`:

```bash
npm install
```

2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically.

3. Run whichever samples you like (note that some samples may require additional setup, see the table above):

```bash
node useUtils.js
```

Alternatively, run a single sample with the required environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):

```bash
node useUtils.js
```

## Next Steps

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.

[useutils]: https://github.com/Azure/azure-sdk-for-js/blob/main/common/tools/dev-tool/test/samples/files/expectations/ts-imports/javascript/useUtils.js
[apiref]: https://learn.microsoft.com/javascript/api/
[freesub]: https://azure.microsoft.com/free/
[createinstance_azureappconfiguration]: https://learn.microsoft.com/azure/azure-app-configuration/
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/common/tools/dev-tool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@azure-samples/ts-imports-js",
"private": true,
"version": "1.0.0",
"description": "Azure Template client library samples for JavaScript",
"engines": {
"node": ">=20.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Azure/azure-sdk-for-js.git",
"directory": "common/tools/dev-tool/test/samples/files/expectations/ts-imports"
},
"keywords": [
"ts-imports"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/common/tools/dev-tool/test/samples/files/expectations/ts-imports",
"dependencies": {
"ts-imports": "latest",
"dotenv": "latest"
},
"devDependencies": {
"cross-env": "latest"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Used in most samples.
SOME_VARIABLE="<value>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary verifies that relative imports written with a `.ts` extension are
* rewritten to `.js` in the published samples.
*/

const { greet } = require("./utils/helpers.js");
const { CONSTANT } = require("./utils/constants.js");

async function main() {
console.log(greet("world"));
console.log("constant:", CONSTANT);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

const CONSTANT = 42;

module.exports = { CONSTANT };
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

function greet(name) {
return `Hello, ${name}!`;
}

module.exports = { greet };
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Azure Template client library samples for TypeScript

These sample programs show how to use the TypeScript client libraries for Azure Template in some common scenarios.

| **File Name** | **Description** |
| ----------------------- | -------------------------------------------------------------------------------------------------------------- |
| [useUtils.ts][useutils] | verifies that relative imports written with a `.ts` extension are rewritten to `.js` in the published samples. |

## Prerequisites

The sample programs are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).

Before running the samples in Node, they must be compiled to JavaScript using the TypeScript compiler. For more information on TypeScript, see the [TypeScript documentation][typescript]. Install the TypeScript compiler using:

```bash
npm install -g typescript
```

You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs:

- [Azure App Configuration][createinstance_azureappconfiguration]

Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.

Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package].

## Setup

To run the samples using the published version of the package:

1. Install the dependencies using `npm`:

```bash
npm install
```

2. Compile the samples:

```bash
npm run build
```

3. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically.

4. Run whichever samples you like (note that some samples may require additional setup, see the table above):

```bash
node dist/useUtils.js
```

Alternatively, run a single sample with the required environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):

```bash
node dist/useUtils.js
```

## Next Steps

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.

[useutils]: https://github.com/Azure/azure-sdk-for-js/blob/main/common/tools/dev-tool/test/samples/files/expectations/ts-imports/typescript/src/useUtils.ts
[apiref]: https://learn.microsoft.com/javascript/api/
[freesub]: https://azure.microsoft.com/free/
[createinstance_azureappconfiguration]: https://learn.microsoft.com/azure/azure-app-configuration/
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/common/tools/dev-tool/README.md
[typescript]: https://www.typescriptlang.org/docs/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@azure-samples/ts-imports-ts",
"private": true,
"version": "1.0.0",
"description": "Azure Template client library samples for TypeScript",
"engines": {
"node": ">=20.0.0"
},
"scripts": {
"build": "tsc",
"prebuild": "rimraf dist/"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Azure/azure-sdk-for-js.git",
"directory": "common/tools/dev-tool/test/samples/files/expectations/ts-imports"
},
"keywords": [
"ts-imports"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/common/tools/dev-tool/test/samples/files/expectations/ts-imports",
"dependencies": {
"ts-imports": "latest",
"dotenv": "latest"
},
"devDependencies": {
"@types/node": "^20.0.0",
"cross-env": "latest",
"rimraf": "latest",
"typescript": "~6.0.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Used in most samples.
SOME_VARIABLE="<value>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary verifies that relative imports written with a `.ts` extension are
* rewritten to `.js` in the published samples.
*/

import { greet } from "./utils/helpers.js";
import { CONSTANT } from "./utils/constants.js";

async function main(): Promise<void> {
console.log(greet("world"));
console.log("constant:", CONSTANT);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export const CONSTANT = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export function greet(name: string): string {
return `Hello, ${name}!`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ES2023",
"module": "nodenext",
"moduleResolution": "nodenext",
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"alwaysStrict": true,
"outDir": "dist",
"rootDir": "src"
},
"include": [
"src/**/*.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"skipFolder": false,
"disableDocsMs": true,
"productName": "Azure Template",
"productSlugs": [],
"apiRefLink": "https://learn.microsoft.com/javascript/api/",
"requiredResources": {
"Azure App Configuration": "https://learn.microsoft.com/azure/azure-app-configuration/"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Used in most samples.
SOME_VARIABLE="<value>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary verifies that relative imports written with a `.ts` extension are
* rewritten to `.js` in the published samples.
*/

import { greet } from "./utils/helpers.ts";
import { CONSTANT } from "./utils/constants.ts";

async function main(): Promise<void> {
console.log(greet("world"));
console.log("constant:", CONSTANT);
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @azsdk-util true
*/

export const CONSTANT = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @azsdk-util true
*/

export function greet(name: string): string {
return `Hello, ${name}!`;
}
3 changes: 2 additions & 1 deletion eng/tsconfigs/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"noEmit": true,
"skipLibCheck": true,
"noUnusedLocals": false,
"noUnusedParameters": false
"noUnusedParameters": false,
"allowImportingTsExtensions": true
},
"include": ["${configDir}/../samples-dev"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
loadScreenshotAssets,
handleComputerActionAndTakeScreenshot,
printFinalOutput,
} from "./computerUseUtil.js";
} from "./computerUseUtil.ts";

const projectEndpoint = process.env["FOUNDRY_PROJECT_ENDPOINT"] || "<project endpoint>";
const deploymentName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { AppConfigurationClient } from "@azure/app-configuration";
import type { EventGridEvent } from "@azure/eventgrid";
import { isSystemEvent, EventGridDeserializer } from "@azure/eventgrid";
import { appConfigTestEvent } from "./testData.js";
import { appConfigTestEvent } from "./testData.ts";

// Load the .env file if it exists
import * as dotenv from "dotenv";
Expand Down
4 changes: 2 additions & 2 deletions sdk/attestation/attestation/samples-dev/attestEnclaves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
*/
import { AttestationClient } from "@azure/attestation";
import { DefaultAzureCredential } from "@azure/identity";
import { writeBanner } from "./utils/helpers.js";
import { decodeString } from "./utils/base64url.js";
import { writeBanner } from "./utils/helpers.ts";
import { decodeString } from "./utils/base64url.ts";
// Load environment from a .env file if it exists.
import "dotenv/config";

Expand Down
Loading
Loading