@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 +7 -0
- package/README.md +1 -1
- package/dist/index.cjs +32 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -7
- package/dist/index.d.ts +21 -7
- package/dist/index.js +32 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -57,8 +57,10 @@ declare class RateLimitError extends BillionSendError {
|
|
|
57
57
|
readonly retryAfter: number | null;
|
|
58
58
|
constructor(init: RateLimitErrorInit);
|
|
59
59
|
}
|
|
60
|
-
/** 5xx: the API failed to answer the request. */
|
|
60
|
+
/** 5xx: the API failed to answer the request. `retryAfter` is the server's hint in seconds. */
|
|
61
61
|
declare class APIError extends BillionSendError {
|
|
62
|
+
readonly retryAfter: number | null;
|
|
63
|
+
constructor(init: RateLimitErrorInit);
|
|
62
64
|
}
|
|
63
65
|
/** The request never got an answer: DNS, connection, TLS or protocol failure. `statusCode` is `null`. */
|
|
64
66
|
declare class APIConnectionError extends BillionSendError {
|
|
@@ -15949,7 +15951,11 @@ interface EventStreamOptions {
|
|
|
15949
15951
|
lastEventId?: string;
|
|
15950
15952
|
/** Reconnect with `Last-Event-ID` when the connection drops (default `true`). */
|
|
15951
15953
|
reconnect?: boolean;
|
|
15952
|
-
/**
|
|
15954
|
+
/**
|
|
15955
|
+
* Reconnects that may fail in a row before giving up (default 5). A reconnect
|
|
15956
|
+
* that opens the stream resets the count, so the server's scheduled closes
|
|
15957
|
+
* never use it up.
|
|
15958
|
+
*/
|
|
15953
15959
|
maxReconnects?: number;
|
|
15954
15960
|
/** Wait before reconnecting, in milliseconds (default 1000; a server `retry` field overrides it). */
|
|
15955
15961
|
reconnectDelayMs?: number;
|
|
@@ -15965,9 +15971,13 @@ type Connect = (lastEventId: string | null) => Promise<Result<StreamConnection>>
|
|
|
15965
15971
|
* or calling {@link EventStream.close}, closes the connection. When the
|
|
15966
15972
|
* connection drops (the server also closes every stream about every ten
|
|
15967
15973
|
* minutes) it reconnects with `Last-Event-ID` and the events in between are
|
|
15968
|
-
* replayed first, unless `reconnect` is false.
|
|
15969
|
-
*
|
|
15970
|
-
*
|
|
15974
|
+
* replayed first, unless `reconnect` is false. A reconnect the API refuses
|
|
15975
|
+
* with a `Retry-After` (a 429, a 5xx) waits that long, and one longer than 60
|
|
15976
|
+
* seconds throws that error at once. After `maxReconnects` reconnects that
|
|
15977
|
+
* fail in a row it throws the last one's error: the API's answer as its own
|
|
15978
|
+
* class ({@link RateLimitError}, {@link APIError}, …), or an
|
|
15979
|
+
* {@link APIConnectionError} when the API could not be reached. An error a
|
|
15980
|
+
* retry cannot fix (a revoked key, say) is thrown at once as its own class.
|
|
15971
15981
|
*/
|
|
15972
15982
|
declare class EventStream implements AsyncIterable<InboxStreamItem> {
|
|
15973
15983
|
#private;
|
|
@@ -16905,13 +16915,17 @@ interface Cursor {
|
|
|
16905
16915
|
* Walks a cursor-paginated list one item at a time, fetching pages on demand.
|
|
16906
16916
|
* Starting with `before` set walks backward (using the first id of each page);
|
|
16907
16917
|
* otherwise it walks forward from `after` (using the last id). A failed page is
|
|
16908
|
-
* thrown as its `BillionSendError` (the same classes the methods return).
|
|
16918
|
+
* thrown as its `BillionSendError` (the same classes the methods return). A
|
|
16919
|
+
* page without a `data` array throws a `BillionSendError` named
|
|
16920
|
+
* `application_error` at once; a page that has more but whose item to
|
|
16921
|
+
* continue from has no id (a non-empty string) hands out its items first, then
|
|
16922
|
+
* throws one.
|
|
16909
16923
|
*/
|
|
16910
16924
|
declare function paginate<Item extends {
|
|
16911
16925
|
id: string;
|
|
16912
16926
|
}>(fetchPage: (cursor: Cursor) => Promise<Result<CursorPage<Item>>>, start?: Cursor): AsyncGenerator<Item, void, undefined>;
|
|
16913
16927
|
|
|
16914
|
-
declare const VERSION = "0.1.
|
|
16928
|
+
declare const VERSION = "0.1.1";
|
|
16915
16929
|
|
|
16916
16930
|
/** Every webhook event name. */
|
|
16917
16931
|
type WebhookEventType = NonNullable<CreateWebhookRequest["events"]>[number];
|
package/dist/index.d.ts
CHANGED
|
@@ -57,8 +57,10 @@ declare class RateLimitError extends BillionSendError {
|
|
|
57
57
|
readonly retryAfter: number | null;
|
|
58
58
|
constructor(init: RateLimitErrorInit);
|
|
59
59
|
}
|
|
60
|
-
/** 5xx: the API failed to answer the request. */
|
|
60
|
+
/** 5xx: the API failed to answer the request. `retryAfter` is the server's hint in seconds. */
|
|
61
61
|
declare class APIError extends BillionSendError {
|
|
62
|
+
readonly retryAfter: number | null;
|
|
63
|
+
constructor(init: RateLimitErrorInit);
|
|
62
64
|
}
|
|
63
65
|
/** The request never got an answer: DNS, connection, TLS or protocol failure. `statusCode` is `null`. */
|
|
64
66
|
declare class APIConnectionError extends BillionSendError {
|
|
@@ -15949,7 +15951,11 @@ interface EventStreamOptions {
|
|
|
15949
15951
|
lastEventId?: string;
|
|
15950
15952
|
/** Reconnect with `Last-Event-ID` when the connection drops (default `true`). */
|
|
15951
15953
|
reconnect?: boolean;
|
|
15952
|
-
/**
|
|
15954
|
+
/**
|
|
15955
|
+
* Reconnects that may fail in a row before giving up (default 5). A reconnect
|
|
15956
|
+
* that opens the stream resets the count, so the server's scheduled closes
|
|
15957
|
+
* never use it up.
|
|
15958
|
+
*/
|
|
15953
15959
|
maxReconnects?: number;
|
|
15954
15960
|
/** Wait before reconnecting, in milliseconds (default 1000; a server `retry` field overrides it). */
|
|
15955
15961
|
reconnectDelayMs?: number;
|
|
@@ -15965,9 +15971,13 @@ type Connect = (lastEventId: string | null) => Promise<Result<StreamConnection>>
|
|
|
15965
15971
|
* or calling {@link EventStream.close}, closes the connection. When the
|
|
15966
15972
|
* connection drops (the server also closes every stream about every ten
|
|
15967
15973
|
* minutes) it reconnects with `Last-Event-ID` and the events in between are
|
|
15968
|
-
* replayed first, unless `reconnect` is false.
|
|
15969
|
-
*
|
|
15970
|
-
*
|
|
15974
|
+
* replayed first, unless `reconnect` is false. A reconnect the API refuses
|
|
15975
|
+
* with a `Retry-After` (a 429, a 5xx) waits that long, and one longer than 60
|
|
15976
|
+
* seconds throws that error at once. After `maxReconnects` reconnects that
|
|
15977
|
+
* fail in a row it throws the last one's error: the API's answer as its own
|
|
15978
|
+
* class ({@link RateLimitError}, {@link APIError}, …), or an
|
|
15979
|
+
* {@link APIConnectionError} when the API could not be reached. An error a
|
|
15980
|
+
* retry cannot fix (a revoked key, say) is thrown at once as its own class.
|
|
15971
15981
|
*/
|
|
15972
15982
|
declare class EventStream implements AsyncIterable<InboxStreamItem> {
|
|
15973
15983
|
#private;
|
|
@@ -16905,13 +16915,17 @@ interface Cursor {
|
|
|
16905
16915
|
* Walks a cursor-paginated list one item at a time, fetching pages on demand.
|
|
16906
16916
|
* Starting with `before` set walks backward (using the first id of each page);
|
|
16907
16917
|
* otherwise it walks forward from `after` (using the last id). A failed page is
|
|
16908
|
-
* thrown as its `BillionSendError` (the same classes the methods return).
|
|
16918
|
+
* thrown as its `BillionSendError` (the same classes the methods return). A
|
|
16919
|
+
* page without a `data` array throws a `BillionSendError` named
|
|
16920
|
+
* `application_error` at once; a page that has more but whose item to
|
|
16921
|
+
* continue from has no id (a non-empty string) hands out its items first, then
|
|
16922
|
+
* throws one.
|
|
16909
16923
|
*/
|
|
16910
16924
|
declare function paginate<Item extends {
|
|
16911
16925
|
id: string;
|
|
16912
16926
|
}>(fetchPage: (cursor: Cursor) => Promise<Result<CursorPage<Item>>>, start?: Cursor): AsyncGenerator<Item, void, undefined>;
|
|
16913
16927
|
|
|
16914
|
-
declare const VERSION = "0.1.
|
|
16928
|
+
declare const VERSION = "0.1.1";
|
|
16915
16929
|
|
|
16916
16930
|
/** Every webhook event name. */
|
|
16917
16931
|
type WebhookEventType = NonNullable<CreateWebhookRequest["events"]>[number];
|
package/dist/index.js
CHANGED
|
@@ -31,6 +31,11 @@ var RateLimitError = class extends BillionSendError {
|
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
33
|
var APIError = class extends BillionSendError {
|
|
34
|
+
retryAfter;
|
|
35
|
+
constructor(init) {
|
|
36
|
+
super(init);
|
|
37
|
+
this.retryAfter = init.retryAfter ?? null;
|
|
38
|
+
}
|
|
34
39
|
};
|
|
35
40
|
var APIConnectionError = class extends BillionSendError {
|
|
36
41
|
constructor(message = "Connection error") {
|
|
@@ -97,6 +102,7 @@ function errorFromResponse(status, statusText, text, headers = {}) {
|
|
|
97
102
|
const init = { message, name, statusCode: status, requestId: headers["x-request-id"] ?? null };
|
|
98
103
|
if (status === 429)
|
|
99
104
|
return new RateLimitError({ ...init, retryAfter: parseRetryAfter(headers["retry-after"]) });
|
|
105
|
+
if (status >= 500) return new APIError({ ...init, retryAfter: parseRetryAfter(headers["retry-after"]) });
|
|
100
106
|
return new (errorClass(status))(init);
|
|
101
107
|
}
|
|
102
108
|
|
|
@@ -157,7 +163,7 @@ function sleep(ms, signal) {
|
|
|
157
163
|
}
|
|
158
164
|
|
|
159
165
|
// src/version.ts
|
|
160
|
-
var VERSION = "0.1.
|
|
166
|
+
var VERSION = "0.1.1";
|
|
161
167
|
|
|
162
168
|
// src/core.ts
|
|
163
169
|
var DEFAULT_BASE_URL = "https://api.billionsend.com";
|
|
@@ -393,12 +399,19 @@ async function* paginate(fetchPage, start = {}) {
|
|
|
393
399
|
while (true) {
|
|
394
400
|
const page = await fetchPage(cursor);
|
|
395
401
|
if (page.error !== null) throw page.error;
|
|
396
|
-
const
|
|
402
|
+
const body = page.data;
|
|
403
|
+
const items = typeof body === "object" && body !== null ? body.data : void 0;
|
|
404
|
+
if (!Array.isArray(items)) throw applicationError("The list page has no data array");
|
|
397
405
|
for (const item of items) yield item;
|
|
398
|
-
if (
|
|
406
|
+
if (page.data.has_more !== true || items.length === 0) return;
|
|
399
407
|
const edge = backward ? items[0] : items[items.length - 1];
|
|
400
|
-
|
|
401
|
-
|
|
408
|
+
const id = typeof edge === "object" && edge !== null ? edge.id : void 0;
|
|
409
|
+
if (typeof id !== "string" || id === "") {
|
|
410
|
+
throw applicationError(
|
|
411
|
+
`The ${backward ? "first" : "last"} item of the list page has no id to continue from`
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
cursor = backward ? { before: id } : { after: id };
|
|
402
415
|
}
|
|
403
416
|
}
|
|
404
417
|
|
|
@@ -1058,6 +1071,7 @@ var EventStream = class {
|
|
|
1058
1071
|
const reconnect = this.#options.reconnect !== false;
|
|
1059
1072
|
const maxReconnects = this.#options.maxReconnects ?? 5;
|
|
1060
1073
|
let failures = 0;
|
|
1074
|
+
let lastError = null;
|
|
1061
1075
|
let gapAt;
|
|
1062
1076
|
try {
|
|
1063
1077
|
while (!this.#closed) {
|
|
@@ -1070,7 +1084,6 @@ var EventStream = class {
|
|
|
1070
1084
|
this.#lastEventId
|
|
1071
1085
|
);
|
|
1072
1086
|
for await (const event of events) {
|
|
1073
|
-
failures = 0;
|
|
1074
1087
|
if (event.event === STREAM_GAP_EVENT) {
|
|
1075
1088
|
if (gapAt === this.#lastEventId) continue;
|
|
1076
1089
|
gapAt = this.#lastEventId;
|
|
@@ -1089,15 +1102,18 @@ var EventStream = class {
|
|
|
1089
1102
|
}
|
|
1090
1103
|
connection.close();
|
|
1091
1104
|
this.#connection = null;
|
|
1105
|
+
lastError = null;
|
|
1092
1106
|
}
|
|
1093
1107
|
if (this.#closed || !reconnect) return;
|
|
1094
1108
|
failures++;
|
|
1095
1109
|
if (failures > maxReconnects) {
|
|
1110
|
+
if (lastError !== null && lastError.statusCode !== null) throw lastError;
|
|
1096
1111
|
throw new APIConnectionError(
|
|
1097
1112
|
`The event stream disconnected; gave up after ${maxReconnects} reconnects`
|
|
1098
1113
|
);
|
|
1099
1114
|
}
|
|
1100
|
-
|
|
1115
|
+
const hinted = retryAfterMs(lastError);
|
|
1116
|
+
if (!await sleep(hinted ?? this.#delayMs, this.#options.signal) || this.#closed) return;
|
|
1101
1117
|
const next = await this.#connect(this.#lastEventId);
|
|
1102
1118
|
if (this.#closed) {
|
|
1103
1119
|
next.data?.close();
|
|
@@ -1105,15 +1121,24 @@ var EventStream = class {
|
|
|
1105
1121
|
}
|
|
1106
1122
|
if (next.error !== null) {
|
|
1107
1123
|
if (!retryable(next.error, false)) throw next.error;
|
|
1124
|
+
const wait = retryAfterMs(next.error);
|
|
1125
|
+
if (wait !== null && wait > MAX_RETRY_AFTER_MS) throw next.error;
|
|
1126
|
+
lastError = next.error;
|
|
1108
1127
|
continue;
|
|
1109
1128
|
}
|
|
1110
1129
|
this.#connection = next.data;
|
|
1130
|
+
failures = 0;
|
|
1131
|
+
lastError = null;
|
|
1111
1132
|
}
|
|
1112
1133
|
} finally {
|
|
1113
1134
|
this.close();
|
|
1114
1135
|
}
|
|
1115
1136
|
}
|
|
1116
1137
|
};
|
|
1138
|
+
function retryAfterMs(error) {
|
|
1139
|
+
const seconds = error instanceof RateLimitError || error instanceof APIError ? error.retryAfter : null;
|
|
1140
|
+
return seconds === null ? null : seconds * 1e3;
|
|
1141
|
+
}
|
|
1117
1142
|
|
|
1118
1143
|
// src/resources/inboxes.ts
|
|
1119
1144
|
var InboxAgentSettings = class extends Resource {
|