@usenadia/node 0.1.0 → 0.1.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
@@ -23,6 +23,7 @@ __export(index_exports, {
23
23
  DEFAULT_BASE_URL: () => DEFAULT_BASE_URL,
24
24
  Nadia: () => Nadia,
25
25
  NadiaError: () => NadiaError,
26
+ NadiaWebhookError: () => NadiaWebhookError,
26
27
  callVideoFunction: () => callVideoFunction,
27
28
  changePostStatus: () => changePostStatus,
28
29
  checkConnection: () => checkConnection,
@@ -79,6 +80,7 @@ __export(index_exports, {
79
80
  listWebhookEvents: () => listWebhookEvents,
80
81
  listWebhooks: () => listWebhooks,
81
82
  moveContent: () => moveContent,
83
+ parseWebhook: () => parseWebhook,
82
84
  pollAutomationPose: () => pollAutomationPose,
83
85
  pollInbox: () => pollInbox,
84
86
  poseAutomationFlow: () => poseAutomationFlow,
@@ -94,15 +96,18 @@ __export(index_exports, {
94
96
  setPostReleaseId: () => setPostReleaseId,
95
97
  startChannelConnection: () => startChannelConnection,
96
98
  subscribeInboxWebhook: () => subscribeInboxWebhook,
99
+ testWebhook: () => testWebhook,
97
100
  updateAutomationFlow: () => updateAutomationFlow,
98
101
  updateAutomationPose: () => updateAutomationPose,
99
102
  updateContent: () => updateContent,
100
103
  uploadFile: () => uploadFile,
101
104
  uploadFromUrl: () => uploadFromUrl,
102
105
  uploadMusicFile: () => uploadMusicFile,
103
- upsertContent: () => upsertContent
106
+ upsertContent: () => upsertContent,
107
+ verifyWebhookSignature: () => verifyWebhookSignature
104
108
  });
105
109
  module.exports = __toCommonJS(index_exports);
110
+ var import_node_crypto = require("crypto");
106
111
 
107
112
  // src/generated/core/bodySerializer.gen.ts
108
113
  var serializeFormDataPair = (data, key, value) => {
@@ -1289,6 +1294,15 @@ var getWebhook = (options) => (options.client ?? client).get({
1289
1294
  url: "/public/v1/webhooks/{id}",
1290
1295
  ...options
1291
1296
  });
1297
+ var testWebhook = (options) => (options.client ?? client).post({
1298
+ security: [{ name: "Authorization", type: "apiKey" }],
1299
+ url: "/public/v1/webhooks/{id}/test",
1300
+ ...options,
1301
+ headers: {
1302
+ "Content-Type": "application/json",
1303
+ ...options.headers
1304
+ }
1305
+ });
1292
1306
  var listContents = (options) => (options?.client ?? client).get({
1293
1307
  security: [{ name: "Authorization", type: "apiKey" }],
1294
1308
  url: "/public/v1/contents",
@@ -1444,9 +1458,12 @@ var NadiaError = class _NadiaError extends Error {
1444
1458
  };
1445
1459
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1446
1460
  var messageOf = (body) => {
1447
- if (body && typeof body === "object" && "msg" in body) {
1448
- const msg = body.msg;
1449
- if (typeof msg === "string") return msg;
1461
+ if (!body || typeof body !== "object") return void 0;
1462
+ const { msg, message } = body;
1463
+ if (typeof msg === "string") return msg;
1464
+ if (typeof message === "string") return message;
1465
+ if (Array.isArray(message) && message.length && message.every((m) => typeof m === "string")) {
1466
+ return message.join("; ");
1450
1467
  }
1451
1468
  return void 0;
1452
1469
  };
@@ -1643,11 +1660,71 @@ var Nadia = class {
1643
1660
  }
1644
1661
  };
1645
1662
  var index_default = Nadia;
1663
+ var NadiaWebhookError = class _NadiaWebhookError extends Error {
1664
+ reason;
1665
+ constructor(reason, message) {
1666
+ super(message);
1667
+ this.name = "NadiaWebhookError";
1668
+ this.reason = reason;
1669
+ Object.setPrototypeOf(this, _NadiaWebhookError.prototype);
1670
+ }
1671
+ };
1672
+ function checkSignature(o) {
1673
+ const { secret, signature, timestamp, body } = o;
1674
+ if (!signature || timestamp === null || timestamp === void 0 || timestamp === "") {
1675
+ return { ok: false, reason: "missing_header", message: "X-Nadia-Signature or X-Nadia-Timestamp is missing." };
1676
+ }
1677
+ const ts = String(timestamp);
1678
+ if (o.tolerance !== void 0) {
1679
+ const seconds = Number(ts);
1680
+ const now = (o.now ?? Date.now()) / 1e3;
1681
+ if (!Number.isFinite(seconds) || Math.abs(now - seconds) > o.tolerance) {
1682
+ return { ok: false, reason: "stale", message: `Timestamp outside the ${o.tolerance}s tolerance.` };
1683
+ }
1684
+ }
1685
+ const expected = Buffer.from(
1686
+ "sha256=" + (0, import_node_crypto.createHmac)("sha256", secret).update(`${ts}.`).update(body).digest("hex")
1687
+ );
1688
+ const received = Buffer.from(signature);
1689
+ if (received.length !== expected.length || !(0, import_node_crypto.timingSafeEqual)(received, expected)) {
1690
+ return { ok: false, reason: "bad_signature", message: "Signature does not match." };
1691
+ }
1692
+ return { ok: true };
1693
+ }
1694
+ function verifyWebhookSignature(options) {
1695
+ return checkSignature(options).ok;
1696
+ }
1697
+ function parseWebhook(options) {
1698
+ const header = (name) => {
1699
+ const h = options.headers;
1700
+ if (typeof h.get === "function") return h.get(name) ?? void 0;
1701
+ const record = h;
1702
+ const key = Object.keys(record).find((k) => k.toLowerCase() === name.toLowerCase());
1703
+ const value = key === void 0 ? void 0 : record[key];
1704
+ return Array.isArray(value) ? value[0] : value;
1705
+ };
1706
+ const check = checkSignature({
1707
+ secret: options.secret,
1708
+ signature: header("X-Nadia-Signature"),
1709
+ timestamp: header("X-Nadia-Timestamp"),
1710
+ body: options.body,
1711
+ tolerance: options.tolerance ?? 300,
1712
+ now: options.now
1713
+ });
1714
+ if (!check.ok) throw new NadiaWebhookError(check.reason, check.message);
1715
+ try {
1716
+ const text = typeof options.body === "string" ? options.body : new TextDecoder().decode(options.body);
1717
+ return JSON.parse(text);
1718
+ } catch {
1719
+ throw new NadiaWebhookError("bad_body", "The body is not JSON.");
1720
+ }
1721
+ }
1646
1722
  // Annotate the CommonJS export names for ESM import in node:
1647
1723
  0 && (module.exports = {
1648
1724
  DEFAULT_BASE_URL,
1649
1725
  Nadia,
1650
1726
  NadiaError,
1727
+ NadiaWebhookError,
1651
1728
  callVideoFunction,
1652
1729
  changePostStatus,
1653
1730
  checkConnection,
@@ -1703,6 +1780,7 @@ var index_default = Nadia;
1703
1780
  listWebhookEvents,
1704
1781
  listWebhooks,
1705
1782
  moveContent,
1783
+ parseWebhook,
1706
1784
  pollAutomationPose,
1707
1785
  pollInbox,
1708
1786
  poseAutomationFlow,
@@ -1718,12 +1796,14 @@ var index_default = Nadia;
1718
1796
  setPostReleaseId,
1719
1797
  startChannelConnection,
1720
1798
  subscribeInboxWebhook,
1799
+ testWebhook,
1721
1800
  updateAutomationFlow,
1722
1801
  updateAutomationPose,
1723
1802
  updateContent,
1724
1803
  uploadFile,
1725
1804
  uploadFromUrl,
1726
1805
  uploadMusicFile,
1727
- upsertContent
1806
+ upsertContent,
1807
+ verifyWebhookSignature
1728
1808
  });
1729
1809
  //# sourceMappingURL=index.js.map