@billionsend/node 0.1.0 → 0.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.1 (2026-11-17)
4
+
5
+ - An inbox event stream counts only the reconnects that fail in a row toward `maxReconnects`: each reconnect that opens the stream resets the count. A quiet inbox, whose stream the server closes every ten minutes or so with only keep-alive comments in between, no longer gives up after an hour.
6
+ - A reconnect the API refuses with a `Retry-After` (a 429, a 5xx) waits that long instead of the reconnect delay; one longer than 60 seconds throws that error at once. When the reconnects run out, the last one's error is thrown as its own class (`RateLimitError`, `APIError`, …) when the API answered; `APIConnectionError` ("gave up after N reconnects") only when it could not be reached.
7
+ - `APIError` carries the server's `Retry-After` too: `retryAfter`, in seconds, `null` without one.
8
+ - The `listAll` / `searchAll` iterators and `paginate()` throw a `BillionSendError` named `application_error` on an answer that is not a list page (no `data` array), and, after handing out the page, when the item to continue from has no `id` (missing, empty or not a string), instead of a `TypeError` or fetching the same page forever.
9
+
3
10
  ## 0.1.0 (2026-11-17)
4
11
 
5
12
  - First version of the BillionSend SDK for Node.js and TypeScript: every operation of the BillionSend API, grouped by resource and named after each operation's `operationId`, with request and response types generated from the OpenAPI document.
