Skip to content

Commit 20057ff

Browse files
authored
[servicebus] Migrate servicebus projects to use snippets extraction (#33267)
### Packages impacted by this PR - @Azure/service-bus ### Issues associated with this PR - #32416 ### Describe the problem that is addressed by this PR Updates all projects under `servicebus` 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)
1 parent b980243 commit 20057ff

32 files changed

+402
-226
lines changed

sdk/servicebus/service-bus/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- dev-tool snippets ignore -->
2+
13
# Release History
24

35
## 7.10.0 (Unreleased)

sdk/servicebus/service-bus/README.md

+75-18
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ authenticate to Service Bus using a connection string or using an Azure Active D
152152
This method takes the connection string to your Service Bus instance. You can get
153153
the connection string from the Azure portal.
154154

155-
```javascript
156-
const { ServiceBusClient } = require("@azure/service-bus");
155+
```ts snippet:ReadmeSampleCreateClient_ConnectionString
156+
import { ServiceBusClient } from "@azure/service-bus";
157157

158158
const serviceBusClient = new ServiceBusClient("<connectionString>");
159159
```
@@ -167,9 +167,9 @@ Authentication with Azure Active Directory uses the [Azure Identity library][azu
167167
The example below uses the [DefaultAzureCredential][defaultazurecredential], one of many
168168
available credential providers from the `@azure/identity` library.
169169

170-
```javascript
171-
const { ServiceBusClient } = require("@azure/service-bus");
172-
const { DefaultAzureCredential } = require("@azure/identity");
170+
```ts snippet:ReadmeSampleCreateClient_AAD
171+
import { DefaultAzureCredential } from "@azure/identity";
172+
import { ServiceBusClient } from "@azure/service-bus";
173173

174174
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
175175
const credential = new DefaultAzureCredential();
@@ -223,7 +223,14 @@ The following sections provide code snippets that cover some of the common tasks
223223
Once you have created an instance of a `ServiceBusClient` class, you can get a `ServiceBusSender`
224224
using the [createSender][sbclient_createsender] method which you can use to [send][sender_sendmessages] messages.
225225
226-
```javascript
226+
```ts snippet:ReadmeSampleSendMessage
227+
import { DefaultAzureCredential } from "@azure/identity";
228+
import { ServiceBusClient } from "@azure/service-bus";
229+
230+
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
231+
const credential = new DefaultAzureCredential();
232+
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);
233+
227234
const sender = serviceBusClient.createSender("my-queue");
228235
229236
const messages = [
@@ -270,7 +277,14 @@ await sender.sendMessages(batch);
270277
Once you have created an instance of a `ServiceBusClient` class, you can get a `ServiceBusReceiver`
271278
using the [createReceiver][sbclient_createreceiver] method.
272279

273-
```javascript
280+
```ts snippet:ReadmeSampleReceiveMessage
281+
import { DefaultAzureCredential } from "@azure/identity";
282+
import { ServiceBusClient } from "@azure/service-bus";
283+
284+
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
285+
const credential = new DefaultAzureCredential();
286+
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);
287+
274288
const receiver = serviceBusClient.createReceiver("my-queue");
275289
```
276290

@@ -289,7 +303,16 @@ You can use this receiver in one of 3 ways to receive messages:
289303
Use the [receiveMessages][receiver_receivemessages] function which returns a promise that
290304
resolves to an array of messages.
291305

292-
```javascript
306+
```ts snippet:ReadmeSampleReceiveMessage_ReceiveMessages
307+
import { DefaultAzureCredential } from "@azure/identity";
308+
import { ServiceBusClient } from "@azure/service-bus";
309+
310+
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
311+
const credential = new DefaultAzureCredential();
312+
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);
313+
314+
const receiver = serviceBusClient.createReceiver("my-queue");
315+
293316
const myMessages = await receiver.receiveMessages(10);
294317
```
295318

@@ -300,7 +323,16 @@ it running as long as you need.
300323

301324
When you are done, call `receiver.close()` to stop receiving any more messages.
302325

303-
```javascript
326+
```ts snippet:ReadmeSampleReceiveMessage_Subscribe
327+
import { DefaultAzureCredential } from "@azure/identity";
328+
import { ServiceBusClient } from "@azure/service-bus";
329+
330+
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
331+
const credential = new DefaultAzureCredential();
332+
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);
333+
334+
const receiver = serviceBusClient.createReceiver("my-queue");
335+
304336
const myMessageHandler = async (message) => {
305337
// your code here
306338
console.log(`message.body: ${message.body}`);
@@ -321,7 +353,16 @@ receiver.subscribe({
321353

322354
Use the [getMessageIterator][receiver_getmessageiterator] to get an async iterator over messages
323355

324-
```javascript
356+
```ts snippet:ReadmeSampleReceiveMessage_AsyncIterator
357+
import { DefaultAzureCredential } from "@azure/identity";
358+
import { ServiceBusClient } from "@azure/service-bus";
359+
360+
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
361+
const credential = new DefaultAzureCredential();
362+
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);
363+
364+
const receiver = serviceBusClient.createReceiver("my-queue");
365+
325366
for await (const message of receiver.getMessageIterator()) {
326367
// your code here
327368
}
@@ -341,7 +382,14 @@ their maximum delivery count.
341382

342383
Creating a receiver for a dead letter sub-queue is similar to creating a receiver for a subscription or queue:
343384

344-
```javascript
385+
```ts snippet:ReadmeSampleDeadLetterQueue
386+
import { DefaultAzureCredential } from "@azure/identity";
387+
import { ServiceBusClient } from "@azure/service-bus";
388+
389+
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
390+
const credential = new DefaultAzureCredential();
391+
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);
392+
345393
// To receive from a queue's dead letter sub-queue
346394
const deadLetterReceiverForQueue = serviceBusClient.createReceiver("queue", {
347395
subQueueType: "deadLetter",
@@ -377,7 +425,14 @@ In order to send messages to a session, use the `ServiceBusClient` to create a s
377425
When sending the message, set the `sessionId` property in the message to ensure
378426
your message lands in the right session.
379427

380-
```javascript
428+
```ts snippet:ReadmeSampleSendMessage_Session
429+
import { DefaultAzureCredential } from "@azure/identity";
430+
import { ServiceBusClient } from "@azure/service-bus";
431+
432+
const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
433+
const credential = new DefaultAzureCredential();
434+
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);
435+
381436
const sender = serviceBusClient.createSender("my-session-queue");
382437
await sender.sendMessages({
383438
body: "my-message-body",
@@ -404,14 +459,14 @@ There are two ways of choosing which session to open:
404459

405460
1. Specify a `sessionId`, which locks a named session.
406461

407-
```javascript
462+
```ts snippet:ignore
408463
const receiver = await serviceBusClient.acceptSession("my-session-queue", "my-session");
409464
```
410465

