@messagebird/sdk 0.4.2 → 0.5.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.mjs CHANGED
@@ -809,6 +809,7 @@ var BirdAPIError = class extends BirdError {
809
809
  vendorCode;
810
810
  remediation;
811
811
  next;
812
+ unmetGates;
812
813
  constructor(fields) {
813
814
  super(fields.message);
814
815
  this.name = "BirdAPIError";
@@ -822,6 +823,7 @@ var BirdAPIError = class extends BirdError {
822
823
  this.vendorCode = fields.vendorCode;
823
824
  this.remediation = fields.remediation;
824
825
  this.next = fields.next;
826
+ this.unmetGates = fields.unmetGates;
825
827
  Object.setPrototypeOf(this, new.target.prototype);
826
828
  }
827
829
  };
@@ -991,7 +993,8 @@ function mapResponseToError(status, body, headers) {
991
993
  param: b.param,
992
994
  vendorCode: b.vendor_code,
993
995
  remediation: b.remediation,
994
- next: b.next ?? []
996
+ next: b.next ?? [],
997
+ unmetGates: b.unmet_gates ?? []
995
998
  };
996
999
  switch (fields.type) {
997
1000
  case "auth_error": return new BirdAuthError(fields);
@@ -1261,6 +1264,24 @@ const getEmailMessage = (options) => (options.client ?? client).get({
1261
1264
  ...options
1262
1265
  });
1263
1266
  /**
1267
+ * Cancel a scheduled message
1268
+ *
1269
+ * Cancels a message that was scheduled with `scheduled_at` before it sends. Only a message that is still scheduled can be canceled; a message that already started sending, was delivered, or was previously canceled returns a conflict error. The message's status becomes `canceled` and an `email.canceled` webhook event fires. Canceling does not return consumed scheduled-send quota.
1270
+ *
1271
+ */
1272
+ const cancelEmailMessage = (options) => (options.client ?? client).post({
1273
+ security: [{
1274
+ scheme: "bearer",
1275
+ type: "http"
1276
+ }, {
1277
+ in: "cookie",
1278
+ name: "bird_session",
1279
+ type: "apiKey"
1280
+ }],
1281
+ url: "/v1/email/messages/{message_id}/cancel",
1282
+ ...options
1283
+ });
1284
+ /**
1264
1285
  * List contacts
1265
1286
  *
1266
1287
  * Returns a paginated list of contacts in the workspace, newest first. Look up a single contact by its exact `email` or `external_id`, or search by email substring with `search`.
@@ -1496,7 +1517,8 @@ const unarchiveContactProperty = (options) => (options.client ?? client).post({
1496
1517
  /**
1497
1518
  * List audiences
1498
1519
  *
1499
- * Returns a paginated list of audiences in the workspace, newest first.
1520
+ * Returns a paginated list of audiences in the workspace, newest first. Filter to audiences whose name contains a substring with `search`.
1521
+ *
1500
1522
  */
1501
1523
  const listAudiences = (options) => (options?.client ?? client).get({
1502
1524
  security: [{
@@ -1781,157 +1803,6 @@ const getSmsTemplate = (options) => (options.client ?? client).get({
1781
1803
  url: "/v1/sms/templates/{template_ref}",
1782
1804
  ...options
1783
1805
  });
1784
- /**
1785
- * List email templates
1786
- *
1787
- * Returns a paginated list of the workspace's email templates, newest first. Filter by category, source, or a case-insensitive search that matches the template's name or description.
1788
- *
1789
- */
1790
- const listEmailTemplates = (options) => (options?.client ?? client).get({
1791
- security: [{
1792
- scheme: "bearer",
1793
- type: "http"
1794
- }, {
1795
- in: "cookie",
1796
- name: "bird_session",
1797
- type: "apiKey"
1798
- }],
1799
- url: "/v1/email/templates",
1800
- ...options
1801
- });
1802
- /**
1803
- * Create an email template
1804
- *
1805
- * Creates a template and its initial editable draft. The body carries the template's name, category, authoring format (`source`), and the draft's content (`subject`, `html`, `text`). A name already used in the workspace returns a conflict.
1806
- *
1807
- */
1808
- const createEmailTemplate = (options) => (options.client ?? client).post({
1809
- security: [{
1810
- scheme: "bearer",
1811
- type: "http"
1812
- }, {
1813
- in: "cookie",
1814
- name: "bird_session",
1815
- type: "apiKey"
1816
- }],
1817
- url: "/v1/email/templates",
1818
- ...options,
1819
- headers: {
1820
- "Content-Type": "application/json",
1821
- ...options.headers
1822
- }
1823
- });
1824
- /**
1825
- * Delete an email template
1826
- *
1827
- * Deletes the template and all its versions. The name becomes available for reuse within the workspace.
1828
- *
1829
- */
1830
- const deleteEmailTemplate = (options) => (options.client ?? client).delete({
1831
- security: [{
1832
- scheme: "bearer",
1833
- type: "http"
1834
- }, {
1835
- in: "cookie",
1836
- name: "bird_session",
1837
- type: "apiKey"
1838
- }],
1839
- url: "/v1/email/templates/{template_id}",
1840
- ...options
1841
- });
1842
- /**
1843
- * Get an email template
1844
- *
1845
- * Returns a single email template with its current draft content (subject, HTML, and plain text), the draft revision, and its draft and published version ids.
1846
- *
1847
- */
1848
- const getEmailTemplate = (options) => (options.client ?? client).get({
1849
- security: [{
1850
- scheme: "bearer",
1851
- type: "http"
1852
- }, {
1853
- in: "cookie",
1854
- name: "bird_session",
1855
- type: "apiKey"
1856
- }],
1857
- url: "/v1/email/templates/{template_id}",
1858
- ...options
1859
- });
1860
- /**
1861
- * Update an email template
1862
- *
1863
- * Updates a template's metadata and its draft content. Only the fields you send are changed. Send the draft `revision` you last read; if it is stale (someone else edited the draft first) the request returns a conflict so you can reload and retry.
1864
- *
1865
- */
1866
- const updateEmailTemplate = (options) => (options.client ?? client).patch({
1867
- security: [{
1868
- scheme: "bearer",
1869
- type: "http"
1870
- }, {
1871
- in: "cookie",
1872
- name: "bird_session",
1873
- type: "apiKey"
1874
- }],
1875
- url: "/v1/email/templates/{template_id}",
1876
- ...options,
1877
- headers: {
1878
- "Content-Type": "application/json",
1879
- ...options.headers
1880
- }
1881
- });
1882
- /**
1883
- * List email template versions
1884
- *
1885
- * Returns every version of the template — the current draft plus all published versions — newest first.
1886
- *
1887
- */
1888
- const listEmailTemplateVersions = (options) => (options.client ?? client).get({
1889
- security: [{
1890
- scheme: "bearer",
1891
- type: "http"
1892
- }, {
1893
- in: "cookie",
1894
- name: "bird_session",
1895
- type: "apiKey"
1896
- }],
1897
- url: "/v1/email/templates/{template_id}/versions",
1898
- ...options
1899
- });
1900
- /**
1901
- * Get an email template version
1902
- *
1903
- * Returns a single version of an email template.
1904
- */
1905
- const getEmailTemplateVersion = (options) => (options.client ?? client).get({
1906
- security: [{
1907
- scheme: "bearer",
1908
- type: "http"
1909
- }, {
1910
- in: "cookie",
1911
- name: "bird_session",
1912
- type: "apiKey"
1913
- }],
1914
- url: "/v1/email/templates/{template_id}/versions/{version_id}",
1915
- ...options
1916
- });
1917
- /**
1918
- * Publish an email template
1919
- *
1920
- * Publishes the template's current draft as a new immutable, numbered version and makes it the live version used by sends. The draft remains editable for future changes. The draft must have a subject and a body; an empty draft is rejected.
1921
- *
1922
- */
1923
- const publishEmailTemplate = (options) => (options.client ?? client).post({
1924
- security: [{
1925
- scheme: "bearer",
1926
- type: "http"
1927
- }, {
1928
- in: "cookie",
1929
- name: "bird_session",
1930
- type: "apiKey"
1931
- }],
1932
- url: "/v1/email/templates/{template_id}/publish",
1933
- ...options
1934
- });
1935
1806
  //#endregion
1936
1807
  //#region src/resources/base.ts
1937
1808
  var Resource = class {
@@ -2106,6 +1977,23 @@ var EmailResource = class extends Resource {
2106
1977
  }));
2107
1978
  }
2108
1979
  /**
1980
+ * Cancel a message scheduled with `scheduled_at` before it sends. Only a
1981
+ * message that is still scheduled can be canceled; one that already started
1982
+ * sending — or was previously canceled — rejects with a conflict error.
1983
+ * Canceling does not return consumed scheduled-send quota.
1984
+ *
1985
+ * @example
1986
+ * await bird.email.cancel("em_abc123");
1987
+ */
1988
+ cancel(messageId, options) {
1989
+ return this.call("POST", options, ({ signal, headers }) => cancelEmailMessage({
1990
+ client: this.client,
1991
+ path: { message_id: messageId },
1992
+ headers,
1993
+ signal
1994
+ }));
1995
+ }
1996
+ /**
2109
1997
  * List messages, newest first. `await` resolves the first page; `for await`
2110
1998
  * walks every message across all pages.
2111
1999
  *
@@ -2487,166 +2375,6 @@ var ContactsResource = class extends Resource {
2487
2375
  }
2488
2376
  };
2489
2377
  //#endregion
2490
- //#region src/resources/emailTemplates.ts
2491
- var EmailTemplatesResource = class extends Resource {
2492
- /**
2493
- * Create a template and its initial editable draft. Pick the authoring format
2494
- * with `source` (`liquid`, `handlebars`, or `html`); the name must be unique
2495
- * in the workspace or the call throws a `BirdConflictError`.
2496
- *
2497
- * @example Create a template
2498
- * const tpl = await bird.emailTemplates.create({
2499
- * name: "welcome-email",
2500
- * description: "Welcome",
2501
- * category: "transactional",
2502
- * source: "handlebars",
2503
- * subject: "Welcome, {{ first_name }}!",
2504
- * html: "<h1>Hi {{ first_name }}</h1>",
2505
- * });
2506
- * console.log(tpl.id, tpl.revision); // "emt_…", 0
2507
- */
2508
- create(params, options) {
2509
- return this.call("POST", options, ({ signal, headers }) => createEmailTemplate({
2510
- client: this.client,
2511
- body: params,
2512
- headers,
2513
- signal
2514
- }));
2515
- }
2516
- /**
2517
- * List the workspace's templates, newest first. `await` resolves the first
2518
- * page; `for await` walks every template across all pages. Filter by
2519
- * `category`, `source`, or a case-insensitive `name` prefix.
2520
- *
2521
- * @example Iterate every template, or take one page
2522
- * for await (const tpl of bird.emailTemplates.list({ category: "transactional" })) {
2523
- * console.log(tpl.id, tpl.name);
2524
- * }
2525
- * const page = await bird.emailTemplates.list({ limit: 50 }); // page.data, page.next_cursor
2526
- */
2527
- list(query, options) {
2528
- return this.paginated("GET", options, ({ signal, headers }, cursor) => listEmailTemplates({
2529
- client: this.client,
2530
- query: {
2531
- ...query,
2532
- starting_after: cursor ?? query?.starting_after
2533
- },
2534
- headers,
2535
- signal
2536
- }));
2537
- }
2538
- /**
2539
- * Fetch a template with its current draft content (subject, HTML, text), the
2540
- * draft `revision`, and its draft/published version ids.
2541
- *
2542
- * @example
2543
- * const tpl = await bird.emailTemplates.get("emt_abc123");
2544
- * tpl.subject;
2545
- * tpl.published_version_id; // null until first publish
2546
- */
2547
- get(templateId, options) {
2548
- return this.call("GET", options, ({ signal, headers }) => getEmailTemplate({
2549
- client: this.client,
2550
- path: { template_id: templateId },
2551
- headers,
2552
- signal
2553
- }));
2554
- }
2555
- /**
2556
- * Update a template's metadata and draft content. Only the fields you send
2557
- * change. Pass the draft `revision` you last read; if another edit landed
2558
- * first the call throws a `BirdConflictError` — reload and retry.
2559
- *
2560
- * @example Edit the draft, guarded by the revision you read
2561
- * const tpl = await bird.emailTemplates.get("emt_abc123");
2562
- * const updated = await bird.emailTemplates.update("emt_abc123", {
2563
- * revision: tpl.revision,
2564
- * subject: "Welcome aboard, {{ first_name }}!",
2565
- * });
2566
- */
2567
- update(templateId, params, options) {
2568
- return this.call("PATCH", options, ({ signal, headers }) => updateEmailTemplate({
2569
- client: this.client,
2570
- path: { template_id: templateId },
2571
- body: params,
2572
- headers,
2573
- signal
2574
- }));
2575
- }
2576
- /**
2577
- * Delete a template and all its versions. The name becomes available for
2578
- * reuse in the workspace.
2579
- *
2580
- * @example
2581
- * await bird.emailTemplates.delete("emt_abc123");
2582
- */
2583
- delete(templateId, options) {
2584
- return this.call("DELETE", options, ({ signal, headers }) => deleteEmailTemplate({
2585
- client: this.client,
2586
- path: { template_id: templateId },
2587
- headers,
2588
- signal
2589
- }));
2590
- }
2591
- /**
2592
- * Publish the current draft as a new immutable, numbered version and make it
2593
- * the live version used by sends. The draft stays editable. The draft must
2594
- * have a subject and a body, or the call throws.
2595
- *
2596
- * @example Publish, then send by template
2597
- * const version = await bird.emailTemplates.publish("emt_abc123");
2598
- * console.log(version.version_number); // 1, 2, 3…
2599
- * await bird.email.send({
2600
- * from: "hello@acme.com",
2601
- * to: ["alice@example.com"],
2602
- * template: { id: "emt_abc123", parameters: { first_name: "Alice" } },
2603
- * });
2604
- */
2605
- publish(templateId, options) {
2606
- return this.call("POST", options, ({ signal, headers }) => publishEmailTemplate({
2607
- client: this.client,
2608
- path: { template_id: templateId },
2609
- headers,
2610
- signal
2611
- }));
2612
- }
2613
- /**
2614
- * List every version of a template — the current draft plus all published
2615
- * versions — newest first. Returns the full set in one response (`.data`);
2616
- * this list is not paginated.
2617
- *
2618
- * @example
2619
- * const { data } = await bird.emailTemplates.listVersions("emt_abc123");
2620
- * for (const v of data) console.log(v.version_number, v.status);
2621
- */
2622
- listVersions(templateId, options) {
2623
- return this.call("GET", options, ({ signal, headers }) => listEmailTemplateVersions({
2624
- client: this.client,
2625
- path: { template_id: templateId },
2626
- headers,
2627
- signal
2628
- }));
2629
- }
2630
- /**
2631
- * Fetch a single version of a template.
2632
- *
2633
- * @example
2634
- * const version = await bird.emailTemplates.getVersion("emt_abc123", "emv_def456");
2635
- * version.status; // "draft" | "published"
2636
- */
2637
- getVersion(templateId, versionId, options) {
2638
- return this.call("GET", options, ({ signal, headers }) => getEmailTemplateVersion({
2639
- client: this.client,
2640
- path: {
2641
- template_id: templateId,
2642
- version_id: versionId
2643
- },
2644
- headers,
2645
- signal
2646
- }));
2647
- }
2648
- };
2649
- //#endregion
2650
2378
  //#region src/resources/sms.ts
2651
2379
  var SmsResource = class extends Resource {
2652
2380
  /**
@@ -2887,8 +2615,6 @@ var BirdClient = class {
2887
2615
  #headers;
2888
2616
  /** The email channel — `bird.email.send(...)`, `.get(...)`, `.list(...)`. */
2889
2617
  email;
2890
- /** Email templates — `bird.emailTemplates.create(...)`, `.list(...)`, `.publish(...)`, … */
2891
- emailTemplates;
2892
2618
  /** The SMS channel — `bird.sms.send(...)`, `.get(...)`, `.list(...)`. */
2893
2619
  sms;
2894
2620
  /** SMS templates — `bird.smsTemplates.list(...)`, `.get(...)`. */
@@ -2908,9 +2634,9 @@ var BirdClient = class {
2908
2634
  this.#headers = {
2909
2635
  ...opts.defaultHeaders,
2910
2636
  Authorization: `Bearer ${opts.apiKey}`,
2911
- "User-Agent": `bird-sdk-js/0.4.2`,
2637
+ "User-Agent": `bird-sdk-js/0.5.0`,
2912
2638
  "Bird-Surface": "sdk-js",
2913
- "Bird-Version": "0.4.2"
2639
+ "Bird-Version": "0.5.0"
2914
2640
  };
2915
2641
  const caller = detectCaller();
2916
2642
  if (caller) this.#headers["Bird-Caller"] = caller;
@@ -2924,7 +2650,6 @@ var BirdClient = class {
2924
2650
  maxRetries: opts.maxRetries ?? DEFAULT_MAX_RETRIES
2925
2651
  });
2926
2652
  this.email = new EmailResource(this.core, this.#client, opts.email);
2927
- this.emailTemplates = new EmailTemplatesResource(this.core, this.#client);
2928
2653
  this.sms = new SmsResource(this.core, this.#client);
2929
2654
  this.smsTemplates = new SmsTemplatesResource(this.core, this.#client);
2930
2655
  this.contacts = new ContactsResource(this.core, this.#client);