@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.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,
@@ -1599,6 +1600,29 @@ var NadiaError = class _NadiaError extends Error {
1599
1600
  return this.status === 0 || this.status === 429 || this.status >= 500;
1600
1601
  }
1601
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;
1602
1626
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1603
1627
  var isUrlImport = (v) => !!v && typeof v === "object" && typeof v.id === "string" && typeof v.status === "string" && !("path" in v);
1604
1628
  var DIRECT_TYPES = {
@@ -1649,6 +1673,7 @@ var Nadia = class {
1649
1673
  /** For the bytes of an upload, which go to the upload host, not the API. */
1650
1674
  fetchImpl;
1651
1675
  importPoll;
1676
+ autoConfirm;
1652
1677
  constructor(apiKey, options = {}) {
1653
1678
  if (!apiKey) {
1654
1679
  throw new Error("An API key is required.");
@@ -1656,6 +1681,7 @@ var Nadia = class {
1656
1681
  const opts = typeof options === "string" ? { baseUrl: options } : options;
1657
1682
  this.retries = opts.retries ?? 2;
1658
1683
  this.fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
1684
+ this.autoConfirm = opts.autoConfirm ?? false;
1659
1685
  this.importPoll = {
1660
1686
  intervalMs: opts.importPoll?.intervalMs ?? 3e3,
1661
1687
  timeoutMs: opts.importPoll?.timeoutMs ?? 15 * 60 * 1e3
@@ -1672,14 +1698,22 @@ var Nadia = class {
1672
1698
  * retries the transient ones. Every method below goes through here — that is
1673
1699
  * the whole point of the facade.
1674
1700
  */
1675
- async call(operation) {
1701
+ async call(operation, confirmationToken) {
1676
1702
  let lastError;
1703
+ const options = confirmationToken ? { client: this.client, fetch: this.withToken(confirmationToken) } : { client: this.client };
1677
1704
  for (let attempt = 0; attempt <= this.retries; attempt++) {
1678
- const { data, error, response } = await operation({ client: this.client });
1705
+ const { data, error, response } = await operation(options);
1679
1706
  if (!response) {
1680
1707
  lastError = new NadiaError(0, error, "The request did not complete.");
1681
1708
  } else if (response.ok) {
1682
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;
1683
1717
  } else {
1684
1718
  lastError = new NadiaError(response.status, error, messageOf(error));
1685
1719
  }
@@ -1690,6 +1724,18 @@ var Nadia = class {
1690
1724
  }
1691
1725
  throw lastError;
1692
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
+ }
1693
1739
  /* --------------------------------------------------------------- *
1694
1740
  * The five calls upstream had, same names, same shape
1695
1741
  * --------------------------------------------------------------- */
@@ -1873,10 +1919,15 @@ var Nadia = class {
1873
1919
  integrations() {
1874
1920
  return this.call((o) => listChannels(o));
1875
1921
  }
1876
- /** Delete a post and everything published with it. */
1877
- 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 = {}) {
1878
1928
  return this.call(
1879
- (o) => deletePost({ ...o, path: { id } })
1929
+ (o) => deletePost({ ...o, path: { id } }),
1930
+ options.confirmationToken
1880
1931
  );
1881
1932
  }
1882
1933
  /* --------------------------------------------------------------- *
@@ -1963,9 +2014,9 @@ var Nadia = class {
1963
2014
  (o) => linkContentPost({ ...o, path: { id }, body: { postGroup } })
1964
2015
  );
1965
2016
  }
1966
- /** Delete a card. */
1967
- deleteContent(id) {
1968
- 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);
1969
2020
  }
1970
2021
  /** The numbers of the published cards, keyed by card id. */
1971
2022
  contentsPerformance() {
@@ -2059,6 +2110,7 @@ function parseWebhook(options) {
2059
2110
  0 && (module.exports = {
2060
2111
  DEFAULT_BASE_URL,
2061
2112
  Nadia,
2113
+ NadiaConfirmationRequired,
2062
2114
  NadiaError,
2063
2115
  NadiaWebhookError,
2064
2116
  catchUpAutomationPose,