@billionsend/node 0.0.0-stage → 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 +21 -0
- package/LICENSE +21 -0
- package/README.md +277 -2
- package/dist/index.cjs +1745 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +16973 -0
- package/dist/index.d.ts +16973 -0
- package/dist/index.js +1723 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1723 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var BillionSendError = class extends Error {
|
|
3
|
+
statusCode;
|
|
4
|
+
name;
|
|
5
|
+
requestId;
|
|
6
|
+
constructor(init) {
|
|
7
|
+
super(init.message);
|
|
8
|
+
this.name = init.name ?? "application_error";
|
|
9
|
+
this.statusCode = init.statusCode ?? null;
|
|
10
|
+
this.requestId = init.requestId ?? null;
|
|
11
|
+
}
|
|
12
|
+
toJSON() {
|
|
13
|
+
return { statusCode: this.statusCode, name: this.name, message: this.message, requestId: this.requestId };
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var ValidationError = class extends BillionSendError {
|
|
17
|
+
};
|
|
18
|
+
var AuthenticationError = class extends BillionSendError {
|
|
19
|
+
};
|
|
20
|
+
var PermissionDeniedError = class extends BillionSendError {
|
|
21
|
+
};
|
|
22
|
+
var NotFoundError = class extends BillionSendError {
|
|
23
|
+
};
|
|
24
|
+
var ConflictError = class extends BillionSendError {
|
|
25
|
+
};
|
|
26
|
+
var RateLimitError = class extends BillionSendError {
|
|
27
|
+
retryAfter;
|
|
28
|
+
constructor(init) {
|
|
29
|
+
super(init);
|
|
30
|
+
this.retryAfter = init.retryAfter ?? null;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var APIError = class extends BillionSendError {
|
|
34
|
+
retryAfter;
|
|
35
|
+
constructor(init) {
|
|
36
|
+
super(init);
|
|
37
|
+
this.retryAfter = init.retryAfter ?? null;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var APIConnectionError = class extends BillionSendError {
|
|
41
|
+
constructor(message = "Connection error") {
|
|
42
|
+
super({ message });
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var APITimeoutError = class extends APIConnectionError {
|
|
46
|
+
constructor(message = "Request timed out") {
|
|
47
|
+
super(message);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var WebhookVerificationError = class extends BillionSendError {
|
|
51
|
+
constructor(message) {
|
|
52
|
+
super({ message, name: "webhook_verification_error" });
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
function applicationError(message, statusCode = null) {
|
|
56
|
+
return new BillionSendError({ message, statusCode });
|
|
57
|
+
}
|
|
58
|
+
function isRecord(value) {
|
|
59
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
60
|
+
}
|
|
61
|
+
function parseRetryAfter(value, now = Date.now()) {
|
|
62
|
+
if (value == null || value.trim() === "") return null;
|
|
63
|
+
const seconds = Number(value);
|
|
64
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds);
|
|
65
|
+
const at = Date.parse(value);
|
|
66
|
+
return Number.isNaN(at) ? null : Math.max(0, (at - now) / 1e3);
|
|
67
|
+
}
|
|
68
|
+
function errorClass(status) {
|
|
69
|
+
switch (status) {
|
|
70
|
+
case 400:
|
|
71
|
+
case 422:
|
|
72
|
+
return ValidationError;
|
|
73
|
+
case 401:
|
|
74
|
+
return AuthenticationError;
|
|
75
|
+
case 403:
|
|
76
|
+
return PermissionDeniedError;
|
|
77
|
+
case 404:
|
|
78
|
+
return NotFoundError;
|
|
79
|
+
case 409:
|
|
80
|
+
return ConflictError;
|
|
81
|
+
default:
|
|
82
|
+
return status >= 500 ? APIError : BillionSendError;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function errorFromResponse(status, statusText, text, headers = {}) {
|
|
86
|
+
let body;
|
|
87
|
+
try {
|
|
88
|
+
body = text === "" ? void 0 : JSON.parse(text);
|
|
89
|
+
} catch {
|
|
90
|
+
body = void 0;
|
|
91
|
+
}
|
|
92
|
+
let name = "application_error";
|
|
93
|
+
let message;
|
|
94
|
+
if (isRecord(body) && typeof body.name === "string" && typeof body.message === "string") {
|
|
95
|
+
name = body.name;
|
|
96
|
+
message = body.message;
|
|
97
|
+
} else {
|
|
98
|
+
const detail = text.trim().slice(0, 200);
|
|
99
|
+
const label = `${status}${statusText ? ` ${statusText}` : ""}`;
|
|
100
|
+
message = detail ? `Unexpected ${label} response: ${detail}` : `Unexpected ${label} response`;
|
|
101
|
+
}
|
|
102
|
+
const init = { message, name, statusCode: status, requestId: headers["x-request-id"] ?? null };
|
|
103
|
+
if (status === 429)
|
|
104
|
+
return new RateLimitError({ ...init, retryAfter: parseRetryAfter(headers["retry-after"]) });
|
|
105
|
+
if (status >= 500) return new APIError({ ...init, retryAfter: parseRetryAfter(headers["retry-after"]) });
|
|
106
|
+
return new (errorClass(status))(init);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/retry.ts
|
|
110
|
+
var MAX_RETRY_AFTER_MS = 6e4;
|
|
111
|
+
var BASE_DELAY_MS = 500;
|
|
112
|
+
var MAX_BACKOFF_MS = 8e3;
|
|
113
|
+
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]);
|
|
114
|
+
function retryDelayMs(attempt, retryAfter, now = Date.now(), random = Math.random) {
|
|
115
|
+
if (retryAfter != null && retryAfter.trim() !== "") {
|
|
116
|
+
let ms;
|
|
117
|
+
const seconds = Number(retryAfter);
|
|
118
|
+
if (Number.isFinite(seconds)) {
|
|
119
|
+
ms = Math.max(0, seconds * 1e3);
|
|
120
|
+
} else {
|
|
121
|
+
const at = Date.parse(retryAfter);
|
|
122
|
+
if (!Number.isNaN(at)) ms = Math.max(0, at - now);
|
|
123
|
+
}
|
|
124
|
+
if (ms !== void 0) return ms > MAX_RETRY_AFTER_MS ? void 0 : Math.ceil(ms);
|
|
125
|
+
}
|
|
126
|
+
const backoff = Math.min(BASE_DELAY_MS * 2 ** attempt, MAX_BACKOFF_MS);
|
|
127
|
+
return Math.round(backoff * (1 - random() * 0.25));
|
|
128
|
+
}
|
|
129
|
+
function retryable(error, idempotencyKey) {
|
|
130
|
+
if (error instanceof APIConnectionError) return true;
|
|
131
|
+
const status = error.statusCode;
|
|
132
|
+
if (status === null) return false;
|
|
133
|
+
if (status === 429 || status >= 500) return true;
|
|
134
|
+
return idempotencyKey && status === 409 && error.name === "concurrent_idempotent_requests";
|
|
135
|
+
}
|
|
136
|
+
function newIdempotencyKey() {
|
|
137
|
+
const webCrypto = globalThis.crypto;
|
|
138
|
+
if (typeof webCrypto?.randomUUID === "function") return webCrypto.randomUUID();
|
|
139
|
+
const bytes = new Uint8Array(16);
|
|
140
|
+
if (typeof webCrypto?.getRandomValues === "function") webCrypto.getRandomValues(bytes);
|
|
141
|
+
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
142
|
+
bytes[6] = (bytes[6] ?? 0) & 15 | 64;
|
|
143
|
+
bytes[8] = (bytes[8] ?? 0) & 63 | 128;
|
|
144
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
145
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
146
|
+
}
|
|
147
|
+
function sleep(ms, signal) {
|
|
148
|
+
return new Promise((resolve) => {
|
|
149
|
+
if (signal?.aborted) {
|
|
150
|
+
resolve(false);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const onAbort = () => {
|
|
154
|
+
clearTimeout(timer);
|
|
155
|
+
resolve(false);
|
|
156
|
+
};
|
|
157
|
+
const timer = setTimeout(() => {
|
|
158
|
+
signal?.removeEventListener("abort", onAbort);
|
|
159
|
+
resolve(true);
|
|
160
|
+
}, ms);
|
|
161
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/version.ts
|
|
166
|
+
var VERSION = "0.1.1";
|
|
167
|
+
|
|
168
|
+
// src/core.ts
|
|
169
|
+
var DEFAULT_BASE_URL = "https://api.billionsend.com";
|
|
170
|
+
var DEFAULT_TIMEOUT_MS = 6e4;
|
|
171
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
172
|
+
function readEnv(name) {
|
|
173
|
+
const env = globalThis.process?.env;
|
|
174
|
+
const value = env?.[name];
|
|
175
|
+
return value === void 0 || value === "" ? void 0 : value;
|
|
176
|
+
}
|
|
177
|
+
function failure(error, headers = null) {
|
|
178
|
+
return { data: null, error, headers };
|
|
179
|
+
}
|
|
180
|
+
function headersOf(response) {
|
|
181
|
+
const out = {};
|
|
182
|
+
response.headers.forEach((value, key) => {
|
|
183
|
+
out[key.toLowerCase()] = value;
|
|
184
|
+
});
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
function describe(cause) {
|
|
188
|
+
if (cause instanceof Error) {
|
|
189
|
+
const inner = cause.cause;
|
|
190
|
+
return inner instanceof Error ? `${cause.message} (${inner.message})` : cause.message;
|
|
191
|
+
}
|
|
192
|
+
return String(cause);
|
|
193
|
+
}
|
|
194
|
+
function lowerCaseKeys(headers) {
|
|
195
|
+
const out = {};
|
|
196
|
+
if (!headers) return out;
|
|
197
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
198
|
+
if (value !== void 0) out[key.toLowerCase()] = value;
|
|
199
|
+
}
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
function fillPath(template, params) {
|
|
203
|
+
let missing;
|
|
204
|
+
const filled = template.replace(/\{([^}]+)\}/g, (_, name) => {
|
|
205
|
+
const value = params?.[name];
|
|
206
|
+
if (value === void 0 || value === null || String(value) === "") {
|
|
207
|
+
missing ??= name;
|
|
208
|
+
return "";
|
|
209
|
+
}
|
|
210
|
+
return encodeURIComponent(String(value));
|
|
211
|
+
});
|
|
212
|
+
return missing === void 0 ? filled : applicationError(`Missing path parameter "${missing}"`);
|
|
213
|
+
}
|
|
214
|
+
function queryString(query) {
|
|
215
|
+
if (!query) return "";
|
|
216
|
+
const params = new URLSearchParams();
|
|
217
|
+
for (const [key, value] of Object.entries(query)) {
|
|
218
|
+
if (value === void 0 || value === null) continue;
|
|
219
|
+
if (Array.isArray(value)) {
|
|
220
|
+
params.append(key, value.map(String).join(","));
|
|
221
|
+
} else {
|
|
222
|
+
params.append(key, String(value));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const text = params.toString();
|
|
226
|
+
return text === "" ? "" : `?${text}`;
|
|
227
|
+
}
|
|
228
|
+
var Core = class {
|
|
229
|
+
baseUrl;
|
|
230
|
+
timeoutMs;
|
|
231
|
+
maxRetries;
|
|
232
|
+
#apiKey;
|
|
233
|
+
#fetch;
|
|
234
|
+
#headers;
|
|
235
|
+
constructor(options) {
|
|
236
|
+
this.#apiKey = options.apiKey ?? readEnv("BILLIONSEND_API_KEY");
|
|
237
|
+
this.baseUrl = (options.baseUrl ?? readEnv("BILLIONSEND_BASE_URL") ?? DEFAULT_BASE_URL).replace(
|
|
238
|
+
/\/+$/,
|
|
239
|
+
""
|
|
240
|
+
);
|
|
241
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
242
|
+
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
243
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
244
|
+
if (typeof fetchImpl !== "function") {
|
|
245
|
+
throw new TypeError("No fetch implementation: use Node.js 18 or later, or pass `fetch` in the options");
|
|
246
|
+
}
|
|
247
|
+
if (!(this.timeoutMs > 0)) throw new TypeError("timeoutMs must be a positive number");
|
|
248
|
+
if (!Number.isInteger(this.maxRetries) || this.maxRetries < 0) {
|
|
249
|
+
throw new TypeError("maxRetries must be a non-negative integer");
|
|
250
|
+
}
|
|
251
|
+
this.#fetch = fetchImpl;
|
|
252
|
+
this.#headers = lowerCaseKeys(options.headers);
|
|
253
|
+
}
|
|
254
|
+
/** Whether an API key is configured. */
|
|
255
|
+
get hasApiKey() {
|
|
256
|
+
return this.#apiKey !== void 0;
|
|
257
|
+
}
|
|
258
|
+
/** Calls one operation and parses its JSON answer. */
|
|
259
|
+
request(method, path, init = {}) {
|
|
260
|
+
return this.#execute(method, path, init, false);
|
|
261
|
+
}
|
|
262
|
+
/** Opens an event stream; the connection is timed only until the response headers arrive. */
|
|
263
|
+
openStream(method, path, init = {}) {
|
|
264
|
+
return this.#execute(method, path, init, true);
|
|
265
|
+
}
|
|
266
|
+
async #execute(lowerMethod, template, init, stream) {
|
|
267
|
+
const method = lowerMethod.toUpperCase();
|
|
268
|
+
const options = init.options ?? {};
|
|
269
|
+
const auth = init.auth !== false;
|
|
270
|
+
if (auth && this.#apiKey === void 0) {
|
|
271
|
+
return failure(
|
|
272
|
+
new AuthenticationError({
|
|
273
|
+
name: "missing_api_key",
|
|
274
|
+
message: "No API key: pass one to new BillionSend() or set BILLIONSEND_API_KEY"
|
|
275
|
+
})
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
const path = fillPath(template, init.path);
|
|
279
|
+
if (typeof path !== "string") return failure(path);
|
|
280
|
+
const url = `${this.baseUrl}${path}${queryString(init.query)}`;
|
|
281
|
+
const headers = {
|
|
282
|
+
accept: stream ? "text/event-stream" : "application/json",
|
|
283
|
+
"user-agent": `billionsend-node/${VERSION}`,
|
|
284
|
+
...this.#headers,
|
|
285
|
+
...lowerCaseKeys(options.headers),
|
|
286
|
+
...lowerCaseKeys(init.headers)
|
|
287
|
+
};
|
|
288
|
+
let body;
|
|
289
|
+
if (init.body !== void 0) {
|
|
290
|
+
body = JSON.stringify(init.body);
|
|
291
|
+
headers["content-type"] = "application/json";
|
|
292
|
+
}
|
|
293
|
+
if (init.idempotencyKey !== void 0) headers["idempotency-key"] = init.idempotencyKey;
|
|
294
|
+
else if (init.idempotent && headers["idempotency-key"] === void 0) {
|
|
295
|
+
headers["idempotency-key"] = newIdempotencyKey();
|
|
296
|
+
}
|
|
297
|
+
if (auth) headers.authorization = `Bearer ${this.#apiKey}`;
|
|
298
|
+
else delete headers.authorization;
|
|
299
|
+
const keyed = headers["idempotency-key"] !== void 0;
|
|
300
|
+
const retryable2 = IDEMPOTENT_METHODS.has(method) || keyed;
|
|
301
|
+
const maxRetries = options.maxRetries ?? this.maxRetries;
|
|
302
|
+
const timeoutMs = options.timeoutMs ?? this.timeoutMs;
|
|
303
|
+
for (let attempt = 0; ; attempt++) {
|
|
304
|
+
const outcome = await this.#attempt(method, url, headers, body, timeoutMs, options.signal, stream);
|
|
305
|
+
const error = outcome.result.error;
|
|
306
|
+
if (error === null || !retryable2 || attempt >= maxRetries || !retryable(error, keyed)) {
|
|
307
|
+
return outcome.result;
|
|
308
|
+
}
|
|
309
|
+
const delay = retryDelayMs(attempt, outcome.retryAfter);
|
|
310
|
+
if (delay === void 0) return outcome.result;
|
|
311
|
+
if (!await sleep(delay, options.signal)) return failure(applicationError("Request was aborted"));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
async #attempt(method, url, headers, body, timeoutMs, signal, stream) {
|
|
315
|
+
const controller = new AbortController();
|
|
316
|
+
let timedOut = false;
|
|
317
|
+
const timer = setTimeout(() => {
|
|
318
|
+
timedOut = true;
|
|
319
|
+
controller.abort();
|
|
320
|
+
}, timeoutMs);
|
|
321
|
+
const onAbort = () => controller.abort();
|
|
322
|
+
if (signal?.aborted) controller.abort();
|
|
323
|
+
else signal?.addEventListener("abort", onAbort, { once: true });
|
|
324
|
+
const release = () => {
|
|
325
|
+
clearTimeout(timer);
|
|
326
|
+
signal?.removeEventListener("abort", onAbort);
|
|
327
|
+
};
|
|
328
|
+
try {
|
|
329
|
+
const response = await this.#fetch(url, { method, headers, body, signal: controller.signal });
|
|
330
|
+
const responseHeaders = headersOf(response);
|
|
331
|
+
if (stream && response.ok) {
|
|
332
|
+
clearTimeout(timer);
|
|
333
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
334
|
+
if (!contentType.includes("text/event-stream")) {
|
|
335
|
+
release();
|
|
336
|
+
controller.abort();
|
|
337
|
+
return {
|
|
338
|
+
result: failure(
|
|
339
|
+
new BillionSendError({
|
|
340
|
+
message: `Expected an event stream, got ${contentType || "no content type"}`,
|
|
341
|
+
statusCode: response.status,
|
|
342
|
+
requestId: responseHeaders["x-request-id"] ?? null
|
|
343
|
+
}),
|
|
344
|
+
responseHeaders
|
|
345
|
+
)
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
let closed = false;
|
|
349
|
+
const connection = {
|
|
350
|
+
response,
|
|
351
|
+
close: () => {
|
|
352
|
+
if (closed) return;
|
|
353
|
+
closed = true;
|
|
354
|
+
release();
|
|
355
|
+
controller.abort();
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
return { result: { data: connection, error: null, headers: responseHeaders } };
|
|
359
|
+
}
|
|
360
|
+
const text = await response.text();
|
|
361
|
+
release();
|
|
362
|
+
if (response.ok) {
|
|
363
|
+
try {
|
|
364
|
+
return { result: { data: JSON.parse(text), error: null, headers: responseHeaders } };
|
|
365
|
+
} catch {
|
|
366
|
+
return {
|
|
367
|
+
result: failure(
|
|
368
|
+
new BillionSendError({
|
|
369
|
+
message: "The response body is not valid JSON",
|
|
370
|
+
statusCode: response.status,
|
|
371
|
+
requestId: responseHeaders["x-request-id"] ?? null
|
|
372
|
+
}),
|
|
373
|
+
responseHeaders
|
|
374
|
+
)
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
result: failure(
|
|
380
|
+
errorFromResponse(response.status, response.statusText, text, responseHeaders),
|
|
381
|
+
responseHeaders
|
|
382
|
+
),
|
|
383
|
+
retryAfter: response.headers.get("retry-after")
|
|
384
|
+
};
|
|
385
|
+
} catch (cause) {
|
|
386
|
+
release();
|
|
387
|
+
if (signal?.aborted) return { result: failure(applicationError("Request was aborted")) };
|
|
388
|
+
if (timedOut)
|
|
389
|
+
return { result: failure(new APITimeoutError(`Request timed out after ${timeoutMs} ms`)) };
|
|
390
|
+
return { result: failure(new APIConnectionError(`Network error: ${describe(cause)}`)) };
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// src/pagination.ts
|
|
396
|
+
async function* paginate(fetchPage, start = {}) {
|
|
397
|
+
const backward = start.before !== void 0 && start.before !== null;
|
|
398
|
+
let cursor = backward ? { before: start.before } : start.after ? { after: start.after } : {};
|
|
399
|
+
while (true) {
|
|
400
|
+
const page = await fetchPage(cursor);
|
|
401
|
+
if (page.error !== null) throw page.error;
|
|
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");
|
|
405
|
+
for (const item of items) yield item;
|
|
406
|
+
if (page.data.has_more !== true || items.length === 0) return;
|
|
407
|
+
const edge = backward ? items[0] : items[items.length - 1];
|
|
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 };
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// src/resource.ts
|
|
419
|
+
var Resource = class {
|
|
420
|
+
core;
|
|
421
|
+
constructor(core) {
|
|
422
|
+
this.core = core;
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
function idempotent(options) {
|
|
426
|
+
return { idempotent: true, idempotencyKey: options?.idempotencyKey, options };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/resources/account.ts
|
|
430
|
+
var Domains = class extends Resource {
|
|
431
|
+
/** `POST /domains` */
|
|
432
|
+
create(payload, options) {
|
|
433
|
+
return this.core.request("post", "/domains", { body: payload, options });
|
|
434
|
+
}
|
|
435
|
+
/** `GET /domains` */
|
|
436
|
+
list(params, options) {
|
|
437
|
+
return this.core.request("get", "/domains", { query: params, options });
|
|
438
|
+
}
|
|
439
|
+
/** Every domain, fetched page by page. */
|
|
440
|
+
listAll(params, options) {
|
|
441
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
442
|
+
}
|
|
443
|
+
/** `GET /domains/{id}` */
|
|
444
|
+
get(id, options) {
|
|
445
|
+
return this.core.request("get", "/domains/{id}", { path: { id }, options });
|
|
446
|
+
}
|
|
447
|
+
/** `PATCH /domains/{id}` */
|
|
448
|
+
update(id, payload, options) {
|
|
449
|
+
return this.core.request("patch", "/domains/{id}", { path: { id }, body: payload, options });
|
|
450
|
+
}
|
|
451
|
+
/** `DELETE /domains/{id}` */
|
|
452
|
+
delete(id, options) {
|
|
453
|
+
return this.core.request("delete", "/domains/{id}", { path: { id }, options });
|
|
454
|
+
}
|
|
455
|
+
/** `POST /domains/{id}/verify` */
|
|
456
|
+
verify(id, options) {
|
|
457
|
+
return this.core.request("post", "/domains/{id}/verify", { path: { id }, options });
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
var ApiKeys = class extends Resource {
|
|
461
|
+
/** `POST /api-keys`: the token is returned only in this answer. */
|
|
462
|
+
create(payload, options) {
|
|
463
|
+
return this.core.request("post", "/api-keys", { body: payload, options });
|
|
464
|
+
}
|
|
465
|
+
/** `GET /api-keys` */
|
|
466
|
+
list(params, options) {
|
|
467
|
+
return this.core.request("get", "/api-keys", { query: params, options });
|
|
468
|
+
}
|
|
469
|
+
/** Every API key, fetched page by page. */
|
|
470
|
+
listAll(params, options) {
|
|
471
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
472
|
+
}
|
|
473
|
+
/** `DELETE /api-keys/{id}` */
|
|
474
|
+
delete(id, options) {
|
|
475
|
+
return this.core.request("delete", "/api-keys/{id}", { path: { id }, options });
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
var Webhooks = class extends Resource {
|
|
479
|
+
/** `POST /webhooks` */
|
|
480
|
+
create(payload, options) {
|
|
481
|
+
return this.core.request("post", "/webhooks", { body: payload, options });
|
|
482
|
+
}
|
|
483
|
+
/** `GET /webhooks` */
|
|
484
|
+
list(params, options) {
|
|
485
|
+
return this.core.request("get", "/webhooks", { query: params, options });
|
|
486
|
+
}
|
|
487
|
+
/** Every webhook, fetched page by page. */
|
|
488
|
+
listAll(params, options) {
|
|
489
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
490
|
+
}
|
|
491
|
+
/** `GET /webhooks/{id}` */
|
|
492
|
+
get(id, options) {
|
|
493
|
+
return this.core.request("get", "/webhooks/{id}", { path: { id }, options });
|
|
494
|
+
}
|
|
495
|
+
/** `PATCH /webhooks/{id}` */
|
|
496
|
+
update(id, payload, options) {
|
|
497
|
+
return this.core.request("patch", "/webhooks/{id}", { path: { id }, body: payload, options });
|
|
498
|
+
}
|
|
499
|
+
/** `DELETE /webhooks/{id}` */
|
|
500
|
+
delete(id, options) {
|
|
501
|
+
return this.core.request("delete", "/webhooks/{id}", { path: { id }, options });
|
|
502
|
+
}
|
|
503
|
+
/** `POST /webhooks/{id}/rotate`: rotates the signing secret. */
|
|
504
|
+
rotate(id, payload, options) {
|
|
505
|
+
return this.core.request("post", "/webhooks/{id}/rotate", { path: { id }, body: payload, options });
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
var Usage = class extends Resource {
|
|
509
|
+
/** `GET /usage` */
|
|
510
|
+
get(options) {
|
|
511
|
+
return this.core.request("get", "/usage", { options });
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
var Deliverability = class extends Resource {
|
|
515
|
+
/** `GET /deliverability` */
|
|
516
|
+
get(options) {
|
|
517
|
+
return this.core.request("get", "/deliverability", { options });
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
var Agent = class extends Resource {
|
|
521
|
+
/** `POST /agent/signup`: needs no API key; the answer carries the new account's key. */
|
|
522
|
+
signup(payload, options) {
|
|
523
|
+
return this.core.request("post", "/agent/signup", { body: payload, options, auth: false });
|
|
524
|
+
}
|
|
525
|
+
/** `GET /agent/status` */
|
|
526
|
+
status(options) {
|
|
527
|
+
return this.core.request("get", "/agent/status", { options });
|
|
528
|
+
}
|
|
529
|
+
/** `POST /agent/claim-email`: sends the claim link again. */
|
|
530
|
+
claimEmail(options) {
|
|
531
|
+
return this.core.request("post", "/agent/claim-email", { options });
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// src/resources/broadcasts.ts
|
|
536
|
+
var Broadcasts = class extends Resource {
|
|
537
|
+
/** `POST /broadcasts` */
|
|
538
|
+
create(payload, options) {
|
|
539
|
+
return this.core.request("post", "/broadcasts", { body: payload, options });
|
|
540
|
+
}
|
|
541
|
+
/** `GET /broadcasts` */
|
|
542
|
+
list(params, options) {
|
|
543
|
+
return this.core.request("get", "/broadcasts", { query: params, options });
|
|
544
|
+
}
|
|
545
|
+
/** Every broadcast, fetched page by page. */
|
|
546
|
+
listAll(params, options) {
|
|
547
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
548
|
+
}
|
|
549
|
+
/** `GET /broadcasts/{id}` */
|
|
550
|
+
get(id, options) {
|
|
551
|
+
return this.core.request("get", "/broadcasts/{id}", { path: { id }, options });
|
|
552
|
+
}
|
|
553
|
+
/** `PATCH /broadcasts/{id}` */
|
|
554
|
+
update(id, payload, options) {
|
|
555
|
+
return this.core.request("patch", "/broadcasts/{id}", { path: { id }, body: payload, options });
|
|
556
|
+
}
|
|
557
|
+
/** `DELETE /broadcasts/{id}` */
|
|
558
|
+
delete(id, options) {
|
|
559
|
+
return this.core.request("delete", "/broadcasts/{id}", { path: { id }, options });
|
|
560
|
+
}
|
|
561
|
+
/** `POST /broadcasts/{id}/send` */
|
|
562
|
+
send(id, payload, options) {
|
|
563
|
+
return this.core.request("post", "/broadcasts/{id}/send", { path: { id }, body: payload, options });
|
|
564
|
+
}
|
|
565
|
+
/** `POST /broadcasts/{id}/duplicate` */
|
|
566
|
+
duplicate(id, options) {
|
|
567
|
+
return this.core.request("post", "/broadcasts/{id}/duplicate", { path: { id }, options });
|
|
568
|
+
}
|
|
569
|
+
/** `POST /broadcasts/{id}/cancel` */
|
|
570
|
+
cancel(id, options) {
|
|
571
|
+
return this.core.request("post", "/broadcasts/{id}/cancel", { path: { id }, options });
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
var Templates = class extends Resource {
|
|
575
|
+
/** `POST /templates` */
|
|
576
|
+
create(payload, options) {
|
|
577
|
+
return this.core.request("post", "/templates", { body: payload, options });
|
|
578
|
+
}
|
|
579
|
+
/** `GET /templates` */
|
|
580
|
+
list(params, options) {
|
|
581
|
+
return this.core.request("get", "/templates", { query: params, options });
|
|
582
|
+
}
|
|
583
|
+
/** Every template, fetched page by page. */
|
|
584
|
+
listAll(params, options) {
|
|
585
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
586
|
+
}
|
|
587
|
+
/** `GET /templates/{id}`: `id` is the template id or its alias. */
|
|
588
|
+
get(id, options) {
|
|
589
|
+
return this.core.request("get", "/templates/{id}", { path: { id }, options });
|
|
590
|
+
}
|
|
591
|
+
/** `PATCH /templates/{id}` */
|
|
592
|
+
update(id, payload, options) {
|
|
593
|
+
return this.core.request("patch", "/templates/{id}", { path: { id }, body: payload, options });
|
|
594
|
+
}
|
|
595
|
+
/** `DELETE /templates/{id}` */
|
|
596
|
+
delete(id, options) {
|
|
597
|
+
return this.core.request("delete", "/templates/{id}", { path: { id }, options });
|
|
598
|
+
}
|
|
599
|
+
/** `POST /templates/{id}/publish` */
|
|
600
|
+
publish(id, options) {
|
|
601
|
+
return this.core.request("post", "/templates/{id}/publish", { path: { id }, options });
|
|
602
|
+
}
|
|
603
|
+
/** `POST /templates/{id}/duplicate` */
|
|
604
|
+
duplicate(id, options) {
|
|
605
|
+
return this.core.request("post", "/templates/{id}/duplicate", { path: { id }, options });
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
// src/resources/contacts.ts
|
|
610
|
+
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
|
+
*/
|
|
615
|
+
create(payload, params, options) {
|
|
616
|
+
return this.core.request("post", "/contacts/batch", {
|
|
617
|
+
body: payload,
|
|
618
|
+
query: params,
|
|
619
|
+
headers: { "x-batch-validation": options?.batchValidation },
|
|
620
|
+
options
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
/** `POST /contacts/batch/get` */
|
|
624
|
+
get(payload, options) {
|
|
625
|
+
return this.core.request("post", "/contacts/batch/get", { body: payload, options });
|
|
626
|
+
}
|
|
627
|
+
/** `POST /contacts/batch/remove` */
|
|
628
|
+
remove(payload, options) {
|
|
629
|
+
return this.core.request("post", "/contacts/batch/remove", { body: payload, options });
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
var ContactTopics = class extends Resource {
|
|
633
|
+
/** `GET /contacts/{id}/topics` */
|
|
634
|
+
list(contactId, options) {
|
|
635
|
+
return this.core.request("get", "/contacts/{id}/topics", { path: { id: contactId }, options });
|
|
636
|
+
}
|
|
637
|
+
/** `PATCH /contacts/{id}/topics` */
|
|
638
|
+
update(contactId, payload, options) {
|
|
639
|
+
return this.core.request("patch", "/contacts/{id}/topics", {
|
|
640
|
+
path: { id: contactId },
|
|
641
|
+
body: payload,
|
|
642
|
+
options
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
var ContactSegments = class extends Resource {
|
|
647
|
+
/** `POST /contacts/{id}/segments/{segmentId}` */
|
|
648
|
+
add(contactId, segmentId, options) {
|
|
649
|
+
return this.core.request("post", "/contacts/{id}/segments/{segmentId}", {
|
|
650
|
+
path: { id: contactId, segmentId },
|
|
651
|
+
options
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
/** `DELETE /contacts/{id}/segments/{segmentId}` */
|
|
655
|
+
remove(contactId, segmentId, options) {
|
|
656
|
+
return this.core.request("delete", "/contacts/{id}/segments/{segmentId}", {
|
|
657
|
+
path: { id: contactId, segmentId },
|
|
658
|
+
options
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
var Contacts = class extends Resource {
|
|
663
|
+
batch = new ContactBatch(this.core);
|
|
664
|
+
topics = new ContactTopics(this.core);
|
|
665
|
+
segments = new ContactSegments(this.core);
|
|
666
|
+
/** `POST /contacts` */
|
|
667
|
+
create(payload, options) {
|
|
668
|
+
return this.core.request("post", "/contacts", { body: payload, options });
|
|
669
|
+
}
|
|
670
|
+
/** `GET /contacts` */
|
|
671
|
+
list(params, options) {
|
|
672
|
+
return this.core.request("get", "/contacts", { query: params, options });
|
|
673
|
+
}
|
|
674
|
+
/** Every contact, fetched page by page. */
|
|
675
|
+
listAll(params, options) {
|
|
676
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
677
|
+
}
|
|
678
|
+
/** `GET /contacts/{id}`: `id` is the contact id or its email address. */
|
|
679
|
+
get(id, options) {
|
|
680
|
+
return this.core.request("get", "/contacts/{id}", { path: { id }, options });
|
|
681
|
+
}
|
|
682
|
+
/** `PATCH /contacts/{id}` */
|
|
683
|
+
update(id, payload, options) {
|
|
684
|
+
return this.core.request("patch", "/contacts/{id}", { path: { id }, body: payload, options });
|
|
685
|
+
}
|
|
686
|
+
/** `DELETE /contacts/{id}` */
|
|
687
|
+
delete(id, params, options) {
|
|
688
|
+
return this.core.request("delete", "/contacts/{id}", { path: { id }, query: params, options });
|
|
689
|
+
}
|
|
690
|
+
/** `POST /contacts/{id}/preferences-link` */
|
|
691
|
+
preferencesLink(id, options) {
|
|
692
|
+
return this.core.request("post", "/contacts/{id}/preferences-link", { path: { id }, options });
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
var AudienceContacts = class extends Resource {
|
|
696
|
+
/** `POST /audiences/{audienceId}/contacts` */
|
|
697
|
+
create(audienceId, payload, options) {
|
|
698
|
+
return this.core.request("post", "/audiences/{audienceId}/contacts", {
|
|
699
|
+
path: { audienceId },
|
|
700
|
+
body: payload,
|
|
701
|
+
options
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
/** `GET /audiences/{audienceId}/contacts/{id}` */
|
|
705
|
+
get(audienceId, id, options) {
|
|
706
|
+
return this.core.request("get", "/audiences/{audienceId}/contacts/{id}", {
|
|
707
|
+
path: { audienceId, id },
|
|
708
|
+
options
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
/** `PATCH /audiences/{audienceId}/contacts/{id}` */
|
|
712
|
+
update(audienceId, id, payload, options) {
|
|
713
|
+
return this.core.request("patch", "/audiences/{audienceId}/contacts/{id}", {
|
|
714
|
+
path: { audienceId, id },
|
|
715
|
+
body: payload,
|
|
716
|
+
options
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
/** `DELETE /audiences/{audienceId}/contacts/{id}` */
|
|
720
|
+
delete(audienceId, id, params, options) {
|
|
721
|
+
return this.core.request("delete", "/audiences/{audienceId}/contacts/{id}", {
|
|
722
|
+
path: { audienceId, id },
|
|
723
|
+
query: params,
|
|
724
|
+
options
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
var Audiences = class extends Resource {
|
|
729
|
+
contacts = new AudienceContacts(this.core);
|
|
730
|
+
};
|
|
731
|
+
var ContactProperties = class extends Resource {
|
|
732
|
+
/** `POST /contact-properties` */
|
|
733
|
+
create(payload, options) {
|
|
734
|
+
return this.core.request("post", "/contact-properties", { body: payload, options });
|
|
735
|
+
}
|
|
736
|
+
/** `GET /contact-properties` */
|
|
737
|
+
list(params, options) {
|
|
738
|
+
return this.core.request("get", "/contact-properties", { query: params, options });
|
|
739
|
+
}
|
|
740
|
+
/** Every contact property, fetched page by page. */
|
|
741
|
+
listAll(params, options) {
|
|
742
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
743
|
+
}
|
|
744
|
+
/** `GET /contact-properties/{id}` */
|
|
745
|
+
get(id, options) {
|
|
746
|
+
return this.core.request("get", "/contact-properties/{id}", { path: { id }, options });
|
|
747
|
+
}
|
|
748
|
+
/** `PATCH /contact-properties/{id}` */
|
|
749
|
+
update(id, payload, options) {
|
|
750
|
+
return this.core.request("patch", "/contact-properties/{id}", { path: { id }, body: payload, options });
|
|
751
|
+
}
|
|
752
|
+
/** `DELETE /contact-properties/{id}` */
|
|
753
|
+
delete(id, options) {
|
|
754
|
+
return this.core.request("delete", "/contact-properties/{id}", { path: { id }, options });
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
var SegmentContacts = class extends Resource {
|
|
758
|
+
/** `GET /segments/{id}/contacts` */
|
|
759
|
+
list(segmentId, params, options) {
|
|
760
|
+
return this.core.request("get", "/segments/{id}/contacts", {
|
|
761
|
+
path: { id: segmentId },
|
|
762
|
+
query: params,
|
|
763
|
+
options
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
/** Every contact the segment resolves to, fetched page by page. */
|
|
767
|
+
listAll(segmentId, params, options) {
|
|
768
|
+
return paginate((cursor) => this.list(segmentId, { ...params, ...cursor }, options), params);
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
var Segments = class extends Resource {
|
|
772
|
+
contacts = new SegmentContacts(this.core);
|
|
773
|
+
/** `POST /segments` */
|
|
774
|
+
create(payload, options) {
|
|
775
|
+
return this.core.request("post", "/segments", { body: payload, options });
|
|
776
|
+
}
|
|
777
|
+
/** `GET /segments` */
|
|
778
|
+
list(params, options) {
|
|
779
|
+
return this.core.request("get", "/segments", { query: params, options });
|
|
780
|
+
}
|
|
781
|
+
/** Every segment, fetched page by page. */
|
|
782
|
+
listAll(params, options) {
|
|
783
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
784
|
+
}
|
|
785
|
+
/** `GET /segments/{id}` */
|
|
786
|
+
get(id, options) {
|
|
787
|
+
return this.core.request("get", "/segments/{id}", { path: { id }, options });
|
|
788
|
+
}
|
|
789
|
+
/** `PATCH /segments/{id}` */
|
|
790
|
+
update(id, payload, options) {
|
|
791
|
+
return this.core.request("patch", "/segments/{id}", { path: { id }, body: payload, options });
|
|
792
|
+
}
|
|
793
|
+
/** `DELETE /segments/{id}` */
|
|
794
|
+
delete(id, options) {
|
|
795
|
+
return this.core.request("delete", "/segments/{id}", { path: { id }, options });
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
var Topics = class extends Resource {
|
|
799
|
+
/** `POST /topics` */
|
|
800
|
+
create(payload, options) {
|
|
801
|
+
return this.core.request("post", "/topics", { body: payload, options });
|
|
802
|
+
}
|
|
803
|
+
/** `GET /topics` (not paginated) */
|
|
804
|
+
list(options) {
|
|
805
|
+
return this.core.request("get", "/topics", { options });
|
|
806
|
+
}
|
|
807
|
+
/** `GET /topics/{id}` */
|
|
808
|
+
get(id, options) {
|
|
809
|
+
return this.core.request("get", "/topics/{id}", { path: { id }, options });
|
|
810
|
+
}
|
|
811
|
+
/** `PATCH /topics/{id}` */
|
|
812
|
+
update(id, payload, options) {
|
|
813
|
+
return this.core.request("patch", "/topics/{id}", { path: { id }, body: payload, options });
|
|
814
|
+
}
|
|
815
|
+
/** `DELETE /topics/{id}` */
|
|
816
|
+
delete(id, options) {
|
|
817
|
+
return this.core.request("delete", "/topics/{id}", { path: { id }, options });
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
// src/resources/emails.ts
|
|
822
|
+
var ReceivedEmailAttachments = class extends Resource {
|
|
823
|
+
/** `GET /emails/receiving/{email_id}/attachments` */
|
|
824
|
+
list(emailId, params, options) {
|
|
825
|
+
return this.core.request("get", "/emails/receiving/{email_id}/attachments", {
|
|
826
|
+
path: { email_id: emailId },
|
|
827
|
+
query: params,
|
|
828
|
+
options
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
/** Every attachment of a received email, fetched page by page. */
|
|
832
|
+
listAll(emailId, params, options) {
|
|
833
|
+
return paginate((cursor) => this.list(emailId, { ...params, ...cursor }, options), params);
|
|
834
|
+
}
|
|
835
|
+
/** `GET /emails/receiving/{email_id}/attachments/{id}` */
|
|
836
|
+
get(emailId, id, options) {
|
|
837
|
+
return this.core.request("get", "/emails/receiving/{email_id}/attachments/{id}", {
|
|
838
|
+
path: { email_id: emailId, id },
|
|
839
|
+
options
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
/** `GET /emails/receiving/{email_id}/attachments/{id}/text` */
|
|
843
|
+
text(emailId, id, options) {
|
|
844
|
+
return this.core.request("get", "/emails/receiving/{email_id}/attachments/{id}/text", {
|
|
845
|
+
path: { email_id: emailId, id },
|
|
846
|
+
options
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
};
|
|
850
|
+
var ReceivedEmails = class extends Resource {
|
|
851
|
+
attachments = new ReceivedEmailAttachments(this.core);
|
|
852
|
+
/** `GET /emails/receiving` */
|
|
853
|
+
list(params, options) {
|
|
854
|
+
return this.core.request("get", "/emails/receiving", { query: params, options });
|
|
855
|
+
}
|
|
856
|
+
/** Every received email matching `params`, fetched page by page. */
|
|
857
|
+
listAll(params, options) {
|
|
858
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
859
|
+
}
|
|
860
|
+
/** `GET /emails/receiving/{id}` */
|
|
861
|
+
get(id, params, options) {
|
|
862
|
+
return this.core.request("get", "/emails/receiving/{id}", { path: { id }, query: params, options });
|
|
863
|
+
}
|
|
864
|
+
/** `DELETE /emails/receiving/{id}` */
|
|
865
|
+
delete(id, options) {
|
|
866
|
+
return this.core.request("delete", "/emails/receiving/{id}", { path: { id }, options });
|
|
867
|
+
}
|
|
868
|
+
/** `POST /emails/receiving/{id}/reply` */
|
|
869
|
+
reply(id, payload, options) {
|
|
870
|
+
return this.core.request("post", "/emails/receiving/{id}/reply", {
|
|
871
|
+
path: { id },
|
|
872
|
+
body: payload,
|
|
873
|
+
...idempotent(options)
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
/** `POST /emails/receiving/{id}/forward` */
|
|
877
|
+
forward(id, payload, options) {
|
|
878
|
+
return this.core.request("post", "/emails/receiving/{id}/forward", {
|
|
879
|
+
path: { id },
|
|
880
|
+
body: payload,
|
|
881
|
+
...idempotent(options)
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
};
|
|
885
|
+
var Emails = class extends Resource {
|
|
886
|
+
receiving = new ReceivedEmails(this.core);
|
|
887
|
+
/** `POST /emails` */
|
|
888
|
+
send(payload, options) {
|
|
889
|
+
return this.core.request("post", "/emails", { body: payload, ...idempotent(options) });
|
|
890
|
+
}
|
|
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
|
+
*/
|
|
895
|
+
batch(payload, options) {
|
|
896
|
+
return this.core.request("post", "/emails/batch", {
|
|
897
|
+
body: payload,
|
|
898
|
+
headers: { "x-batch-validation": options?.batchValidation },
|
|
899
|
+
...idempotent(options)
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
/** `GET /emails` */
|
|
903
|
+
list(params, options) {
|
|
904
|
+
return this.core.request("get", "/emails", { query: params, options });
|
|
905
|
+
}
|
|
906
|
+
/** Every sent email, fetched page by page. */
|
|
907
|
+
listAll(params, options) {
|
|
908
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
909
|
+
}
|
|
910
|
+
/** `GET /emails/{id}` */
|
|
911
|
+
get(id, options) {
|
|
912
|
+
return this.core.request("get", "/emails/{id}", { path: { id }, options });
|
|
913
|
+
}
|
|
914
|
+
/** `PATCH /emails/{id}` */
|
|
915
|
+
update(id, payload, options) {
|
|
916
|
+
return this.core.request("patch", "/emails/{id}", { path: { id }, body: payload, options });
|
|
917
|
+
}
|
|
918
|
+
/** `POST /emails/{id}/cancel` */
|
|
919
|
+
cancel(id, options) {
|
|
920
|
+
return this.core.request("post", "/emails/{id}/cancel", { path: { id }, options });
|
|
921
|
+
}
|
|
922
|
+
/** `DELETE /emails/{id}` */
|
|
923
|
+
delete(id, options) {
|
|
924
|
+
return this.core.request("delete", "/emails/{id}", { path: { id }, options });
|
|
925
|
+
}
|
|
926
|
+
/** `GET /emails/{id}/insights` */
|
|
927
|
+
insights(id, options) {
|
|
928
|
+
return this.core.request("get", "/emails/{id}/insights", { path: { id }, options });
|
|
929
|
+
}
|
|
930
|
+
/** `GET /emails/metrics` */
|
|
931
|
+
metrics(params, options) {
|
|
932
|
+
return this.core.request("get", "/emails/metrics", { query: params, options });
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
// src/sse.ts
|
|
937
|
+
var STREAM_GAP_EVENT = "stream.gap";
|
|
938
|
+
var LINE_BREAK = /\r\n|\r|\n/;
|
|
939
|
+
async function* parseEventStream(body, hooks = { onRetry() {
|
|
940
|
+
} }, initialId = null) {
|
|
941
|
+
const reader = body.getReader();
|
|
942
|
+
const decoder = new TextDecoder("utf-8");
|
|
943
|
+
let buffer = "";
|
|
944
|
+
let first = true;
|
|
945
|
+
let lastId = initialId;
|
|
946
|
+
let eventName = "";
|
|
947
|
+
let dataLines = [];
|
|
948
|
+
let hasData = false;
|
|
949
|
+
function* takeLines(final) {
|
|
950
|
+
while (true) {
|
|
951
|
+
const match = LINE_BREAK.exec(buffer);
|
|
952
|
+
if (!match) return;
|
|
953
|
+
if (!final && match[0] === "\r" && match.index === buffer.length - 1) return;
|
|
954
|
+
const line = buffer.slice(0, match.index);
|
|
955
|
+
buffer = buffer.slice(match.index + match[0].length);
|
|
956
|
+
yield line;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
function processLine(line) {
|
|
960
|
+
if (line === "") {
|
|
961
|
+
if (!hasData) {
|
|
962
|
+
eventName = "";
|
|
963
|
+
return void 0;
|
|
964
|
+
}
|
|
965
|
+
const raw = dataLines.join("\n");
|
|
966
|
+
let data = raw;
|
|
967
|
+
try {
|
|
968
|
+
data = JSON.parse(raw);
|
|
969
|
+
} catch {
|
|
970
|
+
data = raw;
|
|
971
|
+
}
|
|
972
|
+
const event = { id: lastId, event: eventName || "message", data, raw };
|
|
973
|
+
eventName = "";
|
|
974
|
+
dataLines = [];
|
|
975
|
+
hasData = false;
|
|
976
|
+
return event;
|
|
977
|
+
}
|
|
978
|
+
if (line.startsWith(":")) return void 0;
|
|
979
|
+
const colon = line.indexOf(":");
|
|
980
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
981
|
+
let value = colon === -1 ? "" : line.slice(colon + 1);
|
|
982
|
+
if (value.startsWith(" ")) value = value.slice(1);
|
|
983
|
+
switch (field) {
|
|
984
|
+
case "event":
|
|
985
|
+
eventName = value;
|
|
986
|
+
break;
|
|
987
|
+
case "data":
|
|
988
|
+
dataLines.push(value);
|
|
989
|
+
hasData = true;
|
|
990
|
+
break;
|
|
991
|
+
case "id":
|
|
992
|
+
if (!value.includes("\0")) lastId = value;
|
|
993
|
+
break;
|
|
994
|
+
case "retry":
|
|
995
|
+
if (/^\d+$/.test(value)) hooks.onRetry(Number(value));
|
|
996
|
+
break;
|
|
997
|
+
}
|
|
998
|
+
return void 0;
|
|
999
|
+
}
|
|
1000
|
+
try {
|
|
1001
|
+
while (true) {
|
|
1002
|
+
const { done, value } = await reader.read();
|
|
1003
|
+
if (done) {
|
|
1004
|
+
buffer += decoder.decode();
|
|
1005
|
+
for (const line of takeLines(true)) {
|
|
1006
|
+
const event = processLine(line);
|
|
1007
|
+
if (event) yield event;
|
|
1008
|
+
}
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
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
|
+
for (const line of takeLines(false)) {
|
|
1017
|
+
const event = processLine(line);
|
|
1018
|
+
if (event) yield event;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
} finally {
|
|
1022
|
+
reader.releaseLock();
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
var EventStream = class {
|
|
1026
|
+
#connection;
|
|
1027
|
+
#connect;
|
|
1028
|
+
#options;
|
|
1029
|
+
#lastEventId;
|
|
1030
|
+
#delayMs;
|
|
1031
|
+
#closed = false;
|
|
1032
|
+
#started = false;
|
|
1033
|
+
#onAbort = () => this.close();
|
|
1034
|
+
#endsOn;
|
|
1035
|
+
/**
|
|
1036
|
+
* @param endsOn event names after which the stream is over: one inbox's
|
|
1037
|
+
* stream ends with that inbox's `inbox.deleted` or `inbox.expired` (the
|
|
1038
|
+
* server closes it, and a reconnect would answer 404).
|
|
1039
|
+
*/
|
|
1040
|
+
constructor(connection, connect, options = {}, endsOn = []) {
|
|
1041
|
+
this.#endsOn = new Set(endsOn);
|
|
1042
|
+
this.#connection = connection;
|
|
1043
|
+
this.#connect = connect;
|
|
1044
|
+
this.#options = options;
|
|
1045
|
+
this.#lastEventId = options.lastEventId ?? null;
|
|
1046
|
+
this.#delayMs = options.reconnectDelayMs ?? 1e3;
|
|
1047
|
+
options.signal?.addEventListener("abort", this.#onAbort, { once: true });
|
|
1048
|
+
}
|
|
1049
|
+
/** The id of the last event received: pass it as `lastEventId` to resume later. */
|
|
1050
|
+
get lastEventId() {
|
|
1051
|
+
return this.#lastEventId;
|
|
1052
|
+
}
|
|
1053
|
+
/** The response of the current connection. */
|
|
1054
|
+
get response() {
|
|
1055
|
+
return this.#connection?.response ?? null;
|
|
1056
|
+
}
|
|
1057
|
+
/** Closes the connection; a running loop ends without an error. */
|
|
1058
|
+
close() {
|
|
1059
|
+
if (this.#closed) return;
|
|
1060
|
+
this.#closed = true;
|
|
1061
|
+
this.#options.signal?.removeEventListener("abort", this.#onAbort);
|
|
1062
|
+
this.#connection?.close();
|
|
1063
|
+
this.#connection = null;
|
|
1064
|
+
}
|
|
1065
|
+
[Symbol.asyncIterator]() {
|
|
1066
|
+
if (this.#started) throw new TypeError("An EventStream can be iterated only once");
|
|
1067
|
+
this.#started = true;
|
|
1068
|
+
return this.#events();
|
|
1069
|
+
}
|
|
1070
|
+
async *#events() {
|
|
1071
|
+
const reconnect = this.#options.reconnect !== false;
|
|
1072
|
+
const maxReconnects = this.#options.maxReconnects ?? 5;
|
|
1073
|
+
let failures = 0;
|
|
1074
|
+
let lastError = null;
|
|
1075
|
+
let gapAt;
|
|
1076
|
+
try {
|
|
1077
|
+
while (!this.#closed) {
|
|
1078
|
+
const connection = this.#connection;
|
|
1079
|
+
if (connection !== null && connection.response.body !== null) {
|
|
1080
|
+
try {
|
|
1081
|
+
const events = parseEventStream(
|
|
1082
|
+
connection.response.body,
|
|
1083
|
+
{ onRetry: (ms) => this.#delayMs = ms },
|
|
1084
|
+
this.#lastEventId
|
|
1085
|
+
);
|
|
1086
|
+
for await (const event of events) {
|
|
1087
|
+
if (event.event === STREAM_GAP_EVENT) {
|
|
1088
|
+
if (gapAt === this.#lastEventId) continue;
|
|
1089
|
+
gapAt = this.#lastEventId;
|
|
1090
|
+
yield { id: null, event: STREAM_GAP_EVENT, data: event.data };
|
|
1091
|
+
continue;
|
|
1092
|
+
}
|
|
1093
|
+
this.#lastEventId = event.id;
|
|
1094
|
+
yield {
|
|
1095
|
+
id: event.id ?? "",
|
|
1096
|
+
event: event.event,
|
|
1097
|
+
data: event.data
|
|
1098
|
+
};
|
|
1099
|
+
if (this.#endsOn.has(event.event)) return;
|
|
1100
|
+
}
|
|
1101
|
+
} catch {
|
|
1102
|
+
}
|
|
1103
|
+
connection.close();
|
|
1104
|
+
this.#connection = null;
|
|
1105
|
+
lastError = null;
|
|
1106
|
+
}
|
|
1107
|
+
if (this.#closed || !reconnect) return;
|
|
1108
|
+
failures++;
|
|
1109
|
+
if (failures > maxReconnects) {
|
|
1110
|
+
if (lastError !== null && lastError.statusCode !== null) throw lastError;
|
|
1111
|
+
throw new APIConnectionError(
|
|
1112
|
+
`The event stream disconnected; gave up after ${maxReconnects} reconnects`
|
|
1113
|
+
);
|
|
1114
|
+
}
|
|
1115
|
+
const hinted = retryAfterMs(lastError);
|
|
1116
|
+
if (!await sleep(hinted ?? this.#delayMs, this.#options.signal) || this.#closed) return;
|
|
1117
|
+
const next = await this.#connect(this.#lastEventId);
|
|
1118
|
+
if (this.#closed) {
|
|
1119
|
+
next.data?.close();
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
if (next.error !== null) {
|
|
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;
|
|
1127
|
+
continue;
|
|
1128
|
+
}
|
|
1129
|
+
this.#connection = next.data;
|
|
1130
|
+
failures = 0;
|
|
1131
|
+
lastError = null;
|
|
1132
|
+
}
|
|
1133
|
+
} finally {
|
|
1134
|
+
this.close();
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
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
|
+
}
|
|
1142
|
+
|
|
1143
|
+
// src/resources/inboxes.ts
|
|
1144
|
+
var InboxAgentSettings = class extends Resource {
|
|
1145
|
+
/** `GET /inboxes/{inbox_id}/agent` */
|
|
1146
|
+
get(inboxId, options) {
|
|
1147
|
+
return this.core.request("get", "/inboxes/{inbox_id}/agent", { path: { inbox_id: inboxId }, options });
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
var InboxSafety = class extends Resource {
|
|
1151
|
+
/** `GET /inboxes/{inbox_id}/safety` */
|
|
1152
|
+
get(inboxId, options) {
|
|
1153
|
+
return this.core.request("get", "/inboxes/{inbox_id}/safety", { path: { inbox_id: inboxId }, options });
|
|
1154
|
+
}
|
|
1155
|
+
/** `PATCH /inboxes/{inbox_id}/safety` */
|
|
1156
|
+
update(inboxId, payload, options) {
|
|
1157
|
+
return this.core.request("patch", "/inboxes/{inbox_id}/safety", {
|
|
1158
|
+
path: { inbox_id: inboxId },
|
|
1159
|
+
body: payload,
|
|
1160
|
+
options
|
|
1161
|
+
});
|
|
1162
|
+
}
|
|
1163
|
+
};
|
|
1164
|
+
var InboxLabels = class extends Resource {
|
|
1165
|
+
/** `GET /inboxes/{inbox_id}/labels` */
|
|
1166
|
+
list(inboxId, options) {
|
|
1167
|
+
return this.core.request("get", "/inboxes/{inbox_id}/labels", { path: { inbox_id: inboxId }, options });
|
|
1168
|
+
}
|
|
1169
|
+
/** `POST /inboxes/{inbox_id}/labels` */
|
|
1170
|
+
create(inboxId, payload, options) {
|
|
1171
|
+
return this.core.request("post", "/inboxes/{inbox_id}/labels", {
|
|
1172
|
+
path: { inbox_id: inboxId },
|
|
1173
|
+
body: payload,
|
|
1174
|
+
options
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
/** `PATCH /inboxes/{inbox_id}/labels/{label_id}` */
|
|
1178
|
+
update(inboxId, labelId, payload, options) {
|
|
1179
|
+
return this.core.request("patch", "/inboxes/{inbox_id}/labels/{label_id}", {
|
|
1180
|
+
path: { inbox_id: inboxId, label_id: labelId },
|
|
1181
|
+
body: payload,
|
|
1182
|
+
options
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
/** `DELETE /inboxes/{inbox_id}/labels/{label_id}` */
|
|
1186
|
+
delete(inboxId, labelId, options) {
|
|
1187
|
+
return this.core.request("delete", "/inboxes/{inbox_id}/labels/{label_id}", {
|
|
1188
|
+
path: { inbox_id: inboxId, label_id: labelId },
|
|
1189
|
+
options
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
var ThreadLabels = class extends Resource {
|
|
1194
|
+
/** `POST /inboxes/{inbox_id}/threads/{thread_id}/labels`: adds labels. */
|
|
1195
|
+
add(inboxId, threadId, payload, options) {
|
|
1196
|
+
return this.core.request("post", "/inboxes/{inbox_id}/threads/{thread_id}/labels", {
|
|
1197
|
+
path: { inbox_id: inboxId, thread_id: threadId },
|
|
1198
|
+
body: payload,
|
|
1199
|
+
options
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
/** `PUT /inboxes/{inbox_id}/threads/{thread_id}/labels`: replaces the labels. */
|
|
1203
|
+
set(inboxId, threadId, payload, options) {
|
|
1204
|
+
return this.core.request("put", "/inboxes/{inbox_id}/threads/{thread_id}/labels", {
|
|
1205
|
+
path: { inbox_id: inboxId, thread_id: threadId },
|
|
1206
|
+
body: payload,
|
|
1207
|
+
options
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
/** `DELETE /inboxes/{inbox_id}/threads/{thread_id}/labels/{label_id}` */
|
|
1211
|
+
remove(inboxId, threadId, labelId, options) {
|
|
1212
|
+
return this.core.request("delete", "/inboxes/{inbox_id}/threads/{thread_id}/labels/{label_id}", {
|
|
1213
|
+
path: { inbox_id: inboxId, thread_id: threadId, label_id: labelId },
|
|
1214
|
+
options
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
var ThreadEmails = class extends Resource {
|
|
1219
|
+
/** `GET /inboxes/{inbox_id}/threads/{thread_id}/emails` */
|
|
1220
|
+
list(inboxId, threadId, params, options) {
|
|
1221
|
+
return this.core.request("get", "/inboxes/{inbox_id}/threads/{thread_id}/emails", {
|
|
1222
|
+
path: { inbox_id: inboxId, thread_id: threadId },
|
|
1223
|
+
query: params,
|
|
1224
|
+
options
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
/** Every email of a thread, fetched page by page. */
|
|
1228
|
+
listAll(inboxId, threadId, params, options) {
|
|
1229
|
+
return paginate((cursor) => this.list(inboxId, threadId, { ...params, ...cursor }, options), params);
|
|
1230
|
+
}
|
|
1231
|
+
/** `GET /inboxes/{inbox_id}/threads/{thread_id}/emails/{email_id}` */
|
|
1232
|
+
get(inboxId, threadId, emailId, params, options) {
|
|
1233
|
+
return this.core.request("get", "/inboxes/{inbox_id}/threads/{thread_id}/emails/{email_id}", {
|
|
1234
|
+
path: { inbox_id: inboxId, thread_id: threadId, email_id: emailId },
|
|
1235
|
+
query: params,
|
|
1236
|
+
options
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
/** `POST .../emails/{email_id}/reply`: answers the sent email, or (202) the draft the inbox's rules held. */
|
|
1240
|
+
reply(inboxId, threadId, emailId, payload, options) {
|
|
1241
|
+
return this.core.request("post", "/inboxes/{inbox_id}/threads/{thread_id}/emails/{email_id}/reply", {
|
|
1242
|
+
path: { inbox_id: inboxId, thread_id: threadId, email_id: emailId },
|
|
1243
|
+
body: payload,
|
|
1244
|
+
...idempotent(options)
|
|
1245
|
+
});
|
|
1246
|
+
}
|
|
1247
|
+
/** `POST .../emails/{email_id}/forward`: answers the sent email, or (202) the draft the inbox's rules held. */
|
|
1248
|
+
forward(inboxId, threadId, emailId, payload, options) {
|
|
1249
|
+
return this.core.request("post", "/inboxes/{inbox_id}/threads/{thread_id}/emails/{email_id}/forward", {
|
|
1250
|
+
path: { inbox_id: inboxId, thread_id: threadId, email_id: emailId },
|
|
1251
|
+
body: payload,
|
|
1252
|
+
...idempotent(options)
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
var InboxThreads = class extends Resource {
|
|
1257
|
+
labels = new ThreadLabels(this.core);
|
|
1258
|
+
emails = new ThreadEmails(this.core);
|
|
1259
|
+
/** `GET /inboxes/{inbox_id}/threads` */
|
|
1260
|
+
list(inboxId, params, options) {
|
|
1261
|
+
return this.core.request("get", "/inboxes/{inbox_id}/threads", {
|
|
1262
|
+
path: { inbox_id: inboxId },
|
|
1263
|
+
query: params,
|
|
1264
|
+
options
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
/** Every thread matching `params`, fetched page by page. */
|
|
1268
|
+
listAll(inboxId, params, options) {
|
|
1269
|
+
return paginate((cursor) => this.list(inboxId, { ...params, ...cursor }, options), params);
|
|
1270
|
+
}
|
|
1271
|
+
/** `GET /inboxes/{inbox_id}/threads/search` */
|
|
1272
|
+
search(inboxId, params, options) {
|
|
1273
|
+
return this.core.request("get", "/inboxes/{inbox_id}/threads/search", {
|
|
1274
|
+
path: { inbox_id: inboxId },
|
|
1275
|
+
query: params,
|
|
1276
|
+
options
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
/** Every thread a search finds, fetched page by page. */
|
|
1280
|
+
searchAll(inboxId, params, options) {
|
|
1281
|
+
return paginate((cursor) => this.search(inboxId, { ...params, ...cursor }, options), params);
|
|
1282
|
+
}
|
|
1283
|
+
/** `GET /inboxes/{inbox_id}/threads/{thread_id}` */
|
|
1284
|
+
get(inboxId, threadId, options) {
|
|
1285
|
+
return this.core.request("get", "/inboxes/{inbox_id}/threads/{thread_id}", {
|
|
1286
|
+
path: { inbox_id: inboxId, thread_id: threadId },
|
|
1287
|
+
options
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
/** `PATCH /inboxes/{inbox_id}/threads/{thread_id}` */
|
|
1291
|
+
update(inboxId, threadId, payload, options) {
|
|
1292
|
+
return this.core.request("patch", "/inboxes/{inbox_id}/threads/{thread_id}", {
|
|
1293
|
+
path: { inbox_id: inboxId, thread_id: threadId },
|
|
1294
|
+
body: payload,
|
|
1295
|
+
options
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
/** `DELETE /inboxes/{inbox_id}/threads/{thread_id}` */
|
|
1299
|
+
delete(inboxId, threadId, options) {
|
|
1300
|
+
return this.core.request("delete", "/inboxes/{inbox_id}/threads/{thread_id}", {
|
|
1301
|
+
path: { inbox_id: inboxId, thread_id: threadId },
|
|
1302
|
+
options
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
var InboxQuarantine = class extends Resource {
|
|
1307
|
+
/** `GET /inboxes/{inbox_id}/quarantine` */
|
|
1308
|
+
list(inboxId, params, options) {
|
|
1309
|
+
return this.core.request("get", "/inboxes/{inbox_id}/quarantine", {
|
|
1310
|
+
path: { inbox_id: inboxId },
|
|
1311
|
+
query: params,
|
|
1312
|
+
options
|
|
1313
|
+
});
|
|
1314
|
+
}
|
|
1315
|
+
/** Every held email of an inbox, fetched page by page. */
|
|
1316
|
+
listAll(inboxId, params, options) {
|
|
1317
|
+
return paginate((cursor) => this.list(inboxId, { ...params, ...cursor }, options), params);
|
|
1318
|
+
}
|
|
1319
|
+
/** `POST /inboxes/{inbox_id}/quarantine/{email_id}/release` (the body is optional) */
|
|
1320
|
+
release(inboxId, emailId, payload, options) {
|
|
1321
|
+
return this.core.request("post", "/inboxes/{inbox_id}/quarantine/{email_id}/release", {
|
|
1322
|
+
path: { inbox_id: inboxId, email_id: emailId },
|
|
1323
|
+
body: payload,
|
|
1324
|
+
options
|
|
1325
|
+
});
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
var InboxCodes = class extends Resource {
|
|
1329
|
+
/** `GET /inboxes/{inbox_id}/codes/latest`: the newest verification code, optionally waiting for one. */
|
|
1330
|
+
latest(inboxId, params, options) {
|
|
1331
|
+
return this.core.request("get", "/inboxes/{inbox_id}/codes/latest", {
|
|
1332
|
+
path: { inbox_id: inboxId },
|
|
1333
|
+
query: params,
|
|
1334
|
+
options
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
};
|
|
1338
|
+
var InboxDrafts = class extends Resource {
|
|
1339
|
+
/** `POST /inboxes/{inbox_id}/drafts` */
|
|
1340
|
+
create(inboxId, payload, options) {
|
|
1341
|
+
return this.core.request("post", "/inboxes/{inbox_id}/drafts", {
|
|
1342
|
+
path: { inbox_id: inboxId },
|
|
1343
|
+
body: payload,
|
|
1344
|
+
options
|
|
1345
|
+
});
|
|
1346
|
+
}
|
|
1347
|
+
/** `GET /inboxes/{inbox_id}/drafts` */
|
|
1348
|
+
list(inboxId, params, options) {
|
|
1349
|
+
return this.core.request("get", "/inboxes/{inbox_id}/drafts", {
|
|
1350
|
+
path: { inbox_id: inboxId },
|
|
1351
|
+
query: params,
|
|
1352
|
+
options
|
|
1353
|
+
});
|
|
1354
|
+
}
|
|
1355
|
+
/** Every draft of an inbox, fetched page by page. */
|
|
1356
|
+
listAll(inboxId, params, options) {
|
|
1357
|
+
return paginate((cursor) => this.list(inboxId, { ...params, ...cursor }, options), params);
|
|
1358
|
+
}
|
|
1359
|
+
/** `GET /inboxes/{inbox_id}/drafts/{draft_id}` */
|
|
1360
|
+
get(inboxId, draftId, options) {
|
|
1361
|
+
return this.core.request("get", "/inboxes/{inbox_id}/drafts/{draft_id}", {
|
|
1362
|
+
path: { inbox_id: inboxId, draft_id: draftId },
|
|
1363
|
+
options
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
/** `PATCH /inboxes/{inbox_id}/drafts/{draft_id}` */
|
|
1367
|
+
update(inboxId, draftId, payload, options) {
|
|
1368
|
+
return this.core.request("patch", "/inboxes/{inbox_id}/drafts/{draft_id}", {
|
|
1369
|
+
path: { inbox_id: inboxId, draft_id: draftId },
|
|
1370
|
+
body: payload,
|
|
1371
|
+
options
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
/** `DELETE /inboxes/{inbox_id}/drafts/{draft_id}` */
|
|
1375
|
+
delete(inboxId, draftId, options) {
|
|
1376
|
+
return this.core.request("delete", "/inboxes/{inbox_id}/drafts/{draft_id}", {
|
|
1377
|
+
path: { inbox_id: inboxId, draft_id: draftId },
|
|
1378
|
+
options
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
/** `POST /inboxes/{inbox_id}/drafts/{draft_id}/send` */
|
|
1382
|
+
send(inboxId, draftId, options) {
|
|
1383
|
+
return this.core.request("post", "/inboxes/{inbox_id}/drafts/{draft_id}/send", {
|
|
1384
|
+
path: { inbox_id: inboxId, draft_id: draftId },
|
|
1385
|
+
...idempotent(options)
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
/** `POST /inboxes/{inbox_id}/drafts/{draft_id}/reject` */
|
|
1389
|
+
reject(inboxId, draftId, options) {
|
|
1390
|
+
return this.core.request("post", "/inboxes/{inbox_id}/drafts/{draft_id}/reject", {
|
|
1391
|
+
path: { inbox_id: inboxId, draft_id: draftId },
|
|
1392
|
+
options
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1396
|
+
function lastEventIdHeader(id) {
|
|
1397
|
+
return { "last-event-id": id ?? void 0 };
|
|
1398
|
+
}
|
|
1399
|
+
function connectOptions(options, retries) {
|
|
1400
|
+
return {
|
|
1401
|
+
signal: options.signal,
|
|
1402
|
+
headers: options.headers,
|
|
1403
|
+
timeoutMs: options.timeoutMs,
|
|
1404
|
+
maxRetries: retries
|
|
1405
|
+
};
|
|
1406
|
+
}
|
|
1407
|
+
var INBOX_GONE_EVENTS = ["inbox.deleted", "inbox.expired"];
|
|
1408
|
+
function wrap(first, connect, options, endsOn = []) {
|
|
1409
|
+
if (first.error !== null) return first;
|
|
1410
|
+
return { data: new EventStream(first.data, connect, options, endsOn), error: null, headers: first.headers };
|
|
1411
|
+
}
|
|
1412
|
+
var InboxEvents = class extends Resource {
|
|
1413
|
+
/**
|
|
1414
|
+
* `GET /inboxes/events`: every inbox event of the team as a live stream.
|
|
1415
|
+
* Resolves once the stream is open (or with the error that refused it).
|
|
1416
|
+
*/
|
|
1417
|
+
async stream(params, options = {}) {
|
|
1418
|
+
const connect = (lastEventId) => this.core.openStream("get", "/inboxes/events", {
|
|
1419
|
+
query: params,
|
|
1420
|
+
headers: lastEventIdHeader(lastEventId),
|
|
1421
|
+
options: connectOptions(options, 0)
|
|
1422
|
+
});
|
|
1423
|
+
const first = await this.core.openStream("get", "/inboxes/events", {
|
|
1424
|
+
query: params,
|
|
1425
|
+
headers: lastEventIdHeader(options.lastEventId),
|
|
1426
|
+
options: connectOptions(options, options.maxRetries)
|
|
1427
|
+
});
|
|
1428
|
+
return wrap(first, connect, options);
|
|
1429
|
+
}
|
|
1430
|
+
/**
|
|
1431
|
+
* `GET /inboxes/{inbox_id}/events`: one inbox's events as a live stream. It ends
|
|
1432
|
+
* (the loop finishes) after that inbox's `inbox.deleted` or `inbox.expired`.
|
|
1433
|
+
*/
|
|
1434
|
+
async streamInbox(inboxId, params, options = {}) {
|
|
1435
|
+
const connect = (lastEventId) => this.core.openStream("get", "/inboxes/{inbox_id}/events", {
|
|
1436
|
+
path: { inbox_id: inboxId },
|
|
1437
|
+
query: params,
|
|
1438
|
+
headers: lastEventIdHeader(lastEventId),
|
|
1439
|
+
options: connectOptions(options, 0)
|
|
1440
|
+
});
|
|
1441
|
+
const first = await this.core.openStream("get", "/inboxes/{inbox_id}/events", {
|
|
1442
|
+
path: { inbox_id: inboxId },
|
|
1443
|
+
query: params,
|
|
1444
|
+
headers: lastEventIdHeader(options.lastEventId),
|
|
1445
|
+
options: connectOptions(options, options.maxRetries)
|
|
1446
|
+
});
|
|
1447
|
+
return wrap(first, connect, options, INBOX_GONE_EVENTS);
|
|
1448
|
+
}
|
|
1449
|
+
};
|
|
1450
|
+
var Inboxes = class extends Resource {
|
|
1451
|
+
agent = new InboxAgentSettings(this.core);
|
|
1452
|
+
safety = new InboxSafety(this.core);
|
|
1453
|
+
labels = new InboxLabels(this.core);
|
|
1454
|
+
threads = new InboxThreads(this.core);
|
|
1455
|
+
quarantine = new InboxQuarantine(this.core);
|
|
1456
|
+
codes = new InboxCodes(this.core);
|
|
1457
|
+
drafts = new InboxDrafts(this.core);
|
|
1458
|
+
events = new InboxEvents(this.core);
|
|
1459
|
+
/** `POST /inboxes` */
|
|
1460
|
+
create(payload, options) {
|
|
1461
|
+
return this.core.request("post", "/inboxes", { body: payload, options });
|
|
1462
|
+
}
|
|
1463
|
+
/** `GET /inboxes` */
|
|
1464
|
+
list(params, options) {
|
|
1465
|
+
return this.core.request("get", "/inboxes", { query: params, options });
|
|
1466
|
+
}
|
|
1467
|
+
/** Every inbox, fetched page by page. */
|
|
1468
|
+
listAll(params, options) {
|
|
1469
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
1470
|
+
}
|
|
1471
|
+
/** `GET /inboxes/{inbox_id}`: `inboxId` is the inbox id or its email address. */
|
|
1472
|
+
get(inboxId, options) {
|
|
1473
|
+
return this.core.request("get", "/inboxes/{inbox_id}", { path: { inbox_id: inboxId }, options });
|
|
1474
|
+
}
|
|
1475
|
+
/** `PATCH /inboxes/{inbox_id}` */
|
|
1476
|
+
update(inboxId, payload, options) {
|
|
1477
|
+
return this.core.request("patch", "/inboxes/{inbox_id}", {
|
|
1478
|
+
path: { inbox_id: inboxId },
|
|
1479
|
+
body: payload,
|
|
1480
|
+
options
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
/** `DELETE /inboxes/{inbox_id}` */
|
|
1484
|
+
delete(inboxId, options) {
|
|
1485
|
+
return this.core.request("delete", "/inboxes/{inbox_id}", { path: { inbox_id: inboxId }, options });
|
|
1486
|
+
}
|
|
1487
|
+
/** `POST /inboxes/{inbox_id}/emails`: starts a conversation; answers the sent email or (202) a held draft. */
|
|
1488
|
+
send(inboxId, payload, options) {
|
|
1489
|
+
return this.core.request("post", "/inboxes/{inbox_id}/emails", {
|
|
1490
|
+
path: { inbox_id: inboxId },
|
|
1491
|
+
body: payload,
|
|
1492
|
+
...idempotent(options)
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
};
|
|
1496
|
+
|
|
1497
|
+
// src/resources/lists.ts
|
|
1498
|
+
var SuppressionBatch = class extends Resource {
|
|
1499
|
+
/** `POST /suppressions/batch/add` */
|
|
1500
|
+
add(payload, options) {
|
|
1501
|
+
return this.core.request("post", "/suppressions/batch/add", { body: payload, options });
|
|
1502
|
+
}
|
|
1503
|
+
/** `POST /suppressions/batch/remove` */
|
|
1504
|
+
remove(payload, options) {
|
|
1505
|
+
return this.core.request("post", "/suppressions/batch/remove", { body: payload, options });
|
|
1506
|
+
}
|
|
1507
|
+
};
|
|
1508
|
+
var Suppressions = class extends Resource {
|
|
1509
|
+
batch = new SuppressionBatch(this.core);
|
|
1510
|
+
/** `POST /suppressions` */
|
|
1511
|
+
create(payload, options) {
|
|
1512
|
+
return this.core.request("post", "/suppressions", { body: payload, options });
|
|
1513
|
+
}
|
|
1514
|
+
/** `GET /suppressions` */
|
|
1515
|
+
list(params, options) {
|
|
1516
|
+
return this.core.request("get", "/suppressions", { query: params, options });
|
|
1517
|
+
}
|
|
1518
|
+
/** Every suppression, fetched page by page. */
|
|
1519
|
+
listAll(params, options) {
|
|
1520
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
1521
|
+
}
|
|
1522
|
+
/** `GET /suppressions/{id}` */
|
|
1523
|
+
get(id, options) {
|
|
1524
|
+
return this.core.request("get", "/suppressions/{id}", { path: { id }, options });
|
|
1525
|
+
}
|
|
1526
|
+
/** `DELETE /suppressions/{id}` */
|
|
1527
|
+
delete(id, options) {
|
|
1528
|
+
return this.core.request("delete", "/suppressions/{id}", { path: { id }, options });
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
var Unsubscribes = class extends Resource {
|
|
1532
|
+
/** `POST /unsubscribes` */
|
|
1533
|
+
create(payload, options) {
|
|
1534
|
+
return this.core.request("post", "/unsubscribes", { body: payload, options });
|
|
1535
|
+
}
|
|
1536
|
+
/** `GET /unsubscribes` */
|
|
1537
|
+
list(params, options) {
|
|
1538
|
+
return this.core.request("get", "/unsubscribes", { query: params, options });
|
|
1539
|
+
}
|
|
1540
|
+
/** Every opt-out, fetched page by page. */
|
|
1541
|
+
listAll(params, options) {
|
|
1542
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
1543
|
+
}
|
|
1544
|
+
/** `DELETE /unsubscribes/{id}` */
|
|
1545
|
+
delete(id, options) {
|
|
1546
|
+
return this.core.request("delete", "/unsubscribes/{id}", { path: { id }, options });
|
|
1547
|
+
}
|
|
1548
|
+
};
|
|
1549
|
+
var Verifications = class extends Resource {
|
|
1550
|
+
/** `GET /verifications/availability` */
|
|
1551
|
+
availability(options) {
|
|
1552
|
+
return this.core.request("get", "/verifications/availability", { options });
|
|
1553
|
+
}
|
|
1554
|
+
/** `POST /verifications/estimate` */
|
|
1555
|
+
estimate(payload, options) {
|
|
1556
|
+
return this.core.request("post", "/verifications/estimate", { body: payload, options });
|
|
1557
|
+
}
|
|
1558
|
+
/** `POST /verifications`: starts a verification run. */
|
|
1559
|
+
create(payload, options) {
|
|
1560
|
+
return this.core.request("post", "/verifications", { body: payload, options });
|
|
1561
|
+
}
|
|
1562
|
+
/** `GET /verifications` */
|
|
1563
|
+
list(params, options) {
|
|
1564
|
+
return this.core.request("get", "/verifications", { query: params, options });
|
|
1565
|
+
}
|
|
1566
|
+
/** Every verification run, fetched page by page. */
|
|
1567
|
+
listAll(params, options) {
|
|
1568
|
+
return paginate((cursor) => this.list({ ...params, ...cursor }, options), params);
|
|
1569
|
+
}
|
|
1570
|
+
/** `GET /verifications/{id}` */
|
|
1571
|
+
get(id, options) {
|
|
1572
|
+
return this.core.request("get", "/verifications/{id}", { path: { id }, options });
|
|
1573
|
+
}
|
|
1574
|
+
};
|
|
1575
|
+
|
|
1576
|
+
// src/client.ts
|
|
1577
|
+
var BillionSend = class {
|
|
1578
|
+
emails;
|
|
1579
|
+
domains;
|
|
1580
|
+
apiKeys;
|
|
1581
|
+
contacts;
|
|
1582
|
+
audiences;
|
|
1583
|
+
contactProperties;
|
|
1584
|
+
segments;
|
|
1585
|
+
topics;
|
|
1586
|
+
broadcasts;
|
|
1587
|
+
templates;
|
|
1588
|
+
webhooks;
|
|
1589
|
+
suppressions;
|
|
1590
|
+
unsubscribes;
|
|
1591
|
+
verifications;
|
|
1592
|
+
usage;
|
|
1593
|
+
deliverability;
|
|
1594
|
+
inboxes;
|
|
1595
|
+
agent;
|
|
1596
|
+
#core;
|
|
1597
|
+
/**
|
|
1598
|
+
* @param apiKeyOrOptions An API key (`bs_...`), or options. Without a key the
|
|
1599
|
+
* client reads `BILLIONSEND_API_KEY`; only `agent.signup` works with no key at all.
|
|
1600
|
+
*/
|
|
1601
|
+
constructor(apiKeyOrOptions) {
|
|
1602
|
+
const options = typeof apiKeyOrOptions === "string" ? { apiKey: apiKeyOrOptions } : { ...apiKeyOrOptions };
|
|
1603
|
+
const core = new Core(options);
|
|
1604
|
+
this.#core = core;
|
|
1605
|
+
this.emails = new Emails(core);
|
|
1606
|
+
this.domains = new Domains(core);
|
|
1607
|
+
this.apiKeys = new ApiKeys(core);
|
|
1608
|
+
this.contacts = new Contacts(core);
|
|
1609
|
+
this.audiences = new Audiences(core);
|
|
1610
|
+
this.contactProperties = new ContactProperties(core);
|
|
1611
|
+
this.segments = new Segments(core);
|
|
1612
|
+
this.topics = new Topics(core);
|
|
1613
|
+
this.broadcasts = new Broadcasts(core);
|
|
1614
|
+
this.templates = new Templates(core);
|
|
1615
|
+
this.webhooks = new Webhooks(core);
|
|
1616
|
+
this.suppressions = new Suppressions(core);
|
|
1617
|
+
this.unsubscribes = new Unsubscribes(core);
|
|
1618
|
+
this.verifications = new Verifications(core);
|
|
1619
|
+
this.usage = new Usage(core);
|
|
1620
|
+
this.deliverability = new Deliverability(core);
|
|
1621
|
+
this.inboxes = new Inboxes(core);
|
|
1622
|
+
this.agent = new Agent(core);
|
|
1623
|
+
}
|
|
1624
|
+
/** The API base URL this client calls. */
|
|
1625
|
+
get baseUrl() {
|
|
1626
|
+
return this.#core.baseUrl;
|
|
1627
|
+
}
|
|
1628
|
+
};
|
|
1629
|
+
|
|
1630
|
+
// src/webhooks.ts
|
|
1631
|
+
var WEBHOOK_TOLERANCE_SECONDS = 300;
|
|
1632
|
+
var SECRET_PREFIX = "whsec_";
|
|
1633
|
+
var SECRET_MIN_BYTES = 24;
|
|
1634
|
+
var SECRET_MAX_BYTES = 64;
|
|
1635
|
+
function header(headers, name) {
|
|
1636
|
+
if (typeof headers.get === "function" && typeof headers.forEach === "function") {
|
|
1637
|
+
return headers.get(name) ?? void 0;
|
|
1638
|
+
}
|
|
1639
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
1640
|
+
if (key.toLowerCase() !== name) continue;
|
|
1641
|
+
return Array.isArray(value) ? value[0] : value;
|
|
1642
|
+
}
|
|
1643
|
+
return void 0;
|
|
1644
|
+
}
|
|
1645
|
+
function decodeBase64(text) {
|
|
1646
|
+
try {
|
|
1647
|
+
const binary = atob(text);
|
|
1648
|
+
return Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
1649
|
+
} catch {
|
|
1650
|
+
return null;
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
function encodeBase64(bytes) {
|
|
1654
|
+
let binary = "";
|
|
1655
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
1656
|
+
return btoa(binary);
|
|
1657
|
+
}
|
|
1658
|
+
function secretKey(secret) {
|
|
1659
|
+
const encoded = secret.startsWith(SECRET_PREFIX) ? secret.slice(SECRET_PREFIX.length) : null;
|
|
1660
|
+
const key = encoded === null ? null : decodeBase64(encoded);
|
|
1661
|
+
if (encoded === null || key === null || key.length < SECRET_MIN_BYTES || key.length > SECRET_MAX_BYTES || encodeBase64(key) !== encoded) {
|
|
1662
|
+
throw new WebhookVerificationError("The webhook secret is not a whsec_ secret");
|
|
1663
|
+
}
|
|
1664
|
+
return key;
|
|
1665
|
+
}
|
|
1666
|
+
async function subtleCrypto() {
|
|
1667
|
+
const subtle = globalThis.crypto?.subtle;
|
|
1668
|
+
if (subtle) return subtle;
|
|
1669
|
+
const nodeCrypto = await import('crypto');
|
|
1670
|
+
return nodeCrypto.webcrypto.subtle;
|
|
1671
|
+
}
|
|
1672
|
+
function bytesOf(payload) {
|
|
1673
|
+
if (typeof payload === "string") return new TextEncoder().encode(payload);
|
|
1674
|
+
return payload instanceof Uint8Array ? payload : new Uint8Array(payload);
|
|
1675
|
+
}
|
|
1676
|
+
function equal(a, b) {
|
|
1677
|
+
if (a.length !== b.length) return false;
|
|
1678
|
+
let diff = 0;
|
|
1679
|
+
for (let i = 0; i < a.length; i++) diff |= (a[i] ?? 0) ^ (b[i] ?? 0);
|
|
1680
|
+
return diff === 0;
|
|
1681
|
+
}
|
|
1682
|
+
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");
|
|
1686
|
+
if (!id || !timestamp || !signature) {
|
|
1687
|
+
throw new WebhookVerificationError(
|
|
1688
|
+
"Missing the webhook-id, webhook-timestamp or webhook-signature header"
|
|
1689
|
+
);
|
|
1690
|
+
}
|
|
1691
|
+
if (!/^\d+$/.test(timestamp)) throw new WebhookVerificationError("Invalid webhook-timestamp header");
|
|
1692
|
+
const tolerance = options.tolerance ?? WEBHOOK_TOLERANCE_SECONDS;
|
|
1693
|
+
const now = Math.floor((options.now ?? /* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
1694
|
+
const seconds = Number(timestamp);
|
|
1695
|
+
if (now - seconds > tolerance) throw new WebhookVerificationError("The webhook timestamp is too old");
|
|
1696
|
+
if (seconds - now > tolerance) throw new WebhookVerificationError("The webhook timestamp is too new");
|
|
1697
|
+
const body = bytesOf(payload);
|
|
1698
|
+
const subtle = await subtleCrypto();
|
|
1699
|
+
const key = await subtle.importKey("raw", secretKey(secret), { name: "HMAC", hash: "SHA-256" }, false, [
|
|
1700
|
+
"sign"
|
|
1701
|
+
]);
|
|
1702
|
+
const prefix = new TextEncoder().encode(`${id}.${timestamp}.`);
|
|
1703
|
+
const signed = new Uint8Array(prefix.length + body.length);
|
|
1704
|
+
signed.set(prefix);
|
|
1705
|
+
signed.set(body, prefix.length);
|
|
1706
|
+
const expected = new Uint8Array(await subtle.sign("HMAC", key, signed));
|
|
1707
|
+
const matched = signature.split(" ").some((candidate) => {
|
|
1708
|
+
const comma = candidate.indexOf(",");
|
|
1709
|
+
if (comma === -1 || candidate.slice(0, comma) !== "v1") return false;
|
|
1710
|
+
const given = decodeBase64(candidate.slice(comma + 1));
|
|
1711
|
+
return given !== null && equal(given, expected);
|
|
1712
|
+
});
|
|
1713
|
+
if (!matched) throw new WebhookVerificationError("No webhook signature matches");
|
|
1714
|
+
try {
|
|
1715
|
+
return JSON.parse(new TextDecoder().decode(body));
|
|
1716
|
+
} catch {
|
|
1717
|
+
throw new WebhookVerificationError("The webhook body is not JSON");
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
export { APIConnectionError, APIError, APITimeoutError, AuthenticationError, BillionSend, BillionSendError, ConflictError, DEFAULT_BASE_URL, DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT_MS, EventStream, NotFoundError, PermissionDeniedError, RateLimitError, STREAM_GAP_EVENT, VERSION, ValidationError, WEBHOOK_TOLERANCE_SECONDS, WebhookVerificationError, paginate, verifyWebhook };
|
|
1722
|
+
//# sourceMappingURL=index.js.map
|
|
1723
|
+
//# sourceMappingURL=index.js.map
|