@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/README.md +85 -9
- package/dist/index.d.mts +116 -5
- package/dist/index.d.ts +116 -5
- package/dist/index.js +85 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +82 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -8
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
3
|
+
|
|
1
4
|
// src/generated/core/bodySerializer.gen.ts
|
|
2
5
|
var serializeFormDataPair = (data, key, value) => {
|
|
3
6
|
if (typeof value === "string" || value instanceof Blob) {
|
|
@@ -1183,6 +1186,15 @@ var getWebhook = (options) => (options.client ?? client).get({
|
|
|
1183
1186
|
url: "/public/v1/webhooks/{id}",
|
|
1184
1187
|
...options
|
|
1185
1188
|
});
|
|
1189
|
+
var testWebhook = (options) => (options.client ?? client).post({
|
|
1190
|
+
security: [{ name: "Authorization", type: "apiKey" }],
|
|
1191
|
+
url: "/public/v1/webhooks/{id}/test",
|
|
1192
|
+
...options,
|
|
1193
|
+
headers: {
|
|
1194
|
+
"Content-Type": "application/json",
|
|
1195
|
+
...options.headers
|
|
1196
|
+
}
|
|
1197
|
+
});
|
|
1186
1198
|
var listContents = (options) => (options?.client ?? client).get({
|
|
1187
1199
|
security: [{ name: "Authorization", type: "apiKey" }],
|
|
1188
1200
|
url: "/public/v1/contents",
|
|
@@ -1338,9 +1350,12 @@ var NadiaError = class _NadiaError extends Error {
|
|
|
1338
1350
|
};
|
|
1339
1351
|
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1340
1352
|
var messageOf = (body) => {
|
|
1341
|
-
if (body
|
|
1342
|
-
|
|
1343
|
-
|
|
1353
|
+
if (!body || typeof body !== "object") return void 0;
|
|
1354
|
+
const { msg, message } = body;
|
|
1355
|
+
if (typeof msg === "string") return msg;
|
|
1356
|
+
if (typeof message === "string") return message;
|
|
1357
|
+
if (Array.isArray(message) && message.length && message.every((m) => typeof m === "string")) {
|
|
1358
|
+
return message.join("; ");
|
|
1344
1359
|
}
|
|
1345
1360
|
return void 0;
|
|
1346
1361
|
};
|
|
@@ -1537,10 +1552,70 @@ var Nadia = class {
|
|
|
1537
1552
|
}
|
|
1538
1553
|
};
|
|
1539
1554
|
var index_default = Nadia;
|
|
1555
|
+
var NadiaWebhookError = class _NadiaWebhookError extends Error {
|
|
1556
|
+
reason;
|
|
1557
|
+
constructor(reason, message) {
|
|
1558
|
+
super(message);
|
|
1559
|
+
this.name = "NadiaWebhookError";
|
|
1560
|
+
this.reason = reason;
|
|
1561
|
+
Object.setPrototypeOf(this, _NadiaWebhookError.prototype);
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
function checkSignature(o) {
|
|
1565
|
+
const { secret, signature, timestamp, body } = o;
|
|
1566
|
+
if (!signature || timestamp === null || timestamp === void 0 || timestamp === "") {
|
|
1567
|
+
return { ok: false, reason: "missing_header", message: "X-Nadia-Signature or X-Nadia-Timestamp is missing." };
|
|
1568
|
+
}
|
|
1569
|
+
const ts = String(timestamp);
|
|
1570
|
+
if (o.tolerance !== void 0) {
|
|
1571
|
+
const seconds = Number(ts);
|
|
1572
|
+
const now = (o.now ?? Date.now()) / 1e3;
|
|
1573
|
+
if (!Number.isFinite(seconds) || Math.abs(now - seconds) > o.tolerance) {
|
|
1574
|
+
return { ok: false, reason: "stale", message: `Timestamp outside the ${o.tolerance}s tolerance.` };
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
const expected = Buffer.from(
|
|
1578
|
+
"sha256=" + createHmac("sha256", secret).update(`${ts}.`).update(body).digest("hex")
|
|
1579
|
+
);
|
|
1580
|
+
const received = Buffer.from(signature);
|
|
1581
|
+
if (received.length !== expected.length || !timingSafeEqual(received, expected)) {
|
|
1582
|
+
return { ok: false, reason: "bad_signature", message: "Signature does not match." };
|
|
1583
|
+
}
|
|
1584
|
+
return { ok: true };
|
|
1585
|
+
}
|
|
1586
|
+
function verifyWebhookSignature(options) {
|
|
1587
|
+
return checkSignature(options).ok;
|
|
1588
|
+
}
|
|
1589
|
+
function parseWebhook(options) {
|
|
1590
|
+
const header = (name) => {
|
|
1591
|
+
const h = options.headers;
|
|
1592
|
+
if (typeof h.get === "function") return h.get(name) ?? void 0;
|
|
1593
|
+
const record = h;
|
|
1594
|
+
const key = Object.keys(record).find((k) => k.toLowerCase() === name.toLowerCase());
|
|
1595
|
+
const value = key === void 0 ? void 0 : record[key];
|
|
1596
|
+
return Array.isArray(value) ? value[0] : value;
|
|
1597
|
+
};
|
|
1598
|
+
const check = checkSignature({
|
|
1599
|
+
secret: options.secret,
|
|
1600
|
+
signature: header("X-Nadia-Signature"),
|
|
1601
|
+
timestamp: header("X-Nadia-Timestamp"),
|
|
1602
|
+
body: options.body,
|
|
1603
|
+
tolerance: options.tolerance ?? 300,
|
|
1604
|
+
now: options.now
|
|
1605
|
+
});
|
|
1606
|
+
if (!check.ok) throw new NadiaWebhookError(check.reason, check.message);
|
|
1607
|
+
try {
|
|
1608
|
+
const text = typeof options.body === "string" ? options.body : new TextDecoder().decode(options.body);
|
|
1609
|
+
return JSON.parse(text);
|
|
1610
|
+
} catch {
|
|
1611
|
+
throw new NadiaWebhookError("bad_body", "The body is not JSON.");
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1540
1614
|
export {
|
|
1541
1615
|
DEFAULT_BASE_URL,
|
|
1542
1616
|
Nadia,
|
|
1543
1617
|
NadiaError,
|
|
1618
|
+
NadiaWebhookError,
|
|
1544
1619
|
callVideoFunction,
|
|
1545
1620
|
changePostStatus,
|
|
1546
1621
|
checkConnection,
|
|
@@ -1597,6 +1672,7 @@ export {
|
|
|
1597
1672
|
listWebhookEvents,
|
|
1598
1673
|
listWebhooks,
|
|
1599
1674
|
moveContent,
|
|
1675
|
+
parseWebhook,
|
|
1600
1676
|
pollAutomationPose,
|
|
1601
1677
|
pollInbox,
|
|
1602
1678
|
poseAutomationFlow,
|
|
@@ -1612,12 +1688,14 @@ export {
|
|
|
1612
1688
|
setPostReleaseId,
|
|
1613
1689
|
startChannelConnection,
|
|
1614
1690
|
subscribeInboxWebhook,
|
|
1691
|
+
testWebhook,
|
|
1615
1692
|
updateAutomationFlow,
|
|
1616
1693
|
updateAutomationPose,
|
|
1617
1694
|
updateContent,
|
|
1618
1695
|
uploadFile,
|
|
1619
1696
|
uploadFromUrl,
|
|
1620
1697
|
uploadMusicFile,
|
|
1621
|
-
upsertContent
|
|
1698
|
+
upsertContent,
|
|
1699
|
+
verifyWebhookSignature
|
|
1622
1700
|
};
|
|
1623
1701
|
//# sourceMappingURL=index.mjs.map
|