resend 6.4.1 → 6.5.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/dist/index.js DELETED
@@ -1,1241 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
- var __objRest = (source, exclude) => {
26
- var target = {};
27
- for (var prop in source)
28
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
- target[prop] = source[prop];
30
- if (source != null && __getOwnPropSymbols)
31
- for (var prop of __getOwnPropSymbols(source)) {
32
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
- target[prop] = source[prop];
34
- }
35
- return target;
36
- };
37
- var __export = (target, all) => {
38
- for (var name in all)
39
- __defProp(target, name, { get: all[name], enumerable: true });
40
- };
41
- var __copyProps = (to, from, except, desc) => {
42
- if (from && typeof from === "object" || typeof from === "function") {
43
- for (let key of __getOwnPropNames(from))
44
- if (!__hasOwnProp.call(to, key) && key !== except)
45
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
- }
47
- return to;
48
- };
49
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
50
- // If the importer is in node compatibility mode or this is not an ESM
51
- // file that has been converted to a CommonJS file using a Babel-
52
- // compatible transform (i.e. "__esModule" has not been set), then set
53
- // "default" to the CommonJS "module.exports" for node compatibility.
54
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
55
- mod
56
- ));
57
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
-
59
- // src/index.ts
60
- var index_exports = {};
61
- __export(index_exports, {
62
- Resend: () => Resend
63
- });
64
- module.exports = __toCommonJS(index_exports);
65
-
66
- // package.json
67
- var version = "6.4.1";
68
-
69
- // src/common/utils/build-pagination-query.ts
70
- function buildPaginationQuery(options) {
71
- const searchParams = new URLSearchParams();
72
- if (options.limit !== void 0) {
73
- searchParams.set("limit", options.limit.toString());
74
- }
75
- if ("after" in options && options.after !== void 0) {
76
- searchParams.set("after", options.after);
77
- }
78
- if ("before" in options && options.before !== void 0) {
79
- searchParams.set("before", options.before);
80
- }
81
- return searchParams.toString();
82
- }
83
-
84
- // src/api-keys/api-keys.ts
85
- var ApiKeys = class {
86
- constructor(resend) {
87
- this.resend = resend;
88
- }
89
- async create(payload, options = {}) {
90
- const data = await this.resend.post(
91
- "/api-keys",
92
- payload,
93
- options
94
- );
95
- return data;
96
- }
97
- async list(options = {}) {
98
- const queryString = buildPaginationQuery(options);
99
- const url = queryString ? `/api-keys?${queryString}` : "/api-keys";
100
- const data = await this.resend.get(url);
101
- return data;
102
- }
103
- async remove(id) {
104
- const data = await this.resend.delete(
105
- `/api-keys/${id}`
106
- );
107
- return data;
108
- }
109
- };
110
-
111
- // src/common/utils/parse-email-to-api-options.ts
112
- function parseAttachments(attachments) {
113
- return attachments == null ? void 0 : attachments.map((attachment) => ({
114
- content: attachment.content,
115
- filename: attachment.filename,
116
- path: attachment.path,
117
- content_type: attachment.contentType,
118
- content_id: attachment.contentId
119
- }));
120
- }
121
- function parseEmailToApiOptions(email) {
122
- return {
123
- attachments: parseAttachments(email.attachments),
124
- bcc: email.bcc,
125
- cc: email.cc,
126
- from: email.from,
127
- headers: email.headers,
128
- html: email.html,
129
- reply_to: email.replyTo,
130
- scheduled_at: email.scheduledAt,
131
- subject: email.subject,
132
- tags: email.tags,
133
- text: email.text,
134
- to: email.to,
135
- template: email.template ? {
136
- id: email.template.id,
137
- variables: email.template.variables
138
- } : void 0,
139
- topic_id: email.topicId
140
- };
141
- }
142
-
143
- // src/render.ts
144
- async function render(node) {
145
- let render2;
146
- try {
147
- ({ render: render2 } = await import("@react-email/render"));
148
- } catch (e) {
149
- throw new Error(
150
- "Failed to render React component. Make sure to install `@react-email/render` or `@react-email/components`."
151
- );
152
- }
153
- return render2(node);
154
- }
155
-
156
- // src/batch/batch.ts
157
- var Batch = class {
158
- constructor(resend) {
159
- this.resend = resend;
160
- }
161
- async send(payload, options) {
162
- return this.create(payload, options);
163
- }
164
- async create(payload, options) {
165
- var _a;
166
- const emails = [];
167
- for (const email of payload) {
168
- if (email.react) {
169
- email.html = await render(email.react);
170
- email.react = void 0;
171
- }
172
- emails.push(parseEmailToApiOptions(email));
173
- }
174
- const data = await this.resend.post(
175
- "/emails/batch",
176
- emails,
177
- __spreadProps(__spreadValues({}, options), {
178
- headers: __spreadValues({
179
- "x-batch-validation": (_a = options == null ? void 0 : options.batchValidation) != null ? _a : "strict"
180
- }, options == null ? void 0 : options.headers)
181
- })
182
- );
183
- return data;
184
- }
185
- };
186
-
187
- // src/broadcasts/broadcasts.ts
188
- var Broadcasts = class {
189
- constructor(resend) {
190
- this.resend = resend;
191
- }
192
- async create(payload, options = {}) {
193
- if (payload.react) {
194
- payload.html = await render(payload.react);
195
- }
196
- const data = await this.resend.post(
197
- "/broadcasts",
198
- {
199
- name: payload.name,
200
- segment_id: payload.segmentId,
201
- audience_id: payload.audienceId,
202
- preview_text: payload.previewText,
203
- from: payload.from,
204
- html: payload.html,
205
- reply_to: payload.replyTo,
206
- subject: payload.subject,
207
- text: payload.text,
208
- topic_id: payload.topicId
209
- },
210
- options
211
- );
212
- return data;
213
- }
214
- async send(id, payload) {
215
- const data = await this.resend.post(
216
- `/broadcasts/${id}/send`,
217
- { scheduled_at: payload == null ? void 0 : payload.scheduledAt }
218
- );
219
- return data;
220
- }
221
- async list(options = {}) {
222
- const queryString = buildPaginationQuery(options);
223
- const url = queryString ? `/broadcasts?${queryString}` : "/broadcasts";
224
- const data = await this.resend.get(url);
225
- return data;
226
- }
227
- async get(id) {
228
- const data = await this.resend.get(
229
- `/broadcasts/${id}`
230
- );
231
- return data;
232
- }
233
- async remove(id) {
234
- const data = await this.resend.delete(
235
- `/broadcasts/${id}`
236
- );
237
- return data;
238
- }
239
- async update(id, payload) {
240
- if (payload.react) {
241
- payload.html = await render(payload.react);
242
- }
243
- const data = await this.resend.patch(
244
- `/broadcasts/${id}`,
245
- {
246
- name: payload.name,
247
- segment_id: payload.segmentId,
248
- audience_id: payload.audienceId,
249
- from: payload.from,
250
- html: payload.html,
251
- text: payload.text,
252
- subject: payload.subject,
253
- reply_to: payload.replyTo,
254
- preview_text: payload.previewText,
255
- topic_id: payload.topicId
256
- }
257
- );
258
- return data;
259
- }
260
- };
261
-
262
- // src/common/utils/parse-contact-properties-to-api-options.ts
263
- function parseContactPropertyFromApi(contactProperty) {
264
- return {
265
- id: contactProperty.id,
266
- key: contactProperty.key,
267
- createdAt: contactProperty.created_at,
268
- type: contactProperty.type,
269
- fallbackValue: contactProperty.fallback_value
270
- };
271
- }
272
- function parseContactPropertyToApiOptions(contactProperty) {
273
- if ("key" in contactProperty) {
274
- return {
275
- key: contactProperty.key,
276
- type: contactProperty.type,
277
- fallback_value: contactProperty.fallbackValue
278
- };
279
- }
280
- return {
281
- fallback_value: contactProperty.fallbackValue
282
- };
283
- }
284
-
285
- // src/contact-properties/contact-properties.ts
286
- var ContactProperties = class {
287
- constructor(resend) {
288
- this.resend = resend;
289
- }
290
- async create(options) {
291
- const apiOptions = parseContactPropertyToApiOptions(options);
292
- const data = await this.resend.post(
293
- "/contact-properties",
294
- apiOptions
295
- );
296
- return data;
297
- }
298
- async list(options = {}) {
299
- const queryString = buildPaginationQuery(options);
300
- const url = queryString ? `/contact-properties?${queryString}` : "/contact-properties";
301
- const response = await this.resend.get(url);
302
- if (response.data) {
303
- return {
304
- data: __spreadProps(__spreadValues({}, response.data), {
305
- data: response.data.data.map(
306
- (apiContactProperty) => parseContactPropertyFromApi(apiContactProperty)
307
- )
308
- }),
309
- headers: response.headers,
310
- error: null
311
- };
312
- }
313
- return response;
314
- }
315
- async get(id) {
316
- if (!id) {
317
- return {
318
- data: null,
319
- headers: null,
320
- error: {
321
- message: "Missing `id` field.",
322
- statusCode: null,
323
- name: "missing_required_field"
324
- }
325
- };
326
- }
327
- const response = await this.resend.get(
328
- `/contact-properties/${id}`
329
- );
330
- if (response.data) {
331
- return {
332
- data: __spreadValues({
333
- object: "contact_property"
334
- }, parseContactPropertyFromApi(response.data)),
335
- headers: response.headers,
336
- error: null
337
- };
338
- }
339
- return response;
340
- }
341
- async update(payload) {
342
- if (!payload.id) {
343
- return {
344
- data: null,
345
- headers: null,
346
- error: {
347
- message: "Missing `id` field.",
348
- statusCode: null,
349
- name: "missing_required_field"
350
- }
351
- };
352
- }
353
- const apiOptions = parseContactPropertyToApiOptions(payload);
354
- const data = await this.resend.patch(
355
- `/contact-properties/${payload.id}`,
356
- apiOptions
357
- );
358
- return data;
359
- }
360
- async remove(id) {
361
- if (!id) {
362
- return {
363
- data: null,
364
- headers: null,
365
- error: {
366
- message: "Missing `id` field.",
367
- statusCode: null,
368
- name: "missing_required_field"
369
- }
370
- };
371
- }
372
- const data = await this.resend.delete(
373
- `/contact-properties/${id}`
374
- );
375
- return data;
376
- }
377
- };
378
-
379
- // src/contacts/segments/contact-segments.ts
380
- var ContactSegments = class {
381
- constructor(resend) {
382
- this.resend = resend;
383
- }
384
- async list(options) {
385
- if (!options.contactId && !options.email) {
386
- return {
387
- data: null,
388
- headers: null,
389
- error: {
390
- message: "Missing `id` or `email` field.",
391
- statusCode: null,
392
- name: "missing_required_field"
393
- }
394
- };
395
- }
396
- const identifier = options.email ? options.email : options.contactId;
397
- const queryString = buildPaginationQuery(options);
398
- const url = queryString ? `/contacts/${identifier}/segments?${queryString}` : `/contacts/${identifier}/segments`;
399
- const data = await this.resend.get(url);
400
- return data;
401
- }
402
- async add(options) {
403
- if (!options.contactId && !options.email) {
404
- return {
405
- data: null,
406
- headers: null,
407
- error: {
408
- message: "Missing `id` or `email` field.",
409
- statusCode: null,
410
- name: "missing_required_field"
411
- }
412
- };
413
- }
414
- const identifier = options.email ? options.email : options.contactId;
415
- return this.resend.post(
416
- `/contacts/${identifier}/segments/${options.segmentId}`
417
- );
418
- }
419
- async remove(options) {
420
- if (!options.contactId && !options.email) {
421
- return {
422
- data: null,
423
- headers: null,
424
- error: {
425
- message: "Missing `id` or `email` field.",
426
- statusCode: null,
427
- name: "missing_required_field"
428
- }
429
- };
430
- }
431
- const identifier = options.email ? options.email : options.contactId;
432
- return this.resend.delete(
433
- `/contacts/${identifier}/segments/${options.segmentId}`
434
- );
435
- }
436
- };
437
-
438
- // src/contacts/topics/contact-topics.ts
439
- var ContactTopics = class {
440
- constructor(resend) {
441
- this.resend = resend;
442
- }
443
- async update(payload) {
444
- if (!payload.id && !payload.email) {
445
- return {
446
- data: null,
447
- headers: null,
448
- error: {
449
- message: "Missing `id` or `email` field.",
450
- statusCode: null,
451
- name: "missing_required_field"
452
- }
453
- };
454
- }
455
- const identifier = payload.email ? payload.email : payload.id;
456
- return this.resend.patch(
457
- `/contacts/${identifier}/topics`,
458
- payload.topics
459
- );
460
- }
461
- async list(options) {
462
- if (!options.id && !options.email) {
463
- return {
464
- data: null,
465
- headers: null,
466
- error: {
467
- message: "Missing `id` or `email` field.",
468
- statusCode: null,
469
- name: "missing_required_field"
470
- }
471
- };
472
- }
473
- const identifier = options.email ? options.email : options.id;
474
- const queryString = buildPaginationQuery(options);
475
- const url = queryString ? `/contacts/${identifier}/topics?${queryString}` : `/contacts/${identifier}/topics`;
476
- return this.resend.get(url);
477
- }
478
- };
479
-
480
- // src/contacts/contacts.ts
481
- var Contacts = class {
482
- constructor(resend) {
483
- this.resend = resend;
484
- this.topics = new ContactTopics(this.resend);
485
- this.segments = new ContactSegments(this.resend);
486
- }
487
- async create(payload, options = {}) {
488
- if (!payload.audienceId) {
489
- const data2 = await this.resend.post(
490
- "/contacts",
491
- {
492
- unsubscribed: payload.unsubscribed,
493
- email: payload.email,
494
- first_name: payload.firstName,
495
- last_name: payload.lastName,
496
- properties: payload.properties
497
- },
498
- options
499
- );
500
- return data2;
501
- }
502
- const data = await this.resend.post(
503
- `/audiences/${payload.audienceId}/contacts`,
504
- {
505
- unsubscribed: payload.unsubscribed,
506
- email: payload.email,
507
- first_name: payload.firstName,
508
- last_name: payload.lastName,
509
- properties: payload.properties
510
- },
511
- options
512
- );
513
- return data;
514
- }
515
- async list(options = {}) {
516
- if (!("audienceId" in options) || options.audienceId === void 0) {
517
- const queryString2 = buildPaginationQuery(options);
518
- const url2 = queryString2 ? `/contacts?${queryString2}` : "/contacts";
519
- const data2 = await this.resend.get(url2);
520
- return data2;
521
- }
522
- const _a = options, { audienceId } = _a, paginationOptions = __objRest(_a, ["audienceId"]);
523
- const queryString = buildPaginationQuery(paginationOptions);
524
- const url = queryString ? `/audiences/${audienceId}/contacts?${queryString}` : `/audiences/${audienceId}/contacts`;
525
- const data = await this.resend.get(url);
526
- return data;
527
- }
528
- async get(options) {
529
- if (typeof options === "string") {
530
- return this.resend.get(`/contacts/${options}`);
531
- }
532
- if (!options.id && !options.email) {
533
- return {
534
- data: null,
535
- headers: null,
536
- error: {
537
- message: "Missing `id` or `email` field.",
538
- statusCode: null,
539
- name: "missing_required_field"
540
- }
541
- };
542
- }
543
- if (!options.audienceId) {
544
- return this.resend.get(
545
- `/contacts/${(options == null ? void 0 : options.email) ? options == null ? void 0 : options.email : options == null ? void 0 : options.id}`
546
- );
547
- }
548
- return this.resend.get(
549
- `/audiences/${options.audienceId}/contacts/${(options == null ? void 0 : options.email) ? options == null ? void 0 : options.email : options == null ? void 0 : options.id}`
550
- );
551
- }
552
- async update(options) {
553
- if (!options.id && !options.email) {
554
- return {
555
- data: null,
556
- headers: null,
557
- error: {
558
- message: "Missing `id` or `email` field.",
559
- statusCode: null,
560
- name: "missing_required_field"
561
- }
562
- };
563
- }
564
- if (!options.audienceId) {
565
- const data2 = await this.resend.patch(
566
- `/contacts/${(options == null ? void 0 : options.email) ? options == null ? void 0 : options.email : options == null ? void 0 : options.id}`,
567
- {
568
- unsubscribed: options.unsubscribed,
569
- first_name: options.firstName,
570
- last_name: options.lastName,
571
- properties: options.properties
572
- }
573
- );
574
- return data2;
575
- }
576
- const data = await this.resend.patch(
577
- `/audiences/${options.audienceId}/contacts/${(options == null ? void 0 : options.email) ? options == null ? void 0 : options.email : options == null ? void 0 : options.id}`,
578
- {
579
- unsubscribed: options.unsubscribed,
580
- first_name: options.firstName,
581
- last_name: options.lastName,
582
- properties: options.properties
583
- }
584
- );
585
- return data;
586
- }
587
- async remove(payload) {
588
- if (typeof payload === "string") {
589
- return this.resend.delete(
590
- `/contacts/${payload}`
591
- );
592
- }
593
- if (!payload.id && !payload.email) {
594
- return {
595
- data: null,
596
- headers: null,
597
- error: {
598
- message: "Missing `id` or `email` field.",
599
- statusCode: null,
600
- name: "missing_required_field"
601
- }
602
- };
603
- }
604
- if (!payload.audienceId) {
605
- return this.resend.delete(
606
- `/contacts/${(payload == null ? void 0 : payload.email) ? payload == null ? void 0 : payload.email : payload == null ? void 0 : payload.id}`
607
- );
608
- }
609
- return this.resend.delete(
610
- `/audiences/${payload.audienceId}/contacts/${(payload == null ? void 0 : payload.email) ? payload == null ? void 0 : payload.email : payload == null ? void 0 : payload.id}`
611
- );
612
- }
613
- };
614
-
615
- // src/common/utils/parse-domain-to-api-options.ts
616
- function parseDomainToApiOptions(domain) {
617
- return {
618
- name: domain.name,
619
- region: domain.region,
620
- custom_return_path: domain.customReturnPath
621
- };
622
- }
623
-
624
- // src/domains/domains.ts
625
- var Domains = class {
626
- constructor(resend) {
627
- this.resend = resend;
628
- }
629
- async create(payload, options = {}) {
630
- const data = await this.resend.post(
631
- "/domains",
632
- parseDomainToApiOptions(payload),
633
- options
634
- );
635
- return data;
636
- }
637
- async list(options = {}) {
638
- const queryString = buildPaginationQuery(options);
639
- const url = queryString ? `/domains?${queryString}` : "/domains";
640
- const data = await this.resend.get(url);
641
- return data;
642
- }
643
- async get(id) {
644
- const data = await this.resend.get(
645
- `/domains/${id}`
646
- );
647
- return data;
648
- }
649
- async update(payload) {
650
- const data = await this.resend.patch(
651
- `/domains/${payload.id}`,
652
- {
653
- click_tracking: payload.clickTracking,
654
- open_tracking: payload.openTracking,
655
- tls: payload.tls
656
- }
657
- );
658
- return data;
659
- }
660
- async remove(id) {
661
- const data = await this.resend.delete(
662
- `/domains/${id}`
663
- );
664
- return data;
665
- }
666
- async verify(id) {
667
- const data = await this.resend.post(
668
- `/domains/${id}/verify`
669
- );
670
- return data;
671
- }
672
- };
673
-
674
- // src/emails/attachments/attachments.ts
675
- var Attachments = class {
676
- constructor(resend) {
677
- this.resend = resend;
678
- }
679
- async get(options) {
680
- const { emailId, id } = options;
681
- const data = await this.resend.get(
682
- `/emails/${emailId}/attachments/${id}`
683
- );
684
- return data;
685
- }
686
- async list(options) {
687
- const { emailId } = options;
688
- const queryString = buildPaginationQuery(options);
689
- const url = queryString ? `/emails/${emailId}/attachments?${queryString}` : `/emails/${emailId}/attachments`;
690
- const data = await this.resend.get(url);
691
- return data;
692
- }
693
- };
694
-
695
- // src/emails/receiving/attachments/attachments.ts
696
- var Attachments2 = class {
697
- constructor(resend) {
698
- this.resend = resend;
699
- }
700
- async get(options) {
701
- const { emailId, id } = options;
702
- const data = await this.resend.get(
703
- `/emails/receiving/${emailId}/attachments/${id}`
704
- );
705
- return data;
706
- }
707
- async list(options) {
708
- const { emailId } = options;
709
- const queryString = buildPaginationQuery(options);
710
- const url = queryString ? `/emails/receiving/${emailId}/attachments?${queryString}` : `/emails/receiving/${emailId}/attachments`;
711
- const data = await this.resend.get(url);
712
- return data;
713
- }
714
- };
715
-
716
- // src/emails/receiving/receiving.ts
717
- var Receiving = class {
718
- constructor(resend) {
719
- this.resend = resend;
720
- this.attachments = new Attachments2(resend);
721
- }
722
- async get(id) {
723
- const data = await this.resend.get(
724
- `/emails/receiving/${id}`
725
- );
726
- return data;
727
- }
728
- async list(options = {}) {
729
- const queryString = buildPaginationQuery(options);
730
- const url = queryString ? `/emails/receiving?${queryString}` : "/emails/receiving";
731
- const data = await this.resend.get(url);
732
- return data;
733
- }
734
- };
735
-
736
- // src/emails/emails.ts
737
- var Emails = class {
738
- constructor(resend) {
739
- this.resend = resend;
740
- this.attachments = new Attachments(resend);
741
- this.receiving = new Receiving(resend);
742
- }
743
- async send(payload, options = {}) {
744
- return this.create(payload, options);
745
- }
746
- async create(payload, options = {}) {
747
- if (payload.react) {
748
- payload.html = await render(payload.react);
749
- }
750
- const data = await this.resend.post(
751
- "/emails",
752
- parseEmailToApiOptions(payload),
753
- options
754
- );
755
- return data;
756
- }
757
- async get(id) {
758
- const data = await this.resend.get(
759
- `/emails/${id}`
760
- );
761
- return data;
762
- }
763
- async list(options = {}) {
764
- const queryString = buildPaginationQuery(options);
765
- const url = queryString ? `/emails?${queryString}` : "/emails";
766
- const data = await this.resend.get(url);
767
- return data;
768
- }
769
- async update(payload) {
770
- const data = await this.resend.patch(
771
- `/emails/${payload.id}`,
772
- {
773
- scheduled_at: payload.scheduledAt
774
- }
775
- );
776
- return data;
777
- }
778
- async cancel(id) {
779
- const data = await this.resend.post(
780
- `/emails/${id}/cancel`
781
- );
782
- return data;
783
- }
784
- };
785
-
786
- // src/segments/segments.ts
787
- var Segments = class {
788
- constructor(resend) {
789
- this.resend = resend;
790
- }
791
- async create(payload, options = {}) {
792
- const data = await this.resend.post(
793
- "/segments",
794
- payload,
795
- options
796
- );
797
- return data;
798
- }
799
- async list(options = {}) {
800
- const queryString = buildPaginationQuery(options);
801
- const url = queryString ? `/segments?${queryString}` : "/segments";
802
- const data = await this.resend.get(url);
803
- return data;
804
- }
805
- async get(id) {
806
- const data = await this.resend.get(
807
- `/segments/${id}`
808
- );
809
- return data;
810
- }
811
- async remove(id) {
812
- const data = await this.resend.delete(
813
- `/segments/${id}`
814
- );
815
- return data;
816
- }
817
- };
818
-
819
- // src/common/utils/get-pagination-query-properties.ts
820
- function getPaginationQueryProperties(options = {}) {
821
- const query = new URLSearchParams();
822
- if (options.before) query.set("before", options.before);
823
- if (options.after) query.set("after", options.after);
824
- if (options.limit) query.set("limit", options.limit.toString());
825
- return query.size > 0 ? `?${query.toString()}` : "";
826
- }
827
-
828
- // src/common/utils/parse-template-to-api-options.ts
829
- function parseVariables(variables) {
830
- return variables == null ? void 0 : variables.map((variable) => ({
831
- key: variable.key,
832
- type: variable.type,
833
- fallback_value: variable.fallbackValue
834
- }));
835
- }
836
- function parseTemplateToApiOptions(template) {
837
- return {
838
- name: "name" in template ? template.name : void 0,
839
- subject: template.subject,
840
- html: template.html,
841
- text: template.text,
842
- alias: template.alias,
843
- from: template.from,
844
- reply_to: template.replyTo,
845
- variables: parseVariables(template.variables)
846
- };
847
- }
848
-
849
- // src/templates/chainable-template-result.ts
850
- var ChainableTemplateResult = class {
851
- constructor(promise, publishFn) {
852
- this.promise = promise;
853
- this.publishFn = publishFn;
854
- }
855
- // If user calls `then` or only awaits for the result of create() or duplicate(), the behavior should be
856
- // exactly as if they called create() or duplicate() directly. This will act as a normal promise
857
- // biome-ignore lint/suspicious/noThenProperty: This class intentionally implements PromiseLike
858
- then(onfulfilled, onrejected) {
859
- return this.promise.then(onfulfilled, onrejected);
860
- }
861
- async publish() {
862
- const { data, error } = await this.promise;
863
- if (error) {
864
- return {
865
- data: null,
866
- headers: null,
867
- error
868
- };
869
- }
870
- return this.publishFn(data.id);
871
- }
872
- };
873
-
874
- // src/templates/templates.ts
875
- var Templates = class {
876
- constructor(resend) {
877
- this.resend = resend;
878
- }
879
- create(payload) {
880
- const createPromise = this.performCreate(payload);
881
- return new ChainableTemplateResult(createPromise, this.publish.bind(this));
882
- }
883
- // This creation process is being done separately from the public create so that
884
- // the user can chain the publish operation after the create operation. Otherwise, due
885
- // to the async nature of the renderAsync, the return type would be
886
- // Promise<ChainableTemplateResult<CreateTemplateResponse>> which wouldn't be chainable.
887
- async performCreate(payload) {
888
- if (payload.react) {
889
- if (!this.renderAsync) {
890
- try {
891
- const { renderAsync } = await import("@react-email/render");
892
- this.renderAsync = renderAsync;
893
- } catch (e) {
894
- throw new Error(
895
- "Failed to render React component. Make sure to install `@react-email/render`"
896
- );
897
- }
898
- }
899
- payload.html = await this.renderAsync(
900
- payload.react
901
- );
902
- }
903
- return this.resend.post(
904
- "/templates",
905
- parseTemplateToApiOptions(payload)
906
- );
907
- }
908
- async remove(identifier) {
909
- const data = await this.resend.delete(
910
- `/templates/${identifier}`
911
- );
912
- return data;
913
- }
914
- async get(identifier) {
915
- const data = await this.resend.get(
916
- `/templates/${identifier}`
917
- );
918
- return data;
919
- }
920
- async list(options = {}) {
921
- return this.resend.get(
922
- `/templates${getPaginationQueryProperties(options)}`
923
- );
924
- }
925
- duplicate(identifier) {
926
- const promiseDuplicate = this.resend.post(
927
- `/templates/${identifier}/duplicate`
928
- );
929
- return new ChainableTemplateResult(
930
- promiseDuplicate,
931
- this.publish.bind(this)
932
- );
933
- }
934
- async publish(identifier) {
935
- const data = await this.resend.post(
936
- `/templates/${identifier}/publish`
937
- );
938
- return data;
939
- }
940
- async update(identifier, payload) {
941
- const data = await this.resend.patch(
942
- `/templates/${identifier}`,
943
- parseTemplateToApiOptions(payload)
944
- );
945
- return data;
946
- }
947
- };
948
-
949
- // src/topics/topics.ts
950
- var Topics = class {
951
- constructor(resend) {
952
- this.resend = resend;
953
- }
954
- async create(payload) {
955
- const _a = payload, { defaultSubscription } = _a, body = __objRest(_a, ["defaultSubscription"]);
956
- const data = await this.resend.post("/topics", __spreadProps(__spreadValues({}, body), {
957
- default_subscription: defaultSubscription
958
- }));
959
- return data;
960
- }
961
- async list() {
962
- const data = await this.resend.get("/topics");
963
- return data;
964
- }
965
- async get(id) {
966
- if (!id) {
967
- return {
968
- data: null,
969
- headers: null,
970
- error: {
971
- message: "Missing `id` field.",
972
- statusCode: null,
973
- name: "missing_required_field"
974
- }
975
- };
976
- }
977
- const data = await this.resend.get(
978
- `/topics/${id}`
979
- );
980
- return data;
981
- }
982
- async update(payload) {
983
- if (!payload.id) {
984
- return {
985
- data: null,
986
- headers: null,
987
- error: {
988
- message: "Missing `id` field.",
989
- statusCode: null,
990
- name: "missing_required_field"
991
- }
992
- };
993
- }
994
- const data = await this.resend.patch(
995
- `/topics/${payload.id}`,
996
- payload
997
- );
998
- return data;
999
- }
1000
- async remove(id) {
1001
- if (!id) {
1002
- return {
1003
- data: null,
1004
- headers: null,
1005
- error: {
1006
- message: "Missing `id` field.",
1007
- statusCode: null,
1008
- name: "missing_required_field"
1009
- }
1010
- };
1011
- }
1012
- const data = await this.resend.delete(
1013
- `/topics/${id}`
1014
- );
1015
- return data;
1016
- }
1017
- };
1018
-
1019
- // src/webhooks/webhooks.ts
1020
- var import_svix = require("svix");
1021
- var Webhooks = class {
1022
- constructor(resend) {
1023
- this.resend = resend;
1024
- }
1025
- async create(payload, options = {}) {
1026
- const data = await this.resend.post(
1027
- "/webhooks",
1028
- payload,
1029
- options
1030
- );
1031
- return data;
1032
- }
1033
- async get(id) {
1034
- const data = await this.resend.get(
1035
- `/webhooks/${id}`
1036
- );
1037
- return data;
1038
- }
1039
- async list(options = {}) {
1040
- const queryString = buildPaginationQuery(options);
1041
- const url = queryString ? `/webhooks?${queryString}` : "/webhooks";
1042
- const data = await this.resend.get(url);
1043
- return data;
1044
- }
1045
- async update(id, payload) {
1046
- const data = await this.resend.patch(
1047
- `/webhooks/${id}`,
1048
- payload
1049
- );
1050
- return data;
1051
- }
1052
- async remove(id) {
1053
- const data = await this.resend.delete(
1054
- `/webhooks/${id}`
1055
- );
1056
- return data;
1057
- }
1058
- verify(payload) {
1059
- const webhook = new import_svix.Webhook(payload.webhookSecret);
1060
- return webhook.verify(payload.payload, {
1061
- "svix-id": payload.headers.id,
1062
- "svix-timestamp": payload.headers.timestamp,
1063
- "svix-signature": payload.headers.signature
1064
- });
1065
- }
1066
- };
1067
-
1068
- // src/resend.ts
1069
- var defaultBaseUrl = "https://api.resend.com";
1070
- var defaultUserAgent = `resend-node:${version}`;
1071
- var baseUrl = typeof process !== "undefined" && process.env ? process.env.RESEND_BASE_URL || defaultBaseUrl : defaultBaseUrl;
1072
- var userAgent = typeof process !== "undefined" && process.env ? process.env.RESEND_USER_AGENT || defaultUserAgent : defaultUserAgent;
1073
- var Resend = class {
1074
- constructor(key) {
1075
- this.key = key;
1076
- this.apiKeys = new ApiKeys(this);
1077
- this.segments = new Segments(this);
1078
- /**
1079
- * @deprecated Use segments instead
1080
- */
1081
- this.audiences = this.segments;
1082
- this.batch = new Batch(this);
1083
- this.broadcasts = new Broadcasts(this);
1084
- this.contacts = new Contacts(this);
1085
- this.contactProperties = new ContactProperties(this);
1086
- this.domains = new Domains(this);
1087
- this.emails = new Emails(this);
1088
- this.webhooks = new Webhooks(this);
1089
- this.templates = new Templates(this);
1090
- this.topics = new Topics(this);
1091
- if (!key) {
1092
- if (typeof process !== "undefined" && process.env) {
1093
- this.key = process.env.RESEND_API_KEY;
1094
- }
1095
- if (!this.key) {
1096
- throw new Error(
1097
- 'Missing API key. Pass it to the constructor `new Resend("re_123")`'
1098
- );
1099
- }
1100
- }
1101
- this.headers = new Headers({
1102
- Authorization: `Bearer ${this.key}`,
1103
- "User-Agent": userAgent,
1104
- "Content-Type": "application/json"
1105
- });
1106
- }
1107
- async fetchRequest(path, options = {}) {
1108
- try {
1109
- const response = await fetch(`${baseUrl}${path}`, options);
1110
- if (!response.ok) {
1111
- try {
1112
- const rawError = await response.text();
1113
- return {
1114
- data: null,
1115
- error: JSON.parse(rawError),
1116
- headers: Object.fromEntries(response.headers.entries())
1117
- };
1118
- } catch (err) {
1119
- if (err instanceof SyntaxError) {
1120
- return {
1121
- data: null,
1122
- error: {
1123
- name: "application_error",
1124
- statusCode: response.status,
1125
- message: "Internal server error. We are unable to process your request right now, please try again later."
1126
- },
1127
- headers: Object.fromEntries(response.headers.entries())
1128
- };
1129
- }
1130
- const error = {
1131
- message: response.statusText,
1132
- statusCode: response.status,
1133
- name: "application_error"
1134
- };
1135
- if (err instanceof Error) {
1136
- return {
1137
- data: null,
1138
- error: __spreadProps(__spreadValues({}, error), { message: err.message }),
1139
- headers: Object.fromEntries(response.headers.entries())
1140
- };
1141
- }
1142
- return {
1143
- data: null,
1144
- error,
1145
- headers: Object.fromEntries(response.headers.entries())
1146
- };
1147
- }
1148
- }
1149
- const data = await response.json();
1150
- return {
1151
- data,
1152
- error: null,
1153
- headers: Object.fromEntries(response.headers.entries())
1154
- };
1155
- } catch (e) {
1156
- return {
1157
- data: null,
1158
- error: {
1159
- name: "application_error",
1160
- statusCode: null,
1161
- message: "Unable to fetch data. The request could not be resolved."
1162
- },
1163
- headers: null
1164
- };
1165
- }
1166
- }
1167
- async post(path, entity, options = {}) {
1168
- const headers = new Headers(this.headers);
1169
- if (options.headers) {
1170
- for (const [key, value] of new Headers(options.headers).entries()) {
1171
- headers.set(key, value);
1172
- }
1173
- }
1174
- if (options.idempotencyKey) {
1175
- headers.set("Idempotency-Key", options.idempotencyKey);
1176
- }
1177
- const requestOptions = __spreadProps(__spreadValues({
1178
- method: "POST",
1179
- body: JSON.stringify(entity)
1180
- }, options), {
1181
- headers
1182
- });
1183
- return this.fetchRequest(path, requestOptions);
1184
- }
1185
- async get(path, options = {}) {
1186
- const headers = new Headers(this.headers);
1187
- if (options.headers) {
1188
- for (const [key, value] of new Headers(options.headers).entries()) {
1189
- headers.set(key, value);
1190
- }
1191
- }
1192
- const requestOptions = __spreadProps(__spreadValues({
1193
- method: "GET"
1194
- }, options), {
1195
- headers
1196
- });
1197
- return this.fetchRequest(path, requestOptions);
1198
- }
1199
- async put(path, entity, options = {}) {
1200
- const headers = new Headers(this.headers);
1201
- if (options.headers) {
1202
- for (const [key, value] of new Headers(options.headers).entries()) {
1203
- headers.set(key, value);
1204
- }
1205
- }
1206
- const requestOptions = __spreadProps(__spreadValues({
1207
- method: "PUT",
1208
- body: JSON.stringify(entity)
1209
- }, options), {
1210
- headers
1211
- });
1212
- return this.fetchRequest(path, requestOptions);
1213
- }
1214
- async patch(path, entity, options = {}) {
1215
- const headers = new Headers(this.headers);
1216
- if (options.headers) {
1217
- for (const [key, value] of new Headers(options.headers).entries()) {
1218
- headers.set(key, value);
1219
- }
1220
- }
1221
- const requestOptions = __spreadProps(__spreadValues({
1222
- method: "PATCH",
1223
- body: JSON.stringify(entity)
1224
- }, options), {
1225
- headers
1226
- });
1227
- return this.fetchRequest(path, requestOptions);
1228
- }
1229
- async delete(path, query) {
1230
- const requestOptions = {
1231
- method: "DELETE",
1232
- body: JSON.stringify(query),
1233
- headers: this.headers
1234
- };
1235
- return this.fetchRequest(path, requestOptions);
1236
- }
1237
- };
1238
- // Annotate the CommonJS export names for ESM import in node:
1239
- 0 && (module.exports = {
1240
- Resend
1241
- });