@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.mjs CHANGED
@@ -978,6 +978,34 @@ var changePostStatus = (options) => (options.client ?? client).put({
978
978
  ...options.headers
979
979
  }
980
980
  });
981
+ var listPostsAwaitingApproval = (options) => (options?.client ?? client).get({
982
+ security: [{ name: "Authorization", type: "apiKey" }],
983
+ url: "/public/v1/posts/approvals",
984
+ ...options
985
+ });
986
+ var listUndatedDrafts = (options) => (options?.client ?? client).get({
987
+ security: [{ name: "Authorization", type: "apiKey" }],
988
+ url: "/public/v1/posts/drafts",
989
+ ...options
990
+ });
991
+ var approvePost = (options) => (options.client ?? client).post({
992
+ security: [{ name: "Authorization", type: "apiKey" }],
993
+ url: "/public/v1/posts/{id}/approve",
994
+ ...options,
995
+ headers: {
996
+ "Content-Type": "application/json",
997
+ ...options.headers
998
+ }
999
+ });
1000
+ var refusePost = (options) => (options.client ?? client).post({
1001
+ security: [{ name: "Authorization", type: "apiKey" }],
1002
+ url: "/public/v1/posts/{id}/refuse",
1003
+ ...options,
1004
+ headers: {
1005
+ "Content-Type": "application/json",
1006
+ ...options.headers
1007
+ }
1008
+ });
981
1009
  var removePostAutomation = (options) => (options.client ?? client).delete({
982
1010
  security: [{ name: "Authorization", type: "apiKey" }],
983
1011
  url: "/public/v1/posts/{id}/automation",
@@ -1015,6 +1043,15 @@ var setPostCover = (options) => (options.client ?? client).put({
1015
1043
  ...options.headers
1016
1044
  }
1017
1045
  });
1046
+ var setPostThread = (options) => (options.client ?? client).put({
1047
+ security: [{ name: "Authorization", type: "apiKey" }],
1048
+ url: "/public/v1/posts/{id}/thread",
1049
+ ...options,
1050
+ headers: {
1051
+ "Content-Type": "application/json",
1052
+ ...options.headers
1053
+ }
1054
+ });
1018
1055
  var setPostPublication = (options) => (options.client ?? client).put({
1019
1056
  security: [{ name: "Authorization", type: "apiKey" }],
1020
1057
  url: "/public/v1/posts/{id}/publication",
@@ -1078,6 +1115,15 @@ var listMediaComments = (options) => (options.client ?? client).get({
1078
1115
  url: "/public/v1/inbox/comments/{integrationId}/{mediaId}",
1079
1116
  ...options
1080
1117
  });
1118
+ var commentPublication = (options) => (options.client ?? client).post({
1119
+ security: [{ name: "Authorization", type: "apiKey" }],
1120
+ url: "/public/v1/inbox/comments/{integrationId}/{mediaId}",
1121
+ ...options,
1122
+ headers: {
1123
+ "Content-Type": "application/json",
1124
+ ...options.headers
1125
+ }
1126
+ });
1081
1127
  var replyToComment = (options) => (options.client ?? client).post({
1082
1128
  security: [{ name: "Authorization", type: "apiKey" }],
1083
1129
  url: "/public/v1/inbox/comments/{commentId}/reply",
@@ -1441,6 +1487,39 @@ var updateWorkspace = (options) => (options.client ?? client).put({
1441
1487
  ...options.headers
1442
1488
  }
1443
1489
  });
1490
+ var listApiKeys = (options) => (options?.client ?? client).get({
1491
+ security: [{ name: "Authorization", type: "apiKey" }],
1492
+ url: "/public/v1/api-keys",
1493
+ ...options
1494
+ });
1495
+ var createApiKey = (options) => (options.client ?? client).post({
1496
+ security: [{ name: "Authorization", type: "apiKey" }],
1497
+ url: "/public/v1/api-keys",
1498
+ ...options,
1499
+ headers: {
1500
+ "Content-Type": "application/json",
1501
+ ...options.headers
1502
+ }
1503
+ });
1504
+ var revokeApiKey = (options) => (options.client ?? client).delete({
1505
+ security: [{ name: "Authorization", type: "apiKey" }],
1506
+ url: "/public/v1/api-keys/{id}",
1507
+ ...options
1508
+ });
1509
+ var updateApiKey = (options) => (options.client ?? client).put({
1510
+ security: [{ name: "Authorization", type: "apiKey" }],
1511
+ url: "/public/v1/api-keys/{id}",
1512
+ ...options,
1513
+ headers: {
1514
+ "Content-Type": "application/json",
1515
+ ...options.headers
1516
+ }
1517
+ });
1518
+ var rotateApiKey = (options) => (options.client ?? client).post({
1519
+ security: [{ name: "Authorization", type: "apiKey" }],
1520
+ url: "/public/v1/api-keys/{id}/rotate",
1521
+ ...options
1522
+ });
1444
1523
 
1445
1524
  // src/index.ts
1446
1525
  var DEFAULT_BASE_URL = "https://app.usenadia.com/api";
@@ -1462,6 +1541,29 @@ var NadiaError = class _NadiaError extends Error {
1462
1541
  return this.status === 0 || this.status === 429 || this.status >= 500;
1463
1542
  }
1464
1543
  };
1544
+ var NadiaConfirmationRequired = class _NadiaConfirmationRequired extends NadiaError {
1545
+ preview;
1546
+ confirmationToken;
1547
+ replay;
1548
+ constructor(body, replay) {
1549
+ const b = body ?? {};
1550
+ super(
1551
+ 428,
1552
+ body,
1553
+ typeof b.errors === "string" ? b.errors : "This action waits for a yes: nothing was done."
1554
+ );
1555
+ this.name = "NadiaConfirmationRequired";
1556
+ this.preview = b.preview && typeof b.preview === "object" ? b.preview : {};
1557
+ this.confirmationToken = typeof b.confirmationToken === "string" ? b.confirmationToken : "";
1558
+ this.replay = replay;
1559
+ Object.setPrototypeOf(this, _NadiaConfirmationRequired.prototype);
1560
+ }
1561
+ /** Sends the same request again with the token: the action runs. Resolves to its answer. */
1562
+ confirm() {
1563
+ return this.replay(this.confirmationToken);
1564
+ }
1565
+ };
1566
+ var isConfirmation = (status, body) => status === 428 && !!body && typeof body === "object" && body.needsConfirmation === true;
1465
1567
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1466
1568
  var isUrlImport = (v) => !!v && typeof v === "object" && typeof v.id === "string" && typeof v.status === "string" && !("path" in v);
1467
1569
  var DIRECT_TYPES = {
@@ -1512,6 +1614,7 @@ var Nadia = class {
1512
1614
  /** For the bytes of an upload, which go to the upload host, not the API. */
1513
1615
  fetchImpl;
1514
1616
  importPoll;
1617
+ autoConfirm;
1515
1618
  constructor(apiKey, options = {}) {
1516
1619
  if (!apiKey) {
1517
1620
  throw new Error("An API key is required.");
@@ -1519,6 +1622,7 @@ var Nadia = class {
1519
1622
  const opts = typeof options === "string" ? { baseUrl: options } : options;
1520
1623
  this.retries = opts.retries ?? 2;
1521
1624
  this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1625
+ this.autoConfirm = opts.autoConfirm ?? false;
1522
1626
  this.importPoll = {
1523
1627
  intervalMs: opts.importPoll?.intervalMs ?? 3e3,
1524
1628
  timeoutMs: opts.importPoll?.timeoutMs ?? 15 * 60 * 1e3
@@ -1535,14 +1639,22 @@ var Nadia = class {
1535
1639
  * retries the transient ones. Every method below goes through here — that is
1536
1640
  * the whole point of the facade.
1537
1641
  */
1538
- async call(operation) {
1642
+ async call(operation, confirmationToken) {
1539
1643
  let lastError;
1644
+ const options = confirmationToken ? { client: this.client, fetch: this.withToken(confirmationToken) } : { client: this.client };
1540
1645
  for (let attempt = 0; attempt <= this.retries; attempt++) {
1541
- const { data, error, response } = await operation({ client: this.client });
1646
+ const { data, error, response } = await operation(options);
1542
1647
  if (!response) {
1543
1648
  lastError = new NadiaError(0, error, "The request did not complete.");
1544
1649
  } else if (response.ok) {
1545
1650
  return data;
1651
+ } else if (isConfirmation(response.status, error)) {
1652
+ const replay = (token) => this.call(operation, token);
1653
+ const required = new NadiaConfirmationRequired(error, replay);
1654
+ if (this.autoConfirm && !confirmationToken && required.confirmationToken) {
1655
+ return replay(required.confirmationToken);
1656
+ }
1657
+ throw required;
1546
1658
  } else {
1547
1659
  lastError = new NadiaError(response.status, error, messageOf(error));
1548
1660
  }
@@ -1553,8 +1665,20 @@ var Nadia = class {
1553
1665
  }
1554
1666
  throw lastError;
1555
1667
  }
1668
+ /** A fetch that sends each request with `?confirmationToken=<token>`. */
1669
+ withToken(token) {
1670
+ return (async (input, init) => {
1671
+ const req = input instanceof Request ? input : new Request(input, init);
1672
+ const url = new URL(req.url);
1673
+ url.searchParams.set("confirmationToken", token);
1674
+ const body = req.body ? await req.arrayBuffer() : void 0;
1675
+ return this.fetchImpl(
1676
+ new Request(url, { method: req.method, headers: req.headers, body, signal: req.signal })
1677
+ );
1678
+ });
1679
+ }
1556
1680
  /* --------------------------------------------------------------- *
1557
- * The five calls upstream had, same names, same shape
1681
+ * The five basic calls
1558
1682
  * --------------------------------------------------------------- */
1559
1683
  /** Schedule, publish or draft a post on one or more channels. */
1560
1684
  post(body) {
@@ -1577,8 +1701,8 @@ var Nadia = class {
1577
1701
  * time, a part cut by the network sent again, and the parts the confirm
1578
1702
  * says are missing sent again before confirming once more. A server that
1579
1703
  * does not take parts answers a single PUT, which is done instead; a
1580
- * server without that route (older, or self-hosted on an older release)
1581
- * and any other type (HEIC… which the server converts to JPEG) go through
1704
+ * server without that route (an older release) and any other type
1705
+ * (HEIC… which the server converts to JPEG) go through
1582
1706
  * `POST /public/v1/upload`, the whole file in one request, as before. A
1583
1707
  * MOV or an M4V is refused there (`415`): convert it to MP4 first.
1584
1708
  *
@@ -1736,14 +1860,19 @@ var Nadia = class {
1736
1860
  integrations() {
1737
1861
  return this.call((o) => listChannels(o));
1738
1862
  }
1739
- /** Delete a post and everything published with it. */
1740
- deletePost(id) {
1863
+ /**
1864
+ * Delete a post and everything published with it. Waits for a yes by
1865
+ * default: throws {@link NadiaConfirmationRequired} unless `autoConfirm`,
1866
+ * or unless the token of that 428 is passed here.
1867
+ */
1868
+ deletePost(id, options = {}) {
1741
1869
  return this.call(
1742
- (o) => deletePost({ ...o, path: { id } })
1870
+ (o) => deletePost({ ...o, path: { id } }),
1871
+ options.confirmationToken
1743
1872
  );
1744
1873
  }
1745
1874
  /* --------------------------------------------------------------- *
1746
- * The rest of our surface, which upstream never had
1875
+ * The rest of the surface
1747
1876
  * --------------------------------------------------------------- */
1748
1877
  /** Check that the API key is valid. Throws `NadiaError` with status 401 if not. */
1749
1878
  async isConnected() {
@@ -1826,9 +1955,9 @@ var Nadia = class {
1826
1955
  (o) => linkContentPost({ ...o, path: { id }, body: { postGroup } })
1827
1956
  );
1828
1957
  }
1829
- /** Delete a card. */
1830
- deleteContent(id) {
1831
- return this.call((o) => deleteContent({ ...o, path: { id } }));
1958
+ /** Delete a card. Waits for a yes by default, like {@link Nadia.deletePost}. */
1959
+ deleteContent(id, options = {}) {
1960
+ return this.call((o) => deleteContent({ ...o, path: { id } }), options.confirmationToken);
1832
1961
  }
1833
1962
  /** The numbers of the published cards, keyed by card id. */
1834
1963
  contentsPerformance() {
@@ -1921,12 +2050,16 @@ function parseWebhook(options) {
1921
2050
  export {
1922
2051
  DEFAULT_BASE_URL,
1923
2052
  Nadia,
2053
+ NadiaConfirmationRequired,
1924
2054
  NadiaError,
1925
2055
  NadiaWebhookError,
2056
+ approvePost,
1926
2057
  catchUpAutomationPose,
1927
2058
  changePostStatus,
1928
2059
  checkConnection,
2060
+ commentPublication,
1929
2061
  confirmUpload,
2062
+ createApiKey,
1930
2063
  createAutomationFlow,
1931
2064
  createContent,
1932
2065
  createContentFromLink,
@@ -1967,6 +2100,7 @@ export {
1967
2100
  hideComment,
1968
2101
  importMusic,
1969
2102
  linkContentPost,
2103
+ listApiKeys,
1970
2104
  listAutomationFlowPoses,
1971
2105
  listAutomationFlows,
1972
2106
  listAutomationPoseRuns,
@@ -1984,6 +2118,8 @@ export {
1984
2118
  listMusicPillars,
1985
2119
  listNotifications,
1986
2120
  listPosts,
2121
+ listPostsAwaitingApproval,
2122
+ listUndatedDrafts,
1987
2123
  listVideoPerformance,
1988
2124
  listWebhookEvents,
1989
2125
  listWebhooks,
@@ -1995,9 +2131,12 @@ export {
1995
2131
  poseAutomationFlow,
1996
2132
  previewAutomationEmail,
1997
2133
  previewAutomationPoseCatchUp,
2134
+ refusePost,
1998
2135
  removePostAutomation,
1999
2136
  replayAutomationPose,
2000
2137
  replyToComment,
2138
+ revokeApiKey,
2139
+ rotateApiKey,
2001
2140
  runChannelTool,
2002
2141
  searchHooks,
2003
2142
  sendAutomationTestEmail,
@@ -2009,10 +2148,12 @@ export {
2009
2148
  setPostCover,
2010
2149
  setPostPublication,
2011
2150
  setPostReleaseId,
2151
+ setPostThread,
2012
2152
  setPostingTimes,
2013
2153
  startChannelConnection,
2014
2154
  subscribeInboxWebhook,
2015
2155
  testWebhook,
2156
+ updateApiKey,
2016
2157
  updateAutomationFlow,
2017
2158
  updateAutomationPose,
2018
2159
  updateContent,