tosend 1.0.1 → 1.2.0
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/README.md +55 -3
- package/dist/index.d.mts +368 -11
- package/dist/index.d.ts +368 -11
- package/dist/index.js +214 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +213 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,8 @@ interface Attachment {
|
|
|
15
15
|
name: string;
|
|
16
16
|
/** Base64 encoded content */
|
|
17
17
|
content: string;
|
|
18
|
+
/** Embeds the file inline: reference it in the HTML as `cid:<content_id>` */
|
|
19
|
+
content_id?: string;
|
|
18
20
|
}
|
|
19
21
|
interface SendEmailParams {
|
|
20
22
|
/** Sender address */
|
|
@@ -45,23 +47,36 @@ interface BatchEmailResult {
|
|
|
45
47
|
status: 'success' | 'error' | 'spam';
|
|
46
48
|
message_id?: string;
|
|
47
49
|
message?: string;
|
|
48
|
-
errors?:
|
|
50
|
+
errors?: ValidationErrors;
|
|
49
51
|
}
|
|
50
52
|
interface BatchEmailResponse {
|
|
51
53
|
results: BatchEmailResult[];
|
|
52
54
|
}
|
|
53
55
|
interface Domain {
|
|
56
|
+
id: number;
|
|
54
57
|
domain_name: string;
|
|
55
|
-
verification_status:
|
|
56
|
-
|
|
58
|
+
verification_status: string;
|
|
59
|
+
/** 1 for the account's default domain, else 0 */
|
|
60
|
+
is_default: number;
|
|
61
|
+
total_sent: number;
|
|
62
|
+
total_bounced: number;
|
|
63
|
+
total_complained: number;
|
|
57
64
|
}
|
|
58
65
|
interface AccountInfo {
|
|
59
66
|
account: {
|
|
60
|
-
|
|
61
|
-
plan_type: string;
|
|
67
|
+
id: number;
|
|
62
68
|
status: string;
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
plan_type: string;
|
|
70
|
+
/** null means the account does not consume credits */
|
|
71
|
+
credit_balance: number | null;
|
|
72
|
+
monthly_email_limit: number;
|
|
73
|
+
limit_per_second: number;
|
|
74
|
+
ses_region: string;
|
|
75
|
+
/** @deprecated Not returned to a sending key. Use `new ToSendAdmin(key).getInfo()`. */
|
|
76
|
+
bounce_rate?: number;
|
|
77
|
+
/** @deprecated Not returned to a sending key. Use `new ToSendAdmin(key).getInfo()`. */
|
|
78
|
+
complaint_rate?: number;
|
|
79
|
+
ses_status: string;
|
|
65
80
|
};
|
|
66
81
|
domains: Domain[];
|
|
67
82
|
}
|
|
@@ -69,8 +84,25 @@ interface ToSendErrorResponse {
|
|
|
69
84
|
status_code: number;
|
|
70
85
|
error_type: ErrorType;
|
|
71
86
|
message: string;
|
|
72
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Field-level validation detail.
|
|
89
|
+
*
|
|
90
|
+
* The sending API returns a nested map (`{subject: {required: "..."}}`);
|
|
91
|
+
* the Worker's ApiError returns a flat one (`{name: "required"}`). Both
|
|
92
|
+
* shapes reach this type, so consumers must not assume either — see
|
|
93
|
+
* ToSendError.errors.
|
|
94
|
+
*/
|
|
95
|
+
errors?: ValidationErrors;
|
|
73
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Either shape the API may return for `errors`.
|
|
99
|
+
*
|
|
100
|
+
* ToSendError declared the nested form only, so `err.errors.name` was typed
|
|
101
|
+
* as an object while the Worker actually sends the string `'required'`. That
|
|
102
|
+
* compiled because the response was cast, and would have surfaced as an
|
|
103
|
+
* undefined property at runtime.
|
|
104
|
+
*/
|
|
105
|
+
type ValidationErrors = Record<string, string | Record<string, string>>;
|
|
74
106
|
type ErrorType = 'bad_request' | 'unauthorized' | 'forbidden' | 'not_found' | 'validation_error' | 'rate_limit_exceeded' | 'internal_error';
|
|
75
107
|
interface ToSendConfig {
|
|
76
108
|
/** Your ToSend API key */
|
|
@@ -83,9 +115,10 @@ interface ToSendConfig {
|
|
|
83
115
|
declare class ToSendError extends Error {
|
|
84
116
|
readonly code: number;
|
|
85
117
|
readonly errorType: ErrorType;
|
|
86
|
-
|
|
118
|
+
/** See ValidationErrors — the value may be a string or a nested map. */
|
|
119
|
+
readonly errors: ValidationErrors;
|
|
87
120
|
private static readonly ERROR_TYPE_MAP;
|
|
88
|
-
constructor(message: string, code?: number, errors?:
|
|
121
|
+
constructor(message: string, code?: number, errors?: ValidationErrors, errorType?: ErrorType);
|
|
89
122
|
get isValidationError(): boolean;
|
|
90
123
|
get isAuthenticationError(): boolean;
|
|
91
124
|
get isRateLimitError(): boolean;
|
|
@@ -154,5 +187,329 @@ declare class ToSend {
|
|
|
154
187
|
private isValidEmail;
|
|
155
188
|
private request;
|
|
156
189
|
}
|
|
190
|
+
interface AdminDomain {
|
|
191
|
+
/** Domain identifier used across the Admin API */
|
|
192
|
+
id: string;
|
|
193
|
+
domain_name: string;
|
|
194
|
+
verification_status: 'draft' | 'pending' | 'verified' | 'failed';
|
|
195
|
+
is_default: boolean;
|
|
196
|
+
ses_region: string | null;
|
|
197
|
+
total_sent: number;
|
|
198
|
+
total_bounced: number;
|
|
199
|
+
total_complained: number;
|
|
200
|
+
updated_at: string;
|
|
201
|
+
}
|
|
202
|
+
interface DnsRecord {
|
|
203
|
+
type: 'TXT' | 'MX';
|
|
204
|
+
name: string;
|
|
205
|
+
content: string;
|
|
206
|
+
TTL: string;
|
|
207
|
+
priority: string;
|
|
208
|
+
/**
|
|
209
|
+
* NOT returned by the Admin API.
|
|
210
|
+
*
|
|
211
|
+
* The domain-detail endpoint does not resolve DNS on your behalf (that
|
|
212
|
+
* would make a GET fire four lookups and a write), so there is no
|
|
213
|
+
* per-record status. Poll `verification_status` on the domain instead —
|
|
214
|
+
* it is the result of the automatic 10-minute check.
|
|
215
|
+
*
|
|
216
|
+
* @deprecated Always undefined. Kept so existing code still compiles.
|
|
217
|
+
*/
|
|
218
|
+
status?: string;
|
|
219
|
+
}
|
|
220
|
+
interface CreatedDomain {
|
|
221
|
+
/** Same identifier the list and detail endpoints use */
|
|
222
|
+
id: string;
|
|
223
|
+
domain_name: string;
|
|
224
|
+
verification_status: string;
|
|
225
|
+
created_at: string;
|
|
226
|
+
}
|
|
227
|
+
interface AdminDomainDetail {
|
|
228
|
+
id: string;
|
|
229
|
+
domain_name: string;
|
|
230
|
+
verification_status: string;
|
|
231
|
+
dmarc_policy: string | null;
|
|
232
|
+
created_at: string;
|
|
233
|
+
dns_records: {
|
|
234
|
+
dkim: DnsRecord;
|
|
235
|
+
spf: DnsRecord[];
|
|
236
|
+
};
|
|
237
|
+
/** Whether the automatic 10-minute DNS check still covers this domain */
|
|
238
|
+
auto_verification: {
|
|
239
|
+
active: boolean;
|
|
240
|
+
reason: string;
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
interface EmailLog {
|
|
244
|
+
/** Same value POST /v2/emails returned as message_id */
|
|
245
|
+
id: string;
|
|
246
|
+
status: string;
|
|
247
|
+
subject: string | null;
|
|
248
|
+
from: string | null;
|
|
249
|
+
from_name: string | null;
|
|
250
|
+
to: Array<{
|
|
251
|
+
email: string;
|
|
252
|
+
name?: string;
|
|
253
|
+
}>;
|
|
254
|
+
reply_to: string | null;
|
|
255
|
+
recipients_count: number;
|
|
256
|
+
error: string | null;
|
|
257
|
+
has_attachments: boolean;
|
|
258
|
+
sent_at: string | null;
|
|
259
|
+
created_at: string;
|
|
260
|
+
}
|
|
261
|
+
interface Paginated<T> {
|
|
262
|
+
data: T[];
|
|
263
|
+
total: number;
|
|
264
|
+
page: number;
|
|
265
|
+
per_page: number;
|
|
266
|
+
}
|
|
267
|
+
interface ListLogsParams {
|
|
268
|
+
page?: number;
|
|
269
|
+
/** 1-100, default 25 */
|
|
270
|
+
per_page?: number;
|
|
271
|
+
status?: string;
|
|
272
|
+
/** Domain name or domain id */
|
|
273
|
+
domain?: string;
|
|
274
|
+
from?: string;
|
|
275
|
+
to?: string;
|
|
276
|
+
search?: string;
|
|
277
|
+
}
|
|
278
|
+
interface Suppression {
|
|
279
|
+
email: string;
|
|
280
|
+
bounce_sub_type: string | null;
|
|
281
|
+
bounce_reason: string | null;
|
|
282
|
+
hard_bounced_at: string | null;
|
|
283
|
+
last_complaint_at: string | null;
|
|
284
|
+
complaint_count: number;
|
|
285
|
+
created_at: string;
|
|
286
|
+
updated_at: string;
|
|
287
|
+
}
|
|
288
|
+
/** The only events toSend emits. Delivery, open and click do not exist. */
|
|
289
|
+
type WebhookEvent = 'bounced' | 'complaint';
|
|
290
|
+
interface Webhook {
|
|
291
|
+
id: number;
|
|
292
|
+
title: string | null;
|
|
293
|
+
url: string;
|
|
294
|
+
events: WebhookEvent[];
|
|
295
|
+
status: string;
|
|
296
|
+
include_message: boolean;
|
|
297
|
+
created_at: string;
|
|
298
|
+
updated_at: string;
|
|
299
|
+
}
|
|
300
|
+
interface SendingKey {
|
|
301
|
+
id: number;
|
|
302
|
+
title: string | null;
|
|
303
|
+
/** Masked. The full key is returned only by createSendingKey(). */
|
|
304
|
+
key: string;
|
|
305
|
+
status: string;
|
|
306
|
+
domain: string | null;
|
|
307
|
+
created_at: string;
|
|
308
|
+
}
|
|
309
|
+
interface CreatedSendingKey {
|
|
310
|
+
id: number;
|
|
311
|
+
name: string;
|
|
312
|
+
/** Shown once. Store it now — it cannot be retrieved later. */
|
|
313
|
+
api_key: string;
|
|
314
|
+
created_at: string;
|
|
315
|
+
}
|
|
316
|
+
/** GET /v2/info with an admin key */
|
|
317
|
+
interface AdminInfo {
|
|
318
|
+
account: {
|
|
319
|
+
id: number;
|
|
320
|
+
status: string;
|
|
321
|
+
plan_type: string;
|
|
322
|
+
/** The pooled balance for the whole account, never a child team's own */
|
|
323
|
+
credit_balance: number | null;
|
|
324
|
+
credits_shared: boolean;
|
|
325
|
+
limit_per_second: number;
|
|
326
|
+
ses_region: string;
|
|
327
|
+
/** Last 30 days, fraction between 0 and 1 (0.004 = 0.4%) */
|
|
328
|
+
bounce_rate: number;
|
|
329
|
+
/** Last 30 days, fraction between 0 and 1 */
|
|
330
|
+
complaint_rate: number;
|
|
331
|
+
};
|
|
332
|
+
key: {
|
|
333
|
+
id: number;
|
|
334
|
+
title: string;
|
|
335
|
+
type: 'admin';
|
|
336
|
+
/** The team this request acted on */
|
|
337
|
+
acting_on: number;
|
|
338
|
+
/** The team that owns the key */
|
|
339
|
+
owned_by: number;
|
|
340
|
+
scopes: string[];
|
|
341
|
+
};
|
|
342
|
+
/** Present only when the key has `domains:read` */
|
|
343
|
+
domains?: AdminDomain[];
|
|
344
|
+
}
|
|
345
|
+
interface SuppressionReactivation {
|
|
346
|
+
deleted: boolean;
|
|
347
|
+
message: string;
|
|
348
|
+
used_today: number;
|
|
349
|
+
remaining_today: number;
|
|
350
|
+
daily_limit: number;
|
|
351
|
+
}
|
|
352
|
+
/** Where a team sends from. Fixed for the life of the team. */
|
|
353
|
+
type SesRegion = 'us-east-1' | 'eu-central-1';
|
|
354
|
+
interface TeamList {
|
|
355
|
+
account: {
|
|
356
|
+
id: number;
|
|
357
|
+
name: string | null;
|
|
358
|
+
status: string;
|
|
359
|
+
credit_balance: number | null;
|
|
360
|
+
};
|
|
361
|
+
teams: Array<{
|
|
362
|
+
id: number;
|
|
363
|
+
name: string | null;
|
|
364
|
+
status: string;
|
|
365
|
+
ses_region: SesRegion;
|
|
366
|
+
}>;
|
|
367
|
+
/** Credits pool at the parent; child teams have no separate balance */
|
|
368
|
+
credits_shared: boolean;
|
|
369
|
+
}
|
|
370
|
+
interface AdminConfig {
|
|
371
|
+
/** Your admin API key (tsend_admin_...) */
|
|
372
|
+
apiKey: string;
|
|
373
|
+
/** Act on a child team by default. Overridable per call. */
|
|
374
|
+
accountId?: number;
|
|
375
|
+
baseUrl?: string;
|
|
376
|
+
timeout?: number;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Account-level API client.
|
|
380
|
+
*
|
|
381
|
+
* Deliberately a separate class from `ToSend`: admin keys and sending keys are
|
|
382
|
+
* different credentials and are not interchangeable. A sending key cannot
|
|
383
|
+
* manage the account, and an admin key can only send when it holds the
|
|
384
|
+
* `emails:send` scope. Keeping them on separate clients means the mistake is
|
|
385
|
+
* caught at the type level rather than by a 401 in production.
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* ```ts
|
|
389
|
+
* const admin = new ToSendAdmin(process.env.TOSEND_ADMIN_KEY!);
|
|
390
|
+
*
|
|
391
|
+
* const domain = await admin.createDomain('client.example.com');
|
|
392
|
+
* const detail = await admin.getDomain(domain.id);
|
|
393
|
+
* console.log(detail.dns_records.dkim);
|
|
394
|
+
* // No verify call needed — toSend checks DNS every 10 minutes.
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
declare class ToSendAdmin {
|
|
398
|
+
private readonly apiKey;
|
|
399
|
+
private readonly baseUrl;
|
|
400
|
+
private readonly timeout;
|
|
401
|
+
private readonly accountId?;
|
|
402
|
+
constructor(config: string | AdminConfig);
|
|
403
|
+
/**
|
|
404
|
+
* The team this key acts on, the key itself and its scopes. Needs no scope,
|
|
405
|
+
* so it is the way to check a key works.
|
|
406
|
+
*/
|
|
407
|
+
getInfo(): Promise<AdminInfo>;
|
|
408
|
+
/** Return a client scoped to one of your child teams. */
|
|
409
|
+
forAccount(accountId: number): ToSendAdmin;
|
|
410
|
+
/**
|
|
411
|
+
* Send an email as the targeted team (the key's own, or one set with
|
|
412
|
+
* forAccount()). The key must hold the `emails:send` scope.
|
|
413
|
+
*/
|
|
414
|
+
send(params: SendEmailParams): Promise<SendEmailResponse>;
|
|
415
|
+
/** Send up to 100 emails in one request; same scope and targeting as send(). */
|
|
416
|
+
batch(emails: SendEmailParams[]): Promise<BatchEmailResponse>;
|
|
417
|
+
listDomains(params?: {
|
|
418
|
+
page?: number;
|
|
419
|
+
per_page?: number;
|
|
420
|
+
}): Promise<Paginated<AdminDomain>>;
|
|
421
|
+
/**
|
|
422
|
+
* Add a sending domain.
|
|
423
|
+
*
|
|
424
|
+
* The domain starts in `draft`. There is NO verify call to make: toSend
|
|
425
|
+
* checks public DNS every 10 minutes and provisions the domain as soon as
|
|
426
|
+
* the records resolve.
|
|
427
|
+
*
|
|
428
|
+
* A duplicate throws a 409. Treat that as "already exists, go read it" —
|
|
429
|
+
* domain names are globally unique, so a retry after a timeout hits this
|
|
430
|
+
* for a domain your first call created successfully.
|
|
431
|
+
*/
|
|
432
|
+
createDomain(name: string): Promise<CreatedDomain>;
|
|
433
|
+
/** Domain detail plus the DNS records your customer must publish. */
|
|
434
|
+
getDomain(id: string): Promise<AdminDomainDetail>;
|
|
435
|
+
/**
|
|
436
|
+
* Remove a domain.
|
|
437
|
+
*
|
|
438
|
+
* Does NOT remove anything from your customer's DNS zone. Records left
|
|
439
|
+
* published can allow the domain to be re-verified elsewhere, so clean them
|
|
440
|
+
* up in your own workflow.
|
|
441
|
+
*/
|
|
442
|
+
deleteDomain(id: string): Promise<void>;
|
|
443
|
+
listEmails(params?: ListLogsParams): Promise<Paginated<EmailLog>>;
|
|
444
|
+
/** Look up one message by the id POST /v2/emails returned. */
|
|
445
|
+
getEmail(messageId: string): Promise<EmailLog>;
|
|
446
|
+
listSuppressions(params?: {
|
|
447
|
+
page?: number;
|
|
448
|
+
per_page?: number;
|
|
449
|
+
search?: string;
|
|
450
|
+
status?: string;
|
|
451
|
+
}): Promise<Paginated<Suppression>>;
|
|
452
|
+
/** Limited to 500 per day per account, shared with the dashboard. */
|
|
453
|
+
deleteSuppression(email: string): Promise<SuppressionReactivation>;
|
|
454
|
+
listWebhooks(params?: {
|
|
455
|
+
page?: number;
|
|
456
|
+
per_page?: number;
|
|
457
|
+
}): Promise<Paginated<Webhook>>;
|
|
458
|
+
/**
|
|
459
|
+
* The signing `secret` is generated by toSend. The API returns it only in
|
|
460
|
+
* this response (the dashboard can show it later), so store it now.
|
|
461
|
+
*/
|
|
462
|
+
createWebhook(params: {
|
|
463
|
+
url: string;
|
|
464
|
+
events: WebhookEvent[];
|
|
465
|
+
title?: string;
|
|
466
|
+
include_message?: boolean;
|
|
467
|
+
}): Promise<Webhook & {
|
|
468
|
+
secret: string;
|
|
469
|
+
}>;
|
|
470
|
+
updateWebhook(id: number, params: Partial<{
|
|
471
|
+
url: string;
|
|
472
|
+
events: WebhookEvent[];
|
|
473
|
+
title: string;
|
|
474
|
+
status: 'active' | 'inactive';
|
|
475
|
+
include_message: boolean;
|
|
476
|
+
}>): Promise<Webhook>;
|
|
477
|
+
deleteWebhook(id: number): Promise<void>;
|
|
478
|
+
listSendingKeys(params?: {
|
|
479
|
+
page?: number;
|
|
480
|
+
per_page?: number;
|
|
481
|
+
}): Promise<Paginated<SendingKey>>;
|
|
482
|
+
/**
|
|
483
|
+
* Mint a sending key for this team — how you give each client their own key.
|
|
484
|
+
*
|
|
485
|
+
* The raw key is returned once and never again. Pass `domain` (a domain id)
|
|
486
|
+
* to restrict it to one domain; omit it for all domains on the team.
|
|
487
|
+
* Needs the `api_keys:write` and `emails:send` scopes.
|
|
488
|
+
*/
|
|
489
|
+
createSendingKey(name: string, domain?: string): Promise<CreatedSendingKey>;
|
|
490
|
+
deleteSendingKey(id: number): Promise<void>;
|
|
491
|
+
listTeams(): Promise<TeamList>;
|
|
492
|
+
/**
|
|
493
|
+
* Only the parent account can create teams. `ses_region` defaults to
|
|
494
|
+
* 'us-east-1' and cannot be changed once the team exists.
|
|
495
|
+
*/
|
|
496
|
+
createTeam(name: string, options?: {
|
|
497
|
+
ses_region?: SesRegion;
|
|
498
|
+
}): Promise<{
|
|
499
|
+
id: number;
|
|
500
|
+
name: string;
|
|
501
|
+
status: string;
|
|
502
|
+
ses_region: SesRegion;
|
|
503
|
+
}>;
|
|
504
|
+
updateTeam(id: number, params: {
|
|
505
|
+
name?: string;
|
|
506
|
+
status?: 'active' | 'suspended';
|
|
507
|
+
}): Promise<{
|
|
508
|
+
id: number;
|
|
509
|
+
name: string;
|
|
510
|
+
status: string;
|
|
511
|
+
}>;
|
|
512
|
+
private request;
|
|
513
|
+
}
|
|
157
514
|
|
|
158
|
-
export { type AccountInfo, type Address, type Attachment, type BatchEmailResponse, type BatchEmailResult, type Domain, type ErrorType, type SendEmailParams, type SendEmailResponse, ToSend, type ToSendConfig, ToSendError, type ToSendErrorResponse, ToSend as default };
|
|
515
|
+
export { type AccountInfo, type Address, type AdminConfig, type AdminDomain, type AdminDomainDetail, type AdminInfo, type Attachment, type BatchEmailResponse, type BatchEmailResult, type CreatedDomain, type CreatedSendingKey, type DnsRecord, type Domain, type EmailLog, type ErrorType, type ListLogsParams, type Paginated, type SendEmailParams, type SendEmailResponse, type SendingKey, type SesRegion, type Suppression, type SuppressionReactivation, type TeamList, ToSend, ToSendAdmin, type ToSendConfig, ToSendError, type ToSendErrorResponse, type ValidationErrors, type Webhook, type WebhookEvent, ToSend as default };
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
ToSend: () => ToSend,
|
|
24
|
+
ToSendAdmin: () => ToSendAdmin,
|
|
24
25
|
ToSendError: () => ToSendError,
|
|
25
26
|
default: () => index_default
|
|
26
27
|
});
|
|
@@ -28,6 +29,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
28
29
|
var ToSendError = class _ToSendError extends Error {
|
|
29
30
|
code;
|
|
30
31
|
errorType;
|
|
32
|
+
/** See ValidationErrors — the value may be a string or a nested map. */
|
|
31
33
|
errors;
|
|
32
34
|
static ERROR_TYPE_MAP = {
|
|
33
35
|
400: "bad_request",
|
|
@@ -229,10 +231,222 @@ var ToSend = class {
|
|
|
229
231
|
}
|
|
230
232
|
}
|
|
231
233
|
};
|
|
234
|
+
var ToSendAdmin = class _ToSendAdmin {
|
|
235
|
+
apiKey;
|
|
236
|
+
baseUrl;
|
|
237
|
+
timeout;
|
|
238
|
+
accountId;
|
|
239
|
+
constructor(config) {
|
|
240
|
+
if (typeof config === "string") {
|
|
241
|
+
this.apiKey = config;
|
|
242
|
+
this.baseUrl = "https://api.tosend.com";
|
|
243
|
+
this.timeout = 3e4;
|
|
244
|
+
} else {
|
|
245
|
+
this.apiKey = config.apiKey;
|
|
246
|
+
this.baseUrl = config.baseUrl?.replace(/\/$/, "") ?? "https://api.tosend.com";
|
|
247
|
+
this.timeout = config.timeout ?? 3e4;
|
|
248
|
+
this.accountId = config.accountId;
|
|
249
|
+
}
|
|
250
|
+
if (!this.apiKey?.trim()) {
|
|
251
|
+
throw new ToSendError("Admin API key is required", 401);
|
|
252
|
+
}
|
|
253
|
+
if (!this.apiKey.startsWith("tsend_admin_")) {
|
|
254
|
+
throw new ToSendError(
|
|
255
|
+
'This is not an admin API key. Admin keys start with "tsend_admin_" and are created in the dashboard under API Keys.',
|
|
256
|
+
401
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* The team this key acts on, the key itself and its scopes. Needs no scope,
|
|
262
|
+
* so it is the way to check a key works.
|
|
263
|
+
*/
|
|
264
|
+
async getInfo() {
|
|
265
|
+
return this.request("GET", "/v2/info");
|
|
266
|
+
}
|
|
267
|
+
/** Return a client scoped to one of your child teams. */
|
|
268
|
+
forAccount(accountId) {
|
|
269
|
+
return new _ToSendAdmin({
|
|
270
|
+
apiKey: this.apiKey,
|
|
271
|
+
baseUrl: this.baseUrl,
|
|
272
|
+
timeout: this.timeout,
|
|
273
|
+
accountId
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
// ── Sending (requires the emails:send scope) ──
|
|
277
|
+
/**
|
|
278
|
+
* Send an email as the targeted team (the key's own, or one set with
|
|
279
|
+
* forAccount()). The key must hold the `emails:send` scope.
|
|
280
|
+
*/
|
|
281
|
+
async send(params) {
|
|
282
|
+
return this.request("POST", "/v2/emails", params);
|
|
283
|
+
}
|
|
284
|
+
/** Send up to 100 emails in one request; same scope and targeting as send(). */
|
|
285
|
+
async batch(emails) {
|
|
286
|
+
return this.request("POST", "/v2/emails/batch", { emails });
|
|
287
|
+
}
|
|
288
|
+
// ── Domains ──
|
|
289
|
+
async listDomains(params = {}) {
|
|
290
|
+
return this.request("GET", "/v2/domains", void 0, params);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Add a sending domain.
|
|
294
|
+
*
|
|
295
|
+
* The domain starts in `draft`. There is NO verify call to make: toSend
|
|
296
|
+
* checks public DNS every 10 minutes and provisions the domain as soon as
|
|
297
|
+
* the records resolve.
|
|
298
|
+
*
|
|
299
|
+
* A duplicate throws a 409. Treat that as "already exists, go read it" —
|
|
300
|
+
* domain names are globally unique, so a retry after a timeout hits this
|
|
301
|
+
* for a domain your first call created successfully.
|
|
302
|
+
*/
|
|
303
|
+
async createDomain(name) {
|
|
304
|
+
const res = await this.request("POST", "/v2/domains", { name });
|
|
305
|
+
return res.data;
|
|
306
|
+
}
|
|
307
|
+
/** Domain detail plus the DNS records your customer must publish. */
|
|
308
|
+
async getDomain(id) {
|
|
309
|
+
const res = await this.request("GET", `/v2/domains/${encodeURIComponent(id)}`);
|
|
310
|
+
return res.data;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Remove a domain.
|
|
314
|
+
*
|
|
315
|
+
* Does NOT remove anything from your customer's DNS zone. Records left
|
|
316
|
+
* published can allow the domain to be re-verified elsewhere, so clean them
|
|
317
|
+
* up in your own workflow.
|
|
318
|
+
*/
|
|
319
|
+
async deleteDomain(id) {
|
|
320
|
+
await this.request("DELETE", `/v2/domains/${encodeURIComponent(id)}`);
|
|
321
|
+
}
|
|
322
|
+
// ── Logs ──
|
|
323
|
+
async listEmails(params = {}) {
|
|
324
|
+
return this.request("GET", "/v2/emails", void 0, params);
|
|
325
|
+
}
|
|
326
|
+
/** Look up one message by the id POST /v2/emails returned. */
|
|
327
|
+
async getEmail(messageId) {
|
|
328
|
+
const res = await this.request("GET", `/v2/emails/${encodeURIComponent(messageId)}`);
|
|
329
|
+
return res.data;
|
|
330
|
+
}
|
|
331
|
+
// ── Suppressions ──
|
|
332
|
+
async listSuppressions(params = {}) {
|
|
333
|
+
return this.request("GET", "/v2/suppressions", void 0, params);
|
|
334
|
+
}
|
|
335
|
+
/** Limited to 500 per day per account, shared with the dashboard. */
|
|
336
|
+
async deleteSuppression(email) {
|
|
337
|
+
return this.request("DELETE", "/v2/suppressions", void 0, { email });
|
|
338
|
+
}
|
|
339
|
+
// ── Webhooks ──
|
|
340
|
+
async listWebhooks(params = {}) {
|
|
341
|
+
return this.request("GET", "/v2/webhooks", void 0, params);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* The signing `secret` is generated by toSend. The API returns it only in
|
|
345
|
+
* this response (the dashboard can show it later), so store it now.
|
|
346
|
+
*/
|
|
347
|
+
async createWebhook(params) {
|
|
348
|
+
const res = await this.request("POST", "/v2/webhooks", params);
|
|
349
|
+
return res.data;
|
|
350
|
+
}
|
|
351
|
+
async updateWebhook(id, params) {
|
|
352
|
+
const res = await this.request("PUT", `/v2/webhooks/${id}`, params);
|
|
353
|
+
return res.data;
|
|
354
|
+
}
|
|
355
|
+
async deleteWebhook(id) {
|
|
356
|
+
await this.request("DELETE", `/v2/webhooks/${id}`);
|
|
357
|
+
}
|
|
358
|
+
// ── Sending keys ──
|
|
359
|
+
async listSendingKeys(params = {}) {
|
|
360
|
+
return this.request("GET", "/v2/api-keys", void 0, params);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Mint a sending key for this team — how you give each client their own key.
|
|
364
|
+
*
|
|
365
|
+
* The raw key is returned once and never again. Pass `domain` (a domain id)
|
|
366
|
+
* to restrict it to one domain; omit it for all domains on the team.
|
|
367
|
+
* Needs the `api_keys:write` and `emails:send` scopes.
|
|
368
|
+
*/
|
|
369
|
+
async createSendingKey(name, domain) {
|
|
370
|
+
const res = await this.request("POST", "/v2/api-keys", { name, domain });
|
|
371
|
+
return res.data;
|
|
372
|
+
}
|
|
373
|
+
async deleteSendingKey(id) {
|
|
374
|
+
await this.request("DELETE", `/v2/api-keys/${id}`);
|
|
375
|
+
}
|
|
376
|
+
// ── Teams ──
|
|
377
|
+
async listTeams() {
|
|
378
|
+
const res = await this.request("GET", "/v2/teams");
|
|
379
|
+
return res.data;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Only the parent account can create teams. `ses_region` defaults to
|
|
383
|
+
* 'us-east-1' and cannot be changed once the team exists.
|
|
384
|
+
*/
|
|
385
|
+
async createTeam(name, options = {}) {
|
|
386
|
+
const body = { name };
|
|
387
|
+
if (options.ses_region) body.ses_region = options.ses_region;
|
|
388
|
+
const res = await this.request("POST", "/v2/teams", body);
|
|
389
|
+
return res.data;
|
|
390
|
+
}
|
|
391
|
+
async updateTeam(id, params) {
|
|
392
|
+
const res = await this.request("PUT", `/v2/teams/${id}`, params);
|
|
393
|
+
return res.data;
|
|
394
|
+
}
|
|
395
|
+
// ── Private ──
|
|
396
|
+
async request(method, endpoint, body, query) {
|
|
397
|
+
let url = `${this.baseUrl}${endpoint}`;
|
|
398
|
+
if (query) {
|
|
399
|
+
const params = new URLSearchParams();
|
|
400
|
+
for (const [key, value] of Object.entries(query)) {
|
|
401
|
+
if (value !== void 0 && value !== "") params.set(key, String(value));
|
|
402
|
+
}
|
|
403
|
+
const qs = params.toString();
|
|
404
|
+
if (qs) url += `?${qs}`;
|
|
405
|
+
}
|
|
406
|
+
const headers = {
|
|
407
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
408
|
+
"Content-Type": "application/json",
|
|
409
|
+
Accept: "application/json"
|
|
410
|
+
};
|
|
411
|
+
if (this.accountId !== void 0) {
|
|
412
|
+
headers["X-Account-Id"] = String(this.accountId);
|
|
413
|
+
}
|
|
414
|
+
const controller = new AbortController();
|
|
415
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
416
|
+
try {
|
|
417
|
+
const response = await fetch(url, {
|
|
418
|
+
method,
|
|
419
|
+
headers,
|
|
420
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
421
|
+
signal: controller.signal
|
|
422
|
+
});
|
|
423
|
+
clearTimeout(timeoutId);
|
|
424
|
+
const data = await response.json();
|
|
425
|
+
if (!response.ok) {
|
|
426
|
+
throw new ToSendError(
|
|
427
|
+
data.message ?? "Unknown error",
|
|
428
|
+
response.status,
|
|
429
|
+
data.errors ?? {},
|
|
430
|
+
data.error_type
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
return data;
|
|
434
|
+
} catch (error) {
|
|
435
|
+
clearTimeout(timeoutId);
|
|
436
|
+
if (error instanceof ToSendError) throw error;
|
|
437
|
+
if (error instanceof Error) {
|
|
438
|
+
if (error.name === "AbortError") throw new ToSendError("Request timeout", 408);
|
|
439
|
+
throw new ToSendError(error.message);
|
|
440
|
+
}
|
|
441
|
+
throw new ToSendError("Unknown error");
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
};
|
|
232
445
|
var index_default = ToSend;
|
|
233
446
|
// Annotate the CommonJS export names for ESM import in node:
|
|
234
447
|
0 && (module.exports = {
|
|
235
448
|
ToSend,
|
|
449
|
+
ToSendAdmin,
|
|
236
450
|
ToSendError
|
|
237
451
|
});
|
|
238
452
|
//# sourceMappingURL=index.js.map
|