@billionsend/cli 0.2.4 → 0.2.5

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.
Files changed (2) hide show
  1. package/dist/index.js +28 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -103,6 +103,24 @@ var PaginationError = class extends Error {
103
103
  }
104
104
  };
105
105
  var MAX_PAGES = 1e5;
106
+ function pageItems(path2, body, after) {
107
+ const data = typeof body === "object" && body !== null ? body.data : void 0;
108
+ if (!Array.isArray(data))
109
+ throw new PaginationError(path2, after, "the list page has no data array");
110
+ return data;
111
+ }
112
+ function nextCursor(path2, page, after) {
113
+ const last = page.at(-1);
114
+ const id = typeof last === "object" && last !== null ? last.id : void 0;
115
+ if (typeof id !== "string" || id === "") {
116
+ throw new PaginationError(
117
+ path2,
118
+ after,
119
+ "the last item of the list page has no id to continue from"
120
+ );
121
+ }
122
+ return id;
123
+ }
106
124
  function cursorGuard(path2, maxPages = MAX_PAGES) {
107
125
  const seen = /* @__PURE__ */ new Set();
108
126
  let pages = 0;
@@ -111,7 +129,7 @@ function cursorGuard(path2, maxPages = MAX_PAGES) {
111
129
  if (pages > maxPages) {
112
130
  throw new PaginationError(path2, after, `more than ${maxPages} pages walked`);
113
131
  }
114
- if (page.length > 0 && page[page.length - 1]?.id === after) {
132
+ if (after !== void 0 && page.length > 0 && page[page.length - 1]?.id === after) {
115
133
  throw new PaginationError(
116
134
  path2,
117
135
  after,
@@ -445,13 +463,12 @@ function createResendSource(http, log) {
445
463
  const guard = cursorGuard(route);
446
464
  for (; ; ) {
447
465
  const { body } = await get(route, { query: { ...query, limit: PAGE, after } });
448
- const data = body.data ?? [];
466
+ const data = pageItems(route, body, after);
449
467
  guard(data, after);
450
468
  items.push(...data);
451
- const last = data[data.length - 1];
452
469
  const hasMore = body.has_more ?? data.length === PAGE;
453
- if (!hasMore || last === void 0) break;
454
- after = last.id;
470
+ if (!hasMore || data.length === 0) break;
471
+ after = nextCursor(route, data, after);
455
472
  if (label !== void 0) onProgress?.({ label, n: items.length, done: false });
456
473
  }
457
474
  if (label !== void 0) {
@@ -1828,7 +1845,7 @@ function parsePlan(text) {
1828
1845
  import { parseArgs } from "node:util";
1829
1846
 
1830
1847
  // src/meta.ts
1831
- var VERSION = true ? "0.2.4" : "0.0.0-dev";
1848
+ var VERSION = true ? "0.2.5" : "0.0.0-dev";
1832
1849
  var REPO_URL = "https://github.com/billionsend/billionsend-cli";
1833
1850
  var USER_AGENT = `billionsend-cli/${VERSION} (+${REPO_URL})`;
1834
1851
  var CLOUD_API_URL = "https://api.billionsend.com";
@@ -2209,15 +2226,15 @@ function createBillionSendTarget(http, log, baseUrl = "the BillionSend URL") {
2209
2226
  path2,
2210
2227
  paged ? { query: { limit: 100, after } } : {}
2211
2228
  );
2212
- guard(body.data, after);
2213
- rows.push(...body.data);
2214
- const last = body.data.at(-1);
2215
- if (!paged || !body.has_more || !last) {
2229
+ const data = pageItems(path2, body, after);
2230
+ guard(data, after);
2231
+ rows.push(...data);
2232
+ if (!paged || body.has_more !== true || data.length === 0) {
2216
2233
  return rows.sort(
2217
2234
  (a, b) => a.created_at.localeCompare(b.created_at) || a.id.localeCompare(b.id)
2218
2235
  );
2219
2236
  }
2220
- after = last.id;
2237
+ after = nextCursor(path2, data, after);
2221
2238
  }
2222
2239
  }
2223
2240
  async function probe() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@billionsend/cli",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Migrate an email account to BillionSend: contacts, segments, topics, templates, webhooks, domains and suppressions — read from the source provider, written to your BillionSend account, re-run safe",
5
5
  "license": "MIT",
6
6
  "type": "module",