@usenadia/node 0.2.0 → 0.4.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,6 +32,7 @@ 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,
37
38
  catchUpAutomationPose: () => catchUpAutomationPose,
@@ -71,6 +72,7 @@ __export(index_exports, {
71
72
  getPostAutomation: () => getPostAutomation,
72
73
  getPostingTimes: () => getPostingTimes,
73
74
  getSubscription: () => getSubscription,
75
+ getUploadFromUrl: () => getUploadFromUrl,
74
76
  getVideoHistory: () => getVideoHistory,
75
77
  getViewsPerDay: () => getViewsPerDay,
76
78
  getWebhook: () => getWebhook,
@@ -113,6 +115,8 @@ __export(index_exports, {
113
115
  sendAutomationTestEmail: () => sendAutomationTestEmail,
114
116
  sendDirectMessage: () => sendDirectMessage,
115
117
  setAutomationFlowStatus: () => setAutomationFlowStatus,
118
+ setContentWorkspaces: () => setContentWorkspaces,
119
+ setMediaWorkspaces: () => setMediaWorkspaces,
116
120
  setPostAutomation: () => setPostAutomation,
117
121
  setPostCover: () => setPostCover,
118
122
  setPostPublication: () => setPostPublication,
@@ -968,6 +972,11 @@ var uploadFromUrl = (options) => (options.client ?? client).post({
968
972
  ...options.headers
969
973
  }
970
974
  });
975
+ var getUploadFromUrl = (options) => (options.client ?? client).get({
976
+ security: [{ name: "Authorization", type: "apiKey" }],
977
+ url: "/public/v1/upload-from-url/{importId}",
978
+ ...options
979
+ });
971
980
  var createUpload = (options) => (options.client ?? client).post({
972
981
  security: [{ name: "Authorization", type: "apiKey" }],
973
982
  url: "/public/v1/uploads",
@@ -982,6 +991,15 @@ var confirmUpload = (options) => (options.client ?? client).post({
982
991
  url: "/public/v1/uploads/{uploadId}/confirm",
983
992
  ...options
984
993
  });
994
+ var setMediaWorkspaces = (options) => (options.client ?? client).put({
995
+ security: [{ name: "Authorization", type: "apiKey" }],
996
+ url: "/public/v1/media/{id}/workspaces",
997
+ ...options,
998
+ headers: {
999
+ "Content-Type": "application/json",
1000
+ ...options.headers
1001
+ }
1002
+ });
985
1003
  var findSlot = (options) => (options.client ?? client).get({
986
1004
  security: [{ name: "Authorization", type: "apiKey" }],
987
1005
  url: "/public/v1/find-slot/{id}",
@@ -1489,6 +1507,15 @@ var linkContentPost = (options) => (options.client ?? client).put({
1489
1507
  ...options.headers
1490
1508
  }
1491
1509
  });
1510
+ var setContentWorkspaces = (options) => (options.client ?? client).put({
1511
+ security: [{ name: "Authorization", type: "apiKey" }],
1512
+ url: "/public/v1/contents/{id}/workspaces",
1513
+ ...options,
1514
+ headers: {
1515
+ "Content-Type": "application/json",
1516
+ ...options.headers
1517
+ }
1518
+ });
1492
1519
  var searchHooks = (options) => (options?.client ?? client).get({
1493
1520
  security: [{ name: "Authorization", type: "apiKey" }],
1494
1521
  url: "/public/v1/hooks",
@@ -1573,7 +1600,31 @@ var NadiaError = class _NadiaError extends Error {
1573
1600
  return this.status === 0 || this.status === 429 || this.status >= 500;
1574
1601
  }
1575
1602
  };
1603
+ var NadiaConfirmationRequired = class _NadiaConfirmationRequired extends NadiaError {
1604
+ preview;
1605
+ confirmationToken;
1606
+ replay;
1607
+ constructor(body, replay) {
1608
+ const b = body ?? {};
1609
+ super(
1610
+ 428,
1611
+ body,
1612
+ typeof b.errors === "string" ? b.errors : "This action waits for a yes: nothing was done."
1613
+ );
1614
+ this.name = "NadiaConfirmationRequired";
1615
+ this.preview = b.preview && typeof b.preview === "object" ? b.preview : {};
1616
+ this.confirmationToken = typeof b.confirmationToken === "string" ? b.confirmationToken : "";
1617
+ this.replay = replay;
1618
+ Object.setPrototypeOf(this, _NadiaConfirmationRequired.prototype);
1619
+ }
1620
+ /** Sends the same request again with the token: the action runs. Resolves to its answer. */
1621
+ confirm() {
1622
+ return this.replay(this.confirmationToken);
1623
+ }
1624
+ };
1625
+ var isConfirmation = (status, body) => status === 428 && !!body && typeof body === "object" && body.needsConfirmation === true;
1576
1626
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1627
+ var isUrlImport = (v) => !!v && typeof v === "object" && typeof v.id === "string" && typeof v.status === "string" && !("path" in v);
1577
1628
  var DIRECT_TYPES = {
1578
1629
  mp4: "video/mp4",
1579
1630
  jpg: "image/jpeg",
@@ -1621,6 +1672,8 @@ var Nadia = class {
1621
1672
  retries;
1622
1673
  /** For the bytes of an upload, which go to the upload host, not the API. */
1623
1674
  fetchImpl;
1675
+ importPoll;
1676
+ autoConfirm;
1624
1677
  constructor(apiKey, options = {}) {
1625
1678
  if (!apiKey) {
1626
1679
  throw new Error("An API key is required.");
@@ -1628,6 +1681,11 @@ var Nadia = class {
1628
1681
  const opts = typeof options === "string" ? { baseUrl: options } : options;
1629
1682
  this.retries = opts.retries ?? 2;
1630
1683
  this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1684
+ this.autoConfirm = opts.autoConfirm ?? false;
1685
+ this.importPoll = {
1686
+ intervalMs: opts.importPoll?.intervalMs ?? 3e3,
1687
+ timeoutMs: opts.importPoll?.timeoutMs ?? 15 * 60 * 1e3
1688
+ };
1631
1689
  this.client = client;
1632
1690
  this.client.setConfig({
1633
1691
  baseUrl: opts.baseUrl ?? DEFAULT_BASE_URL,
@@ -1640,14 +1698,22 @@ var Nadia = class {
1640
1698
  * retries the transient ones. Every method below goes through here — that is
1641
1699
  * the whole point of the facade.
1642
1700
  */
1643
- async call(operation) {
1701
+ async call(operation, confirmationToken) {
1644
1702
  let lastError;
1703
+ const options = confirmationToken ? { client: this.client, fetch: this.withToken(confirmationToken) } : { client: this.client };
1645
1704
  for (let attempt = 0; attempt <= this.retries; attempt++) {
1646
- const { data, error, response } = await operation({ client: this.client });
1705
+ const { data, error, response } = await operation(options);
1647
1706
  if (!response) {
1648
1707
  lastError = new NadiaError(0, error, "The request did not complete.");
1649
1708
  } else if (response.ok) {
1650
1709
  return data;
1710
+ } else if (isConfirmation(response.status, error)) {
1711
+ const replay = (token) => this.call(operation, token);
1712
+ const required = new NadiaConfirmationRequired(error, replay);
1713
+ if (this.autoConfirm && !confirmationToken && required.confirmationToken) {
1714
+ return replay(required.confirmationToken);
1715
+ }
1716
+ throw required;
1651
1717
  } else {
1652
1718
  lastError = new NadiaError(response.status, error, messageOf(error));
1653
1719
  }
@@ -1658,6 +1724,18 @@ var Nadia = class {
1658
1724
  }
1659
1725
  throw lastError;
1660
1726
  }
1727
+ /** A fetch that sends each request with `?confirmationToken=<token>`. */
1728
+ withToken(token) {
1729
+ return (async (input, init) => {
1730
+ const req = input instanceof Request ? input : new Request(input, init);
1731
+ const url = new URL(req.url);
1732
+ url.searchParams.set("confirmationToken", token);
1733
+ const body = req.body ? await req.arrayBuffer() : void 0;
1734
+ return this.fetchImpl(
1735
+ new Request(url, { method: req.method, headers: req.headers, body, signal: req.signal })
1736
+ );
1737
+ });
1738
+ }
1661
1739
  /* --------------------------------------------------------------- *
1662
1740
  * The five calls upstream had, same names, same shape
1663
1741
  * --------------------------------------------------------------- */
@@ -1683,8 +1761,9 @@ var Nadia = class {
1683
1761
  * says are missing sent again before confirming once more. A server that
1684
1762
  * does not take parts answers a single PUT, which is done instead; a
1685
1763
  * 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.
1764
+ * and any other type (HEIC… which the server converts to JPEG) go through
1765
+ * `POST /public/v1/upload`, the whole file in one request, as before. A
1766
+ * MOV or an M4V is refused there (`415`): convert it to MP4 first.
1688
1767
  *
1689
1768
  * Resolves to the media: `id` and `path` always (what a post takes),
1690
1769
  * plus `size`, `duration`, `width`, `height`, `codec` when it went in
@@ -1731,6 +1810,47 @@ var Nadia = class {
1731
1810
  }
1732
1811
  }
1733
1812
  }
1813
+ /**
1814
+ * Import a file from a public HTTPS URL into the media library
1815
+ * (`POST /public/v1/upload-from-url`): the path of the URL ends in `.png`,
1816
+ * `.jpg`, `.jpeg`, `.gif`, `.webp` or `.mp4`.
1817
+ *
1818
+ * A long import (not done within 25 s) is answered `202` by the server and
1819
+ * goes on there; it is read again here (`GET
1820
+ * /public/v1/upload-from-url/{importId}`) until it ends. Resolves to the
1821
+ * media either way; a failed import throws the `NadiaError` the server
1822
+ * would have answered at once (its status and `msg`).
1823
+ */
1824
+ async uploadFromUrl(url) {
1825
+ const first = await this.call((o) => uploadFromUrl({ ...o, body: { url } }));
1826
+ return this.importEnd(first);
1827
+ }
1828
+ /**
1829
+ * The end of an import from a URL: the media at once (`201`), or an import
1830
+ * (`202`) read again every few seconds until it is done or failed.
1831
+ */
1832
+ async importEnd(answer) {
1833
+ if (!isUrlImport(answer)) return answer;
1834
+ const deadline = Date.now() + this.importPoll.timeoutMs;
1835
+ let current = answer;
1836
+ while (current.status === "running") {
1837
+ if (Date.now() >= deadline) {
1838
+ throw new NadiaError(
1839
+ 504,
1840
+ current,
1841
+ `The import from a URL ${current.id} did not end within ${Math.round(this.importPoll.timeoutMs / 1e3)} s: read it again with getUploadFromUrl.`
1842
+ );
1843
+ }
1844
+ await sleep(this.importPoll.intervalMs);
1845
+ const id = current.id;
1846
+ current = await this.call(
1847
+ (o) => getUploadFromUrl({ ...o, path: { importId: id } })
1848
+ );
1849
+ }
1850
+ if (current.status === "done" && current.media) return current.media;
1851
+ const error = current.error ?? { statusCode: 500, msg: "The import from a URL failed." };
1852
+ throw new NadiaError(error.statusCode, { msg: error.msg }, error.msg);
1853
+ }
1734
1854
  /** The whole file in one multipart request: `POST /public/v1/upload`. */
1735
1855
  uploadWhole(source) {
1736
1856
  const form = new FormData();
@@ -1799,10 +1919,15 @@ var Nadia = class {
1799
1919
  integrations() {
1800
1920
  return this.call((o) => listChannels(o));
1801
1921
  }
1802
- /** Delete a post and everything published with it. */
1803
- deletePost(id) {
1922
+ /**
1923
+ * Delete a post and everything published with it. Waits for a yes by
1924
+ * default: throws {@link NadiaConfirmationRequired} unless `autoConfirm`,
1925
+ * or unless the token of that 428 is passed here.
1926
+ */
1927
+ deletePost(id, options = {}) {
1804
1928
  return this.call(
1805
- (o) => deletePost({ ...o, path: { id } })
1929
+ (o) => deletePost({ ...o, path: { id } }),
1930
+ options.confirmationToken
1806
1931
  );
1807
1932
  }
1808
1933
  /* --------------------------------------------------------------- *
@@ -1889,9 +2014,9 @@ var Nadia = class {
1889
2014
  (o) => linkContentPost({ ...o, path: { id }, body: { postGroup } })
1890
2015
  );
1891
2016
  }
1892
- /** Delete a card. */
1893
- deleteContent(id) {
1894
- return this.call((o) => deleteContent({ ...o, path: { id } }));
2017
+ /** Delete a card. Waits for a yes by default, like {@link Nadia.deletePost}. */
2018
+ deleteContent(id, options = {}) {
2019
+ return this.call((o) => deleteContent({ ...o, path: { id } }), options.confirmationToken);
1895
2020
  }
1896
2021
  /** The numbers of the published cards, keyed by card id. */
1897
2022
  contentsPerformance() {
@@ -1985,6 +2110,7 @@ function parseWebhook(options) {
1985
2110
  0 && (module.exports = {
1986
2111
  DEFAULT_BASE_URL,
1987
2112
  Nadia,
2113
+ NadiaConfirmationRequired,
1988
2114
  NadiaError,
1989
2115
  NadiaWebhookError,
1990
2116
  catchUpAutomationPose,
@@ -2023,6 +2149,7 @@ function parseWebhook(options) {
2023
2149
  getPostAutomation,
2024
2150
  getPostingTimes,
2025
2151
  getSubscription,
2152
+ getUploadFromUrl,
2026
2153
  getVideoHistory,
2027
2154
  getViewsPerDay,
2028
2155
  getWebhook,
@@ -2065,6 +2192,8 @@ function parseWebhook(options) {
2065
2192
  sendAutomationTestEmail,
2066
2193
  sendDirectMessage,
2067
2194
  setAutomationFlowStatus,
2195
+ setContentWorkspaces,
2196
+ setMediaWorkspaces,
2068
2197
  setPostAutomation,
2069
2198
  setPostCover,
2070
2199
  setPostPublication,