@azure/service-bus 7.10.0-alpha.20250303.1 → 7.10.0-alpha.20250304.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -152,8 +152,8 @@ authenticate to Service Bus using a connection string or using an Azure Active D
152
152
  This method takes the connection string to your Service Bus instance. You can get
153
153
  the connection string from the Azure portal.
154
154
 
155
- ```javascript
156
- const { ServiceBusClient } = require("@azure/service-bus");
155
+ ```ts snippet:ReadmeSampleCreateClient_ConnectionString
156
+ import { ServiceBusClient } from "@azure/service-bus";
157
157
 
158
158
  const serviceBusClient = new ServiceBusClient("<connectionString>");
159
159
  ```
@@ -167,9 +167,9 @@ Authentication with Azure Active Directory uses the [Azure Identity library][azu
167
167
  The example below uses the [DefaultAzureCredential][defaultazurecredential], one of many
168
168
  available credential providers from the `@azure/identity` library.
169
169
 
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";
173
173
 
174
174
  const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
175
175
  const credential = new DefaultAzureCredential();
@@ -223,7 +223,14 @@ The following sections provide code snippets that cover some of the common tasks
223
223
  Once you have created an instance of a `ServiceBusClient` class, you can get a `ServiceBusSender`
224
224
  using the [createSender][sbclient_createsender] method which you can use to [send][sender_sendmessages] messages.
225
225
 
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
+
227
234
  const sender = serviceBusClient.createSender("my-queue");
228
235
 
229
236
  const messages = [
@@ -270,7 +277,14 @@ await sender.sendMessages(batch);
270
277
  Once you have created an instance of a `ServiceBusClient` class, you can get a `ServiceBusReceiver`
271
278
  using the [createReceiver][sbclient_createreceiver] method.
272
279
 
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
+
274
288
  const receiver = serviceBusClient.createReceiver("my-queue");
275
289
  ```
276
290
 
@@ -289,7 +303,16 @@ You can use this receiver in one of 3 ways to receive messages:
289
303
  Use the [receiveMessages][receiver_receivemessages] function which returns a promise that
290
304
  resolves to an array of messages.
291
305
 
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
+
293
316
  const myMessages = await receiver.receiveMessages(10);
294
317
  ```
295
318
 
@@ -300,7 +323,16 @@ it running as long as you need.
300
323
 
301
324
  When you are done, call `receiver.close()` to stop receiving any more messages.
302
325
 
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
+
304
336
  const myMessageHandler = async (message) => {
305
337
  // your code here
306
338
  console.log(`message.body: ${message.body}`);
@@ -321,7 +353,16 @@ receiver.subscribe({
321
353
 
322
354
  Use the [getMessageIterator][receiver_getmessageiterator] to get an async iterator over messages
323
355
 
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
+
325
366
  for await (const message of receiver.getMessageIterator()) {
326
367
  // your code here
327
368
  }
@@ -341,7 +382,14 @@ their maximum delivery count.
341
382
 
342
383
  Creating a receiver for a dead letter sub-queue is similar to creating a receiver for a subscription or queue:
343
384
 
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
+
345
393
  // To receive from a queue's dead letter sub-queue
346
394
  const deadLetterReceiverForQueue = serviceBusClient.createReceiver("queue", {
347
395
  subQueueType: "deadLetter",
@@ -377,7 +425,14 @@ In order to send messages to a session, use the `ServiceBusClient` to create a s
377
425
  When sending the message, set the `sessionId` property in the message to ensure
378
426
  your message lands in the right session.
379
427
 
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
+
381
436
  const sender = serviceBusClient.createSender("my-session-queue");
382
437
  await sender.sendMessages({
383
438
  body: "my-message-body",
@@ -404,14 +459,14 @@ There are two ways of choosing which session to open:
404
459
 
405
460
  1. Specify a `sessionId`, which locks a named session.
406
461
 
407
- ```javascript
462
+ ```ts snippet:ignore
408
463
  const receiver = await serviceBusClient.acceptSession("my-session-queue", "my-session");
409
464
  ```
410
465
 
411
466
  2. Do not specify a session id. In this case Service Bus will find the next available session
412
467
  that is not already locked.
413
468
 
414
- ```javascript
469
+ ```ts snippet:ignore
415
470
  const receiver = await serviceBusClient.acceptNextSession("my-session-queue");
416
471
  ```
417
472
 
@@ -435,7 +490,11 @@ You can read more about how sessions work [here][docsms_messagesessions].
435
490
 
436
491
  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).
437
492
 
438
- ```js
493
+ ```ts snippet:ReadmeSampleAdministrationClient
494
+ import { ServiceBusAdministrationClient } from "@azure/service-bus";
495
+
496
+ const queueName = "my-session-queue";
497
+
439
498
  // Get the connection string from the portal
440
499
  // OR
441
500
  // 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);
447
506
 
448
507
  const queueRuntimeProperties =
449
508
  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}`);
451
510
 
452
511
  await serviceBusAdministrationClient.deleteQueue(queueName);
453
512
  ```
@@ -518,8 +577,6 @@ directory for detailed examples on how to use this library to send and receive m
518
577
 
519
578
  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.
520
579
 
521
-
522
-
523
580
  [apiref]: https://learn.microsoft.com/javascript/api/@azure/service-bus/
524
581
  [azure_identity]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/README.md
525
582
  [defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential
@@ -9,7 +9,7 @@ import { managementClientLogger, receiverLogger, senderLogger } from "../log.js"
9
9
  import { toBuffer, waitForSendable } from "../util/utils.js";
10
10
  import { InvalidMaxMessageCountError, throwErrorIfConnectionClosed, throwTypeErrorIfParameterIsEmptyString, throwTypeErrorIfParameterMissing, throwTypeErrorIfParameterNotLong, throwTypeErrorIfParameterTypeMismatch, } from "../util/errors.js";
11
11
  import { max32BitNumber } from "../util/constants.js";
12
- import { Buffer } from "buffer";
12
+ import { Buffer } from "node:buffer";
13
13
  import { translateServiceBusError } from "../serviceBusError.js";
14
14
  import { defaultDataTransformer, tryToJsonDecode } from "../dataTransformer.js";
15
15
  import { delay, isDefined, isObjectWithProperties } from "@azure/core-util";