411466
2. Do not specify a session id. In this case Service Bus will find the next available session
412467
that is not already locked.
413468

414-
```javascript
469+
```ts snippet:ignore
415470
const receiver = await serviceBusClient.acceptNextSession("my-session-queue");
416471
```
417472

@@ -435,7 +490,11 @@ You can read more about how sessions work [here][docsms_messagesessions].
435490

436491
Note: Service Bus doesn't support setting CORS rules for namespaces yet, hence `ServiceBusAdministrationClient` won't work in the browser without disabling web-security. For more info, refer [here](https://github.com/Azure/azure-sdk-for-js/issues/4983).
437492

438-
```js
493+
```ts snippet:ReadmeSampleAdministrationClient
494+
import { ServiceBusAdministrationClient } from "@azure/service-bus";
495+
496+
const queueName = "my-session-queue";
497+
439498
// Get the connection string from the portal
440499
// OR
441500
// use the token credential overload, provide the host name of your Service Bus instance and the AAD credentials from the @azure/identity library
@@ -447,7 +506,7 @@ console.log("Created queue with name - ", createQueueResponse.name);
447506

448507
const queueRuntimeProperties =
449508
await serviceBusAdministrationClient.getQueueRuntimeProperties(queueName);
450-
console.log("Number of messages in the queue = ", queueRuntimeProperties.totalMessageCount);
509+
console.log(`Number of messages in the queue = ${queueRuntimeProperties.totalMessageCount}`);
451510

452511
await serviceBusAdministrationClient.deleteQueue(queueName);
453512
```
@@ -518,8 +577,6 @@ directory for detailed examples on how to use this library to send and receive m
518577

519578
If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.
520579

