@usenadia/node 0.3.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.mjs CHANGED
@@ -1462,6 +1462,29 @@ var NadiaError = class _NadiaError extends Error {
1462
1462
  return this.status === 0 || this.status === 429 || this.status >= 500;
1463
1463
  }
1464
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;
1465
1488
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1466
1489
  var isUrlImport = (v) => !!v && typeof v === "object" && typeof v.id === "string" && typeof v.status === "string" && !("path" in v);
1467
1490
  var DIRECT_TYPES = {
@@ -1512,6 +1535,7 @@ var Nadia = class {
1512
1535
  /** For the bytes of an upload, which go to the upload host, not the API. */
1513
1536
  fetchImpl;
1514
1537
  importPoll;
1538
+ autoConfirm;
1515
1539
  constructor(apiKey, options = {}) {
1516
1540
  if (!apiKey) {
1517
1541
  throw new Error("An API key is required.");
@@ -1519,6 +1543,7 @@ var Nadia = class {
1519
1543
  const opts = typeof options === "string" ? { baseUrl: options } : options;
1520
1544
  this.retries = opts.retries ?? 2;
1521
1545
  this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1546
+ this.autoConfirm = opts.autoConfirm ?? false;
1522
1547
  this.importPoll = {
1523
1548
  intervalMs: opts.importPoll?.intervalMs ?? 3e3,
1524
1549
  timeoutMs: opts.importPoll?.timeoutMs ?? 15 * 60 * 1e3
@@ -1535,14 +1560,22 @@ var Nadia = class {
1535
1560
  * retries the transient ones. Every method below goes through here — that is
1536
1561
  * the whole point of the facade.
1537
1562
  */
1538
- async call(operation) {
1563
+ async call(operation, confirmationToken) {
1539
1564
  let lastError;
1565
+ const options = confirmationToken ? { client: this.client, fetch: this.withToken(confirmationToken) } : { client: this.client };
1540
1566
  for (let attempt = 0; attempt <= this.retries; attempt++) {
1541
- const { data, error, response } = await operation({ client: this.client });
1567
+ const { data, error, response } = await operation(options);
1542
1568
  if (!response) {
1543
1569
  lastError = new NadiaError(0, error, "The request did not complete.");
1544
1570
  } else if (response.ok) {
1545
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;
1546
1579
  } else {
1547
1580
  lastError = new NadiaError(response.status, error, messageOf(error));
1548
1581
  }
@@ -1553,6 +1586,18 @@ var Nadia = class {
1553
1586
  }
1554
1587
  throw lastError;
1555
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
+ }
1556
1601
  /* --------------------------------------------------------------- *
1557
1602
  * The five calls upstream had, same names, same shape
1558
1603
  * --------------------------------------------------------------- */
@@ -1736,10 +1781,15 @@ var Nadia = class {
1736
1781
  integrations() {
1737
1782
  return this.call((o) => listChannels(o));
1738
1783
  }
1739
- /** Delete a post and everything published with it. */
1740
- deletePost(id) {
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 = {}) {
1741
1790
  return this.call(
1742
- (o) => deletePost({ ...o, path: { id } })
1791
+ (o) => deletePost({ ...o, path: { id } }),
1792
+ options.confirmationToken
1743
1793
  );
1744
1794
  }
1745
1795
  /* --------------------------------------------------------------- *
@@ -1826,9 +1876,9 @@ var Nadia = class {
1826
1876
  (o) => linkContentPost({ ...o, path: { id }, body: { postGroup } })
1827
1877
  );
1828
1878
  }
1829
- /** Delete a card. */
1830
- deleteContent(id) {
1831
- 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);
1832
1882
  }
1833
1883
  /** The numbers of the published cards, keyed by card id. */
1834
1884
  contentsPerformance() {
@@ -1921,6 +1971,7 @@ function parseWebhook(options) {
1921
1971
  export {
1922
1972
  DEFAULT_BASE_URL,
1923
1973
  Nadia,
1974
+ NadiaConfirmationRequired,
1924
1975
  NadiaError,
1925
1976
  NadiaWebhookError,
1926
1977
  catchUpAutomationPose,