@messagebird/sdk 0.1.1 → 0.2.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/dist/index.js CHANGED
@@ -1237,6 +1237,22 @@ var createEmailMessage = (options) => (options.client ?? client).post({
1237
1237
  ...options.headers
1238
1238
  }
1239
1239
  });
1240
+ var createEmailMessageBatch = (options) => (options.client ?? client).post({
1241
+ security: [
1242
+ { scheme: "bearer", type: "http" },
1243
+ {
1244
+ in: "cookie",
1245
+ name: "bird_session",
1246
+ type: "apiKey"
1247
+ }
1248
+ ],
1249
+ url: "/v1/email/batches",
1250
+ ...options,
1251
+ headers: {
1252
+ "Content-Type": "application/json",
1253
+ ...options.headers
1254
+ }
1255
+ });
1240
1256
  var getEmailMessage = (options) => (options.client ?? client).get({
1241
1257
  security: [
1242
1258
  { scheme: "bearer", type: "http" },
@@ -1307,7 +1323,6 @@ var EmailResource = class extends Resource {
1307
1323
  * channel defaults may be omitted (per-send value wins).
1308
1324
  *
1309
1325
  * @example Send a message
1310
- * // bird:snippet:start email.send
1311
1326
  * const msg = await bird.email.send({
1312
1327
  * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1313
1328
  * to: ["delivered@messagebird.dev"],
@@ -1315,7 +1330,6 @@ var EmailResource = class extends Resource {
1315
1330
  * html: "<p>My first Bird email.</p>",
1316
1331
  * });
1317
1332
  * console.log(msg.id, msg.status); // "em_…", "accepted"
1318
- * // bird:snippet:end email.send
1319
1333
  *
1320
1334
  * @example A richer send — cc/bcc, reply-to, tags, metadata, click-tracking off, and an idempotency key (safe to retry; the server dedupes)
1321
1335
  * await bird.email.send(
@@ -1334,12 +1348,11 @@ var EmailResource = class extends Resource {
1334
1348
  * );
1335
1349
  *
1336
1350
  * @example Branch on the typed error hierarchy
1337
- * // bird:snippet:start email.errors
1338
1351
  * import { BirdRateLimitError, BirdValidationError, BirdAPIError } from "@messagebird/sdk";
1339
1352
  *
1340
1353
  * try {
1341
1354
  * await bird.email.send({
1342
- * from: "onboarding@messagebird.dev",
1355
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1343
1356
  * to: ["delivered@messagebird.dev"],
1344
1357
  * subject: "Hello from Bird",
1345
1358
  * html: "<p>My first Bird email.</p>",
@@ -1350,13 +1363,11 @@ var EmailResource = class extends Resource {
1350
1363
  * else if (err instanceof BirdAPIError) console.error(err.code, err.requestId);
1351
1364
  * else throw err;
1352
1365
  * }
1353
- * // bird:snippet:end email.errors
1354
1366
  *
1355
1367
  * @example Errors as values with `.safe()`
1356
- * // bird:snippet:start email.safe
1357
1368
  * const { data, error } = await bird.email
1358
1369
  * .send({
1359
- * from: "onboarding@messagebird.dev",
1370
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1360
1371
  * to: ["delivered@messagebird.dev"],
1361
1372
  * subject: "Hello from Bird",
1362
1373
  * html: "<p>My first Bird email.</p>",
@@ -1364,7 +1375,6 @@ var EmailResource = class extends Resource {
1364
1375
  * .safe();
1365
1376
  * if (error) console.error(error.message);
1366
1377
  * else console.log(data.id);
1367
- * // bird:snippet:end email.safe
1368
1378
  */
1369
1379
  send(params, options) {
1370
1380
  const body = { ...this.#defaults, ...params };
@@ -1374,22 +1384,61 @@ var EmailResource = class extends Resource {
1374
1384
  ({ signal, headers }) => createEmailMessage({ client: this.client, body, headers, signal })
1375
1385
  );
1376
1386
  }
1387
+ /**
1388
+ * Send a batch of up to 100 independent email messages in one request. The
1389
+ * batch is validated as a unit — if any item fails validation (unverified
1390
+ * sender, all recipients suppressed, field-level errors) the whole batch is
1391
+ * rejected with a `BirdValidationError` and nothing is queued. Resolves with
1392
+ * one accepted item per submitted message, in submission order, once the batch
1393
+ * is accepted (the API's 202). Channel defaults are applied per item.
1394
+ *
1395
+ * @example Send a batch of messages
1396
+ * const batch = await bird.email.sendBatch([
1397
+ * {
1398
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1399
+ * to: ["alice@example.com"],
1400
+ * subject: "Your receipt",
1401
+ * html: "<p>Thanks, Alice.</p>",
1402
+ * },
1403
+ * {
1404
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1405
+ * to: ["bob@example.com"],
1406
+ * subject: "Your receipt",
1407
+ * html: "<p>Thanks, Bob.</p>",
1408
+ * },
1409
+ * ]);
1410
+ * for (const item of batch.data) console.log(item.id, item.status);
1411
+ */
1412
+ sendBatch(params, options) {
1413
+ const body = params.map((item) => ({
1414
+ ...this.#defaults,
1415
+ ...item
1416
+ }));
1417
+ return this.call(
1418
+ "POST",
1419
+ options,
1420
+ ({ signal, headers }) => createEmailMessageBatch({ client: this.client, body, headers, signal })
1421
+ );
1422
+ }
1377
1423
  /**
1378
1424
  * Fetch a message with aggregate delivery status.
1379
1425
  *
1380
1426
  * @example
1381
- * // bird:snippet:start email.get
1382
1427
  * const msg = await bird.email.get("em_abc123");
1383
1428
  * msg.status; // "accepted" | "processed" | "delivered" | "bounced" | …
1384
1429
  * msg.delivered_count;
1385
1430
  * msg.bounced_count;
1386
- * // bird:snippet:end email.get
1387
1431
  */
1388
1432
  get(messageId, options) {
1389
1433
  return this.call(
1390
1434
  "GET",
1391
1435
  options,
1392
- ({ signal, headers }) => getEmailMessage({ client: this.client, path: { message_id: messageId }, headers, signal })
1436
+ ({ signal, headers }) => getEmailMessage({
1437
+ client: this.client,
1438
+ path: { message_id: messageId },
1439
+ headers,
1440
+ signal
1441
+ })
1393
1442
  );
1394
1443
  }
1395
1444
  /**
@@ -1397,14 +1446,10 @@ var EmailResource = class extends Resource {
1397
1446
  * walks every message across all pages.
1398
1447
  *
1399
1448
  * @example Iterate every message, or take one page
1400
- * // bird:snippet:start email.list.paginate
1401
- * // bird:snippet:start email.list.iterate
1402
1449
  * for await (const message of bird.email.list({ status: "bounced" })) {
1403
1450
  * console.log(message.id);
1404
1451
  * }
1405
- * // bird:snippet:end email.list.iterate
1406
1452
  * const page = await bird.email.list({ limit: 50 }); // page.data, page.next_cursor
1407
- * // bird:snippet:end email.list.paginate
1408
1453
  */
1409
1454
  list(query, options) {
1410
1455
  return this.paginated(
@@ -1437,6 +1482,11 @@ var WebhooksResource = class {
1437
1482
  * types are returned as-is (handle them in a `default` case) so a newer server
1438
1483
  * event can't break an older SDK.
1439
1484
  *
1485
+ * @example One call verifies the signature and returns the typed event
1486
+ * // Pass the RAW request body; set the secret via new BirdClient({ webhooks: { secret } }).
1487
+ * const event = bird.webhooks.unwrap(rawBody, headers);
1488
+ * console.log(event.type); // discriminated union — narrow on event.type
1489
+ *
1440
1490
  * @example Verify and dispatch — pass the raw request body, never the parsed JSON
1441
1491
  * // new BirdClient({ apiKey, webhooks: { secret } })
1442
1492
  * try {
@@ -1527,7 +1577,12 @@ var BirdClient = class {
1527
1577
  this.#headers = {
1528
1578
  ...opts.defaultHeaders,
1529
1579
  Authorization: `Bearer ${opts.apiKey}`,
1530
- "User-Agent": `bird-sdk-js/${"0.1.1"}`
1580
+ "User-Agent": `bird-sdk-js/${"0.2.1"}`,
1581
+ // X-Bird-* client-identity headers (ADR-0067): the API attributes the SDK
1582
+ // surface from these, not the User-Agent. Edge-safe, so no os/arch/runtime
1583
+ // (those need Node globals this SDK must not touch); surface + version only.
1584
+ "X-Bird-Surface": "sdk-js",
1585
+ "X-Bird-Version": "0.2.1"
1531
1586
  };
1532
1587
  this.#client = createClient(
1533
1588
  createConfig({
@@ -1603,6 +1658,33 @@ var BirdClient = class {
1603
1658
  }
1604
1659
  };
1605
1660
 
1606
- export { BirdAPIError, BirdAuthError, BirdBadRequestError, BirdBillingError, BirdClient, BirdConflictError, BirdConnectionError, BirdError, BirdInternalError, BirdMisdirectedError, BirdNotFoundError, BirdNotImplementedError, BirdPayloadTooLargeError, BirdPermissionError, BirdPreconditionError, BirdRateLimitError, BirdServiceUnavailableError, BirdTimeoutError, BirdValidationError, BirdWebhookVerificationError, baseUrlForRegion, regionFromApiKey };
1661
+ // src/event-types.gen.ts
1662
+ var WebhookEventType = {
1663
+ DomainFailed: "domain.failed",
1664
+ DomainVerified: "domain.verified",
1665
+ EmailAccepted: "email.accepted",
1666
+ EmailBounced: "email.bounced",
1667
+ EmailClicked: "email.clicked",
1668
+ EmailComplained: "email.complained",
1669
+ EmailDeferred: "email.deferred",
1670
+ EmailDelivered: "email.delivered",
1671
+ EmailListUnsubscribed: "email.list_unsubscribed",
1672
+ EmailOpened: "email.opened",
1673
+ EmailOutOfBandBounce: "email.out_of_band_bounce",
1674
+ EmailProcessed: "email.processed",
1675
+ EmailReceived: "email.received",
1676
+ EmailRejected: "email.rejected",
1677
+ EmailSuppressionCreated: "email_suppression.created",
1678
+ EmailUnsubscribed: "email.unsubscribed",
1679
+ SmsAccepted: "sms.accepted",
1680
+ SmsDelivered: "sms.delivered",
1681
+ SmsExpired: "sms.expired",
1682
+ SmsFailed: "sms.failed",
1683
+ SmsRejected: "sms.rejected",
1684
+ SmsSent: "sms.sent",
1685
+ SmsUndelivered: "sms.undelivered"
1686
+ };
1687
+
1688
+ export { BirdAPIError, BirdAuthError, BirdBadRequestError, BirdBillingError, BirdClient, BirdConflictError, BirdConnectionError, BirdError, BirdInternalError, BirdMisdirectedError, BirdNotFoundError, BirdNotImplementedError, BirdPayloadTooLargeError, BirdPermissionError, BirdPreconditionError, BirdRateLimitError, BirdServiceUnavailableError, BirdTimeoutError, BirdValidationError, BirdWebhookVerificationError, WebhookEventType, baseUrlForRegion, regionFromApiKey };
1607
1689
  //# sourceMappingURL=index.js.map
1608
1690
  //# sourceMappingURL=index.js.map