521-
522-
523580
[apiref]: https://learn.microsoft.com/javascript/api/@azure/service-bus/
524581
[azure_identity]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/README.md
525582
[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential

sdk/servicebus/service-bus/migrationguide.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- dev-tool snippets ignore -->
2+
13
# Guide to migrate from @azure/service-bus v1 to v7
24

35
This guide is intended to assist in the migration from version 1 of the Service Bus client library `@azure/service-bus` to version 7 of the same library. It will focus on side-by-side comparisons for similar operations between the two packages.
@@ -261,8 +263,6 @@ Additionally, since a message cannot be settled if the receiver that was used to
261263
- Optional [prefetch](https://learn.microsoft.com/azure/service-bus-messaging/service-bus-prefetch) support to speed up the message flow by having a message readily available for local retrieval when and before the application asks for one.
262264
- An optional method on `ServiceBusSender` to allow pre-initializing the sender link to remove the upfront cost from the first send operation.
263265

264-
265-
266266
## Additional samples
267267

268268
More examples can be found at [Samples for @azure/service-bus](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/servicebus/service-bus/samples/)

sdk/servicebus/service-bus/package.json

+17-16
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@
3838
"check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
3939
"clean": "dev-tool run vendored rimraf --glob dist dist-* types *.tgz *.log coverage coverage-browser .nyc_output",
4040
"execute:samples": "dev-tool samples run samples-dev",
41-
"extract-api": "tsc -p . && dev-tool run extract-api",
41+
"extract-api": "dev-tool run build-package && dev-tool run extract-api",
4242
"format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"samples/**/*.{ts,js}\" \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
4343
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
4444
"integration-test:browser": "dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser --no-test-proxy",
4545
"integration-test:node": "dev-tool run test:vitest --no-test-proxy --esm",
4646
"lint": "eslint package.json api-extractor.json README.md src test",
4747
"lint:fix": "eslint package.json api-extractor.json README.md src test --fix --fix-type [problem,suggestion]",
4848
"pack": "npm pack 2>&1",
49-
"perf-test:node": "tsc -p . --module \"commonjs\" && node dist-esm/test/perf/track-2/index.spec.js",
49+
"perf-test:node": "dev-tool run build-package --module \"commonjs\" && node dist-esm/test/perf/track-2/index.spec.js",
5050
"test": "npm run test:node && npm run test:browser",
5151
"test:browser": "npm run clean && npm run integration-test:browser",
5252
"test:node": "npm run clean && npm run integration-test:node",
5353
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
5454
"unit-test:browser": "echo skipped",
5555
"unit-test:node": "dev-tool run test:vitest --no-test-proxy -- -c vitest.unit.config.ts",
56-
"update-snippets": "echo skipped"
56+
"update-snippets": "dev-tool run update-snippets"
5757
},
5858
"sideEffects": false,
5959
"//metadata": {
@@ -88,21 +88,21 @@
8888
"dependencies": {
8989
"@azure/abort-controller": "^2.1.2",
9090
"@azure/core-amqp": "^4.3.2",
91-
"@azure/core-auth": "^1.8.0",
91+
"@azure/core-auth": "^1.9.0",
9292
"@azure/core-client": "^1.9.2",
9393
"@azure/core-paging": "^1.6.2",
94-
"@azure/core-rest-pipeline": "^1.17.0",
94+
"@azure/core-rest-pipeline": "^1.19.0",
9595
"@azure/core-tracing": "^1.2.0",
96-
"@azure/core-util": "^1.10.0",
96+
"@azure/core-util": "^1.11.0",
9797
"@azure/core-xml": "^1.4.3",
9898
"@azure/logger": "^1.1.4",
99-
"buffer": "^6.0.0",
100-
"is-buffer": "^2.0.3",
99+
"buffer": "^6.0.3",
100+
"is-buffer": "^2.0.5",
101101
"jssha": "^3.3.1",
102102
"long": "^5.3.1",
103103
"process": "^0.11.10",
104-
"rhea-promise": "^3.0.0",
105-
"tslib": "^2.7.0"
104+
"rhea-promise": "^3.0.3",
105+
"tslib": "^2.8.1"
106106
},
107107
"devDependencies": {
108108
"@azure-tools/test-credential": "^2.1.0",
@@ -111,15 +111,15 @@
111111
"@azure-tools/vite-plugin-browser-test-map": "^1.0.0",
112112
"@azure/dev-tool": "^1.0.0",
113113
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
114-
"@azure/identity": "^4.4.1",
114+
"@azure/identity": "^4.7.0",
115115
"@rollup/plugin-inject": "^5.0.5",
116116
"@types/chai-as-promised": "^8.0.1",
117117
"@types/debug": "^4.1.4",
118118
"@types/is-buffer": "^2.0.0",
119119
"@types/node": "^18.0.0",
120120
"@types/ws": "^7.2.4",
121-
"@vitest/browser": "^3.0.3",
122-
"@vitest/coverage-istanbul": "^3.0.3",
121+
"@vitest/browser": "^3.0.6",
122+
"@vitest/coverage-istanbul": "^3.0.6",
123123
"chai": "^5.1.1",
124124
"chai-as-promised": "^8.0.0",
125125
"chai-exclude": "^3.0.0",
@@ -128,9 +128,9 @@
128128
"eslint": "^9.9.0",
129129
"events": "^3.0.0",
130130
"https-proxy-agent": "^7.0.0",
131-
"playwright": "^1.46.1",
131+
"playwright": "^1.50.1",
132132
"typescript": "~5.7.2",
133-
"vitest": "^3.0.3",
133+
"vitest": "^3.0.6",
134134
"ws": "^8.0.0"
135135
},
136136
"type": "module",
@@ -189,5 +189,6 @@
189189
"default": "./dist/commonjs/experimental/index.js"
190190
}
191191
}
192-
}
192+
},
193+
"react-native": "./dist/react-native/index.js"
193194
}

