@usenadia/node 0.1.0 → 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
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -23,13 +33,18 @@ __export(index_exports, {
23
33
  DEFAULT_BASE_URL: () => DEFAULT_BASE_URL,
24
34
  Nadia: () => Nadia,
25
35
  NadiaError: () => NadiaError,
26
- callVideoFunction: () => callVideoFunction,
36
+ NadiaWebhookError: () => NadiaWebhookError,
37
+ catchUpAutomationPose: () => catchUpAutomationPose,
27
38
  changePostStatus: () => changePostStatus,
28
39
  checkConnection: () => checkConnection,
40
+ confirmUpload: () => confirmUpload,
29
41
  createAutomationFlow: () => createAutomationFlow,
30
42
  createContent: () => createContent,
43
+ createContentFromLink: () => createContentFromLink,
31
44
  createPost: () => createPost,
45
+ createUpload: () => createUpload,
32
46
  createWebhook: () => createWebhook,
47
+ createWorkspace: () => createWorkspace,
33
48
  default: () => index_default,
34
49
  deleteAutomationFlow: () => deleteAutomationFlow,
35
50
  deleteAutomationPose: () => deleteAutomationPose,
@@ -39,8 +54,8 @@ __export(index_exports, {
39
54
  deletePost: () => deletePost,
40
55
  deletePostGroup: () => deletePostGroup,
41
56
  deleteWebhook: () => deleteWebhook,
57
+ deleteWorkspace: () => deleteWorkspace,
42
58
  findSlot: () => findSlot,
43
- generateVideo: () => generateVideo,
44
59
  getAccountHistory: () => getAccountHistory,
45
60
  getAutomationFlow: () => getAutomationFlow,
46
61
  getAutomationPose: () => getAutomationPose,
@@ -51,11 +66,15 @@ __export(index_exports, {
51
66
  getConversation: () => getConversation,
52
67
  getHookForms: () => getHookForms,
53
68
  getMusicTrack: () => getMusicTrack,
69
+ getPost: () => getPost,
54
70
  getPostAnalytics: () => getPostAnalytics,
71
+ getPostAutomation: () => getPostAutomation,
72
+ getPostingTimes: () => getPostingTimes,
73
+ getSubscription: () => getSubscription,
55
74
  getVideoHistory: () => getVideoHistory,
75
+ getViewsPerDay: () => getViewsPerDay,
56
76
  getWebhook: () => getWebhook,
57
77
  hideComment: () => hideComment,
58
- importHooks: () => importHooks,
59
78
  importMusic: () => importMusic,
60
79
  linkContentPost: () => linkContentPost,
61
80
  listAutomationFlowPoses: () => listAutomationFlowPoses,
@@ -78,12 +97,15 @@ __export(index_exports, {
78
97
  listVideoPerformance: () => listVideoPerformance,
79
98
  listWebhookEvents: () => listWebhookEvents,
80
99
  listWebhooks: () => listWebhooks,
100
+ listWorkspaces: () => listWorkspaces,
81
101
  moveContent: () => moveContent,
102
+ parseWebhook: () => parseWebhook,
82
103
  pollAutomationPose: () => pollAutomationPose,
83
104
  pollInbox: () => pollInbox,
84
105
  poseAutomationFlow: () => poseAutomationFlow,
85
106
  previewAutomationEmail: () => previewAutomationEmail,
86
- replaceHookFamilies: () => replaceHookFamilies,
107
+ previewAutomationPoseCatchUp: () => previewAutomationPoseCatchUp,
108
+ removePostAutomation: () => removePostAutomation,
87
109
  replayAutomationPose: () => replayAutomationPose,
88
110
  replyToComment: () => replyToComment,
89
111
  runChannelTool: () => runChannelTool,
@@ -91,18 +113,27 @@ __export(index_exports, {
91
113
  sendAutomationTestEmail: () => sendAutomationTestEmail,
92
114
  sendDirectMessage: () => sendDirectMessage,
93
115
  setAutomationFlowStatus: () => setAutomationFlowStatus,
116
+ setPostAutomation: () => setPostAutomation,
117
+ setPostCover: () => setPostCover,
118
+ setPostPublication: () => setPostPublication,
94
119
  setPostReleaseId: () => setPostReleaseId,
120
+ setPostingTimes: () => setPostingTimes,
95
121
  startChannelConnection: () => startChannelConnection,
96
122
  subscribeInboxWebhook: () => subscribeInboxWebhook,
123
+ testWebhook: () => testWebhook,
97
124
  updateAutomationFlow: () => updateAutomationFlow,
98
125
  updateAutomationPose: () => updateAutomationPose,
99
126
  updateContent: () => updateContent,
127
+ updateWorkspace: () => updateWorkspace,
128
+ uploadContentType: () => uploadContentType,
100
129
  uploadFile: () => uploadFile,
101
130
  uploadFromUrl: () => uploadFromUrl,
102
131
  uploadMusicFile: () => uploadMusicFile,
103
- upsertContent: () => upsertContent
132
+ upsertContent: () => upsertContent,
133
+ verifyWebhookSignature: () => verifyWebhookSignature
104
134
  });
105
135
  module.exports = __toCommonJS(index_exports);
136
+ var import_node_crypto = require("crypto");
106
137
 
107
138
  // src/generated/core/bodySerializer.gen.ts
108
139
  var serializeFormDataPair = (data, key, value) => {
@@ -937,11 +968,39 @@ var uploadFromUrl = (options) => (options.client ?? client).post({
937
968
  ...options.headers
938
969
  }
939
970
  });
971
+ var createUpload = (options) => (options.client ?? client).post({
972
+ security: [{ name: "Authorization", type: "apiKey" }],
973
+ url: "/public/v1/uploads",
974
+ ...options,
975
+ headers: {
976
+ "Content-Type": "application/json",
977
+ ...options.headers
978
+ }
979
+ });
980
+ var confirmUpload = (options) => (options.client ?? client).post({
981
+ security: [{ name: "Authorization", type: "apiKey" }],
982
+ url: "/public/v1/uploads/{uploadId}/confirm",
983
+ ...options
984
+ });
940
985
  var findSlot = (options) => (options.client ?? client).get({
941
986
  security: [{ name: "Authorization", type: "apiKey" }],
942
987
  url: "/public/v1/find-slot/{id}",
943
988
  ...options
944
989
  });
990
+ var getPostingTimes = (options) => (options.client ?? client).get({
991
+ security: [{ name: "Authorization", type: "apiKey" }],
992
+ url: "/public/v1/integrations/{id}/posting-times",
993
+ ...options
994
+ });
995
+ var setPostingTimes = (options) => (options.client ?? client).put({
996
+ security: [{ name: "Authorization", type: "apiKey" }],
997
+ url: "/public/v1/integrations/{id}/posting-times",
998
+ ...options,
999
+ headers: {
1000
+ "Content-Type": "application/json",
1001
+ ...options.headers
1002
+ }
1003
+ });
945
1004
  var listPosts = (options) => (options.client ?? client).get({
946
1005
  security: [{ name: "Authorization", type: "apiKey" }],
947
1006
  url: "/public/v1/posts",
@@ -961,6 +1020,11 @@ var deletePost = (options) => (options.client ?? client).delete({
961
1020
  url: "/public/v1/posts/{id}",
962
1021
  ...options
963
1022
  });
1023
+ var getPost = (options) => (options.client ?? client).get({
1024
+ security: [{ name: "Authorization", type: "apiKey" }],
1025
+ url: "/public/v1/posts/{id}",
1026
+ ...options
1027
+ });
964
1028
  var deletePostGroup = (options) => (options.client ?? client).delete({
965
1029
  security: [{ name: "Authorization", type: "apiKey" }],
966
1030
  url: "/public/v1/posts/group/{group}",
@@ -971,6 +1035,25 @@ var checkConnection = (options) => (options?.client ?? client).get({
971
1035
  url: "/public/v1/is-connected",
972
1036
  ...options
973
1037
  });
1038
+ var listWorkspaces = (options) => (options?.client ?? client).get({
1039
+ security: [{ name: "Authorization", type: "apiKey" }],
1040
+ url: "/public/v1/workspaces",
1041
+ ...options
1042
+ });
1043
+ var createWorkspace = (options) => (options.client ?? client).post({
1044
+ security: [{ name: "Authorization", type: "apiKey" }],
1045
+ url: "/public/v1/workspaces",
1046
+ ...options,
1047
+ headers: {
1048
+ "Content-Type": "application/json",
1049
+ ...options.headers
1050
+ }
1051
+ });
1052
+ var getSubscription = (options) => (options?.client ?? client).get({
1053
+ security: [{ name: "Authorization", type: "apiKey" }],
1054
+ url: "/public/v1/subscription",
1055
+ ...options
1056
+ });
974
1057
  var listGroups = (options) => (options?.client ?? client).get({
975
1058
  security: [{ name: "Authorization", type: "apiKey" }],
976
1059
  url: "/public/v1/groups",
@@ -991,24 +1074,6 @@ var listNotifications = (options) => (options?.client ?? client).get({
991
1074
  url: "/public/v1/notifications",
992
1075
  ...options
993
1076
  });
994
- var generateVideo = (options) => (options.client ?? client).post({
995
- security: [{ name: "Authorization", type: "apiKey" }],
996
- url: "/public/v1/generate-video",
997
- ...options,
998
- headers: {
999
- "Content-Type": "application/json",
1000
- ...options.headers
1001
- }
1002
- });
1003
- var callVideoFunction = (options) => (options.client ?? client).post({
1004
- security: [{ name: "Authorization", type: "apiKey" }],
1005
- url: "/public/v1/video/function",
1006
- ...options,
1007
- headers: {
1008
- "Content-Type": "application/json",
1009
- ...options.headers
1010
- }
1011
- });
1012
1077
  var deleteChannel = (options) => (options.client ?? client).delete({
1013
1078
  security: [{ name: "Authorization", type: "apiKey" }],
1014
1079
  url: "/public/v1/integrations/{id}",
@@ -1033,6 +1098,25 @@ var changePostStatus = (options) => (options.client ?? client).put({
1033
1098
  ...options.headers
1034
1099
  }
1035
1100
  });
1101
+ var removePostAutomation = (options) => (options.client ?? client).delete({
1102
+ security: [{ name: "Authorization", type: "apiKey" }],
1103
+ url: "/public/v1/posts/{id}/automation",
1104
+ ...options
1105
+ });
1106
+ var getPostAutomation = (options) => (options.client ?? client).get({
1107
+ security: [{ name: "Authorization", type: "apiKey" }],
1108
+ url: "/public/v1/posts/{id}/automation",
1109
+ ...options
1110
+ });
1111
+ var setPostAutomation = (options) => (options.client ?? client).put({
1112
+ security: [{ name: "Authorization", type: "apiKey" }],
1113
+ url: "/public/v1/posts/{id}/automation",
1114
+ ...options,
1115
+ headers: {
1116
+ "Content-Type": "application/json",
1117
+ ...options.headers
1118
+ }
1119
+ });
1036
1120
  var setPostReleaseId = (options) => (options.client ?? client).put({
1037
1121
  security: [{ name: "Authorization", type: "apiKey" }],
1038
1122
  url: "/public/v1/posts/{id}/release-id",
@@ -1042,6 +1126,24 @@ var setPostReleaseId = (options) => (options.client ?? client).put({
1042
1126
  ...options.headers
1043
1127
  }
1044
1128
  });
1129
+ var setPostCover = (options) => (options.client ?? client).put({
1130
+ security: [{ name: "Authorization", type: "apiKey" }],
1131
+ url: "/public/v1/posts/{id}/cover",
1132
+ ...options,
1133
+ headers: {
1134
+ "Content-Type": "application/json",
1135
+ ...options.headers
1136
+ }
1137
+ });
1138
+ var setPostPublication = (options) => (options.client ?? client).put({
1139
+ security: [{ name: "Authorization", type: "apiKey" }],
1140
+ url: "/public/v1/posts/{id}/publication",
1141
+ ...options,
1142
+ headers: {
1143
+ "Content-Type": "application/json",
1144
+ ...options.headers
1145
+ }
1146
+ });
1045
1147
  var getChannelAnalytics = (options) => (options.client ?? client).get({
1046
1148
  security: [{ name: "Authorization", type: "apiKey" }],
1047
1149
  url: "/public/v1/analytics/{integration}",
@@ -1062,6 +1164,11 @@ var getVideoHistory = (options) => (options.client ?? client).get({
1062
1164
  url: "/public/v1/video-history/{integrationId}",
1063
1165
  ...options
1064
1166
  });
1167
+ var getViewsPerDay = (options) => (options.client ?? client).get({
1168
+ security: [{ name: "Authorization", type: "apiKey" }],
1169
+ url: "/public/v1/views-per-day/{integrationId}",
1170
+ ...options
1171
+ });
1065
1172
  var getPostAnalytics = (options) => (options.client ?? client).get({
1066
1173
  security: [{ name: "Authorization", type: "apiKey" }],
1067
1174
  url: "/public/v1/analytics/post/{postId}",
@@ -1217,7 +1324,7 @@ var poseAutomationFlow = (options) => (options.client ?? client).post({
1217
1324
  ...options.headers
1218
1325
  }
1219
1326
  });
1220
- var listChannelAutomationPoses = (options) => (options.client ?? client).get({
1327
+ var listChannelAutomationPoses = (options) => (options?.client ?? client).get({
1221
1328
  security: [{ name: "Authorization", type: "apiKey" }],
1222
1329
  url: "/public/v1/automation-flow-poses",
1223
1330
  ...options
@@ -1260,6 +1367,16 @@ var replayAutomationPose = (options) => (options.client ?? client).post({
1260
1367
  ...options.headers
1261
1368
  }
1262
1369
  });
1370
+ var previewAutomationPoseCatchUp = (options) => (options.client ?? client).get({
1371
+ security: [{ name: "Authorization", type: "apiKey" }],
1372
+ url: "/public/v1/automation-flow-poses/{id}/catch-up",
1373
+ ...options
1374
+ });
1375
+ var catchUpAutomationPose = (options) => (options.client ?? client).post({
1376
+ security: [{ name: "Authorization", type: "apiKey" }],
1377
+ url: "/public/v1/automation-flow-poses/{id}/catch-up",
1378
+ ...options
1379
+ });
1263
1380
  var listWebhookEvents = (options) => (options?.client ?? client).get({
1264
1381
  security: [{ name: "Authorization", type: "apiKey" }],
1265
1382
  url: "/public/v1/webhooks/events",
@@ -1289,6 +1406,15 @@ var getWebhook = (options) => (options.client ?? client).get({
1289
1406
  url: "/public/v1/webhooks/{id}",
1290
1407
  ...options
1291
1408
  });
1409
+ var testWebhook = (options) => (options.client ?? client).post({
1410
+ security: [{ name: "Authorization", type: "apiKey" }],
1411
+ url: "/public/v1/webhooks/{id}/test",
1412
+ ...options,
1413
+ headers: {
1414
+ "Content-Type": "application/json",
1415
+ ...options.headers
1416
+ }
1417
+ });
1292
1418
  var listContents = (options) => (options?.client ?? client).get({
1293
1419
  security: [{ name: "Authorization", type: "apiKey" }],
1294
1420
  url: "/public/v1/contents",
@@ -1336,6 +1462,15 @@ var upsertContent = (options) => (options.client ?? client).post({
1336
1462
  ...options.headers
1337
1463
  }
1338
1464
  });
1465
+ var createContentFromLink = (options) => (options.client ?? client).post({
1466
+ security: [{ name: "Authorization", type: "apiKey" }],
1467
+ url: "/public/v1/contents/from-link",
1468
+ ...options,
1469
+ headers: {
1470
+ "Content-Type": "application/json",
1471
+ ...options.headers
1472
+ }
1473
+ });
1339
1474
  var moveContent = (options) => (options.client ?? client).put({
1340
1475
  security: [{ name: "Authorization", type: "apiKey" }],
1341
1476
  url: "/public/v1/contents/{id}/move",
@@ -1369,24 +1504,6 @@ var listHookFamilies = (options) => (options?.client ?? client).get({
1369
1504
  url: "/public/v1/hooks/families",
1370
1505
  ...options
1371
1506
  });
1372
- var replaceHookFamilies = (options) => (options.client ?? client).put({
1373
- security: [{ name: "Authorization", type: "apiKey" }],
1374
- url: "/public/v1/hooks/families",
1375
- ...options,
1376
- headers: {
1377
- "Content-Type": "application/json",
1378
- ...options.headers
1379
- }
1380
- });
1381
- var importHooks = (options) => (options.client ?? client).post({
1382
- security: [{ name: "Authorization", type: "apiKey" }],
1383
- url: "/public/v1/hooks/import",
1384
- ...options,
1385
- headers: {
1386
- "Content-Type": "application/json",
1387
- ...options.headers
1388
- }
1389
- });
1390
1507
  var listMusic = (options) => (options?.client ?? client).get({
1391
1508
  security: [{ name: "Authorization", type: "apiKey" }],
1392
1509
  url: "/public/v1/music",
@@ -1421,6 +1538,20 @@ var uploadMusicFile = (options) => (options.client ?? client).put({
1421
1538
  ...options.headers
1422
1539
  }
1423
1540
  });
1541
+ var deleteWorkspace = (options) => (options.client ?? client).delete({
1542
+ security: [{ name: "Authorization", type: "apiKey" }],
1543
+ url: "/public/v1/workspaces/{id}",
1544
+ ...options
1545
+ });
1546
+ var updateWorkspace = (options) => (options.client ?? client).put({
1547
+ security: [{ name: "Authorization", type: "apiKey" }],
1548
+ url: "/public/v1/workspaces/{id}",
1549
+ ...options,
1550
+ headers: {
1551
+ "Content-Type": "application/json",
1552
+ ...options.headers
1553
+ }
1554
+ });
1424
1555
 
1425
1556
  // src/index.ts
1426
1557
  var DEFAULT_BASE_URL = "https://app.usenadia.com/api";
@@ -1443,22 +1574,60 @@ var NadiaError = class _NadiaError extends Error {
1443
1574
  }
1444
1575
  };
1445
1576
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1577
+ var DIRECT_TYPES = {
1578
+ mp4: "video/mp4",
1579
+ jpg: "image/jpeg",
1580
+ jpeg: "image/jpeg",
1581
+ png: "image/png",
1582
+ webp: "image/webp",
1583
+ gif: "image/gif"
1584
+ };
1585
+ var PARALLEL_PARTS = 4;
1586
+ var PART_TRIES = 3;
1587
+ var UPLOAD_ROUNDS = 4;
1588
+ function directType(name, blobType) {
1589
+ const ext = /\.([a-z0-9]+)$/i.exec(name)?.[1]?.toLowerCase();
1590
+ if (ext && DIRECT_TYPES[ext]) return DIRECT_TYPES[ext];
1591
+ return Object.values(DIRECT_TYPES).includes(blobType) ? blobType : void 0;
1592
+ }
1593
+ var uploadContentType = (filename) => directType(filename, "");
1594
+ async function openSource(file, filename) {
1595
+ if (typeof file !== "string") {
1596
+ const own = file.name;
1597
+ return { blob: file, name: filename ?? (own || "upload") };
1598
+ }
1599
+ const fs = await import("fs");
1600
+ const { basename } = await import("path");
1601
+ const blob = typeof fs.openAsBlob === "function" ? await fs.openAsBlob(file) : new Blob([await fs.promises.readFile(file)]);
1602
+ return { blob, name: filename ?? basename(file) };
1603
+ }
1604
+ var range = (from, to) => Array.from({ length: to - from + 1 }, (_, i) => from + i);
1605
+ function missingParts(body) {
1606
+ const list = body?.missingParts;
1607
+ return Array.isArray(list) ? list.filter((n) => Number.isInteger(n) && n > 0) : [];
1608
+ }
1446
1609
  var messageOf = (body) => {
1447
- if (body && typeof body === "object" && "msg" in body) {
1448
- const msg = body.msg;
1449
- if (typeof msg === "string") return msg;
1610
+ if (!body || typeof body !== "object") return void 0;
1611
+ const { msg, message } = body;
1612
+ if (typeof msg === "string") return msg;
1613
+ if (typeof message === "string") return message;
1614
+ if (Array.isArray(message) && message.length && message.every((m) => typeof m === "string")) {
1615
+ return message.join("; ");
1450
1616
  }
1451
1617
  return void 0;
1452
1618
  };
1453
1619
  var Nadia = class {
1454
1620
  client;
1455
1621
  retries;
1622
+ /** For the bytes of an upload, which go to the upload host, not the API. */
1623
+ fetchImpl;
1456
1624
  constructor(apiKey, options = {}) {
1457
1625
  if (!apiKey) {
1458
1626
  throw new Error("An API key is required.");
1459
1627
  }
1460
1628
  const opts = typeof options === "string" ? { baseUrl: options } : options;
1461
1629
  this.retries = opts.retries ?? 2;
1630
+ this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1462
1631
  this.client = client;
1463
1632
  this.client.setConfig({
1464
1633
  baseUrl: opts.baseUrl ?? DEFAULT_BASE_URL,
@@ -1504,10 +1673,68 @@ var Nadia = class {
1504
1673
  (o) => listPosts({ ...o, query })
1505
1674
  );
1506
1675
  }
1507
- /** Upload a file and get a URL usable in a post. */
1508
- upload(file, filename = "upload") {
1676
+ /**
1677
+ * Upload a file and get a URL usable in a post: a path on disk, or a
1678
+ * `Blob` / `File` with its name.
1679
+ *
1680
+ * An MP4, JPEG, PNG, WebP or GIF goes in parts of 20 MB straight to the
1681
+ * storage (`POST /public/v1/uploads`, then its confirm): four parts at a
1682
+ * time, a part cut by the network sent again, and the parts the confirm
1683
+ * says are missing sent again before confirming once more. A server that
1684
+ * does not take parts answers a single PUT, which is done instead; a
1685
+ * server without that route (older, or self-hosted on an older release)
1686
+ * and any other type (HEIC, MOV… which the server converts) go through
1687
+ * `POST /public/v1/upload`, the whole file in one request, as before.
1688
+ *
1689
+ * Resolves to the media: `id` and `path` always (what a post takes),
1690
+ * plus `size`, `duration`, `width`, `height`, `codec` when it went in
1691
+ * parts or in a single PUT.
1692
+ */
1693
+ async upload(file, filename) {
1694
+ const source = await openSource(file, filename);
1695
+ const type = directType(source.name, source.blob.type);
1696
+ if (!type) return this.uploadWhole(source);
1697
+ let opened;
1698
+ try {
1699
+ opened = await this.call(
1700
+ (o) => createUpload({
1701
+ ...o,
1702
+ body: { filename: source.name, contentType: type, size: source.blob.size }
1703
+ })
1704
+ );
1705
+ } catch (err) {
1706
+ if (err instanceof NadiaError && (err.status === 404 || err.status === 405)) {
1707
+ return this.uploadWhole(source);
1708
+ }
1709
+ throw err;
1710
+ }
1711
+ if (opened.parts) {
1712
+ await this.sendParts(opened, source.blob, range(1, opened.parts));
1713
+ } else {
1714
+ await this.sendWhole(opened, source.blob);
1715
+ }
1716
+ for (let round = 1; ; round++) {
1717
+ try {
1718
+ return await this.call(
1719
+ (o) => confirmUpload({ ...o, path: { uploadId: opened.uploadId } })
1720
+ );
1721
+ } catch (err) {
1722
+ if (!(err instanceof NadiaError) || err.status !== 409 || round >= UPLOAD_ROUNDS) throw err;
1723
+ const missing = missingParts(err.body);
1724
+ if (missing.length && opened.parts) {
1725
+ await this.sendParts(opened, source.blob, missing);
1726
+ } else if (/^CONFIRM_IN_PROGRESS/.test(err.message)) {
1727
+ await sleep(3e3);
1728
+ } else {
1729
+ throw err;
1730
+ }
1731
+ }
1732
+ }
1733
+ }
1734
+ /** The whole file in one multipart request: `POST /public/v1/upload`. */
1735
+ uploadWhole(source) {
1509
1736
  const form = new FormData();
1510
- form.append("file", file, filename);
1737
+ form.append("file", source.blob, source.name);
1511
1738
  return this.call(
1512
1739
  (o) => uploadFile({
1513
1740
  ...o,
@@ -1516,6 +1743,58 @@ var Nadia = class {
1516
1743
  })
1517
1744
  );
1518
1745
  }
1746
+ /** Each part to `<uploadUrl>/<n>`, a few at a time, each retried. */
1747
+ async sendParts(opened, blob, numbers) {
1748
+ const queue = [...numbers];
1749
+ const size = opened.partSize;
1750
+ const worker = async () => {
1751
+ for (let n = queue.shift(); n !== void 0; n = queue.shift()) {
1752
+ const from = (n - 1) * size;
1753
+ await this.put(`${opened.uploadUrl}/${n}`, blob.slice(from, Math.min(from + size, blob.size)), {
1754
+ "Content-Type": "application/octet-stream"
1755
+ }, 200);
1756
+ }
1757
+ };
1758
+ await Promise.all(Array.from({ length: Math.min(PARALLEL_PARTS, queue.length) }, worker));
1759
+ }
1760
+ /** The single PUT of a server that takes no parts. */
1761
+ sendWhole(opened, blob) {
1762
+ const headers = Object.fromEntries(
1763
+ Object.entries(opened.headers ?? {}).filter(([k]) => k.toLowerCase() !== "content-length")
1764
+ );
1765
+ return this.put(opened.uploadUrl, blob, headers, 201);
1766
+ }
1767
+ /**
1768
+ * A PUT of raw bytes, without the API key (the URL is the credential):
1769
+ * three tries on a network error, a 5xx or a 429, none on anything else.
1770
+ */
1771
+ async put(url, body, headers, ok) {
1772
+ let last;
1773
+ for (let attempt = 1; attempt <= PART_TRIES; attempt++) {
1774
+ let response;
1775
+ try {
1776
+ response = await this.fetchImpl(url, { method: "PUT", headers, body });
1777
+ } catch (err) {
1778
+ last = new NadiaError(0, err, `The upload of ${url.replace(/\/v2\/[^/]+/, "/v2/\u2026")} did not complete.`);
1779
+ }
1780
+ if (response) {
1781
+ if (response.status === ok || response.ok) {
1782
+ await response.body?.cancel();
1783
+ return;
1784
+ }
1785
+ const text = await response.text();
1786
+ let parsed = text;
1787
+ try {
1788
+ parsed = JSON.parse(text);
1789
+ } catch {
1790
+ }
1791
+ last = new NadiaError(response.status, parsed, messageOf(parsed) ?? `The upload answered ${response.status}`);
1792
+ if (!last.isTransient) throw last;
1793
+ }
1794
+ if (attempt < PART_TRIES) await sleep(attempt * 1e3);
1795
+ }
1796
+ throw last;
1797
+ }
1519
1798
  /** List your connected channels. */
1520
1799
  integrations() {
1521
1800
  return this.call((o) => listChannels(o));
@@ -1643,18 +1922,82 @@ var Nadia = class {
1643
1922
  }
1644
1923
  };
1645
1924
  var index_default = Nadia;
1925
+ var NadiaWebhookError = class _NadiaWebhookError extends Error {
1926
+ reason;
1927
+ constructor(reason, message) {
1928
+ super(message);
1929
+ this.name = "NadiaWebhookError";
1930
+ this.reason = reason;
1931
+ Object.setPrototypeOf(this, _NadiaWebhookError.prototype);
1932
+ }
1933
+ };
1934
+ function checkSignature(o) {
1935
+ const { secret, signature, timestamp, body } = o;
1936
+ if (!signature || timestamp === null || timestamp === void 0 || timestamp === "") {
1937
+ return { ok: false, reason: "missing_header", message: "X-Nadia-Signature or X-Nadia-Timestamp is missing." };
1938
+ }
1939
+ const ts = String(timestamp);
1940
+ if (o.tolerance !== void 0) {
1941
+ const seconds = Number(ts);
1942
+ const now = (o.now ?? Date.now()) / 1e3;
1943
+ if (!Number.isFinite(seconds) || Math.abs(now - seconds) > o.tolerance) {
1944
+ return { ok: false, reason: "stale", message: `Timestamp outside the ${o.tolerance}s tolerance.` };
1945
+ }
1946
+ }
1947
+ const expected = Buffer.from(
1948
+ "sha256=" + (0, import_node_crypto.createHmac)("sha256", secret).update(`${ts}.`).update(body).digest("hex")
1949
+ );
1950
+ const received = Buffer.from(signature);
1951
+ if (received.length !== expected.length || !(0, import_node_crypto.timingSafeEqual)(received, expected)) {
1952
+ return { ok: false, reason: "bad_signature", message: "Signature does not match." };
1953
+ }
1954
+ return { ok: true };
1955
+ }
1956
+ function verifyWebhookSignature(options) {
1957
+ return checkSignature(options).ok;
1958
+ }
1959
+ function parseWebhook(options) {
1960
+ const header = (name) => {
1961
+ const h = options.headers;
1962
+ if (typeof h.get === "function") return h.get(name) ?? void 0;
1963
+ const record = h;
1964
+ const key = Object.keys(record).find((k) => k.toLowerCase() === name.toLowerCase());
1965
+ const value = key === void 0 ? void 0 : record[key];
1966
+ return Array.isArray(value) ? value[0] : value;
1967
+ };
1968
+ const check = checkSignature({
1969
+ secret: options.secret,
1970
+ signature: header("X-Nadia-Signature"),
1971
+ timestamp: header("X-Nadia-Timestamp"),
1972
+ body: options.body,
1973
+ tolerance: options.tolerance ?? 300,
1974
+ now: options.now
1975
+ });
1976
+ if (!check.ok) throw new NadiaWebhookError(check.reason, check.message);
1977
+ try {
1978
+ const text = typeof options.body === "string" ? options.body : new TextDecoder().decode(options.body);
1979
+ return JSON.parse(text);
1980
+ } catch {
1981
+ throw new NadiaWebhookError("bad_body", "The body is not JSON.");
1982
+ }
1983
+ }
1646
1984
  // Annotate the CommonJS export names for ESM import in node:
1647
1985
  0 && (module.exports = {
1648
1986
  DEFAULT_BASE_URL,
1649
1987
  Nadia,
1650
1988
  NadiaError,
1651
- callVideoFunction,
1989
+ NadiaWebhookError,
1990
+ catchUpAutomationPose,
1652
1991
  changePostStatus,
1653
1992
  checkConnection,
1993
+ confirmUpload,
1654
1994
  createAutomationFlow,
1655
1995
  createContent,
1996
+ createContentFromLink,
1656
1997
  createPost,
1998
+ createUpload,
1657
1999
  createWebhook,
2000
+ createWorkspace,
1658
2001
  deleteAutomationFlow,
1659
2002
  deleteAutomationPose,
1660
2003
  deleteChannel,
@@ -1663,8 +2006,8 @@ var index_default = Nadia;
1663
2006
  deletePost,
1664
2007
  deletePostGroup,
1665
2008
  deleteWebhook,
2009
+ deleteWorkspace,
1666
2010
  findSlot,
1667
- generateVideo,
1668
2011
  getAccountHistory,
1669
2012
  getAutomationFlow,
1670
2013
  getAutomationPose,
@@ -1675,11 +2018,15 @@ var index_default = Nadia;
1675
2018
  getConversation,
1676
2019
  getHookForms,
1677
2020
  getMusicTrack,
2021
+ getPost,
1678
2022
  getPostAnalytics,
2023
+ getPostAutomation,
2024
+ getPostingTimes,
2025
+ getSubscription,
1679
2026
  getVideoHistory,
2027
+ getViewsPerDay,
1680
2028
  getWebhook,
1681
2029
  hideComment,
1682
- importHooks,
1683
2030
  importMusic,
1684
2031
  linkContentPost,
1685
2032
  listAutomationFlowPoses,
@@ -1702,12 +2049,15 @@ var index_default = Nadia;
1702
2049
  listVideoPerformance,
1703
2050
  listWebhookEvents,
1704
2051
  listWebhooks,
2052
+ listWorkspaces,
1705
2053
  moveContent,
2054
+ parseWebhook,
1706
2055
  pollAutomationPose,
1707
2056
  pollInbox,
1708
2057
  poseAutomationFlow,
1709
2058
  previewAutomationEmail,
1710
- replaceHookFamilies,
2059
+ previewAutomationPoseCatchUp,
2060
+ removePostAutomation,
1711
2061
  replayAutomationPose,
1712
2062
  replyToComment,
1713
2063
  runChannelTool,
@@ -1715,15 +2065,23 @@ var index_default = Nadia;
1715
2065
  sendAutomationTestEmail,
1716
2066
  sendDirectMessage,
1717
2067
  setAutomationFlowStatus,
2068
+ setPostAutomation,
2069
+ setPostCover,
2070
+ setPostPublication,
1718
2071
  setPostReleaseId,
2072
+ setPostingTimes,
1719
2073
  startChannelConnection,
1720
2074
  subscribeInboxWebhook,
2075
+ testWebhook,
1721
2076
  updateAutomationFlow,
1722
2077
  updateAutomationPose,
1723
2078
  updateContent,
2079
+ updateWorkspace,
2080
+ uploadContentType,
1724
2081
  uploadFile,
1725
2082
  uploadFromUrl,
1726
2083
  uploadMusicFile,
1727
- upsertContent
2084
+ upsertContent,
2085
+ verifyWebhookSignature
1728
2086
  });
1729
2087
  //# sourceMappingURL=index.js.map