package/README.md CHANGED
@@ -153,7 +153,7 @@ const { data: code } = await bs.inboxes.codes.latest("support@acme.dev", { from:
153
153
  - `InboxStreamEvent`: `{ id, event, data }`, where `event` is the webhook event name (`inbox.email.received`, `inbox.thread.created`, `inbox.draft.sent`, `email.received`, …) and `data` is `{ type, created_at, inbox_id, data }`, the webhook payload's metadata with the ids to read next (`thread_id`, `email_id`, `draft_id`).
154
154
  - `StreamGapEvent`: `{ id: null, event: "stream.gap", data: { reason } }` when the stream resumed from further back than the server keeps (24 hours): events may be missing, so read the lists again. One stream hands it out once per position.
155
155
 
156
- When the connection drops (the server also closes every stream about every ten minutes), the stream reconnects with `Last-Event-ID` and replays what it missed before the live events. It gives up after `maxReconnects` failures in a row (default 5) with an `APIConnectionError`; a refusal a retry cannot fix (a revoked key) is thrown as its own class. One inbox's stream ends after that inbox's `inbox.deleted` or `inbox.expired`.
156
+ When the connection drops (the server also closes every stream about every ten minutes), the stream reconnects with `Last-Event-ID` and replays what it missed before the live events. `maxReconnects` (default 5) bounds the reconnects that fail in a row; one that opens the stream resets the count. A reconnect the API refuses with a `Retry-After` waits that long (longer than 60 seconds: that error is thrown at once). When the reconnects run out, the last one's error is thrown: the API's answer as its own class (`RateLimitError`, `APIError`), or an `APIConnectionError` when the API could not be reached; a refusal a retry cannot fix (a revoked key) is thrown at once. One inbox's stream ends after that inbox's `inbox.deleted` or `inbox.expired`.
157
157
 
158
158
  ```ts
159
159
  const opened = await bs.inboxes.events.stream({ types: "inbox.email.received,inbox.thread.created" }, {
package/dist/index.cjs CHANGED
@@ -33,6 +33,11 @@ var RateLimitError = class extends BillionSendError {
33
33
  }
34
34
  };
35
35
  var APIError = class extends BillionSendError {
36
+ retryAfter;
37
+ constructor(init) {
38
+ super(init);
39
+ this.retryAfter = init.retryAfter ?? null;
40
+ }
36
41
  };
37
42
  var APIConnectionError = class extends BillionSendError {
38
43
  constructor(message = "Connection error") {
@@ -99,6 +104,7 @@ function errorFromResponse(status, statusText, text, headers = {}) {
99
104
  const init = { message, name, statusCode: status, requestId: headers["x-request-id"] ?? null };
100
105
  if (status === 429)
101
106
  return new RateLimitError({ ...init, retryAfter: parseRetryAfter(headers["retry-after"]) });
107
+ if (status >= 500) return new APIError({ ...init, retryAfter: parseRetryAfter(headers["retry-after"]) });
102
108
  return new (errorClass(status))(init);
103
109
  }
104
110
 
@@ -159,7 +165,7 @@ function sleep(ms, signal) {
159
165
  }
160
166
 
161
167
  // src/version.ts
162
- var VERSION = "0.1.0";
168
+ var VERSION = "0.1.1";
163
169
 
164
170
  // src/core.ts
165
171
  var DEFAULT_BASE_URL = "https://api.billionsend.com";
@@ -395,12 +401,19 @@ async function* paginate(fetchPage, start = {}) {
395
401
  while (true) {
396
402
  const page = await fetchPage(cursor);
397
403
  if (page.error !== null) throw page.error;
398
- const items = page.data.data;
404
+ const body = page.data;
405
+ const items = typeof body === "object" && body !== null ? body.data : void 0;
406
+ if (!Array.isArray(items)) throw applicationError("The list page has no data array");
399
407
  for (const item of items) yield item;
400
- if (!page.data.has_more || items.length === 0) return;
408
+ if (page.data.has_more !== true || items.length === 0) return;
401
409
  const edge = backward ? items[0] : items[items.length - 1];
402
- if (edge === void 0) return;
403
- cursor = backward ? { before: edge.id } : { after: edge.id };
410
+ const id = typeof edge === "object" && edge !== null ? edge.id : void 0;
411
+ if (typeof id !== "string" || id === "") {
412
+ throw applicationError(
413
+ `The ${backward ? "first" : "last"} item of the list page has no id to continue from`
414
+ );
415
+ }
416
+ cursor = backward ? { before: id } : { after: id };
404
417
  }
405
418
  }
406
419
 
@@ -1060,6 +1073,7 @@ var EventStream = class {
1060
1073
  const reconnect = this.#options.reconnect !== false;
1061
1074
  const maxReconnects = this.#options.maxReconnects ?? 5;
1062
1075
  let failures = 0;
1076
+ let lastError = null;
1063
1077
  let gapAt;
1064
1078
  try {
1065
1079
  while (!this.#closed) {
@@ -1072,7 +1086,6 @@ var EventStream = class {
1072
1086
  this.#lastEventId
1073
1087
  );
1074
1088
  for await (const event of events) {
1075
- failures = 0;
1076
1089
  if (event.event === STREAM_GAP_EVENT) {
1077
1090
  if (gapAt === this.#lastEventId) continue;
1078
1091
  gapAt = this.#lastEventId;
@@ -1091,15 +1104,18 @@ var EventStream = class {
1091
1104
  }
1092
1105
  connection.close();
1093
1106
  this.#connection = null;
1107
+ lastError = null;
1094
1108
  }
1095
1109
  if (this.#closed || !reconnect) return;
1096
1110
  failures++;
1097
1111
  if (failures > maxReconnects) {
1112
+ if (lastError !== null && lastError.statusCode !== null) throw lastError;
1098
1113
  throw new APIConnectionError(
1099
1114
  `The event stream disconnected; gave up after ${maxReconnects} reconnects`
1100
1115
  );
1101
1116
  }
1102
- if (!await sleep(this.#delayMs, this.#options.signal) || this.#closed) return;
1117
+ const hinted = retryAfterMs(lastError);
1118
+ if (!await sleep(hinted ?? this.#delayMs, this.#options.signal) || this.#closed) return;
1103
1119
  const next = await this.#connect(this.#lastEventId);
1104
1120
  if (this.#closed) {
1105
1121
  next.data?.close();
@@ -1107,15 +1123,24 @@ var EventStream = class {
1107
1123
  }
1108
1124
  if (next.error !== null) {
1109
1125
  if (!retryable(next.error, false)) throw next.error;
1126
+ const wait = retryAfterMs(next.error);
1127
+ if (wait !== null && wait > MAX_RETRY_AFTER_MS) throw next.error;
1128
+ lastError = next.error;
1110
1129
  continue;
1111
1130
  }
1112
1131
  this.#connection = next.data;
1132
+ failures = 0;
1133
+ lastError = null;
1113
1134
  }
1114
1135
  } finally {
1115
1136
  this.close();
1116
1137
  }
1117
1138
  }
1118
1139
  };
1140
+ function retryAfterMs(error) {
1141
+ const seconds = error instanceof RateLimitError || error instanceof APIError ? error.retryAfter : null;
1142
+ return seconds === null ? null : seconds * 1e3;
1143
+ }
1119
1144
 
1120
1145
  // src/resources/inboxes.ts
1121
1146
  var InboxAgentSettings = class extends Resource {