@messagebird/sdk 0.1.1 → 0.2.0

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" },
@@ -1339,7 +1355,7 @@ var EmailResource = class extends Resource {
1339
1355
  *
1340
1356
  * try {
1341
1357
  * await bird.email.send({
1342
- * from: "onboarding@messagebird.dev",
1358
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1343
1359
  * to: ["delivered@messagebird.dev"],
1344
1360
  * subject: "Hello from Bird",
1345
1361
  * html: "<p>My first Bird email.</p>",
@@ -1356,7 +1372,7 @@ var EmailResource = class extends Resource {
1356
1372
  * // bird:snippet:start email.safe
1357
1373
  * const { data, error } = await bird.email
1358
1374
  * .send({
1359
- * from: "onboarding@messagebird.dev",
1375
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1360
1376
  * to: ["delivered@messagebird.dev"],
1361
1377
  * subject: "Hello from Bird",
1362
1378
  * html: "<p>My first Bird email.</p>",
@@ -1374,6 +1390,44 @@ var EmailResource = class extends Resource {
1374
1390
  ({ signal, headers }) => createEmailMessage({ client: this.client, body, headers, signal })
1375
1391
  );
1376
1392
  }
1393
+ /**
1394
+ * Send a batch of up to 100 independent email messages in one request. The
1395
+ * batch is validated as a unit — if any item fails validation (unverified
1396
+ * sender, all recipients suppressed, field-level errors) the whole batch is
1397
+ * rejected with a `BirdValidationError` and nothing is queued. Resolves with
1398
+ * one accepted item per submitted message, in submission order, once the batch
1399
+ * is accepted (the API's 202). Channel defaults are applied per item.
1400
+ *
1401
+ * @example Send a batch of messages
1402
+ * // bird:snippet:start email.sendBatch
1403
+ * const batch = await bird.email.sendBatch([
1404
+ * {
1405
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1406
+ * to: ["alice@example.com"],
1407
+ * subject: "Your receipt",
1408
+ * html: "<p>Thanks, Alice.</p>",
1409
+ * },
1410
+ * {
1411
+ * from: { email: "onboarding@messagebird.dev", name: "Bird" },
1412
+ * to: ["bob@example.com"],
1413
+ * subject: "Your receipt",
1414
+ * html: "<p>Thanks, Bob.</p>",
1415
+ * },
1416
+ * ]);
1417
+ * for (const item of batch.data) console.log(item.id, item.status);
1418
+ * // bird:snippet:end email.sendBatch
1419
+ */
1420
+ sendBatch(params, options) {
1421
+ const body = params.map((item) => ({
1422
+ ...this.#defaults,
1423
+ ...item
1424
+ }));
1425
+ return this.call(
1426
+ "POST",
1427
+ options,
1428
+ ({ signal, headers }) => createEmailMessageBatch({ client: this.client, body, headers, signal })
1429
+ );
1430
+ }
1377
1431
  /**
1378
1432
  * Fetch a message with aggregate delivery status.
1379
1433
  *
@@ -1389,7 +1443,12 @@ var EmailResource = class extends Resource {
1389
1443
  return this.call(
1390
1444
  "GET",
1391
1445
  options,
1392
- ({ signal, headers }) => getEmailMessage({ client: this.client, path: { message_id: messageId }, headers, signal })
1446
+ ({ signal, headers }) => getEmailMessage({
1447
+ client: this.client,
1448
+ path: { message_id: messageId },
1449
+ headers,
1450
+ signal
1451
+ })
1393
1452
  );
1394
1453
  }
1395
1454
  /**
@@ -1437,6 +1496,13 @@ var WebhooksResource = class {
1437
1496
  * types are returned as-is (handle them in a `default` case) so a newer server
1438
1497
  * event can't break an older SDK.
1439
1498
  *
1499
+ * @example One call verifies the signature and returns the typed event
1500
+ * // bird:snippet:start webhook.unwrap
1501
+ * // Pass the RAW request body; set the secret via new BirdClient({ webhooks: { secret } }).
1502
+ * const event = bird.webhooks.unwrap(rawBody, headers);
1503
+ * console.log(event.type); // discriminated union — narrow on event.type
1504
+ * // bird:snippet:end webhook.unwrap
1505
+ *
1440
1506
  * @example Verify and dispatch — pass the raw request body, never the parsed JSON
1441
1507
  * // new BirdClient({ apiKey, webhooks: { secret } })
1442
1508
  * try {
@@ -1527,7 +1593,7 @@ var BirdClient = class {
1527
1593
  this.#headers = {
1528
1594
  ...opts.defaultHeaders,
1529
1595
  Authorization: `Bearer ${opts.apiKey}`,
1530
- "User-Agent": `bird-sdk-js/${"0.1.1"}`
1596
+ "User-Agent": `bird-sdk-js/${"0.2.0"}`
1531
1597
  };
1532
1598
  this.#client = createClient(
1533
1599
  createConfig({
@@ -1603,6 +1669,33 @@ var BirdClient = class {
1603
1669
  }
1604
1670
  };
1605
1671
 
1606
- export { BirdAPIError, BirdAuthError, BirdBadRequestError, BirdBillingError, BirdClient, BirdConflictError, BirdConnectionError, BirdError, BirdInternalError, BirdMisdirectedError, BirdNotFoundError, BirdNotImplementedError, BirdPayloadTooLargeError, BirdPermissionError, BirdPreconditionError, BirdRateLimitError, BirdServiceUnavailableError, BirdTimeoutError, BirdValidationError, BirdWebhookVerificationError, baseUrlForRegion, regionFromApiKey };
1672
+ // src/event-types.gen.ts
1673
+ var WebhookEventType = {
1674
+ DomainFailed: "domain.failed",
1675
+ DomainVerified: "domain.verified",
1676
+ EmailAccepted: "email.accepted",
1677
+ EmailBounced: "email.bounced",
1678
+ EmailClicked: "email.clicked",
1679
+ EmailComplained: "email.complained",
1680
+ EmailDeferred: "email.deferred",
1681
+ EmailDelivered: "email.delivered",
1682
+ EmailListUnsubscribed: "email.list_unsubscribed",
1683
+ EmailOpened: "email.opened",
1684
+ EmailOutOfBandBounce: "email.out_of_band_bounce",
1685
+ EmailProcessed: "email.processed",
1686
+ EmailReceived: "email.received",
1687
+ EmailRejected: "email.rejected",
1688
+ EmailSuppressionCreated: "email_suppression.created",
1689
+ EmailUnsubscribed: "email.unsubscribed",
1690
+ SmsAccepted: "sms.accepted",
1691
+ SmsDelivered: "sms.delivered",
1692
+ SmsExpired: "sms.expired",
1693
+ SmsFailed: "sms.failed",
1694
+ SmsRejected: "sms.rejected",
1695
+ SmsSent: "sms.sent",
1696
+ SmsUndelivered: "sms.undelivered"
1697
+ };
1698
+
1699
+ 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
1700
  //# sourceMappingURL=index.js.map
1608
1701
  //# sourceMappingURL=index.js.map