diff --git a/sdk/communication/communication-chat/package.json b/sdk/communication/communication-chat/package.json index 10d7d3c567a4..499e80d82e69 100644 --- a/sdk/communication/communication-chat/package.json +++ b/sdk/communication/communication-chat/package.json @@ -11,16 +11,14 @@ "build:autorest": "autorest ./swagger/README.md --typescript --version=3.0.6267 --v3 --package-version=1.1.0-beta.1 && rushx format", "build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1", "build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1", - "build:samples": "dev-tool samples prep && cd dist-samples && tsc -p .", + "build:samples": "echo Obsolete.", "build:test": "tsc -p . && rollup -c rollup.test.config.js 2>&1", "build": "tsc -p . && rollup -c 2>&1 && api-extractor run --local", - "check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", + "check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", "clean": "rimraf dist dist-* temp types *.tgz *.log", - "execute:samples": "npm run build:samples && npm run execute:js-samples && npm run execute:ts-samples", - "execute:js-samples": "dev-tool samples run dist-samples/javascript", - "execute:ts-samples": "dev-tool samples run dist-samples/typescript/dist/dist-samples/typescript/src/", + "execute:samples": "dev-tool samples run samples-dev", "extract-api": "tsc -p . && api-extractor run --local", - "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", + "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"", "integration-test:browser": "karma start --single-run", "integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace -t 300000 dist-esm/test/public/*.spec.js dist-esm/test/public/node/*.spec.js", "integration-test": "npm run integration-test:node && npm run integration-test:browser", @@ -125,5 +123,15 @@ "typescript": "~4.2.0", "util": "^0.12.1", "typedoc": "0.15.2" + }, + "//sampleConfiguration": { + "productName": "Azure Communication Services - Chat", + "productSlugs": [ + "azure", + "azure-communication-services" + ], + "requiredResources": { + "Azure Communication Services account": "https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource" + } } } diff --git a/sdk/communication/communication-chat/sample.env b/sdk/communication/communication-chat/sample.env index 8ee9a11d8421..7a6eb9cadccc 100644 --- a/sdk/communication/communication-chat/sample.env +++ b/sdk/communication/communication-chat/sample.env @@ -1,7 +1,4 @@ -# Used in most samples. Retrieve these values from a Communication Services instance +# Used in most samples. Retrieve these values from a Communication Services resource # in the Azure Portal. -COMMUNICATION_CONNECTION_STRING="" - -# Our tests assume that TEST_MODE is "playback" by default. You can -# change it to "record" to generate new recordings, or "live" to bypass the recorder entirely. -# TEST_MODE=record \ No newline at end of file +COMMUNICATION_CONNECTION_STRING="endpoint=https://.communication.azure.net/;accessKey=" +COMMUNICATION_ENDPOINT="https://.communication.azure.com" diff --git a/sdk/communication/communication-chat/samples/typescript/src/messageOperations.ts b/sdk/communication/communication-chat/samples-dev/messageOperations.ts similarity index 89% rename from sdk/communication/communication-chat/samples/typescript/src/messageOperations.ts rename to sdk/communication/communication-chat/samples-dev/messageOperations.ts index e721e57f306f..e045a040252b 100644 --- a/sdk/communication/communication-chat/samples/typescript/src/messageOperations.ts +++ b/sdk/communication/communication-chat/samples-dev/messageOperations.ts @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * Demonstrates how to use the ChatThreadClient to do message operations + * @summary Perform message thread operations using the ChatThreadClient. */ import { ChatClient } from "@azure/communication-chat"; @@ -58,8 +58,7 @@ export async function main() { console.log("Deleted message."); } -main().catch((err) => { - console.log("error code: ", err.code); - console.log("error message: ", err.message); - console.log("error stack: ", err.stack); +main().catch((error) => { + console.error("Encountered an error in message operations: ", error); + process.exit(1); }); diff --git a/sdk/communication/communication-chat/samples/typescript/src/participantsOperations.ts b/sdk/communication/communication-chat/samples-dev/participantsOperations.ts similarity index 91% rename from sdk/communication/communication-chat/samples/typescript/src/participantsOperations.ts rename to sdk/communication/communication-chat/samples-dev/participantsOperations.ts index 71503dc75f71..4774d8ee3e3e 100644 --- a/sdk/communication/communication-chat/samples/typescript/src/participantsOperations.ts +++ b/sdk/communication/communication-chat/samples-dev/participantsOperations.ts @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * Demonstrates how to use the ChatThreadClient to do participant operations + * @summary Demonstrates how to use the ChatThreadClient to do participant operations. */ import { ChatClient } from "@azure/communication-chat"; @@ -73,8 +73,7 @@ export async function main() { console.log("Removed chat participant user."); } -main().catch((err) => { - console.log("error code: ", err.code); - console.log("error message: ", err.message); - console.log("error stack: ", err.stack); +main().catch((error) => { + console.error("Encountered an error in participants operations: ", error); + process.exit(1); }); diff --git a/sdk/communication/communication-chat/samples/typescript/src/threadOperations.ts b/sdk/communication/communication-chat/samples-dev/threadOperations.ts similarity index 90% rename from sdk/communication/communication-chat/samples/typescript/src/threadOperations.ts rename to sdk/communication/communication-chat/samples-dev/threadOperations.ts index 5f51c3190e2a..3234b70cf8f1 100644 --- a/sdk/communication/communication-chat/samples/typescript/src/threadOperations.ts +++ b/sdk/communication/communication-chat/samples-dev/threadOperations.ts @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * Demonstrates how to use the ChatClient to do thread operations + * @summary Perform thread operations using the ChatClient. */ import { ChatClient } from "@azure/communication-chat"; @@ -68,8 +68,7 @@ export async function main() { await chatClient.deleteChatThread(threadId); } -main().catch((err) => { - console.log("error code: ", err.code); - console.log("error message: ", err.message); - console.log("error stack: ", err.stack); +main().catch((error) => { + console.error("Encountered an error in thread operations: ", error); + process.exit(1); }); diff --git a/sdk/communication/communication-chat/samples/javascript/README.md b/sdk/communication/communication-chat/samples/javascript/README.md deleted file mode 100644 index 63cf202f4d93..000000000000 --- a/sdk/communication/communication-chat/samples/javascript/README.md +++ /dev/null @@ -1,62 +0,0 @@ ---- -page_type: sample -languages: - - javascript -products: - - azure -urlFragment: communication-chat-javascript ---- - -# Azure Communication Service Communication Chat client library sample for JavaScript - -These sample programs show how to use the TypeScript client libraries for Azure Communication Service Communication Chat to create chat threads and send messages. - -| **File Name** | **Description** | -| ---------------------------------------------------- | ----------------------------------------------------- | -| [threadOperations.js][thread-operations] | create, retrieve, update and delete a chat thread | -| [messageOperations.js][message-operations] | send, list, update and delete chat messages | -| [participantsOperations.js][participants-operations] | add, list, and remove participants from a chat thread | - -## Prerequisites - -The sample is compatible with Node.js >= 8.0.0. - -You need [an Azure subscription][freesub] and [an Azure Communication Service Instance][azcomsvc] to run these sample program. - -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 sample 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: - -```bash -node threadOperations.js -``` - -Alternatively, run a single sample with the correct environment variables set (step 3 is not required if you do this), for example (cross-platform): - -```bash -npx cross-env COMMUNICATION_CONNECTION_STRING="" node threadOperations.js -``` - -## Next Steps - -Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. - -[thread-operations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/javascript/threadOperations.js -[participants-operations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/javascript/participantsOperations.js -[message-operations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/javascript/messageOperations.js -[apiref]: https://docs.microsoft.com/javascript/api/@azure/communication-chat -[azcomsvc]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp -[freesub]: https://azure.microsoft.com/free/ -[package]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/README.md diff --git a/sdk/communication/communication-chat/samples/javascript/package.json b/sdk/communication/communication-chat/samples/javascript/package.json deleted file mode 100644 index 8e76f55106d4..000000000000 --- a/sdk/communication/communication-chat/samples/javascript/package.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "name": "azure-communication-chat-samples-js", - "private": true, - "version": "0.1.0", - "description": "Azure Communication Service Chat client library samples for JavaScript", - "engine": { - "node": ">=8.0.0" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/Azure/azure-sdk-for-js.git" - }, - "keywords": [ - "Azure", - "Communication", - "Chat", - "Node.js", - "JavaScript" - ], - "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/master/sdk/communication/communication-chat", - "sideEffects": false, - "dependencies": { - "@azure/communication-chat": "latest", - "@azure/communication-identity": "latest", - "@azure/identity": "^1.1.0", - "dotenv": "^8.2.0" - } -} diff --git a/sdk/communication/communication-chat/samples/tsconfig.json b/sdk/communication/communication-chat/samples/tsconfig.json deleted file mode 100644 index 8c89eac7173a..000000000000 --- a/sdk/communication/communication-chat/samples/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "module": "commonjs", - "outDir": "typescript/dist", - "lib": ["DOM", "ES6"] - }, - "include": ["typescript/src/**.ts"], - "exclude": ["typescript/*.json", "**/node_modules/", "../node_modules", "../typings"] -} diff --git a/sdk/communication/communication-chat/samples/typescript/README.md b/sdk/communication/communication-chat/samples/typescript/README.md deleted file mode 100644 index 6db2f007aa5c..000000000000 --- a/sdk/communication/communication-chat/samples/typescript/README.md +++ /dev/null @@ -1,68 +0,0 @@ ---- -page_type: sample -languages: - - typescript -products: - - azure -urlFragment: communication-chat-typescript ---- - -# Azure Communication Service Communication Chat client library sample for TypeScript - -These sample programs show how to use the TypeScript client libraries for Azure Communication Service Communication Chat to create chat threads and send messages. - -| **File Name** | **Description** | -| ---------------------------------------------------- | ----------------------------------------------------- | -| [threadOperations.ts][thread-operations] | create, retrieve, update and delete a chat thread | -| [messageOperations.ts][message-operations] | send, list, update and delete chat messages | -| [participantsOperations.ts][participants-operations] | add, list, and remove participants from a chat thread | - -## Prerequisites - -The sample is compatible with Node.js >= 8.0.0. - -You need [an Azure subscription][freesub] and [an Azure Communication Service Instance][azcomsvc] to run these sample program. - -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 sample using the published version of the package: - -1. Install the dependencies using `npm`: - -```bash -npm install -``` - -2. Compile the sample - -```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/threadOperations.js -``` - -Alternatively, run a single sample with the correct environment variables set (step 3 is not required if you do this), for example (cross-platform): - -```bash -npx cross-env COMMUNICATION_CONNECTION_STRING="" node dist/threadOperations.js -``` - -## Next Steps - -Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. - -[thread-operations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/typescript/src/threadOperations.ts -[participants-operations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/typescript/src/participantsOperations.ts -[message-operations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/typescript/src/messageOperations.ts -[apiref]: https://docs.microsoft.com/javascript/api/@azure/communication-chat -[azcomsvc]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp -[freesub]: https://azure.microsoft.com/free/ -[package]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/README.md diff --git a/sdk/communication/communication-chat/samples/typescript/package.json b/sdk/communication/communication-chat/samples/typescript/package.json deleted file mode 100644 index e3ce9870b77c..000000000000 --- a/sdk/communication/communication-chat/samples/typescript/package.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "azure-communication-chat-samples-ts", - "version": "0.1.0", - "description": "Azure Communication Chat client library samples for TypeScript", - "engine": { - "node": ">=8.0.0" - }, - "scripts": { - "build": "tsc", - "prebuild": "rimraf dist/" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/Azure/azure-sdk-for-js.git" - }, - "keywords": [ - "Azure", - "Communication", - "Node.js", - "JavaScript" - ], - "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/master/sdk/communication/communication-chat", - "sideEffects": false, - "dependencies": { - "@azure/communication-chat": "latest", - "@azure/communication-identity": "latest", - "dotenv": "^8.2.0" - }, - "devDependencies": { - "@types/node": "^8.0.0", - "rimraf": "^3.0.0", - "typescript": "~3.6.4" - } -} diff --git a/sdk/communication/communication-chat/samples/v1/javascript/README.md b/sdk/communication/communication-chat/samples/v1/javascript/README.md new file mode 100644 index 000000000000..172861330365 --- /dev/null +++ b/sdk/communication/communication-chat/samples/v1/javascript/README.md @@ -0,0 +1,67 @@ +--- +page_type: sample +languages: + - javascript +products: + - azure + - azure-communication-services +urlFragment: communication-chat-javascript +--- + +# Azure Communication Services - Chat client library samples for JavaScript + +These sample programs show how to use the JavaScript client libraries for Azure Communication Services - Chat in some common scenarios. + +| **File Name** | **Description** | +| --------------------------------------------------- | -------------------------------------------------------------------------- | +| [messageOperations.js][messageoperations] | Perform message thread operations using the ChatThreadClient. | +| [participantsOperations.js][participantsoperations] | Demonstrates how to use the ChatThreadClient to do participant operations. | +| [threadOperations.js][threadoperations] | Perform thread operations using the ChatClient. | + +## Prerequisites + +The sample programs are compatible with Node.js >=12.0.0. + +You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs: + +- [Azure Communication Services account][createinstance_azurecommunicationservicesaccount] + +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 messageOperations.js +``` + +Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): + +```bash +npx cross-env COMMUNICATION_CONNECTION_STRING="" node messageOperations.js +``` + +## Next Steps + +Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. + +[messageoperations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/v1/javascript/messageOperations.js +[participantsoperations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/v1/javascript/participantsOperations.js +[threadoperations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/v1/javascript/threadOperations.js +[apiref]: https://docs.microsoft.com/javascript/api/@azure/communication-chat +[freesub]: https://azure.microsoft.com/free/ +[createinstance_azurecommunicationservicesaccount]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource +[package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/communication/communication-chat/README.md diff --git a/sdk/communication/communication-chat/samples/javascript/messageOperations.js b/sdk/communication/communication-chat/samples/v1/javascript/messageOperations.js similarity index 88% rename from sdk/communication/communication-chat/samples/javascript/messageOperations.js rename to sdk/communication/communication-chat/samples/v1/javascript/messageOperations.js index 6ecab6a38fc5..eae90cb11f6a 100644 --- a/sdk/communication/communication-chat/samples/javascript/messageOperations.js +++ b/sdk/communication/communication-chat/samples/v1/javascript/messageOperations.js @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * Demonstrates how to use the ChatThreadClient to do message operations + * @summary Perform message thread operations using the ChatThreadClient. */ const { ChatClient } = require("@azure/communication-chat"); @@ -13,7 +13,8 @@ const { const { CommunicationIdentityClient } = require("@azure/communication-identity"); // Load the .env file if it exists -require("dotenv").config(); +const dotenv = require("dotenv"); +dotenv.config(); async function main() { const connectionString = @@ -57,8 +58,7 @@ async function main() { console.log("Deleted message."); } -main().catch((err) => { - console.log("error code: ", err.code); - console.log("error message: ", err.message); - console.log("error stack: ", err.stack); +main().catch((error) => { + console.error("Encountered an error in message operations: ", error); + process.exit(1); }); diff --git a/sdk/communication/communication-chat/samples/v1/javascript/package.json b/sdk/communication/communication-chat/samples/v1/javascript/package.json new file mode 100644 index 000000000000..71582936a58a --- /dev/null +++ b/sdk/communication/communication-chat/samples/v1/javascript/package.json @@ -0,0 +1,33 @@ +{ + "name": "azure-communication-chat-samples-js", + "private": true, + "version": "1.0.0", + "description": "Azure Communication Services - Chat client library samples for JavaScript", + "engine": { + "node": ">=12.0.0" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Azure/azure-sdk-for-js.git", + "directory": "sdk/communication/communication-chat" + }, + "keywords": [ + "azure", + "cloud", + "Azure", + "communication", + "chat" + ], + "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/master/sdk/communication/communication-chat", + "dependencies": { + "@azure/communication-chat": "next", + "dotenv": "latest", + "@azure/communication-common": "^1.0.0", + "@azure/communication-identity": "^1.0.0" + } +} diff --git a/sdk/communication/communication-chat/samples/javascript/participantsOperations.js b/sdk/communication/communication-chat/samples/v1/javascript/participantsOperations.js similarity index 89% rename from sdk/communication/communication-chat/samples/javascript/participantsOperations.js rename to sdk/communication/communication-chat/samples/v1/javascript/participantsOperations.js index 41a70568619a..1a2741676f52 100644 --- a/sdk/communication/communication-chat/samples/javascript/participantsOperations.js +++ b/sdk/communication/communication-chat/samples/v1/javascript/participantsOperations.js @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * Demonstrates how to use the ChatThreadClient to do participant operations + * @summary Demonstrates how to use the ChatThreadClient to do participant operations. */ const { ChatClient } = require("@azure/communication-chat"); @@ -14,7 +14,8 @@ const { const { CommunicationIdentityClient } = require("@azure/communication-identity"); // Load the .env file if it exists -require("dotenv").config(); +const dotenv = require("dotenv"); +dotenv.config(); async function main() { const connectionString = @@ -72,8 +73,7 @@ async function main() { console.log("Removed chat participant user."); } -main().catch((err) => { - console.log("error code: ", err.code); - console.log("error message: ", err.message); - console.log("error stack: ", err.stack); +main().catch((error) => { + console.error("Encountered an error in participants operations: ", error); + process.exit(1); }); diff --git a/sdk/communication/communication-chat/samples/typescript/sample.env b/sdk/communication/communication-chat/samples/v1/javascript/sample.env similarity index 92% rename from sdk/communication/communication-chat/samples/typescript/sample.env rename to sdk/communication/communication-chat/samples/v1/javascript/sample.env index 441f489b2d32..7a6eb9cadccc 100644 --- a/sdk/communication/communication-chat/samples/typescript/sample.env +++ b/sdk/communication/communication-chat/samples/v1/javascript/sample.env @@ -1,4 +1,4 @@ -# Used in most samples. Retrieve these values from a Communication Service instance +# Used in most samples. Retrieve these values from a Communication Services resource # in the Azure Portal. COMMUNICATION_CONNECTION_STRING="endpoint=https://.communication.azure.net/;accessKey=" COMMUNICATION_ENDPOINT="https://.communication.azure.com" diff --git a/sdk/communication/communication-chat/samples/javascript/threadOperations.js b/sdk/communication/communication-chat/samples/v1/javascript/threadOperations.js similarity index 88% rename from sdk/communication/communication-chat/samples/javascript/threadOperations.js rename to sdk/communication/communication-chat/samples/v1/javascript/threadOperations.js index 34caf917a1fa..8f13f41af7d2 100644 --- a/sdk/communication/communication-chat/samples/javascript/threadOperations.js +++ b/sdk/communication/communication-chat/samples/v1/javascript/threadOperations.js @@ -2,7 +2,7 @@ // Licensed under the MIT License. /** - * Demonstrates how to use the ChatClient to do thread operations + * @summary Perform thread operations using the ChatClient. */ const { ChatClient } = require("@azure/communication-chat"); @@ -13,7 +13,8 @@ const { const { CommunicationIdentityClient } = require("@azure/communication-identity"); // Load the .env file if it exists -require("dotenv").config(); +const dotenv = require("dotenv"); +dotenv.config(); async function main() { const connectionString = @@ -67,8 +68,7 @@ async function main() { await chatClient.deleteChatThread(threadId); } -main().catch((err) => { - console.log("error code: ", err.code); - console.log("error message: ", err.message); - console.log("error stack: ", err.stack); +main().catch((error) => { + console.error("Encountered an error in thread operations: ", error); + process.exit(1); }); diff --git a/sdk/communication/communication-chat/samples/v1/typescript/README.md b/sdk/communication/communication-chat/samples/v1/typescript/README.md new file mode 100644 index 000000000000..9494d2d90ff6 --- /dev/null +++ b/sdk/communication/communication-chat/samples/v1/typescript/README.md @@ -0,0 +1,80 @@ +--- +page_type: sample +languages: + - typescript +products: + - azure + - azure-communication-services +urlFragment: communication-chat-typescript +--- + +# Azure Communication Services - Chat client library samples for TypeScript + +These sample programs show how to use the TypeScript client libraries for Azure Communication Services - Chat in some common scenarios. + +| **File Name** | **Description** | +| --------------------------------------------------- | -------------------------------------------------------------------------- | +| [messageOperations.ts][messageoperations] | Perform message thread operations using the ChatThreadClient. | +| [participantsOperations.ts][participantsoperations] | Demonstrates how to use the ChatThreadClient to do participant operations. | +| [threadOperations.ts][threadoperations] | Perform thread operations using the ChatClient. | + +## Prerequisites + +The sample programs are compatible with Node.js >=12.0.0. + +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 Communication Services account][createinstance_azurecommunicationservicesaccount] + +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/messageOperations.js +``` + +Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): + +```bash +npx cross-env COMMUNICATION_CONNECTION_STRING="" node dist/messageOperations.js +``` + +## Next Steps + +Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. + +[messageoperations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/v1/typescript/src/messageOperations.ts +[participantsoperations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/v1/typescript/src/participantsOperations.ts +[threadoperations]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/communication/communication-chat/samples/v1/typescript/src/threadOperations.ts +[apiref]: https://docs.microsoft.com/javascript/api/@azure/communication-chat +[freesub]: https://azure.microsoft.com/free/ +[createinstance_azurecommunicationservicesaccount]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource +[package]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/communication/communication-chat/README.md +[typescript]: https://www.typescriptlang.org/docs/home.html diff --git a/sdk/communication/communication-chat/samples/v1/typescript/package.json b/sdk/communication/communication-chat/samples/v1/typescript/package.json new file mode 100644 index 000000000000..2aa74539c518 --- /dev/null +++ b/sdk/communication/communication-chat/samples/v1/typescript/package.json @@ -0,0 +1,41 @@ +{ + "name": "azure-communication-chat-samples-ts", + "private": true, + "version": "1.0.0", + "description": "Azure Communication Services - Chat client library samples for TypeScript", + "engine": { + "node": ">=12.0.0" + }, + "scripts": { + "build": "tsc", + "prebuild": "rimraf dist/" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Azure/azure-sdk-for-js.git", + "directory": "sdk/communication/communication-chat" + }, + "keywords": [ + "azure", + "cloud", + "Azure", + "communication", + "chat" + ], + "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/master/sdk/communication/communication-chat", + "dependencies": { + "@azure/communication-chat": "next", + "dotenv": "latest", + "@azure/communication-common": "^1.0.0", + "@azure/communication-identity": "^1.0.0" + }, + "devDependencies": { + "typescript": "~4.2.0", + "rimraf": "latest" + } +} diff --git a/sdk/communication/communication-chat/samples/javascript/sample.env b/sdk/communication/communication-chat/samples/v1/typescript/sample.env similarity index 92% rename from sdk/communication/communication-chat/samples/javascript/sample.env rename to sdk/communication/communication-chat/samples/v1/typescript/sample.env index 441f489b2d32..7a6eb9cadccc 100644 --- a/sdk/communication/communication-chat/samples/javascript/sample.env +++ b/sdk/communication/communication-chat/samples/v1/typescript/sample.env @@ -1,4 +1,4 @@ -# Used in most samples. Retrieve these values from a Communication Service instance +# Used in most samples. Retrieve these values from a Communication Services resource # in the Azure Portal. COMMUNICATION_CONNECTION_STRING="endpoint=https://.communication.azure.net/;accessKey=" COMMUNICATION_ENDPOINT="https://.communication.azure.com" diff --git a/sdk/communication/communication-chat/samples/v1/typescript/src/messageOperations.ts b/sdk/communication/communication-chat/samples/v1/typescript/src/messageOperations.ts new file mode 100644 index 000000000000..e045a040252b --- /dev/null +++ b/sdk/communication/communication-chat/samples/v1/typescript/src/messageOperations.ts @@ -0,0 +1,64 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Perform message thread operations using the ChatThreadClient. + */ + +import { ChatClient } from "@azure/communication-chat"; +import { + AzureCommunicationTokenCredential, + parseConnectionString +} from "@azure/communication-common"; +import { CommunicationIdentityClient } from "@azure/communication-identity"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + const connectionString = + process.env["COMMUNICATION_CONNECTION_STRING"] || + "endpoint=https://.communication.azure.com/;"; + const endpoint = parseConnectionString(connectionString).endpoint; + + const identityClient = new CommunicationIdentityClient(connectionString); + const user = await identityClient.createUser(); + const userToken = await identityClient.getToken(user, ["chat"]); + + // create ChatClient + const chatClient = new ChatClient( + endpoint, + new AzureCommunicationTokenCredential(userToken.token) + ); + const createChatThreadResult = await chatClient.createChatThread({ topic: "Hello, World!" }); + const threadId = createChatThreadResult.chatThread ? createChatThreadResult.chatThread.id : ""; + const chatThreadClient = chatClient.getChatThreadClient(threadId); + + // send a message + const sendMessageResult = await chatThreadClient.sendMessage({ content: "Hello world." }); + console.log(`Sent message with id ${sendMessageResult.id}`); + + // get a message by id + const message = await chatThreadClient.getMessage(sendMessageResult.id); + console.log(`Retrieved message.`, message); + + // list all messages with newest first + let i = 0; + for await (const message of chatThreadClient.listMessages()) { + console.log(`Message ${++i}:`, message); + } + + // update a message + await chatThreadClient.updateMessage(message.id, { content: "New content" }); + console.log(`Updated message.`); + + // delete a message + await chatThreadClient.deleteMessage(sendMessageResult.id); + console.log("Deleted message."); +} + +main().catch((error) => { + console.error("Encountered an error in message operations: ", error); + process.exit(1); +}); diff --git a/sdk/communication/communication-chat/samples/v1/typescript/src/participantsOperations.ts b/sdk/communication/communication-chat/samples/v1/typescript/src/participantsOperations.ts new file mode 100644 index 000000000000..4774d8ee3e3e --- /dev/null +++ b/sdk/communication/communication-chat/samples/v1/typescript/src/participantsOperations.ts @@ -0,0 +1,79 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Demonstrates how to use the ChatThreadClient to do participant operations. + */ + +import { ChatClient } from "@azure/communication-chat"; +import { + AzureCommunicationTokenCredential, + getIdentifierKind, + parseConnectionString +} from "@azure/communication-common"; +import { CommunicationIdentityClient } from "@azure/communication-identity"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + const connectionString = + process.env["COMMUNICATION_CONNECTION_STRING"] || + "endpoint=https://.communication.azure.com/;"; + const endpoint = parseConnectionString(connectionString).endpoint; + + const identityClient = new CommunicationIdentityClient(connectionString); + const user = await identityClient.createUser(); + const userToken = await identityClient.getToken(user, ["chat"]); + const userSue = await identityClient.createUserAndToken(["chat"]); + + // create ChatClient + const chatClient = new ChatClient( + endpoint, + new AzureCommunicationTokenCredential(userToken.token) + ); + const createChatThreadResult = await chatClient.createChatThread({ topic: "Hello, World!" }); + const threadId = createChatThreadResult.chatThread ? createChatThreadResult.chatThread.id : ""; + const chatThreadClient = chatClient.getChatThreadClient(threadId); + + // add a new participant + const addParticipantsRequest = { + participants: [ + { + id: userSue.user, + displayName: "Sue" + } + ] + }; + await chatThreadClient.addParticipants(addParticipantsRequest); + console.log(`Added chat participant user.`); + + // list chat participants + for await (const participant of chatThreadClient.listParticipants()) { + const id = getIdentifierKind(participant.id); + switch (id.kind) { + case "communicationUser": + console.log(`User with id ${id.communicationUserId}`); + break; + case "microsoftTeamsUser": + console.log(`Microsoft Teams user with id ${id.microsoftTeamsUserId}`); + break; + case "phoneNumber": + console.log(`Phone ${id.phoneNumber}`); + break; + case "unknown": + console.log(`Unknown user with id ${id.id}`); + break; + } + } + + // remove a participant + await chatThreadClient.removeParticipant(userSue.user); + console.log("Removed chat participant user."); +} + +main().catch((error) => { + console.error("Encountered an error in participants operations: ", error); + process.exit(1); +}); diff --git a/sdk/communication/communication-chat/samples/v1/typescript/src/threadOperations.ts b/sdk/communication/communication-chat/samples/v1/typescript/src/threadOperations.ts new file mode 100644 index 000000000000..3234b70cf8f1 --- /dev/null +++ b/sdk/communication/communication-chat/samples/v1/typescript/src/threadOperations.ts @@ -0,0 +1,74 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Perform thread operations using the ChatClient. + */ + +import { ChatClient } from "@azure/communication-chat"; +import { + AzureCommunicationTokenCredential, + parseConnectionString +} from "@azure/communication-common"; +import { CommunicationIdentityClient } from "@azure/communication-identity"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +export async function main() { + const connectionString = + process.env["COMMUNICATION_CONNECTION_STRING"] || + "endpoint=https://.communication.azure.com/;"; + const endpoint = parseConnectionString(connectionString).endpoint; + + const identityClient = new CommunicationIdentityClient(connectionString); + const user = await identityClient.createUser(); + const userToken = await identityClient.getToken(user, ["chat"]); + + // create ChatClient + const chatClient = new ChatClient( + endpoint, + new AzureCommunicationTokenCredential(userToken.token) + ); + + // create chat thread + console.log("Creating Thread..."); + const createChatThreadRequest = { + topic: "Hello, World!" + }; + const createChatThreadOptions = { + participants: [ + { + id: user, + displayName: "Jack" + } + ] + }; + const createChatThreadResult = await chatClient.createChatThread( + createChatThreadRequest, + createChatThreadOptions + ); + const threadId = createChatThreadResult.chatThread ? createChatThreadResult.chatThread.id : ""; + + console.log(`Created Thread with id: ${threadId}.`); + + // get ChatThreadClient for thread + const chatThreadClient = chatClient.getChatThreadClient(threadId); + + // get proprerties of created chat thread + const chatThread = await chatThreadClient.getProperties(); + console.log(`Retrieved created thread. Topic: ${chatThread.topic}`); + + // update the thread's topic + await chatThreadClient.updateTopic("New Topic"); + console.log(`Updated thread's topic.`); + + // delete the chat thread + await chatClient.deleteChatThread(threadId); +} + +main().catch((error) => { + console.error("Encountered an error in thread operations: ", error); + process.exit(1); +}); diff --git a/sdk/communication/communication-chat/samples/typescript/tsconfig.json b/sdk/communication/communication-chat/samples/v1/typescript/tsconfig.json similarity index 65% rename from sdk/communication/communication-chat/samples/typescript/tsconfig.json rename to sdk/communication/communication-chat/samples/v1/typescript/tsconfig.json index 9e7ca6c9974b..416c2dd82e00 100644 --- a/sdk/communication/communication-chat/samples/typescript/tsconfig.json +++ b/sdk/communication/communication-chat/samples/v1/typescript/tsconfig.json @@ -1,13 +1,17 @@ { "compilerOptions": { + "target": "ES2018", "module": "commonjs", "moduleResolution": "node", + "resolveJsonModule": true, + "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": true, "alwaysStrict": true, "outDir": "dist", "rootDir": "src" }, - "include": ["src/**.ts"], - "exclude": ["node_modules"] + "include": [ + "src/**.ts" + ] } diff --git a/sdk/communication/communication-chat/test.env b/sdk/communication/communication-chat/test.env new file mode 100644 index 000000000000..745b49828b9f --- /dev/null +++ b/sdk/communication/communication-chat/test.env @@ -0,0 +1,5 @@ +COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING="endpoint=;accessKey=" + +# Our tests assume that TEST_MODE is "playback" by default. You can +# change it to "record" to generate new recordings, or "live" to bypass the recorder entirely. +# TEST_MODE=record diff --git a/sdk/communication/communication-chat/tsconfig.json b/sdk/communication/communication-chat/tsconfig.json index 733a8ef32522..49dc324fcfd0 100644 --- a/sdk/communication/communication-chat/tsconfig.json +++ b/sdk/communication/communication-chat/tsconfig.json @@ -2,7 +2,10 @@ "extends": "../../../tsconfig.package", "compilerOptions": { "outDir": "./dist-esm", - "declarationDir": "./types" + "declarationDir": "./types", + "paths": { + "@azure/communication-chat": ["./src/index"] + } }, - "exclude": ["node_modules", "types", "temp", "browser", "dist", "dist-esm", "./samples/**/*.ts"] + "include": ["src/**/*.ts", "test/**/*.ts", "samples-dev/**/*.ts"] }