@usenadia/node 0.3.0 → 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.js CHANGED
@@ -32,12 +32,16 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  DEFAULT_BASE_URL: () => DEFAULT_BASE_URL,
34
34
  Nadia: () => Nadia,
35
+ NadiaConfirmationRequired: () => NadiaConfirmationRequired,
35
36
  NadiaError: () => NadiaError,
36
37
  NadiaWebhookError: () => NadiaWebhookError,
38
+ approvePost: () => approvePost,
37
39
  catchUpAutomationPose: () => catchUpAutomationPose,
38
40
  changePostStatus: () => changePostStatus,
39
41
  checkConnection: () => checkConnection,
42
+ commentPublication: () => commentPublication,
40
43
  confirmUpload: () => confirmUpload,
44
+ createApiKey: () => createApiKey,
41
45
  createAutomationFlow: () => createAutomationFlow,
42
46
  createContent: () => createContent,
43
47
  createContentFromLink: () => createContentFromLink,
@@ -78,6 +82,7 @@ __export(index_exports, {
78
82
  hideComment: () => hideComment,
79
83
  importMusic: () => importMusic,
80
84
  linkContentPost: () => linkContentPost,
85
+ listApiKeys: () => listApiKeys,
81
86
  listAutomationFlowPoses: () => listAutomationFlowPoses,
82
87
  listAutomationFlows: () => listAutomationFlows,
83
88
  listAutomationPoseRuns: () => listAutomationPoseRuns,
@@ -95,6 +100,8 @@ __export(index_exports, {
95
100
  listMusicPillars: () => listMusicPillars,
96
101
  listNotifications: () => listNotifications,
97
102
  listPosts: () => listPosts,
103
+ listPostsAwaitingApproval: () => listPostsAwaitingApproval,
104
+ listUndatedDrafts: () => listUndatedDrafts,
98
105
  listVideoPerformance: () => listVideoPerformance,
99
106
  listWebhookEvents: () => listWebhookEvents,
100
107
  listWebhooks: () => listWebhooks,
@@ -106,9 +113,12 @@ __export(index_exports, {
106
113
  poseAutomationFlow: () => poseAutomationFlow,
107
114
  previewAutomationEmail: () => previewAutomationEmail,
108
115
  previewAutomationPoseCatchUp: () => previewAutomationPoseCatchUp,
116
+ refusePost: () => refusePost,
109
117
  removePostAutomation: () => removePostAutomation,
110
118
  replayAutomationPose: () => replayAutomationPose,
111
119
  replyToComment: () => replyToComment,
120
+ revokeApiKey: () => revokeApiKey,
121
+ rotateApiKey: () => rotateApiKey,
112
122
  runChannelTool: () => runChannelTool,
113
123
  searchHooks: () => searchHooks,
114
124
  sendAutomationTestEmail: () => sendAutomationTestEmail,
@@ -120,10 +130,12 @@ __export(index_exports, {
120
130
  setPostCover: () => setPostCover,
121
131
  setPostPublication: () => setPostPublication,
122
132
  setPostReleaseId: () => setPostReleaseId,
133
+ setPostThread: () => setPostThread,
123
134
  setPostingTimes: () => setPostingTimes,
124
135
  startChannelConnection: () => startChannelConnection,
125
136
  subscribeInboxWebhook: () => subscribeInboxWebhook,
126
137
  testWebhook: () => testWebhook,
138
+ updateApiKey: () => updateApiKey,
127
139
  updateAutomationFlow: () => updateAutomationFlow,
128
140
  updateAutomationPose: () => updateAutomationPose,
129
141
  updateContent: () => updateContent,
@@ -1115,6 +1127,34 @@ var changePostStatus = (options) => (options.client ?? client).put({
1115
1127
  ...options.headers
1116
1128
  }
1117
1129
  });
1130
+ var listPostsAwaitingApproval = (options) => (options?.client ?? client).get({
1131
+ security: [{ name: "Authorization", type: "apiKey" }],
1132
+ url: "/public/v1/posts/approvals",
1133
+ ...options
1134
+ });
1135
+ var listUndatedDrafts = (options) => (options?.client ?? client).get({
1136
+ security: [{ name: "Authorization", type: "apiKey" }],
1137
+ url: "/public/v1/posts/drafts",
1138
+ ...options
1139
+ });
1140
+ var approvePost = (options) => (options.client ?? client).post({
1141
+ security: [{ name: "Authorization", type: "apiKey" }],
1142
+ url: "/public/v1/posts/{id}/approve",
1143
+ ...options,
1144
+ headers: {
1145
+ "Content-Type": "application/json",
1146
+ ...options.headers
1147
+ }
1148
+ });
1149
+ var refusePost = (options) => (options.client ?? client).post({
1150
+ security: [{ name: "Authorization", type: "apiKey" }],
1151
+ url: "/public/v1/posts/{id}/refuse",
1152
+ ...options,
1153
+ headers: {
1154
+ "Content-Type": "application/json",
1155
+ ...options.headers
1156
+ }
1157
+ });
1118
1158
  var removePostAutomation = (options) => (options.client ?? client).delete({
1119
1159
  security: [{ name: "Authorization", type: "apiKey" }],
1120
1160
  url: "/public/v1/posts/{id}/automation",
@@ -1152,6 +1192,15 @@ var setPostCover = (options) => (options.client ?? client).put({
1152
1192
  ...options.headers
1153
1193
  }
1154
1194
  });
1195
+ var setPostThread = (options) => (options.client ?? client).put({
1196
+ security: [{ name: "Authorization", type: "apiKey" }],
1197
+ url: "/public/v1/posts/{id}/thread",
1198
+ ...options,
1199
+ headers: {
1200
+ "Content-Type": "application/json",
1201
+ ...options.headers
1202
+ }
1203
+ });
1155
1204
  var setPostPublication = (options) => (options.client ?? client).put({
1156
1205
  security: [{ name: "Authorization", type: "apiKey" }],
1157
1206
  url: "/public/v1/posts/{id}/publication",
@@ -1215,6 +1264,15 @@ var listMediaComments = (options) => (options.client ?? client).get({
1215
1264
  url: "/public/v1/inbox/comments/{integrationId}/{mediaId}",
1216
1265
  ...options
1217
1266
  });
1267
+ var commentPublication = (options) => (options.client ?? client).post({
1268
+ security: [{ name: "Authorization", type: "apiKey" }],
1269
+ url: "/public/v1/inbox/comments/{integrationId}/{mediaId}",
1270
+ ...options,
1271
+ headers: {
1272
+ "Content-Type": "application/json",
1273
+ ...options.headers
1274
+ }
1275
+ });
1218
1276
  var replyToComment = (options) => (options.client ?? client).post({
1219
1277
  security: [{ name: "Authorization", type: "apiKey" }],
1220
1278
  url: "/public/v1/inbox/comments/{commentId}/reply",
@@ -1578,6 +1636,39 @@ var updateWorkspace = (options) => (options.client ?? client).put({
1578
1636
  ...options.headers
1579
1637
  }
1580
1638
  });
1639
+ var listApiKeys = (options) => (options?.client ?? client).get({
1640
+ security: [{ name: "Authorization", type: "apiKey" }],
1641
+ url: "/public/v1/api-keys",
1642
+ ...options
1643
+ });
1644
+ var createApiKey = (options) => (options.client ?? client).post({
1645
+ security: [{ name: "Authorization", type: "apiKey" }],
1646
+ url: "/public/v1/api-keys",
1647
+ ...options,
1648
+ headers: {
1649
+ "Content-Type": "application/json",
1650
+ ...options.headers
1651
+ }
1652
+ });
1653
+ var revokeApiKey = (options) => (options.client ?? client).delete({
1654
+ security: [{ name: "Authorization", type: "apiKey" }],
1655
+ url: "/public/v1/api-keys/{id}",
1656
+ ...options
1657
+ });
1658
+ var updateApiKey = (options) => (options.client ?? client).put({
1659
+ security: [{ name: "Authorization", type: "apiKey" }],
1660
+ url: "/public/v1/api-keys/{id}",
1661
+ ...options,
1662
+ headers: {
1663
+ "Content-Type": "application/json",
1664
+ ...options.headers
1665
+ }
1666
+ });
1667
+ var rotateApiKey = (options) => (options.client ?? client).post({
1668
+ security: [{ name: "Authorization", type: "apiKey" }],
1669
+ url: "/public/v1/api-keys/{id}/rotate",
1670
+ ...options
1671
+ });
1581
1672
 
1582
1673
  // src/index.ts
1583
1674
  var DEFAULT_BASE_URL = "https://app.usenadia.com/api";
@@ -1599,6 +1690,29 @@ var NadiaError = class _NadiaError extends Error {
1599
1690
  return this.status === 0 || this.status === 429 || this.status >= 500;
1600
1691
  }
1601
1692
  };
1693
+ var NadiaConfirmationRequired = class _NadiaConfirmationRequired extends NadiaError {
1694
+ preview;
1695
+ confirmationToken;
1696
+ replay;
1697
+ constructor(body, replay) {
1698
+ const b = body ?? {};
1699
+ super(
1700
+ 428,
1701
+ body,
1702
+ typeof b.errors === "string" ? b.errors : "This action waits for a yes: nothing was done."
1703
+ );
1704
+ this.name = "NadiaConfirmationRequired";
1705
+ this.preview = b.preview && typeof b.preview === "object" ? b.preview : {};
1706
+ this.confirmationToken = typeof b.confirmationToken === "string" ? b.confirmationToken : "";
1707
+ this.replay = replay;
1708
+ Object.setPrototypeOf(this, _NadiaConfirmationRequired.prototype);
1709
+ }
1710
+ /** Sends the same request again with the token: the action runs. Resolves to its answer. */
1711
+ confirm() {
1712
+ return this.replay(this.confirmationToken);
1713
+ }
1714
+ };
1715
+ var isConfirmation = (status, body) => status === 428 && !!body && typeof body === "object" && body.needsConfirmation === true;
1602
1716
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1603
1717
  var isUrlImport = (v) => !!v && typeof v === "object" && typeof v.id === "string" && typeof v.status === "string" && !("path" in v);
1604
1718
  var DIRECT_TYPES = {
@@ -1649,6 +1763,7 @@ var Nadia = class {
1649
1763
  /** For the bytes of an upload, which go to the upload host, not the API. */
1650
1764
  fetchImpl;
1651
1765
  importPoll;
1766
+ autoConfirm;
1652
1767
  constructor(apiKey, options = {}) {
1653
1768
  if (!apiKey) {
1654
1769
  throw new Error("An API key is required.");
@@ -1656,6 +1771,7 @@ var Nadia = class {
1656
1771
  const opts = typeof options === "string" ? { baseUrl: options } : options;
1657
1772
  this.retries = opts.retries ?? 2;
1658
1773
  this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1774
+ this.autoConfirm = opts.autoConfirm ?? false;
1659
1775
  this.importPoll = {
1660
1776
  intervalMs: opts.importPoll?.intervalMs ?? 3e3,
1661
1777
  timeoutMs: opts.importPoll?.timeoutMs ?? 15 * 60 * 1e3
@@ -1672,14 +1788,22 @@ var Nadia = class {
1672
1788
  * retries the transient ones. Every method below goes through here — that is
1673
1789
  * the whole point of the facade.
1674
1790
  */
1675
- async call(operation) {
1791
+ async call(operation, confirmationToken) {
1676
1792
  let lastError;
1793
+ const options = confirmationToken ? { client: this.client, fetch: this.withToken(confirmationToken) } : { client: this.client };
1677
1794
  for (let attempt = 0; attempt <= this.retries; attempt++) {
1678
- const { data, error, response } = await operation({ client: this.client });
1795
+ const { data, error, response } = await operation(options);
1679
1796
  if (!response) {
1680
1797
  lastError = new NadiaError(0, error, "The request did not complete.");
1681
1798
  } else if (response.ok) {
1682
1799
  return data;
1800
+ } else if (isConfirmation(response.status, error)) {
1801
+ const replay = (token) => this.call(operation, token);
1802
+ const required = new NadiaConfirmationRequired(error, replay);
1803
+ if (this.autoConfirm && !confirmationToken && required.confirmationToken) {
1804
+ return replay(required.confirmationToken);
1805
+ }
1806
+ throw required;
1683
1807
  } else {
1684
1808
  lastError = new NadiaError(response.status, error, messageOf(error));
1685
1809
  }
@@ -1690,8 +1814,20 @@ var Nadia = class {
1690
1814
  }
1691
1815
  throw lastError;
1692
1816
  }
1817
+ /** A fetch that sends each request with `?confirmationToken=<token>`. */
1818
+ withToken(token) {
1819
+ return (async (input, init) => {
1820
+ const req = input instanceof Request ? input : new Request(input, init);
1821
+ const url = new URL(req.url);
1822
+ url.searchParams.set("confirmationToken", token);
1823
+ const body = req.body ? await req.arrayBuffer() : void 0;
1824
+ return this.fetchImpl(
1825
+ new Request(url, { method: req.method, headers: req.headers, body, signal: req.signal })
1826
+ );
1827
+ });
1828
+ }
1693
1829
  /* --------------------------------------------------------------- *
1694
- * The five calls upstream had, same names, same shape
1830
+ * The five basic calls
1695
1831
  * --------------------------------------------------------------- */
1696
1832
  /** Schedule, publish or draft a post on one or more channels. */
1697
1833
  post(body) {
@@ -1714,8 +1850,8 @@ var Nadia = class {
1714
1850
  * time, a part cut by the network sent again, and the parts the confirm
1715
1851
  * says are missing sent again before confirming once more. A server that
1716
1852
  * does not take parts answers a single PUT, which is done instead; a
1717
- * server without that route (older, or self-hosted on an older release)
1718
- * and any other type (HEIC… which the server converts to JPEG) go through
1853
+ * server without that route (an older release) and any other type
1854
+ * (HEIC… which the server converts to JPEG) go through
1719
1855
  * `POST /public/v1/upload`, the whole file in one request, as before. A
1720
1856
  * MOV or an M4V is refused there (`415`): convert it to MP4 first.
1721
1857
  *
@@ -1873,14 +2009,19 @@ var Nadia = class {
1873
2009
  integrations() {
1874
2010
  return this.call((o) => listChannels(o));
1875
2011
  }
1876
- /** Delete a post and everything published with it. */
1877
- deletePost(id) {
2012
+ /**
2013
+ * Delete a post and everything published with it. Waits for a yes by
2014
+ * default: throws {@link NadiaConfirmationRequired} unless `autoConfirm`,
2015
+ * or unless the token of that 428 is passed here.
2016
+ */
2017
+ deletePost(id, options = {}) {
1878
2018
  return this.call(
1879
- (o) => deletePost({ ...o, path: { id } })
2019
+ (o) => deletePost({ ...o, path: { id } }),
2020
+ options.confirmationToken
1880
2021
  );
1881
2022
  }
1882
2023
  /* --------------------------------------------------------------- *
1883
- * The rest of our surface, which upstream never had
2024
+ * The rest of the surface
1884
2025
  * --------------------------------------------------------------- */
1885
2026
  /** Check that the API key is valid. Throws `NadiaError` with status 401 if not. */
1886
2027
  async isConnected() {
@@ -1963,9 +2104,9 @@ var Nadia = class {
1963
2104
  (o) => linkContentPost({ ...o, path: { id }, body: { postGroup } })
1964
2105
  );
1965
2106
  }
1966
- /** Delete a card. */
1967
- deleteContent(id) {
1968
- return this.call((o) => deleteContent({ ...o, path: { id } }));
2107
+ /** Delete a card. Waits for a yes by default, like {@link Nadia.deletePost}. */
2108
+ deleteContent(id, options = {}) {
2109
+ return this.call((o) => deleteContent({ ...o, path: { id } }), options.confirmationToken);
1969
2110
  }
1970
2111
  /** The numbers of the published cards, keyed by card id. */
1971
2112
  contentsPerformance() {
@@ -2059,12 +2200,16 @@ function parseWebhook(options) {
2059
2200
  0 && (module.exports = {
2060
2201
  DEFAULT_BASE_URL,
2061
2202
  Nadia,
2203
+ NadiaConfirmationRequired,
2062
2204
  NadiaError,
2063
2205
  NadiaWebhookError,
2206
+ approvePost,
2064
2207
  catchUpAutomationPose,
2065
2208
  changePostStatus,
2066
2209
  checkConnection,
2210
+ commentPublication,
2067
2211
  confirmUpload,
2212
+ createApiKey,
2068
2213
  createAutomationFlow,
2069
2214
  createContent,
2070
2215
  createContentFromLink,
@@ -2104,6 +2249,7 @@ function parseWebhook(options) {
2104
2249
  hideComment,
2105
2250
  importMusic,
2106
2251
  linkContentPost,
2252
+ listApiKeys,
2107
2253
  listAutomationFlowPoses,
2108
2254
  listAutomationFlows,
2109
2255
  listAutomationPoseRuns,
@@ -2121,6 +2267,8 @@ function parseWebhook(options) {
2121
2267
  listMusicPillars,
2122
2268
  listNotifications,
2123
2269
  listPosts,
2270
+ listPostsAwaitingApproval,
2271
+ listUndatedDrafts,
2124
2272
  listVideoPerformance,
2125
2273
  listWebhookEvents,
2126
2274
  listWebhooks,
@@ -2132,9 +2280,12 @@ function parseWebhook(options) {
2132
2280
  poseAutomationFlow,
2133
2281
  previewAutomationEmail,
2134
2282
  previewAutomationPoseCatchUp,
2283
+ refusePost,
2135
2284
  removePostAutomation,
2136
2285
  replayAutomationPose,
2137
2286
  replyToComment,
2287
+ revokeApiKey,
2288
+ rotateApiKey,
2138
2289
  runChannelTool,
2139
2290
  searchHooks,
2140
2291
  sendAutomationTestEmail,
@@ -2146,10 +2297,12 @@ function parseWebhook(options) {
2146
2297
  setPostCover,
2147
2298
  setPostPublication,
2148
2299
  setPostReleaseId,
2300
+ setPostThread,
2149
2301
  setPostingTimes,
2150
2302
  startChannelConnection,
2151
2303
  subscribeInboxWebhook,
2152
2304
  testWebhook,
2305
+ updateApiKey,
2153
2306
  updateAutomationFlow,
2154
2307
  updateAutomationPose,
2155
2308
  updateContent,