@billionsend/node 0.1.1 → 0.2.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 +31 -1
- package/README.md +38 -14
- package/dist/index.cjs +411 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10335 -3912
- package/dist/index.d.ts +10335 -3912
- package/dist/index.js +411 -52
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
package/dist/index.js
CHANGED
|
@@ -79,7 +79,7 @@ function errorClass(status) {
|
|
|
79
79
|
case 409:
|
|
80
80
|
return ConflictError;
|
|
81
81
|
default:
|
|
82
|
-
return
|
|
82
|
+
return BillionSendError;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
function errorFromResponse(status, statusText, text, headers = {}) {
|
|
@@ -139,8 +139,9 @@ function newIdempotencyKey() {
|
|
|
139
139
|
const bytes = new Uint8Array(16);
|
|
140
140
|
if (typeof webCrypto?.getRandomValues === "function") webCrypto.getRandomValues(bytes);
|
|
141
141
|
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
142
|
-
|
|
143
|
-
|
|
142
|
+
const view = new DataView(bytes.buffer);
|
|
143
|
+
view.setUint8(6, view.getUint8(6) & 15 | 64);
|
|
144
|
+
view.setUint8(8, view.getUint8(8) & 63 | 128);
|
|
144
145
|
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
145
146
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
146
147
|
}
|
|
@@ -163,7 +164,7 @@ function sleep(ms, signal) {
|
|
|
163
164
|
}
|
|
164
165
|
|
|
165
166
|
// src/version.ts
|
|
166
|
-
var VERSION = "0.
|
|
167
|
+
var VERSION = "0.2.1";
|
|
167
168
|
|
|
168
169
|
// src/core.ts
|
|
169
170
|
var DEFAULT_BASE_URL = "https://api.billionsend.com";
|
|
@@ -174,6 +175,10 @@ function readEnv(name) {
|
|
|
174
175
|
const value = env?.[name];
|
|
175
176
|
return value === void 0 || value === "" ? void 0 : value;
|
|
176
177
|
}
|
|
178
|
+
function cleanKey(key) {
|
|
179
|
+
const trimmed = key?.trim();
|
|
180
|
+
return trimmed === void 0 || trimmed === "" ? void 0 : trimmed;
|
|
181
|
+
}
|
|
177
182
|
function failure(error, headers = null) {
|
|
178
183
|
return { data: null, error, headers };
|
|
179
184
|
}
|
|
@@ -199,17 +204,33 @@ function lowerCaseKeys(headers) {
|
|
|
199
204
|
}
|
|
200
205
|
return out;
|
|
201
206
|
}
|
|
207
|
+
var HEADER_NAME = /^[!#$%&'*+.^_`|~0-9a-z-]+$/;
|
|
208
|
+
var HEADER_VALUE = /^[\t\x20-\x7e]*$/;
|
|
209
|
+
function invalidHeader(headers) {
|
|
210
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
211
|
+
if (!HEADER_NAME.test(name)) return JSON.stringify(name.replace(/[^\x20-\x7e]/g, "?"));
|
|
212
|
+
if (!HEADER_VALUE.test(value)) return name;
|
|
213
|
+
}
|
|
214
|
+
return void 0;
|
|
215
|
+
}
|
|
202
216
|
function fillPath(template, params) {
|
|
203
217
|
let missing;
|
|
218
|
+
let dotted;
|
|
204
219
|
const filled = template.replace(/\{([^}]+)\}/g, (_, name) => {
|
|
205
220
|
const value = params?.[name];
|
|
206
221
|
if (value === void 0 || value === null || String(value) === "") {
|
|
207
222
|
missing ??= name;
|
|
208
223
|
return "";
|
|
209
224
|
}
|
|
210
|
-
|
|
225
|
+
const text = String(value);
|
|
226
|
+
if (text === "." || text === "..") dotted ??= name;
|
|
227
|
+
return encodeURIComponent(text);
|
|
211
228
|
});
|
|
212
|
-
|
|
229
|
+
if (missing !== void 0) return applicationError(`Missing path parameter "${missing}"`);
|
|
230
|
+
if (dotted !== void 0) {
|
|
231
|
+
return applicationError(`Invalid path parameter "${dotted}": "." and ".." are not ids`);
|
|
232
|
+
}
|
|
233
|
+
return filled;
|
|
213
234
|
}
|
|
214
235
|
function queryString(query) {
|
|
215
236
|
if (!query) return "";
|
|
@@ -217,7 +238,7 @@ function queryString(query) {
|
|
|
217
238
|
for (const [key, value] of Object.entries(query)) {
|
|
218
239
|
if (value === void 0 || value === null) continue;
|
|
219
240
|
if (Array.isArray(value)) {
|
|
220
|
-
params.append(key, value.map(String).join(","));
|
|
241
|
+
if (value.length > 0) params.append(key, value.map(String).join(","));
|
|
221
242
|
} else {
|
|
222
243
|
params.append(key, String(value));
|
|
223
244
|
}
|
|
@@ -233,7 +254,7 @@ var Core = class {
|
|
|
233
254
|
#fetch;
|
|
234
255
|
#headers;
|
|
235
256
|
constructor(options) {
|
|
236
|
-
this.#apiKey = options.apiKey ?? readEnv("BILLIONSEND_API_KEY");
|
|
257
|
+
this.#apiKey = cleanKey(options.apiKey) ?? cleanKey(readEnv("BILLIONSEND_API_KEY"));
|
|
237
258
|
this.baseUrl = (options.baseUrl ?? readEnv("BILLIONSEND_BASE_URL") ?? DEFAULT_BASE_URL).replace(
|
|
238
259
|
/\/+$/,
|
|
239
260
|
""
|
|
@@ -251,10 +272,6 @@ var Core = class {
|
|
|
251
272
|
this.#fetch = fetchImpl;
|
|
252
273
|
this.#headers = lowerCaseKeys(options.headers);
|
|
253
274
|
}
|
|
254
|
-
/** Whether an API key is configured. */
|
|
255
|
-
get hasApiKey() {
|
|
256
|
-
return this.#apiKey !== void 0;
|
|
257
|
-
}
|
|
258
275
|
/** Calls one operation and parses its JSON answer. */
|
|
259
276
|
request(method, path, init = {}) {
|
|
260
277
|
return this.#execute(method, path, init, false);
|
|
@@ -286,7 +303,10 @@ var Core = class {
|
|
|
286
303
|
...lowerCaseKeys(init.headers)
|
|
287
304
|
};
|
|
288
305
|
let body;
|
|
289
|
-
if (init.
|
|
306
|
+
if (init.form !== void 0) {
|
|
307
|
+
body = init.form;
|
|
308
|
+
delete headers["content-type"];
|
|
309
|
+
} else if (init.body !== void 0) {
|
|
290
310
|
body = JSON.stringify(init.body);
|
|
291
311
|
headers["content-type"] = "application/json";
|
|
292
312
|
}
|
|
@@ -296,14 +316,24 @@ var Core = class {
|
|
|
296
316
|
}
|
|
297
317
|
if (auth) headers.authorization = `Bearer ${this.#apiKey}`;
|
|
298
318
|
else delete headers.authorization;
|
|
319
|
+
const invalid = invalidHeader(headers);
|
|
320
|
+
if (invalid !== void 0) {
|
|
321
|
+
return failure(
|
|
322
|
+
new BillionSendError({
|
|
323
|
+
name: "invalid_header",
|
|
324
|
+
message: `The ${invalid} header has a value HTTP cannot carry (only printable ASCII and tabs); nothing was sent`
|
|
325
|
+
})
|
|
326
|
+
);
|
|
327
|
+
}
|
|
299
328
|
const keyed = headers["idempotency-key"] !== void 0;
|
|
300
|
-
const
|
|
329
|
+
const safeToRepeat = IDEMPOTENT_METHODS.has(method) || keyed;
|
|
301
330
|
const maxRetries = options.maxRetries ?? this.maxRetries;
|
|
302
331
|
const timeoutMs = options.timeoutMs ?? this.timeoutMs;
|
|
303
332
|
for (let attempt = 0; ; attempt++) {
|
|
304
333
|
const outcome = await this.#attempt(method, url, headers, body, timeoutMs, options.signal, stream);
|
|
305
334
|
const error = outcome.result.error;
|
|
306
|
-
|
|
335
|
+
const mayRepeat = safeToRepeat || error?.statusCode === 429;
|
|
336
|
+
if (error === null || !mayRepeat || attempt >= maxRetries || !retryable(error, keyed)) {
|
|
307
337
|
return outcome.result;
|
|
308
338
|
}
|
|
309
339
|
const delay = retryDelayMs(attempt, outcome.retryAfter);
|
|
@@ -312,6 +342,7 @@ var Core = class {
|
|
|
312
342
|
}
|
|
313
343
|
}
|
|
314
344
|
async #attempt(method, url, headers, body, timeoutMs, signal, stream) {
|
|
345
|
+
if (signal?.aborted) return { result: failure(applicationError("Request was aborted")) };
|
|
315
346
|
const controller = new AbortController();
|
|
316
347
|
let timedOut = false;
|
|
317
348
|
const timer = setTimeout(() => {
|
|
@@ -319,8 +350,7 @@ var Core = class {
|
|
|
319
350
|
controller.abort();
|
|
320
351
|
}, timeoutMs);
|
|
321
352
|
const onAbort = () => controller.abort();
|
|
322
|
-
|
|
323
|
-
else signal?.addEventListener("abort", onAbort, { once: true });
|
|
353
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
324
354
|
const release = () => {
|
|
325
355
|
clearTimeout(timer);
|
|
326
356
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -360,6 +390,8 @@ var Core = class {
|
|
|
360
390
|
const text = await response.text();
|
|
361
391
|
release();
|
|
362
392
|
if (response.ok) {
|
|
393
|
+
if (text.trim() === "")
|
|
394
|
+
return { result: { data: null, error: null, headers: responseHeaders } };
|
|
363
395
|
try {
|
|
364
396
|
return { result: { data: JSON.parse(text), error: null, headers: responseHeaders } };
|
|
365
397
|
} catch {
|
|
@@ -427,7 +459,22 @@ function idempotent(options) {
|
|
|
427
459
|
}
|
|
428
460
|
|
|
429
461
|
// src/resources/account.ts
|
|
462
|
+
var DomainClaims = class extends Resource {
|
|
463
|
+
/** `POST /domains/claim`: starts a claim on a domain another team already holds. */
|
|
464
|
+
create(payload, options) {
|
|
465
|
+
return this.core.request("post", "/domains/claim", { body: payload, options });
|
|
466
|
+
}
|
|
467
|
+
/** `GET /domains/{id}/claim` */
|
|
468
|
+
get(id, options) {
|
|
469
|
+
return this.core.request("get", "/domains/{id}/claim", { path: { id }, options });
|
|
470
|
+
}
|
|
471
|
+
/** `POST /domains/{id}/claim/verify` */
|
|
472
|
+
verify(id, options) {
|
|
473
|
+
return this.core.request("post", "/domains/{id}/claim/verify", { path: { id }, options });
|
|
474
|
+
}
|
|
475
|
+
};
|
|
430
476
|
var Domains = class extends Resource {
|
|
477
|
+
claims = new DomainClaims(this.core);
|
|
431
478
|
/** `POST /domains` */
|
|
432
479
|
create(payload, options) {
|
|
433
480
|
return this.core.request("post", "/domains", { body: payload, options });
|
|
@@ -470,12 +517,56 @@ var ApiKeys = class extends Resource {
|
|
|
470
517
|
listAll(params, options) {
|
|
471
518
|
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
472
519
|
}
|
|
520
|
+
/** `PATCH /api-keys/{id}` */
|
|
521
|
+
update(id, payload, options) {
|
|
522
|
+
return this.core.request("patch", "/api-keys/{id}", { path: { id }, body: payload, options });
|
|
523
|
+
}
|
|
473
524
|
/** `DELETE /api-keys/{id}` */
|
|
474
525
|
delete(id, options) {
|
|
475
526
|
return this.core.request("delete", "/api-keys/{id}", { path: { id }, options });
|
|
476
527
|
}
|
|
477
528
|
};
|
|
529
|
+
var WebhookEventAttempts = class extends Resource {
|
|
530
|
+
/** `GET /webhooks/{id}/events/{event_id}/attempts` */
|
|
531
|
+
list(id, eventId, params, options) {
|
|
532
|
+
return this.core.request("get", "/webhooks/{id}/events/{event_id}/attempts", {
|
|
533
|
+
path: { id, event_id: eventId },
|
|
534
|
+
query: params,
|
|
535
|
+
options
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
/** Every delivery attempt of one webhook event, fetched page by page. */
|
|
539
|
+
listAll(id, eventId, params, options) {
|
|
540
|
+
return paginate((cursor) => this.list(id, eventId, { ...params, ...cursor }, options), params);
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
var WebhookEvents = class extends Resource {
|
|
544
|
+
attempts = new WebhookEventAttempts(this.core);
|
|
545
|
+
/** `GET /webhooks/{id}/events` */
|
|
546
|
+
list(id, params, options) {
|
|
547
|
+
return this.core.request("get", "/webhooks/{id}/events", { path: { id }, query: params, options });
|
|
548
|
+
}
|
|
549
|
+
/** Every event of one webhook, fetched page by page. */
|
|
550
|
+
listAll(id, params, options) {
|
|
551
|
+
return paginate((cursor) => this.list(id, { ...params, ...cursor }, options), params);
|
|
552
|
+
}
|
|
553
|
+
/** `GET /webhooks/{id}/events/{event_id}` */
|
|
554
|
+
get(id, eventId, options) {
|
|
555
|
+
return this.core.request("get", "/webhooks/{id}/events/{event_id}", {
|
|
556
|
+
path: { id, event_id: eventId },
|
|
557
|
+
options
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
/** `POST /webhooks/{id}/events/{event_id}/replay` */
|
|
561
|
+
replay(id, eventId, options) {
|
|
562
|
+
return this.core.request("post", "/webhooks/{id}/events/{event_id}/replay", {
|
|
563
|
+
path: { id, event_id: eventId },
|
|
564
|
+
options
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
};
|
|
478
568
|
var Webhooks = class extends Resource {
|
|
569
|
+
events = new WebhookEvents(this.core);
|
|
479
570
|
/** `POST /webhooks` */
|
|
480
571
|
create(payload, options) {
|
|
481
572
|
return this.core.request("post", "/webhooks", { body: payload, options });
|
|
@@ -500,9 +591,41 @@ var Webhooks = class extends Resource {
|
|
|
500
591
|
delete(id, options) {
|
|
501
592
|
return this.core.request("delete", "/webhooks/{id}", { path: { id }, options });
|
|
502
593
|
}
|
|
503
|
-
/** `POST /webhooks/{id}/rotate`: rotates the signing secret. */
|
|
504
|
-
|
|
505
|
-
return this.core.request("post", "/webhooks/{id}/rotate", {
|
|
594
|
+
/** `POST /webhooks/{id}/signing-secret/rotate`: rotates the signing secret. */
|
|
595
|
+
rotateSigningSecret(id, payload, options) {
|
|
596
|
+
return this.core.request("post", "/webhooks/{id}/signing-secret/rotate", {
|
|
597
|
+
path: { id },
|
|
598
|
+
body: payload,
|
|
599
|
+
options
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
var Logs = class extends Resource {
|
|
604
|
+
/** `GET /logs` */
|
|
605
|
+
list(params, options) {
|
|
606
|
+
return this.core.request("get", "/logs", { query: params, options });
|
|
607
|
+
}
|
|
608
|
+
/** Every API request log entry, fetched page by page. */
|
|
609
|
+
listAll(params, options) {
|
|
610
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
611
|
+
}
|
|
612
|
+
/** `GET /logs/{id}` */
|
|
613
|
+
get(id, options) {
|
|
614
|
+
return this.core.request("get", "/logs/{id}", { path: { id }, options });
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
var OauthGrants = class extends Resource {
|
|
618
|
+
/** `GET /oauth/grants` */
|
|
619
|
+
list(params, options) {
|
|
620
|
+
return this.core.request("get", "/oauth/grants", { query: params, options });
|
|
621
|
+
}
|
|
622
|
+
/** Every OAuth grant, fetched page by page. */
|
|
623
|
+
listAll(params, options) {
|
|
624
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
625
|
+
}
|
|
626
|
+
/** `DELETE /oauth/grants/{id}` */
|
|
627
|
+
revoke(id, options) {
|
|
628
|
+
return this.core.request("delete", "/oauth/grants/{id}", { path: { id }, options });
|
|
506
629
|
}
|
|
507
630
|
};
|
|
508
631
|
var Usage = class extends Resource {
|
|
@@ -532,6 +655,113 @@ var Agent = class extends Resource {
|
|
|
532
655
|
}
|
|
533
656
|
};
|
|
534
657
|
|
|
658
|
+
// src/resources/automations.ts
|
|
659
|
+
var AutomationRuns = class extends Resource {
|
|
660
|
+
/** `GET /automations/{id}/runs` */
|
|
661
|
+
list(id, params, options) {
|
|
662
|
+
return this.core.request("get", "/automations/{id}/runs", { path: { id }, query: params, options });
|
|
663
|
+
}
|
|
664
|
+
/** Every run of one automation, fetched page by page. */
|
|
665
|
+
listAll(id, params, options) {
|
|
666
|
+
return paginate((cursor) => this.list(id, { ...params, ...cursor }, options), params);
|
|
667
|
+
}
|
|
668
|
+
/** `GET /automations/{id}/runs/{run_id}` */
|
|
669
|
+
get(id, runId, options) {
|
|
670
|
+
return this.core.request("get", "/automations/{id}/runs/{run_id}", {
|
|
671
|
+
path: { id, run_id: runId },
|
|
672
|
+
options
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
/** `POST /automations/{id}/runs/{run_id}/cancel` */
|
|
676
|
+
cancel(id, runId, options) {
|
|
677
|
+
return this.core.request("post", "/automations/{id}/runs/{run_id}/cancel", {
|
|
678
|
+
path: { id, run_id: runId },
|
|
679
|
+
options
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
var Automations = class extends Resource {
|
|
684
|
+
runs = new AutomationRuns(this.core);
|
|
685
|
+
/** `POST /automations` */
|
|
686
|
+
create(payload, options) {
|
|
687
|
+
return this.core.request("post", "/automations", { body: payload, options });
|
|
688
|
+
}
|
|
689
|
+
/** `GET /automations` */
|
|
690
|
+
list(params, options) {
|
|
691
|
+
return this.core.request("get", "/automations", { query: params, options });
|
|
692
|
+
}
|
|
693
|
+
/** Every automation, fetched page by page. */
|
|
694
|
+
listAll(params, options) {
|
|
695
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
696
|
+
}
|
|
697
|
+
/** `GET /automations/{id}` */
|
|
698
|
+
get(id, options) {
|
|
699
|
+
return this.core.request("get", "/automations/{id}", { path: { id }, options });
|
|
700
|
+
}
|
|
701
|
+
/** `PATCH /automations/{id}` */
|
|
702
|
+
update(id, payload, options) {
|
|
703
|
+
return this.core.request("patch", "/automations/{id}", { path: { id }, body: payload, options });
|
|
704
|
+
}
|
|
705
|
+
/** `DELETE /automations/{id}` */
|
|
706
|
+
remove(id, options) {
|
|
707
|
+
return this.core.request("delete", "/automations/{id}", { path: { id }, options });
|
|
708
|
+
}
|
|
709
|
+
/** `POST /automations/validate`: checks a definition without saving it. */
|
|
710
|
+
validate(payload, options) {
|
|
711
|
+
return this.core.request("post", "/automations/validate", { body: payload, options });
|
|
712
|
+
}
|
|
713
|
+
/** `POST /automations/{id}/duplicate` */
|
|
714
|
+
duplicate(id, options) {
|
|
715
|
+
return this.core.request("post", "/automations/{id}/duplicate", { path: { id }, options });
|
|
716
|
+
}
|
|
717
|
+
/** `POST /automations/{id}/simulate` */
|
|
718
|
+
simulate(id, payload, options) {
|
|
719
|
+
return this.core.request("post", "/automations/{id}/simulate", { path: { id }, body: payload, options });
|
|
720
|
+
}
|
|
721
|
+
/** `POST /automations/{id}/stop` */
|
|
722
|
+
stop(id, options) {
|
|
723
|
+
return this.core.request("post", "/automations/{id}/stop", { path: { id }, options });
|
|
724
|
+
}
|
|
725
|
+
/** `GET /automations/{id}/metrics` */
|
|
726
|
+
metrics(id, params, options) {
|
|
727
|
+
return this.core.request("get", "/automations/{id}/metrics", { path: { id }, query: params, options });
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
var Events = class extends Resource {
|
|
731
|
+
/** `POST /events`: defines a custom event. */
|
|
732
|
+
create(payload, options) {
|
|
733
|
+
return this.core.request("post", "/events", { body: payload, options });
|
|
734
|
+
}
|
|
735
|
+
/** `GET /events` */
|
|
736
|
+
list(params, options) {
|
|
737
|
+
return this.core.request("get", "/events", { query: params, options });
|
|
738
|
+
}
|
|
739
|
+
/** Every custom event, fetched page by page. */
|
|
740
|
+
listAll(params, options) {
|
|
741
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
742
|
+
}
|
|
743
|
+
/** `GET /events/{id_or_name}` */
|
|
744
|
+
get(idOrName, options) {
|
|
745
|
+
return this.core.request("get", "/events/{id_or_name}", { path: { id_or_name: idOrName }, options });
|
|
746
|
+
}
|
|
747
|
+
/** `PATCH /events/{id_or_name}` */
|
|
748
|
+
update(idOrName, payload, options) {
|
|
749
|
+
return this.core.request("patch", "/events/{id_or_name}", {
|
|
750
|
+
path: { id_or_name: idOrName },
|
|
751
|
+
body: payload,
|
|
752
|
+
options
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
/** `DELETE /events/{id_or_name}` */
|
|
756
|
+
remove(idOrName, options) {
|
|
757
|
+
return this.core.request("delete", "/events/{id_or_name}", { path: { id_or_name: idOrName }, options });
|
|
758
|
+
}
|
|
759
|
+
/** `POST /events/send`: sends an event that starts the automations listening for it. */
|
|
760
|
+
send(payload, options) {
|
|
761
|
+
return this.core.request("post", "/events/send", { body: payload, ...idempotent(options) });
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
|
|
535
765
|
// src/resources/broadcasts.ts
|
|
536
766
|
var Broadcasts = class extends Resource {
|
|
537
767
|
/** `POST /broadcasts` */
|
|
@@ -558,6 +788,30 @@ var Broadcasts = class extends Resource {
|
|
|
558
788
|
delete(id, options) {
|
|
559
789
|
return this.core.request("delete", "/broadcasts/{id}", { path: { id }, options });
|
|
560
790
|
}
|
|
791
|
+
/** `GET /broadcasts/{id}/recipients`: the recipients with the given outcome (`type` is required). */
|
|
792
|
+
recipients(id, params, options) {
|
|
793
|
+
return this.core.request("get", "/broadcasts/{id}/recipients", {
|
|
794
|
+
path: { id },
|
|
795
|
+
query: params,
|
|
796
|
+
options
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
/** Every recipient of a broadcast, fetched page by page. */
|
|
800
|
+
recipientsAll(id, params, options) {
|
|
801
|
+
return paginate((cursor) => this.recipients(id, { ...params, ...cursor }, options), params);
|
|
802
|
+
}
|
|
803
|
+
/** `GET /broadcasts/{id}/clicked-links` */
|
|
804
|
+
clickedLinks(id, params, options) {
|
|
805
|
+
return this.core.request("get", "/broadcasts/{id}/clicked-links", {
|
|
806
|
+
path: { id },
|
|
807
|
+
query: params,
|
|
808
|
+
options
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
/** Every link clicked in a broadcast, fetched page by page. */
|
|
812
|
+
clickedLinksAll(id, params, options) {
|
|
813
|
+
return paginate((cursor) => this.clickedLinks(id, { ...params, ...cursor }, options), params);
|
|
814
|
+
}
|
|
561
815
|
/** `POST /broadcasts/{id}/send` */
|
|
562
816
|
send(id, payload, options) {
|
|
563
817
|
return this.core.request("post", "/broadcasts/{id}/send", { path: { id }, body: payload, options });
|
|
@@ -608,10 +862,7 @@ var Templates = class extends Resource {
|
|
|
608
862
|
|
|
609
863
|
// src/resources/contacts.ts
|
|
610
864
|
var ContactBatch = class extends Resource {
|
|
611
|
-
/**
|
|
612
|
-
* `POST /contacts/batch`: up to 1000 contacts, each shaped like {@link Contacts.create}'s payload.
|
|
613
|
-
* The API document leaves the items untyped; the endpoint validates each one as a contact.
|
|
614
|
-
*/
|
|
865
|
+
/** `POST /contacts/batch`: up to 1000 contacts, each shaped like {@link Contacts.create}'s payload. */
|
|
615
866
|
create(payload, params, options) {
|
|
616
867
|
return this.core.request("post", "/contacts/batch", {
|
|
617
868
|
body: payload,
|
|
@@ -643,7 +894,50 @@ var ContactTopics = class extends Resource {
|
|
|
643
894
|
});
|
|
644
895
|
}
|
|
645
896
|
};
|
|
897
|
+
var ContactImports = class extends Resource {
|
|
898
|
+
/** `POST /contacts/imports`: uploads a CSV and imports it in the background; poll {@link ContactImports.get}. */
|
|
899
|
+
create(params, options) {
|
|
900
|
+
const form = new FormData();
|
|
901
|
+
const { file, filename, column_map, on_conflict, segments, topics } = params;
|
|
902
|
+
const named = typeof file !== "string" && "name" in file && typeof file.name === "string" && file.name !== "";
|
|
903
|
+
form.append(
|
|
904
|
+
"file",
|
|
905
|
+
typeof file === "string" ? new Blob([file], { type: "text/csv" }) : file,
|
|
906
|
+
filename ?? (named ? file.name : "contacts.csv")
|
|
907
|
+
);
|
|
908
|
+
const json = (value) => typeof value === "string" ? value : JSON.stringify(value);
|
|
909
|
+
if (column_map !== void 0) form.append("column_map", json(column_map));
|
|
910
|
+
if (on_conflict !== void 0) form.append("on_conflict", on_conflict);
|
|
911
|
+
if (segments !== void 0) form.append("segments", json(segments));
|
|
912
|
+
if (topics !== void 0) form.append("topics", json(topics));
|
|
913
|
+
return this.core.request("post", "/contacts/imports", { form, options });
|
|
914
|
+
}
|
|
915
|
+
/** `GET /contacts/imports` */
|
|
916
|
+
list(params, options) {
|
|
917
|
+
return this.core.request("get", "/contacts/imports", { query: params, options });
|
|
918
|
+
}
|
|
919
|
+
/** Every contact import, fetched page by page. */
|
|
920
|
+
listAll(params, options) {
|
|
921
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
922
|
+
}
|
|
923
|
+
/** `GET /contacts/imports/{id}` */
|
|
924
|
+
get(id, options) {
|
|
925
|
+
return this.core.request("get", "/contacts/imports/{id}", { path: { id }, options });
|
|
926
|
+
}
|
|
927
|
+
};
|
|
646
928
|
var ContactSegments = class extends Resource {
|
|
929
|
+
/** `GET /contacts/{id}/segments` */
|
|
930
|
+
list(contactId, params, options) {
|
|
931
|
+
return this.core.request("get", "/contacts/{id}/segments", {
|
|
932
|
+
path: { id: contactId },
|
|
933
|
+
query: params,
|
|
934
|
+
options
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
/** Every segment a contact belongs to, fetched page by page. */
|
|
938
|
+
listAll(contactId, params, options) {
|
|
939
|
+
return paginate((cursor) => this.list(contactId, { ...params, ...cursor }, options), params);
|
|
940
|
+
}
|
|
647
941
|
/** `POST /contacts/{id}/segments/{segmentId}` */
|
|
648
942
|
add(contactId, segmentId, options) {
|
|
649
943
|
return this.core.request("post", "/contacts/{id}/segments/{segmentId}", {
|
|
@@ -661,6 +955,7 @@ var ContactSegments = class extends Resource {
|
|
|
661
955
|
};
|
|
662
956
|
var Contacts = class extends Resource {
|
|
663
957
|
batch = new ContactBatch(this.core);
|
|
958
|
+
imports = new ContactImports(this.core);
|
|
664
959
|
topics = new ContactTopics(this.core);
|
|
665
960
|
segments = new ContactSegments(this.core);
|
|
666
961
|
/** `POST /contacts` */
|
|
@@ -882,16 +1177,47 @@ var ReceivedEmails = class extends Resource {
|
|
|
882
1177
|
});
|
|
883
1178
|
}
|
|
884
1179
|
};
|
|
1180
|
+
var EmailAttachments = class extends Resource {
|
|
1181
|
+
/** `GET /emails/{email_id}/attachments` */
|
|
1182
|
+
list(emailId, params, options) {
|
|
1183
|
+
return this.core.request("get", "/emails/{email_id}/attachments", {
|
|
1184
|
+
path: { email_id: emailId },
|
|
1185
|
+
query: params,
|
|
1186
|
+
options
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
/** Every attachment of a sent email, fetched page by page. */
|
|
1190
|
+
listAll(emailId, params, options) {
|
|
1191
|
+
return paginate((cursor) => this.list(emailId, { ...params, ...cursor }, options), params);
|
|
1192
|
+
}
|
|
1193
|
+
/** `GET /emails/{email_id}/attachments/{id}` */
|
|
1194
|
+
get(emailId, id, options) {
|
|
1195
|
+
return this.core.request("get", "/emails/{email_id}/attachments/{id}", {
|
|
1196
|
+
path: { email_id: emailId, id },
|
|
1197
|
+
options
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
};
|
|
885
1201
|
var Emails = class extends Resource {
|
|
886
1202
|
receiving = new ReceivedEmails(this.core);
|
|
1203
|
+
attachments = new EmailAttachments(this.core);
|
|
1204
|
+
metrics;
|
|
1205
|
+
constructor(core) {
|
|
1206
|
+
super(core);
|
|
1207
|
+
this.metrics = Object.assign(
|
|
1208
|
+
(params, options) => core.request("get", "/emails/metrics", { query: params, options }),
|
|
1209
|
+
{
|
|
1210
|
+
links: (params, options) => core.request("get", "/emails/metrics/links", { query: params, options }),
|
|
1211
|
+
countries: (params, options) => core.request("get", "/emails/metrics/countries", { query: params, options }),
|
|
1212
|
+
clients: (params, options) => core.request("get", "/emails/metrics/clients", { query: params, options })
|
|
1213
|
+
}
|
|
1214
|
+
);
|
|
1215
|
+
}
|
|
887
1216
|
/** `POST /emails` */
|
|
888
1217
|
send(payload, options) {
|
|
889
1218
|
return this.core.request("post", "/emails", { body: payload, ...idempotent(options) });
|
|
890
1219
|
}
|
|
891
|
-
/**
|
|
892
|
-
* `POST /emails/batch`: up to 100 emails, each shaped like {@link Emails.send}'s payload.
|
|
893
|
-
* The API document leaves the items untyped; the endpoint validates each one as a send.
|
|
894
|
-
*/
|
|
1220
|
+
/** `POST /emails/batch`: up to 100 emails, each shaped like {@link Emails.send}'s payload. */
|
|
895
1221
|
batch(payload, options) {
|
|
896
1222
|
return this.core.request("post", "/emails/batch", {
|
|
897
1223
|
body: payload,
|
|
@@ -927,25 +1253,29 @@ var Emails = class extends Resource {
|
|
|
927
1253
|
insights(id, options) {
|
|
928
1254
|
return this.core.request("get", "/emails/{id}/insights", { path: { id }, options });
|
|
929
1255
|
}
|
|
930
|
-
/** `
|
|
931
|
-
|
|
932
|
-
return this.core.request("
|
|
1256
|
+
/** `POST /emails/{id}/share` */
|
|
1257
|
+
share(id, payload, options) {
|
|
1258
|
+
return this.core.request("post", "/emails/{id}/share", { path: { id }, body: payload, options });
|
|
1259
|
+
}
|
|
1260
|
+
/** `DELETE /emails/{id}/share` */
|
|
1261
|
+
unshare(id, options) {
|
|
1262
|
+
return this.core.request("delete", "/emails/{id}/share", { path: { id }, options });
|
|
933
1263
|
}
|
|
934
1264
|
};
|
|
935
1265
|
|
|
936
1266
|
// src/sse.ts
|
|
937
1267
|
var STREAM_GAP_EVENT = "stream.gap";
|
|
1268
|
+
var DEFAULT_IDLE_TIMEOUT_MS = 45e3;
|
|
938
1269
|
var LINE_BREAK = /\r\n|\r|\n/;
|
|
939
|
-
async function* parseEventStream(body, hooks =
|
|
940
|
-
} }, initialId = null) {
|
|
1270
|
+
async function* parseEventStream(body, hooks, initialId = null) {
|
|
941
1271
|
const reader = body.getReader();
|
|
942
1272
|
const decoder = new TextDecoder("utf-8");
|
|
943
1273
|
let buffer = "";
|
|
944
|
-
let first = true;
|
|
945
1274
|
let lastId = initialId;
|
|
946
1275
|
let eventName = "";
|
|
947
1276
|
let dataLines = [];
|
|
948
1277
|
let hasData = false;
|
|
1278
|
+
let idPending = false;
|
|
949
1279
|
function* takeLines(final) {
|
|
950
1280
|
while (true) {
|
|
951
1281
|
const match = LINE_BREAK.exec(buffer);
|
|
@@ -960,8 +1290,11 @@ async function* parseEventStream(body, hooks = { onRetry() {
|
|
|
960
1290
|
if (line === "") {
|
|
961
1291
|
if (!hasData) {
|
|
962
1292
|
eventName = "";
|
|
1293
|
+
if (idPending && lastId !== null) hooks.onId?.(lastId);
|
|
1294
|
+
idPending = false;
|
|
963
1295
|
return void 0;
|
|
964
1296
|
}
|
|
1297
|
+
idPending = false;
|
|
965
1298
|
const raw = dataLines.join("\n");
|
|
966
1299
|
let data = raw;
|
|
967
1300
|
try {
|
|
@@ -989,7 +1322,10 @@ async function* parseEventStream(body, hooks = { onRetry() {
|
|
|
989
1322
|
hasData = true;
|
|
990
1323
|
break;
|
|
991
1324
|
case "id":
|
|
992
|
-
if (!value.includes("\0"))
|
|
1325
|
+
if (!value.includes("\0")) {
|
|
1326
|
+
lastId = value;
|
|
1327
|
+
idPending = true;
|
|
1328
|
+
}
|
|
993
1329
|
break;
|
|
994
1330
|
case "retry":
|
|
995
1331
|
if (/^\d+$/.test(value)) hooks.onRetry(Number(value));
|
|
@@ -1008,11 +1344,8 @@ async function* parseEventStream(body, hooks = { onRetry() {
|
|
|
1008
1344
|
}
|
|
1009
1345
|
return;
|
|
1010
1346
|
}
|
|
1347
|
+
hooks.onActivity?.();
|
|
1011
1348
|
buffer += decoder.decode(value, { stream: true });
|
|
1012
|
-
if (first && buffer.length > 0) {
|
|
1013
|
-
if (buffer.charCodeAt(0) === 65279) buffer = buffer.slice(1);
|
|
1014
|
-
first = false;
|
|
1015
|
-
}
|
|
1016
1349
|
for (const line of takeLines(false)) {
|
|
1017
1350
|
const event = processLine(line);
|
|
1018
1351
|
if (event) yield event;
|
|
@@ -1077,10 +1410,23 @@ var EventStream = class {
|
|
|
1077
1410
|
while (!this.#closed) {
|
|
1078
1411
|
const connection = this.#connection;
|
|
1079
1412
|
if (connection !== null && connection.response.body !== null) {
|
|
1413
|
+
const idleMs = this.#options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
|
|
1414
|
+
let idle;
|
|
1415
|
+
const arm = () => {
|
|
1416
|
+
clearTimeout(idle);
|
|
1417
|
+
if (idleMs > 0) idle = setTimeout(() => connection.close(), idleMs);
|
|
1418
|
+
};
|
|
1419
|
+
arm();
|
|
1080
1420
|
try {
|
|
1081
1421
|
const events = parseEventStream(
|
|
1082
1422
|
connection.response.body,
|
|
1083
|
-
{
|
|
1423
|
+
{
|
|
1424
|
+
onRetry: (ms) => this.#delayMs = ms,
|
|
1425
|
+
onActivity: arm,
|
|
1426
|
+
// An id-only block (the position a new stream opens at) counts: a connection that ends
|
|
1427
|
+
// before its first event must still resume from it.
|
|
1428
|
+
onId: (id) => this.#lastEventId = id
|
|
1429
|
+
},
|
|
1084
1430
|
this.#lastEventId
|
|
1085
1431
|
);
|
|
1086
1432
|
for await (const event of events) {
|
|
@@ -1099,6 +1445,8 @@ var EventStream = class {
|
|
|
1099
1445
|
if (this.#endsOn.has(event.event)) return;
|
|
1100
1446
|
}
|
|
1101
1447
|
} catch {
|
|
1448
|
+
} finally {
|
|
1449
|
+
clearTimeout(idle);
|
|
1102
1450
|
}
|
|
1103
1451
|
connection.close();
|
|
1104
1452
|
this.#connection = null;
|
|
@@ -1146,6 +1494,14 @@ var InboxAgentSettings = class extends Resource {
|
|
|
1146
1494
|
get(inboxId, options) {
|
|
1147
1495
|
return this.core.request("get", "/inboxes/{inbox_id}/agent", { path: { inbox_id: inboxId }, options });
|
|
1148
1496
|
}
|
|
1497
|
+
/** `PATCH /inboxes/{inbox_id}/agent` */
|
|
1498
|
+
update(inboxId, payload, options) {
|
|
1499
|
+
return this.core.request("patch", "/inboxes/{inbox_id}/agent", {
|
|
1500
|
+
path: { inbox_id: inboxId },
|
|
1501
|
+
body: payload,
|
|
1502
|
+
options
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1149
1505
|
};
|
|
1150
1506
|
var InboxSafety = class extends Resource {
|
|
1151
1507
|
/** `GET /inboxes/{inbox_id}/safety` */
|
|
@@ -1541,10 +1897,6 @@ var Unsubscribes = class extends Resource {
|
|
|
1541
1897
|
listAll(params, options) {
|
|
1542
1898
|
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
1543
1899
|
}
|
|
1544
|
-
/** `DELETE /unsubscribes/{id}` */
|
|
1545
|
-
delete(id, options) {
|
|
1546
|
-
return this.core.request("delete", "/unsubscribes/{id}", { path: { id }, options });
|
|
1547
|
-
}
|
|
1548
1900
|
};
|
|
1549
1901
|
var Verifications = class extends Resource {
|
|
1550
1902
|
/** `GET /verifications/availability` */
|
|
@@ -1593,6 +1945,10 @@ var BillionSend = class {
|
|
|
1593
1945
|
deliverability;
|
|
1594
1946
|
inboxes;
|
|
1595
1947
|
agent;
|
|
1948
|
+
automations;
|
|
1949
|
+
events;
|
|
1950
|
+
logs;
|
|
1951
|
+
oauthGrants;
|
|
1596
1952
|
#core;
|
|
1597
1953
|
/**
|
|
1598
1954
|
* @param apiKeyOrOptions An API key (`bs_...`), or options. Without a key the
|
|
@@ -1620,6 +1976,10 @@ var BillionSend = class {
|
|
|
1620
1976
|
this.deliverability = new Deliverability(core);
|
|
1621
1977
|
this.inboxes = new Inboxes(core);
|
|
1622
1978
|
this.agent = new Agent(core);
|
|
1979
|
+
this.automations = new Automations(core);
|
|
1980
|
+
this.events = new Events(core);
|
|
1981
|
+
this.logs = new Logs(core);
|
|
1982
|
+
this.oauthGrants = new OauthGrants(core);
|
|
1623
1983
|
}
|
|
1624
1984
|
/** The API base URL this client calls. */
|
|
1625
1985
|
get baseUrl() {
|
|
@@ -1656,7 +2016,8 @@ function encodeBase64(bytes) {
|
|
|
1656
2016
|
return btoa(binary);
|
|
1657
2017
|
}
|
|
1658
2018
|
function secretKey(secret) {
|
|
1659
|
-
const
|
|
2019
|
+
const text = secret.trim();
|
|
2020
|
+
const encoded = text.startsWith(SECRET_PREFIX) ? text.slice(SECRET_PREFIX.length) : null;
|
|
1660
2021
|
const key = encoded === null ? null : decodeBase64(encoded);
|
|
1661
2022
|
if (encoded === null || key === null || key.length < SECRET_MIN_BYTES || key.length > SECRET_MAX_BYTES || encodeBase64(key) !== encoded) {
|
|
1662
2023
|
throw new WebhookVerificationError("The webhook secret is not a whsec_ secret");
|
|
@@ -1675,14 +2036,12 @@ function bytesOf(payload) {
|
|
|
1675
2036
|
}
|
|
1676
2037
|
function equal(a, b) {
|
|
1677
2038
|
if (a.length !== b.length) return false;
|
|
1678
|
-
|
|
1679
|
-
for (let i = 0; i < a.length; i++) diff |= (a[i] ?? 0) ^ (b[i] ?? 0);
|
|
1680
|
-
return diff === 0;
|
|
2039
|
+
return a.reduce((diff, byte, i) => diff | byte ^ b[i], 0) === 0;
|
|
1681
2040
|
}
|
|
1682
2041
|
async function verifyWebhook(payload, headers, secret, options = {}) {
|
|
1683
|
-
const id = header(headers, "webhook-id");
|
|
1684
|
-
const timestamp = header(headers, "webhook-timestamp");
|
|
1685
|
-
const signature = header(headers, "webhook-signature");
|
|
2042
|
+
const id = header(headers, "webhook-id")?.trim();
|
|
2043
|
+
const timestamp = header(headers, "webhook-timestamp")?.trim();
|
|
2044
|
+
const signature = header(headers, "webhook-signature")?.trim();
|
|
1686
2045
|
if (!id || !timestamp || !signature) {
|
|
1687
2046
|
throw new WebhookVerificationError(
|
|
1688
2047
|
"Missing the webhook-id, webhook-timestamp or webhook-signature header"
|
|
@@ -1704,7 +2063,7 @@ async function verifyWebhook(payload, headers, secret, options = {}) {
|
|
|
1704
2063
|
signed.set(prefix);
|
|
1705
2064
|
signed.set(body, prefix.length);
|
|
1706
2065
|
const expected = new Uint8Array(await subtle.sign("HMAC", key, signed));
|
|
1707
|
-
const matched = signature.split(
|
|
2066
|
+
const matched = signature.split(/\s+/).some((candidate) => {
|
|
1708
2067
|
const comma = candidate.indexOf(",");
|
|
1709
2068
|
if (comma === -1 || candidate.slice(0, comma) !== "v1") return false;
|
|
1710
2069
|
const given = decodeBase64(candidate.slice(comma + 1));
|