sdk/servicebus/service-bus/samples-dev/advanced/administrationClient.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ import { ServiceBusAdministrationClient } from "@azure/service-bus";
1414
import { DefaultAzureCredential } from "@azure/identity";
1515

1616
// Load the .env file if it exists
17-
import * as dotenv from "dotenv";
18-
dotenv.config();
19-
17+
import "dotenv/config";
2018
// Define connection string and related Service Bus entity names here
2119
const fqdn = process.env.SERVICEBUS_FQDN || "<your-servicebus-namespace>.servicebus.windows.net";
2220
const queueName = process.env.QUEUE_NAME || "<queue name>";
2321

24-
export async function main() {
22+
export async function main(): Promise<void> {
2523
// You can also use AAD credentials from `@azure/identity` along with the host url
2624
// instead of the connection string for authentication.
2725
const credential = new DefaultAzureCredential();

sdk/servicebus/service-bus/samples-dev/advanced/deferral.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,28 @@
1414
* @azsdk-weight 55
1515
*/
1616

17-
import {
18-
ServiceBusClient,
19-
delay,
17+
import type {
2018
ProcessErrorArgs,
2119
ServiceBusReceivedMessage,
2220
ServiceBusMessage,
2321
} from "@azure/service-bus";
22+
import { ServiceBusClient, delay } from "@azure/service-bus";
2423
import { DefaultAzureCredential } from "@azure/identity";
2524

2625
// Load the .env file if it exists
27-
import * as dotenv from "dotenv";
28-
dotenv.config();
29-
26+
import "dotenv/config";
3027
// Define connection string and related Service Bus entity names here
3128
const fqdn = process.env.SERVICEBUS_FQDN || "<your-servicebus-namespace>.servicebus.windows.net";
3229
const queueName = process.env.QUEUE_NAME || "<queue name>";
3330
const credential = new DefaultAzureCredential();
3431

35-
export async function main() {
32+
export async function main(): Promise<void> {
3633
await sendMessages();
3734
await receiveMessage();
3835
}
3936

4037
// Shuffle and send messages
41-
async function sendMessages() {
38+
async function sendMessages(): Promise<void> {
4239
const sbClient = new ServiceBusClient(fqdn, credential);
4340
// createSender() can also be used to create a sender for a topic.
4441
const sender = sbClient.createSender(queueName);
@@ -75,7 +72,7 @@ async function sendMessages() {
7572
await sbClient.close();
7673
}
7774

78-
async function receiveMessage() {
75+
async function receiveMessage(): Promise<void> {
7976
const sbClient = new ServiceBusClient(fqdn, credential);
8077

8178
// If receiving from a subscription, you can use the createReceiver(topicName, subscriptionName) overload

sdk/servicebus/service-bus/samples-dev/advanced/listingEntities.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import { ServiceBusAdministrationClient } from "@azure/service-bus";
1313
import { DefaultAzureCredential } from "@azure/identity";
1414

1515
// Load the .env file if it exists
16-
import * as dotenv from "dotenv";
17-
dotenv.config();
18-
16+
import "dotenv/config";
1917
// Define connection string and related Service Bus entity names here
2018
const fqdn = process.env.SERVICEBUS_FQDN || "<your-servicebus-namespace>.servicebus.windows.net";
2119

22-
export async function main() {
20+
export async function main(): Promise<void> {
2321
const credential = new DefaultAzureCredential();
2422
const serviceBusAdministrationClient = new ServiceBusAdministrationClient(fqdn, credential);
2523
const baseQueueName = "random-queue";

0 commit comments

Comments
 (0)