@usenadia/node 0.2.0 → 0.3.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
@@ -834,6 +834,11 @@ var uploadFromUrl = (options) => (options.client ?? client).post({
834
834
  ...options.headers
835
835
  }
836
836
  });
837
+ var getUploadFromUrl = (options) => (options.client ?? client).get({
838
+ security: [{ name: "Authorization", type: "apiKey" }],
839
+ url: "/public/v1/upload-from-url/{importId}",
840
+ ...options
841
+ });
837
842
  var createUpload = (options) => (options.client ?? client).post({
838
843
  security: [{ name: "Authorization", type: "apiKey" }],
839
844
  url: "/public/v1/uploads",
@@ -848,6 +853,15 @@ var confirmUpload = (options) => (options.client ?? client).post({
848
853
  url: "/public/v1/uploads/{uploadId}/confirm",
849
854
  ...options
850
855
  });
856
+ var setMediaWorkspaces = (options) => (options.client ?? client).put({
857
+ security: [{ name: "Authorization", type: "apiKey" }],
858
+ url: "/public/v1/media/{id}/workspaces",
859
+ ...options,
860
+ headers: {
861
+ "Content-Type": "application/json",
862
+ ...options.headers
863
+ }
864
+ });
851
865
  var findSlot = (options) => (options.client ?? client).get({
852
866
  security: [{ name: "Authorization", type: "apiKey" }],
853
867
  url: "/public/v1/find-slot/{id}",
@@ -1355,6 +1369,15 @@ var linkContentPost = (options) => (options.client ?? client).put({
1355
1369
  ...options.headers
1356
1370
  }
1357
1371
  });
1372
+ var setContentWorkspaces = (options) => (options.client ?? client).put({
1373
+ security: [{ name: "Authorization", type: "apiKey" }],
1374
+ url: "/public/v1/contents/{id}/workspaces",
1375
+ ...options,
1376
+ headers: {
1377
+ "Content-Type": "application/json",
1378
+ ...options.headers
1379
+ }
1380
+ });
1358
1381
  var searchHooks = (options) => (options?.client ?? client).get({
1359
1382
  security: [{ name: "Authorization", type: "apiKey" }],
1360
1383
  url: "/public/v1/hooks",
@@ -1440,6 +1463,7 @@ var NadiaError = class _NadiaError extends Error {
1440
1463
  }
1441
1464
  };
1442
1465
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1466
+ var isUrlImport = (v) => !!v && typeof v === "object" && typeof v.id === "string" && typeof v.status === "string" && !("path" in v);
1443
1467
  var DIRECT_TYPES = {
1444
1468
  mp4: "video/mp4",
1445
1469
  jpg: "image/jpeg",
@@ -1487,6 +1511,7 @@ var Nadia = class {
1487
1511
  retries;
1488
1512
  /** For the bytes of an upload, which go to the upload host, not the API. */
1489
1513
  fetchImpl;
1514
+ importPoll;
1490
1515
  constructor(apiKey, options = {}) {
1491
1516
  if (!apiKey) {
1492
1517
  throw new Error("An API key is required.");
@@ -1494,6 +1519,10 @@ var Nadia = class {
1494
1519
  const opts = typeof options === "string" ? { baseUrl: options } : options;
1495
1520
  this.retries = opts.retries ?? 2;
1496
1521
  this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1522
+ this.importPoll = {
1523
+ intervalMs: opts.importPoll?.intervalMs ?? 3e3,
1524
+ timeoutMs: opts.importPoll?.timeoutMs ?? 15 * 60 * 1e3
1525
+ };
1497
1526
  this.client = client;
1498
1527
  this.client.setConfig({
1499
1528
  baseUrl: opts.baseUrl ?? DEFAULT_BASE_URL,
@@ -1549,8 +1578,9 @@ var Nadia = class {
1549
1578
  * says are missing sent again before confirming once more. A server that
1550
1579
  * does not take parts answers a single PUT, which is done instead; a
1551
1580
  * server without that route (older, or self-hosted on an older release)
1552
- * and any other type (HEIC, MOV… which the server converts) go through
1553
- * `POST /public/v1/upload`, the whole file in one request, as before.
1581
+ * and any other type (HEIC… which the server converts to JPEG) go through
1582
+ * `POST /public/v1/upload`, the whole file in one request, as before. A
1583
+ * MOV or an M4V is refused there (`415`): convert it to MP4 first.
1554
1584
  *
1555
1585
  * Resolves to the media: `id` and `path` always (what a post takes),
1556
1586
  * plus `size`, `duration`, `width`, `height`, `codec` when it went in
@@ -1597,6 +1627,47 @@ var Nadia = class {
1597
1627
  }
1598
1628
  }
1599
1629
  }
1630
+ /**
1631
+ * Import a file from a public HTTPS URL into the media library
1632
+ * (`POST /public/v1/upload-from-url`): the path of the URL ends in `.png`,
1633
+ * `.jpg`, `.jpeg`, `.gif`, `.webp` or `.mp4`.
1634
+ *
1635
+ * A long import (not done within 25 s) is answered `202` by the server and
1636
+ * goes on there; it is read again here (`GET
1637
+ * /public/v1/upload-from-url/{importId}`) until it ends. Resolves to the
1638
+ * media either way; a failed import throws the `NadiaError` the server
1639
+ * would have answered at once (its status and `msg`).
1640
+ */
1641
+ async uploadFromUrl(url) {
1642
+ const first = await this.call((o) => uploadFromUrl({ ...o, body: { url } }));
1643
+ return this.importEnd(first);
1644
+ }
1645
+ /**
1646
+ * The end of an import from a URL: the media at once (`201`), or an import
1647
+ * (`202`) read again every few seconds until it is done or failed.
1648
+ */
1649
+ async importEnd(answer) {
1650
+ if (!isUrlImport(answer)) return answer;
1651
+ const deadline = Date.now() + this.importPoll.timeoutMs;
1652
+ let current = answer;
1653
+ while (current.status === "running") {
1654
+ if (Date.now() >= deadline) {
1655
+ throw new NadiaError(
1656
+ 504,
1657
+ current,
1658
+ `The import from a URL ${current.id} did not end within ${Math.round(this.importPoll.timeoutMs / 1e3)} s: read it again with getUploadFromUrl.`
1659
+ );
1660
+ }
1661
+ await sleep(this.importPoll.intervalMs);
1662
+ const id = current.id;
1663
+ current = await this.call(
1664
+ (o) => getUploadFromUrl({ ...o, path: { importId: id } })
1665
+ );
1666
+ }
1667
+ if (current.status === "done" && current.media) return current.media;
1668
+ const error = current.error ?? { statusCode: 500, msg: "The import from a URL failed." };
1669
+ throw new NadiaError(error.statusCode, { msg: error.msg }, error.msg);
1670
+ }
1600
1671
  /** The whole file in one multipart request: `POST /public/v1/upload`. */
1601
1672
  uploadWhole(source) {
1602
1673
  const form = new FormData();
@@ -1889,6 +1960,7 @@ export {
1889
1960
  getPostAutomation,
1890
1961
  getPostingTimes,
1891
1962
  getSubscription,
1963
+ getUploadFromUrl,
1892
1964
  getVideoHistory,
1893
1965
  getViewsPerDay,
1894
1966
  getWebhook,
@@ -1931,6 +2003,8 @@ export {
1931
2003
  sendAutomationTestEmail,
1932
2004
  sendDirectMessage,
1933
2005
  setAutomationFlowStatus,
2006
+ setContentWorkspaces,
2007
+ setMediaWorkspaces,
1934
2008
  setPostAutomation,
1935
2009
  setPostCover,
1936
2010
  setPostPublication,