@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/README.md +52 -7
- package/dist/index.d.mts +552 -29
- package/dist/index.d.ts +552 -29
- package/dist/index.js +139 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +135 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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",
|
|
@@ -1439,7 +1462,31 @@ var NadiaError = class _NadiaError extends Error {
|
|
|
1439
1462
|
return this.status === 0 || this.status === 429 || this.status >= 500;
|
|
1440
1463
|
}
|
|
1441
1464
|
};
|
|
1465
|
+
var NadiaConfirmationRequired = class _NadiaConfirmationRequired extends NadiaError {
|
|
1466
|
+
preview;
|
|
1467
|
+
confirmationToken;
|
|
1468
|
+
replay;
|
|
1469
|
+
constructor(body, replay) {
|
|
1470
|
+
const b = body ?? {};
|
|
1471
|
+
super(
|
|
1472
|
+
428,
|
|
1473
|
+
body,
|
|
1474
|
+
typeof b.errors === "string" ? b.errors : "This action waits for a yes: nothing was done."
|
|
1475
|
+
);
|
|
1476
|
+
this.name = "NadiaConfirmationRequired";
|
|
1477
|
+
this.preview = b.preview && typeof b.preview === "object" ? b.preview : {};
|
|
1478
|
+
this.confirmationToken = typeof b.confirmationToken === "string" ? b.confirmationToken : "";
|
|
1479
|
+
this.replay = replay;
|
|
1480
|
+
Object.setPrototypeOf(this, _NadiaConfirmationRequired.prototype);
|
|
1481
|
+
}
|
|
1482
|
+
/** Sends the same request again with the token: the action runs. Resolves to its answer. */
|
|
1483
|
+
confirm() {
|
|
1484
|
+
return this.replay(this.confirmationToken);
|
|
1485
|
+
}
|
|
1486
|
+
};
|
|
1487
|
+
var isConfirmation = (status, body) => status === 428 && !!body && typeof body === "object" && body.needsConfirmation === true;
|
|
1442
1488
|
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1489
|
+
var isUrlImport = (v) => !!v && typeof v === "object" && typeof v.id === "string" && typeof v.status === "string" && !("path" in v);
|
|
1443
1490
|
var DIRECT_TYPES = {
|
|
1444
1491
|
mp4: "video/mp4",
|
|
1445
1492
|
jpg: "image/jpeg",
|
|
@@ -1487,6 +1534,8 @@ var Nadia = class {
|
|
|
1487
1534
|
retries;
|
|
1488
1535
|
/** For the bytes of an upload, which go to the upload host, not the API. */
|
|
1489
1536
|
fetchImpl;
|
|
1537
|
+
importPoll;
|
|
1538
|
+
autoConfirm;
|
|
1490
1539
|
constructor(apiKey, options = {}) {
|
|
1491
1540
|
if (!apiKey) {
|
|
1492
1541
|
throw new Error("An API key is required.");
|
|
@@ -1494,6 +1543,11 @@ var Nadia = class {
|
|
|
1494
1543
|
const opts = typeof options === "string" ? { baseUrl: options } : options;
|
|
1495
1544
|
this.retries = opts.retries ?? 2;
|
|
1496
1545
|
this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
|
|
1546
|
+
this.autoConfirm = opts.autoConfirm ?? false;
|
|
1547
|
+
this.importPoll = {
|
|
1548
|
+
intervalMs: opts.importPoll?.intervalMs ?? 3e3,
|
|
1549
|
+
timeoutMs: opts.importPoll?.timeoutMs ?? 15 * 60 * 1e3
|
|
1550
|
+
};
|
|
1497
1551
|
this.client = client;
|
|
1498
1552
|
this.client.setConfig({
|
|
1499
1553
|
baseUrl: opts.baseUrl ?? DEFAULT_BASE_URL,
|
|
@@ -1506,14 +1560,22 @@ var Nadia = class {
|
|
|
1506
1560
|
* retries the transient ones. Every method below goes through here — that is
|
|
1507
1561
|
* the whole point of the facade.
|
|
1508
1562
|
*/
|
|
1509
|
-
async call(operation) {
|
|
1563
|
+
async call(operation, confirmationToken) {
|
|
1510
1564
|
let lastError;
|
|
1565
|
+
const options = confirmationToken ? { client: this.client, fetch: this.withToken(confirmationToken) } : { client: this.client };
|
|
1511
1566
|
for (let attempt = 0; attempt <= this.retries; attempt++) {
|
|
1512
|
-
const { data, error, response } = await operation(
|
|
1567
|
+
const { data, error, response } = await operation(options);
|
|
1513
1568
|
if (!response) {
|
|
1514
1569
|
lastError = new NadiaError(0, error, "The request did not complete.");
|
|
1515
1570
|
} else if (response.ok) {
|
|
1516
1571
|
return data;
|
|
1572
|
+
} else if (isConfirmation(response.status, error)) {
|
|
1573
|
+
const replay = (token) => this.call(operation, token);
|
|
1574
|
+
const required = new NadiaConfirmationRequired(error, replay);
|
|
1575
|
+
if (this.autoConfirm && !confirmationToken && required.confirmationToken) {
|
|
1576
|
+
return replay(required.confirmationToken);
|
|
1577
|
+
}
|
|
1578
|
+
throw required;
|
|
1517
1579
|
} else {
|
|
1518
1580
|
lastError = new NadiaError(response.status, error, messageOf(error));
|
|
1519
1581
|
}
|
|
@@ -1524,6 +1586,18 @@ var Nadia = class {
|
|
|
1524
1586
|
}
|
|
1525
1587
|
throw lastError;
|
|
1526
1588
|
}
|
|
1589
|
+
/** A fetch that sends each request with `?confirmationToken=<token>`. */
|
|
1590
|
+
withToken(token) {
|
|
1591
|
+
return (async (input, init) => {
|
|
1592
|
+
const req = input instanceof Request ? input : new Request(input, init);
|
|
1593
|
+
const url = new URL(req.url);
|
|
1594
|
+
url.searchParams.set("confirmationToken", token);
|
|
1595
|
+
const body = req.body ? await req.arrayBuffer() : void 0;
|
|
1596
|
+
return this.fetchImpl(
|
|
1597
|
+
new Request(url, { method: req.method, headers: req.headers, body, signal: req.signal })
|
|
1598
|
+
);
|
|
1599
|
+
});
|
|
1600
|
+
}
|
|
1527
1601
|
/* --------------------------------------------------------------- *
|
|
1528
1602
|
* The five calls upstream had, same names, same shape
|
|
1529
1603
|
* --------------------------------------------------------------- */
|
|
@@ -1549,8 +1623,9 @@ var Nadia = class {
|
|
|
1549
1623
|
* says are missing sent again before confirming once more. A server that
|
|
1550
1624
|
* does not take parts answers a single PUT, which is done instead; a
|
|
1551
1625
|
* server without that route (older, or self-hosted on an older release)
|
|
1552
|
-
* and any other type (HEIC
|
|
1553
|
-
* `POST /public/v1/upload`, the whole file in one request, as before.
|
|
1626
|
+
* and any other type (HEIC… which the server converts to JPEG) go through
|
|
1627
|
+
* `POST /public/v1/upload`, the whole file in one request, as before. A
|
|
1628
|
+
* MOV or an M4V is refused there (`415`): convert it to MP4 first.
|
|
1554
1629
|
*
|
|
1555
1630
|
* Resolves to the media: `id` and `path` always (what a post takes),
|
|
1556
1631
|
* plus `size`, `duration`, `width`, `height`, `codec` when it went in
|
|
@@ -1597,6 +1672,47 @@ var Nadia = class {
|
|
|
1597
1672
|
}
|
|
1598
1673
|
}
|
|
1599
1674
|
}
|
|
1675
|
+
/**
|
|
1676
|
+
* Import a file from a public HTTPS URL into the media library
|
|
1677
|
+
* (`POST /public/v1/upload-from-url`): the path of the URL ends in `.png`,
|
|
1678
|
+
* `.jpg`, `.jpeg`, `.gif`, `.webp` or `.mp4`.
|
|
1679
|
+
*
|
|
1680
|
+
* A long import (not done within 25 s) is answered `202` by the server and
|
|
1681
|
+
* goes on there; it is read again here (`GET
|
|
1682
|
+
* /public/v1/upload-from-url/{importId}`) until it ends. Resolves to the
|
|
1683
|
+
* media either way; a failed import throws the `NadiaError` the server
|
|
1684
|
+
* would have answered at once (its status and `msg`).
|
|
1685
|
+
*/
|
|
1686
|
+
async uploadFromUrl(url) {
|
|
1687
|
+
const first = await this.call((o) => uploadFromUrl({ ...o, body: { url } }));
|
|
1688
|
+
return this.importEnd(first);
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* The end of an import from a URL: the media at once (`201`), or an import
|
|
1692
|
+
* (`202`) read again every few seconds until it is done or failed.
|
|
1693
|
+
*/
|
|
1694
|
+
async importEnd(answer) {
|
|
1695
|
+
if (!isUrlImport(answer)) return answer;
|
|
1696
|
+
const deadline = Date.now() + this.importPoll.timeoutMs;
|
|
1697
|
+
let current = answer;
|
|
1698
|
+
while (current.status === "running") {
|
|
1699
|
+
if (Date.now() >= deadline) {
|
|
1700
|
+
throw new NadiaError(
|
|
1701
|
+
504,
|
|
1702
|
+
current,
|
|
1703
|
+
`The import from a URL ${current.id} did not end within ${Math.round(this.importPoll.timeoutMs / 1e3)} s: read it again with getUploadFromUrl.`
|
|
1704
|
+
);
|
|
1705
|
+
}
|
|
1706
|
+
await sleep(this.importPoll.intervalMs);
|
|
1707
|
+
const id = current.id;
|
|
1708
|
+
current = await this.call(
|
|
1709
|
+
(o) => getUploadFromUrl({ ...o, path: { importId: id } })
|
|
1710
|
+
);
|
|
1711
|
+
}
|
|
1712
|
+
if (current.status === "done" && current.media) return current.media;
|
|
1713
|
+
const error = current.error ?? { statusCode: 500, msg: "The import from a URL failed." };
|
|
1714
|
+
throw new NadiaError(error.statusCode, { msg: error.msg }, error.msg);
|
|
1715
|
+
}
|
|
1600
1716
|
/** The whole file in one multipart request: `POST /public/v1/upload`. */
|
|
1601
1717
|
uploadWhole(source) {
|
|
1602
1718
|
const form = new FormData();
|
|
@@ -1665,10 +1781,15 @@ var Nadia = class {
|
|
|
1665
1781
|
integrations() {
|
|
1666
1782
|
return this.call((o) => listChannels(o));
|
|
1667
1783
|
}
|
|
1668
|
-
/**
|
|
1669
|
-
|
|
1784
|
+
/**
|
|
1785
|
+
* Delete a post and everything published with it. Waits for a yes by
|
|
1786
|
+
* default: throws {@link NadiaConfirmationRequired} unless `autoConfirm`,
|
|
1787
|
+
* or unless the token of that 428 is passed here.
|
|
1788
|
+
*/
|
|
1789
|
+
deletePost(id, options = {}) {
|
|
1670
1790
|
return this.call(
|
|
1671
|
-
(o) => deletePost({ ...o, path: { id } })
|
|
1791
|
+
(o) => deletePost({ ...o, path: { id } }),
|
|
1792
|
+
options.confirmationToken
|
|
1672
1793
|
);
|
|
1673
1794
|
}
|
|
1674
1795
|
/* --------------------------------------------------------------- *
|
|
@@ -1755,9 +1876,9 @@ var Nadia = class {
|
|
|
1755
1876
|
(o) => linkContentPost({ ...o, path: { id }, body: { postGroup } })
|
|
1756
1877
|
);
|
|
1757
1878
|
}
|
|
1758
|
-
/** Delete a card. */
|
|
1759
|
-
deleteContent(id) {
|
|
1760
|
-
return this.call((o) => deleteContent({ ...o, path: { id } }));
|
|
1879
|
+
/** Delete a card. Waits for a yes by default, like {@link Nadia.deletePost}. */
|
|
1880
|
+
deleteContent(id, options = {}) {
|
|
1881
|
+
return this.call((o) => deleteContent({ ...o, path: { id } }), options.confirmationToken);
|
|
1761
1882
|
}
|
|
1762
1883
|
/** The numbers of the published cards, keyed by card id. */
|
|
1763
1884
|
contentsPerformance() {
|
|
@@ -1850,6 +1971,7 @@ function parseWebhook(options) {
|
|
|
1850
1971
|
export {
|
|
1851
1972
|
DEFAULT_BASE_URL,
|
|
1852
1973
|
Nadia,
|
|
1974
|
+
NadiaConfirmationRequired,
|
|
1853
1975
|
NadiaError,
|
|
1854
1976
|
NadiaWebhookError,
|
|
1855
1977
|
catchUpAutomationPose,
|
|
@@ -1889,6 +2011,7 @@ export {
|
|
|
1889
2011
|
getPostAutomation,
|
|
1890
2012
|
getPostingTimes,
|
|
1891
2013
|
getSubscription,
|
|
2014
|
+
getUploadFromUrl,
|
|
1892
2015
|
getVideoHistory,
|
|
1893
2016
|
getViewsPerDay,
|
|
1894
2017
|
getWebhook,
|
|
@@ -1931,6 +2054,8 @@ export {
|
|
|
1931
2054
|
sendAutomationTestEmail,
|
|
1932
2055
|
sendDirectMessage,
|
|
1933
2056
|
setAutomationFlowStatus,
|
|
2057
|
+
setContentWorkspaces,
|
|
2058
|
+
setMediaWorkspaces,
|
|
1934
2059
|
setPostAutomation,
|
|
1935
2060
|
setPostCover,
|
|
1936
2061
|
setPostPublication,
|